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

taosdata / TDengine / #4906

30 Dec 2025 10:52AM UTC coverage: 65.514% (+0.09%) from 65.423%
#4906

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

4080 existing lines in 123 files now uncovered.

193840 of 295877 relevant lines covered (65.51%)

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

50
    SColCmpr *pCol = p + nCols;
2,953,917✔
51
    pCol->id = pSchema->colId;
2,953,917✔
52
    pCol->alg = compress;
2,953,917✔
53
    pWp->nCols = nCols + 1;
2,953,917✔
54
    pWp->version = ver;
2,953,917✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
28,432,771✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
28,432,771✔
58
      if (pOCmpr->id == pSchema->colId) {
28,432,771✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
63,444✔
60
        if (left) {
63,444✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
42,097✔
62
        }
63
        nCols--;
63,444✔
64
        break;
63,444✔
65
      }
66
    }
67
    pWp->nCols = nCols;
63,444✔
68
    pWp->version = ver;
63,444✔
69
  }
70
  return 0;
3,017,361✔
71
}
72

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
90,393✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
90,393✔
92
  if (dropColId == newColNum) {
13,718✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
5,478✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
8,240✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
8,240✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
60,028✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
57,312✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
2,716✔
104
  return 0;
2,716✔
105
}
106

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

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

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

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

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

169
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
6,878,380✔
170

171
  return 0;
6,878,380✔
172
}
173

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

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

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

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

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
4,907,120✔
211
    SColRef* pColRef = &pRef->pColRef[i];
4,400,623✔
212
    if (pColRef->hasRef) {
4,400,623✔
213
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
2,385,849✔
214
      if (code) {
2,385,849✔
215
        goto _return;
×
216
      }
217
    }
218
  }
219

220
  if (taosHashGetSize(pColRefHash) > 1000) {
506,497✔
UNCOV
221
    code = TSDB_CODE_VTABLE_TOO_MANY_REFERENCE;
×
UNCOV
222
    goto _return;
×
223
  }
224

225

226

227
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
506,497✔
228
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
506,497✔
229
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
506,497✔
230
    pMetaRsp->tuid = pEntry->uid;
296,859✔
231
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
209,638✔
232
    pMetaRsp->tuid = pEntry->uid;
209,638✔
233
    pMetaRsp->suid = pEntry->ctbEntry.suid;
209,638✔
234
  }
235

236
  pMetaRsp->tableType = tableType;
506,497✔
237
  pMetaRsp->virtualStb = false; // super table will never be processed here
506,497✔
238
  pMetaRsp->numOfColRefs = pRef->nCols;
506,497✔
239
  pMetaRsp->rversion = pRef->version;
506,497✔
240

241
  taosHashCleanup(pColRefHash);
506,497✔
242
  return code;
506,497✔
UNCOV
243
_return:
×
UNCOV
244
  taosHashCleanup(pColRefHash);
×
UNCOV
245
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
UNCOV
246
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
UNCOV
247
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
UNCOV
248
  return code;
×
249
}
250

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

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

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

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

272
  SIndexMultiTerm *terms = indexMultiTermCreate();
249,515✔
273
  if (terms == NULL) {
249,515✔
274
    return terrno;
×
275
  }
276

277
  int16_t nCols = taosArrayGetSize(pTagVals);
249,515✔
278
  for (int i = 0; i < nCols; i++) {
607,414✔
279
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
357,899✔
280
    char     type = pTagVal->type;
357,899✔
281

282
    char   *key = pTagVal->pKey;
357,899✔
283
    int32_t nKey = strlen(key);
357,899✔
284

285
    SIndexTerm *term = NULL;
357,899✔
286
    if (type == TSDB_DATA_TYPE_NULL) {
357,899✔
287
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,610✔
288
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
355,289✔
289
      if (pTagVal->nData > 0) {
338,299✔
290
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
336,559✔
291
        if (val == NULL) {
336,559✔
292
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
293
        }
294
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
336,559✔
295
        if (len < 0) {
336,559✔
296
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
297
        }
298
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
336,559✔
299
        type = TSDB_DATA_TYPE_VARCHAR;
336,559✔
300
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
336,559✔
301
        taosMemoryFree(val);
336,559✔
302
      } else if (pTagVal->nData == 0) {
1,740✔
303
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,740✔
304
      }
305
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
16,990✔
306
      double val = *(double *)(&pTagVal->i64);
13,510✔
307
      int    len = sizeof(val);
13,510✔
308
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
13,510✔
309
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,480✔
310
      int val = *(int *)(&pTagVal->i64);
3,480✔
311
      int len = sizeof(val);
3,480✔
312
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,480✔
313
    }
314

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

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

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

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

357
  SIndexMultiTerm *terms = indexMultiTermCreate();
109,136✔
358
  if (terms == NULL) {
109,136✔
359
    return terrno;
×
360
  }
361

362
  int16_t nCols = taosArrayGetSize(pTagVals);
109,136✔
363
  for (int i = 0; i < nCols; i++) {
312,922✔
364
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
203,786✔
365
    char     type = pTagVal->type;
203,786✔
366

367
    char   *key = pTagVal->pKey;
203,786✔
368
    int32_t nKey = strlen(key);
203,786✔
369

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

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

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

431
  metaWLock(pMeta);
17,996✔
432
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
95,300✔
433
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
77,304✔
434
    tb_uid_t suid = 0;
77,304✔
435
    int8_t   sysTbl = 0;
77,304✔
436
    int      type;
77,304✔
437
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
77,304✔
438
    if (code) return code;
77,304✔
439
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
77,304✔
440
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
73,124✔
441
      if (pVal) {
73,124✔
442
        nCtbDropped = *pVal + 1;
56,698✔
443
      } else {
444
        nCtbDropped = 1;
16,426✔
445
      }
446
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
73,124✔
447
      if (code) return code;
73,124✔
448
    }
449
    /*
450
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
451
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
452
    }
453
    */
454
    metaDebug("batch drop table:%" PRId64, uid);
77,304✔
455
  }
456
  metaULock(pMeta);
17,996✔
457

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

474
  pMeta->changed = true;
17,996✔
475
  return 0;
17,996✔
476
}
477

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

484
  TBC *pCur;
23,613✔
485
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
23,613✔
486
  if (code < 0) {
23,613✔
487
    return code;
×
488
  }
489

490
  code = tdbTbcMoveToFirst(pCur);
23,613✔
491
  if (code) {
23,613✔
492
    tdbTbcClose(pCur);
×
493
    return code;
×
494
  }
495

496
  void *pData = NULL, *pKey = NULL;
23,613✔
497
  int   nData = 0, nKey = 0;
23,613✔
498

499
  while (1) {
172,511✔
500
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
196,124✔
501
    if (ret < 0) {
196,124✔
502
      break;
23,613✔
503
    }
504

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

514
    if (me.type != TSDB_SUPER_TABLE) {
172,511✔
515
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
154,600✔
516
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
154,600✔
517
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
154,600✔
518
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
154,600✔
519
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
154,600✔
520
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
154,600✔
521
        if (taosArrayPush(uidList, &me.uid) == NULL) {
77,304✔
522
          code = terrno;
×
523
          break;
×
524
        }
525
      }
526
    }
527
    tDecoderClear(&dc);
172,511✔
528
  }
529
  tdbFree(pData);
23,613✔
530
  tdbFree(pKey);
23,613✔
531
  tdbTbcClose(pCur);
23,613✔
532

533
  return 0;
23,613✔
534
}
535

536
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
23,613✔
537
  int32_t code = 0;
23,613✔
538

539
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
23,613✔
540
  if (tbUids == NULL) {
23,613✔
541
    return terrno;
×
542
  }
543

544
  code = metaFilterTableByHash(pMeta, tbUids);
23,613✔
545
  if (code != 0) {
23,613✔
546
    goto end;
×
547
  }
548
  if (TARRAY_SIZE(tbUids) == 0) {
23,613✔
549
    goto end;
5,617✔
550
  }
551

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

556
end:
17,996✔
557
  taosArrayDestroy(tbUids);
23,613✔
558

559
  return code;
23,613✔
560
}
561

562
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
12,882,120✔
563
  metaRLock(pMeta);
12,882,120✔
564

565
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
12,889,595✔
566

567
  metaULock(pMeta);
12,870,599✔
568

569
  if (ret != 0) {
12,877,448✔
570
    metaError("ttl failed to find expired table, ret:%d", ret);
×
571
  }
572

573
  return ret;
12,878,629✔
574
}
575

576
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
77,304✔
577
  int64_t btime;
578
  if (pME->type == TSDB_CHILD_TABLE) {
77,304✔
579
    btime = pME->ctbEntry.btime;
73,124✔
580
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,180✔
581
    btime = pME->ntbEntry.btime;
4,180✔
582
  } else {
583
    return TSDB_CODE_FAILED;
×
584
  }
585

586
  btimeKey->btime = btime;
77,304✔
587
  btimeKey->uid = pME->uid;
77,304✔
588
  return 0;
77,304✔
589
}
590

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

601
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
77,304✔
602
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
77,304✔
603

604
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
77,304✔
605
  if (pME->type == TSDB_CHILD_TABLE) {
77,304✔
606
    ctx.ttlDays = pME->ctbEntry.ttlDays;
73,124✔
607
  } else {
608
    ctx.ttlDays = pME->ntbEntry.ttlDays;
4,180✔
609
  }
610

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

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

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

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

639
  tDecoderInit(&dc, pData, nData);
77,304✔
640
  rc = metaDecodeEntry(&dc, &e);
77,304✔
641
  if (rc < 0) {
77,304✔
642
    tDecoderClear(&dc);
×
643
    return rc;
×
644
  }
645

646
  if (type) *type = e.type;
77,304✔
647

648
  if (e.type == TSDB_CHILD_TABLE) {
77,304✔
649
    if (pSuid) *pSuid = e.ctbEntry.suid;
73,124✔
650
    void *tData = NULL;
73,124✔
651
    int   tLen = 0;
73,124✔
652

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

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

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

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

694
            const void *pTagData = NULL;
73,124✔
695
            int32_t     nTagData = 0;
73,124✔
696

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

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

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

746
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
77,304✔
747
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
77,304✔
748

749
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
77,304✔
750

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

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

779
    --pMeta->pVnode->config.vndStats.numOfNTables;
4,180✔
780
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
4,180✔
781

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

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

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

824
  tDecoderClear(&dc);
77,304✔
825
  tdbFree(pData);
77,304✔
826

827
  return 0;
77,304✔
828
}
829

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

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

846
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
11,274,317✔
847
  pMeta->changed = true;
11,274,317✔
848
  switch (pReq->action) {
11,274,317✔
849
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,305,033✔
850
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
851
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
852
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,305,033✔
853
    case TSDB_ALTER_TABLE_DROP_COLUMN:
90,393✔
854
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
90,393✔
855
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
463,343✔
856
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
463,343✔
857
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
42,426✔
858
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
42,426✔
859
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,217,994✔
860
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,217,994✔
861
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,125✔
862
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,125✔
863
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
23,712✔
864
      return metaUpdateTableOptions2(pMeta, version, pReq);
23,712✔
865
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5,803✔
866
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5,803✔
867
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
70,462✔
868
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
70,462✔
869
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
52,026✔
870
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
52,026✔
871
    default:
×
872
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
873
      break;
874
  }
875
}
876

877
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
1,008,443✔
878
  if (!tsTtlChangeOnWrite) return 0;
1,008,443✔
879

880
  if (changeTimeMs <= 0) {
1,008,443✔
881
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
882
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
883
  }
884

885
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
1,008,443✔
886

887
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
1,008,443✔
888
}
889

890
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
611,284,104✔
891
  if (!tsTtlChangeOnWrite) return 0;
611,284,104✔
892

893
  metaWLock(pMeta);
1,016,936✔
894
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
1,008,443✔
895
  metaULock(pMeta);
1,008,443✔
896
  return ret;
1,008,443✔
897
}
898

899
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
75,377,162✔
900
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
901
  if (IS_VAR_DATA_TYPE(type)) {
75,377,162✔
902
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
9,251,383✔
903
  } else {
904
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
66,125,779✔
905
  }
906

907
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
75,389,110✔
908
  if (*ppTagIdxKey == NULL) {
75,381,058✔
909
    return terrno;
×
910
  }
911

912
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
75,382,703✔
913
  (*ppTagIdxKey)->cid = cid;
75,380,064✔
914
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
75,379,525✔
915
  (*ppTagIdxKey)->type = type;
75,376,645✔
916

917
  // refactor
918
  if (IS_VAR_DATA_TYPE(type)) {
75,377,325✔
919
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
9,268,802✔
920
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
9,265,606✔
921
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
9,266,137✔
922
  } else {
923
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
66,108,523✔
924
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
66,116,809✔
925
  }
926

927
  return 0;
75,376,072✔
928
}
929

930
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
72,430,387✔
931
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
72,430,387✔
932
}
72,429,154✔
933

934
static void colCompressDebug(SHashObj *pColCmprObj) {
76,481,602✔
935
  void *p = taosHashIterate(pColCmprObj, NULL);
76,481,602✔
936
  while (p) {
650,202,052✔
937
    uint32_t cmprAlg = *(uint32_t *)p;
573,697,994✔
938
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
573,697,414✔
939
    p = taosHashIterate(pColCmprObj, p);
573,717,660✔
940

941
    uint8_t l1, l2, lvl;
573,818,252✔
942
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
573,830,527✔
943

944
    const char *l1str = columnEncodeStr(l1);
573,840,832✔
945
    const char *l2str = columnCompressStr(l2);
573,732,250✔
946
    const char *lvlstr = columnLevelStr(lvl);
573,696,446✔
947
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
573,683,519✔
948
  }
949
  return;
76,504,058✔
950
}
951

952
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
76,499,012✔
953
  int rc = 0;
76,499,012✔
954

955
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
76,499,012✔
956
  if (pColCmprObj == NULL) {
76,500,132✔
957
    pColCmprObj = NULL;
×
958
    return TSDB_CODE_OUT_OF_MEMORY;
×
959
  }
960

961
  void      *pData = NULL;
76,500,132✔
962
  int        nData = 0;
76,506,626✔
963
  SMetaEntry e = {0};
76,506,222✔
964
  SDecoder   dc = {0};
76,505,354✔
965

966
  *ppColCmprObj = NULL;
76,504,381✔
967

968
  metaRLock(pMeta);
76,496,967✔
969
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
76,514,154✔
970
  if (rc < 0) {
76,501,770✔
971
    taosHashCleanup(pColCmprObj);
14,400✔
972
    metaULock(pMeta);
14,400✔
973
    return TSDB_CODE_FAILED;
14,400✔
974
  }
975
  int64_t version = ((SUidIdxVal *)pData)[0].version;
76,487,370✔
976
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
76,480,987✔
977
  if (rc < 0) {
76,480,811✔
978
    metaULock(pMeta);
×
979
    taosHashCleanup(pColCmprObj);
×
980
    metaError("failed to get table entry");
×
981
    return rc;
×
982
  }
983

984
  tDecoderInit(&dc, pData, nData);
76,480,811✔
985
  rc = metaDecodeEntry(&dc, &e);
76,488,304✔
986
  if (rc < 0) {
76,474,425✔
987
    tDecoderClear(&dc);
×
988
    tdbFree(pData);
×
989
    metaULock(pMeta);
×
990
    taosHashCleanup(pColCmprObj);
×
991
    return rc;
×
992
  }
993
  if (withExtSchema(e.type)) {
76,474,425✔
994
    SColCmprWrapper *p = &e.colCmpr;
76,486,387✔
995
    for (int32_t i = 0; i < p->nCols; i++) {
650,286,163✔
996
      SColCmpr *pCmpr = &p->pColCmpr[i];
573,786,737✔
997
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
573,789,783✔
998
      if (rc < 0) {
573,799,776✔
999
        tDecoderClear(&dc);
×
1000
        tdbFree(pData);
×
1001
        metaULock(pMeta);
×
1002
        taosHashCleanup(pColCmprObj);
×
1003
        return rc;
×
1004
      }
1005
    }
1006
  } else {
1007
    tDecoderClear(&dc);
×
1008
    tdbFree(pData);
×
1009
    metaULock(pMeta);
×
1010
    taosHashCleanup(pColCmprObj);
×
1011
    return 0;
×
1012
  }
1013
  tDecoderClear(&dc);
76,459,233✔
1014
  tdbFree(pData);
76,501,036✔
1015
  metaULock(pMeta);
76,491,686✔
1016

1017
  *ppColCmprObj = pColCmprObj;
76,484,833✔
1018
  colCompressDebug(pColCmprObj);
76,487,693✔
1019

1020
  return 0;
76,504,496✔
1021
}
1022
// refactor later
1023
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
3,769,180✔
1024
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
3,768,846✔
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