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

taosdata / TDengine / #4923

09 Jan 2026 08:13AM UTC coverage: 65.373% (+0.2%) from 65.161%
#4923

push

travis-ci

web-flow
merge: from main to 3.0 branch #34232

33 of 56 new or added lines in 8 files covered. (58.93%)

3042 existing lines in 131 files now uncovered.

198273 of 303297 relevant lines covered (65.37%)

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

50
    SColCmpr *pCol = p + nCols;
2,994,098✔
51
    pCol->id = pSchema->colId;
2,994,098✔
52
    pCol->alg = compress;
2,994,098✔
53
    pWp->nCols = nCols + 1;
2,994,098✔
54
    pWp->version = ver;
2,994,098✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
40,172,267✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
40,172,267✔
58
      if (pOCmpr->id == pSchema->colId) {
40,172,267✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
67,302✔
60
        if (left) {
67,302✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
45,723✔
62
        }
63
        nCols--;
67,302✔
64
        break;
67,302✔
65
      }
66
    }
67
    pWp->nCols = nCols;
67,302✔
68
    pWp->version = ver;
67,302✔
69
  }
70
  return 0;
3,061,400✔
71
}
72

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

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

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

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

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

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

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

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

171
  return 0;
6,967,273✔
172
}
173

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

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

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

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

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
4,969,367✔
211
    SColRef* pColRef = &pRef->pColRef[i];
4,456,279✔
212
    if (pColRef->hasRef) {
4,456,279✔
213
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
2,417,405✔
214
      if (code) {
2,417,405✔
215
        goto _return;
×
216
      }
217
    }
218
  }
219

220
  if (taosHashGetSize(pColRefHash) > 1000) {
513,088✔
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));
513,088✔
228
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
513,088✔
229
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
513,088✔
230
    pMetaRsp->tuid = pEntry->uid;
300,728✔
231
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
212,360✔
232
    pMetaRsp->tuid = pEntry->uid;
212,360✔
233
    pMetaRsp->suid = pEntry->ctbEntry.suid;
212,360✔
234
  }
235

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

241
  taosHashCleanup(pColRefHash);
513,088✔
242
  return code;
513,088✔
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) {
251,905✔
252
  int32_t code = 0;
251,905✔
253

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

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

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

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

277
  int16_t nCols = taosArrayGetSize(pTagVals);
251,905✔
278
  for (int i = 0; i < nCols; i++) {
613,103✔
279
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
361,198✔
280
    char     type = pTagVal->type;
361,198✔
281

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

285
    SIndexTerm *term = NULL;
361,198✔
286
    if (type == TSDB_DATA_TYPE_NULL) {
361,198✔
287
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,622✔
288
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
358,576✔
289
      if (pTagVal->nData > 0) {
341,499✔
290
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
339,751✔
291
        if (val == NULL) {
339,751✔
292
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
293
        }
294
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
339,751✔
295
        if (len < 0) {
339,751✔
296
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
297
        }
298
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
339,751✔
299
        type = TSDB_DATA_TYPE_VARCHAR;
339,751✔
300
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
339,751✔
301
        taosMemoryFree(val);
339,751✔
302
      } else if (pTagVal->nData == 0) {
1,748✔
303
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,748✔
304
      }
305
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
17,077✔
306
      double val = *(double *)(&pTagVal->i64);
13,581✔
307
      int    len = sizeof(val);
13,581✔
308
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
13,581✔
309
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,496✔
310
      int val = *(int *)(&pTagVal->i64);
3,496✔
311
      int len = sizeof(val);
3,496✔
312
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,496✔
313
    }
314

315
    if (term != NULL) {
361,198✔
316
      int32_t ret = indexMultiTermAdd(terms, term);
361,198✔
317
      if (ret < 0) {
361,198✔
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);
251,905✔
327
  indexMultiTermDestroy(terms);
251,905✔
328

329
  taosArrayDestroy(pTagVals);
251,905✔
330
  return code;
251,905✔
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,899✔
338
int32_t code = 0;
109,899✔
339
#ifdef USE_INVERTED_INDEX
340
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
109,899✔
341
    return TSDB_CODE_INVALID_PARA;
×
342
  }
343
  void       *data = pCtbEntry->ctbEntry.pTags;
109,899✔
344
  const char *tagName = pSchema->name;
109,899✔
345

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

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

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

362
  int16_t nCols = taosArrayGetSize(pTagVals);
109,899✔
363
  for (int i = 0; i < nCols; i++) {
315,108✔
364
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
205,209✔
365
    char     type = pTagVal->type;
205,209✔
366

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

370
    SIndexTerm *term = NULL;
205,209✔
371
    if (type == TSDB_DATA_TYPE_NULL) {
205,209✔
372
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
373
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
205,209✔
374
      if (pTagVal->nData > 0) {
203,024✔
375
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
203,024✔
376
        if (val == NULL) {
203,024✔
377
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
378
        }
379
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
203,024✔
380
        if (len < 0) {
203,024✔
381
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
382
        }
383
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
203,024✔
384
        type = TSDB_DATA_TYPE_VARCHAR;
203,024✔
385
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
203,024✔
386
        taosMemoryFree(val);
203,024✔
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,185✔
391
      double val = *(double *)(&pTagVal->i64);
1,311✔
392
      int    len = sizeof(val);
1,311✔
393
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
1,311✔
394
    } else if (type == TSDB_DATA_TYPE_BOOL) {
874✔
395
      int val = *(int *)(&pTagVal->i64);
874✔
396
      int len = sizeof(val);
874✔
397
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
874✔
398
    }
399
    if (term != NULL) {
205,209✔
400
      int32_t ret = indexMultiTermAdd(terms, term);
205,209✔
401
      if (ret < 0) {
205,209✔
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,899✔
411
  indexMultiTermDestroy(terms);
109,899✔
412
  taosArrayDestroy(pTagVals);
109,899✔
413
  return code;
109,899✔
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,643✔
422
  int32_t code = 0;
17,643✔
423
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
17,643✔
424

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

431
  metaWLock(pMeta);
17,643✔
432
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
95,951✔
433
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
78,308✔
434
    tb_uid_t suid = 0;
78,308✔
435
    int8_t   sysTbl = 0;
78,308✔
436
    int      type;
78,308✔
437
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
78,308✔
438
    if (code) return code;
78,308✔
439
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
78,308✔
440
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
74,717✔
441
      if (pVal) {
74,717✔
442
        nCtbDropped = *pVal + 1;
58,017✔
443
      } else {
444
        nCtbDropped = 1;
16,700✔
445
      }
446
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
74,717✔
447
      if (code) return code;
74,717✔
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);
78,308✔
455
  }
456
  metaULock(pMeta);
17,643✔
457

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

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

478
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
23,992✔
479
  int32_t code = 0;
23,992✔
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,992✔
485
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
23,992✔
486
  if (code < 0) {
23,992✔
487
    return code;
×
488
  }
489

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

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

499
  while (1) {
174,820✔
500
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
198,812✔
501
    if (ret < 0) {
198,812✔
502
      break;
23,992✔
503
    }
504

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

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

533
  return 0;
23,992✔
534
}
535

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

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

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

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

556
end:
17,643✔
557
  taosArrayDestroy(tbUids);
23,992✔
558

559
  return code;
23,992✔
560
}
561

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

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

567
  metaULock(pMeta);
12,979,341✔
568

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

573
  return ret;
12,983,888✔
574
}
575

576
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
78,308✔
577
  int64_t btime;
578
  if (pME->type == TSDB_CHILD_TABLE) {
78,308✔
579
    btime = pME->ctbEntry.btime;
74,717✔
580
  } else if (pME->type == TSDB_NORMAL_TABLE) {
3,591✔
581
    btime = pME->ntbEntry.btime;
3,591✔
582
  } else {
583
    return TSDB_CODE_FAILED;
×
584
  }
585

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

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

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

604
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
78,308✔
605
  if (pME->type == TSDB_CHILD_TABLE) {
78,308✔
606
    ctx.ttlDays = pME->ctbEntry.ttlDays;
74,717✔
607
  } else {
608
    ctx.ttlDays = pME->ntbEntry.ttlDays;
3,591✔
609
  }
610

611
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
78,308✔
612
  if (ret < 0) {
78,308✔
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;
78,308✔
617
}
618

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

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

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

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

646
  if (type) *type = e.type;
78,308✔
647

648
  if (e.type == TSDB_CHILD_TABLE) {
78,308✔
649
    if (pSuid) *pSuid = e.ctbEntry.suid;
74,717✔
650
    void *tData = NULL;
74,717✔
651
    int   tLen = 0;
74,717✔
652

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

659
        tDecoderInit(&tdc, tData, tLen);
74,717✔
660
        ret = metaDecodeEntry(&tdc, &stbEntry);
74,717✔
661
        if (ret < 0) {
74,717✔
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;
74,717✔
669
        
670
        ret = metaStableTagFilterCacheUpdateUid(
74,717✔
671
          pMeta, &e, &stbEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
672
        if (ret < 0) {
74,717✔
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;
74,717✔
679
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
74,717✔
680
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
74,717✔
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++) {
385,337✔
689
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
310,620✔
690
            if (!IS_IDX_ON(pTagColumn)) continue;
310,620✔
691
            STagIdxKey *pTagIdxKey = NULL;
74,717✔
692
            int32_t     nTagIdxKey;
74,717✔
693

694
            const void *pTagData = NULL;
74,717✔
695
            int32_t     nTagData = 0;
74,717✔
696

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

730
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
78,308✔
731
  if (ret < 0) {
78,308✔
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);
78,308✔
736
  if (ret < 0) {
78,308✔
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);
78,308✔
741
  if (ret < 0) {
78,308✔
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);
78,308✔
747
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
78,308✔
748

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

751
  if (e.type == TSDB_CHILD_TABLE) {
78,308✔
752
    ret =
753
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
74,717✔
754
    if (ret < 0) {
74,717✔
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;
74,717✔
760
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
74,717✔
761
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
74,717✔
762
    if (ret < 0) {
74,717✔
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);
74,717✔
767
    if (ret < 0) {
74,717✔
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) {
3,591✔
777
    // drop schema.db (todo)
778

779
    --pMeta->pVnode->config.vndStats.numOfNTables;
3,591✔
780
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
3,591✔
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);
78,308✔
819
  if (ret < 0) {
78,308✔
820
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
78,308✔
821
              tstrerror(ret));
822
  }
823

824
  tDecoderClear(&dc);
78,308✔
825
  tdbFree(pData);
78,308✔
826

827
  return 0;
78,308✔
828
}
829

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

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

846
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
11,438,442✔
847
  pMeta->changed = true;
11,438,442✔
848
  switch (pReq->action) {
11,438,442✔
849
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,348,724✔
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,348,724✔
853
    case TSDB_ALTER_TABLE_DROP_COLUMN:
94,614✔
854
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
94,614✔
855
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
472,309✔
856
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
472,309✔
857
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
42,963✔
858
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
42,963✔
859
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,322,831✔
860
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,322,831✔
861
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,160✔
862
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,160✔
863
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
23,942✔
864
      return metaUpdateTableOptions2(pMeta, version, pReq);
23,942✔
865
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5,869✔
866
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5,869✔
867
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
71,378✔
868
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
71,378✔
869
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
52,652✔
870
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
52,652✔
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) {
564,262✔
878
  if (!tsTtlChangeOnWrite) return 0;
564,262✔
879

880
  if (changeTimeMs <= 0) {
564,262✔
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};
564,262✔
886

887
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
564,262✔
888
}
889

890
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
672,456,824✔
891
  if (!tsTtlChangeOnWrite) return 0;
672,456,824✔
892

893
  metaWLock(pMeta);
569,146✔
894
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
564,262✔
895
  metaULock(pMeta);
564,262✔
896
  return ret;
564,262✔
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,
76,627,578✔
900
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
901
  if (IS_VAR_DATA_TYPE(type)) {
76,627,578✔
902
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
9,440,672✔
903
  } else {
904
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
67,186,906✔
905
  }
906

907
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
76,636,902✔
908
  if (*ppTagIdxKey == NULL) {
76,633,856✔
909
    return terrno;
×
910
  }
911

912
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
76,630,375✔
913
  (*ppTagIdxKey)->cid = cid;
76,631,172✔
914
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
76,633,850✔
915
  (*ppTagIdxKey)->type = type;
76,634,050✔
916

917
  // refactor
918
  if (IS_VAR_DATA_TYPE(type)) {
76,630,290✔
919
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
9,461,212✔
920
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
9,453,617✔
921
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
9,453,423✔
922
  } else {
923
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
67,169,078✔
924
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
67,174,791✔
925
  }
926

927
  return 0;
76,627,197✔
928
}
929

930
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
73,520,496✔
931
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
73,520,496✔
932
}
73,519,905✔
933

934
static void colCompressDebug(SHashObj *pColCmprObj) {
77,908,418✔
935
  void *p = taosHashIterate(pColCmprObj, NULL);
77,908,418✔
936
  while (p) {
663,534,679✔
937
    uint32_t cmprAlg = *(uint32_t *)p;
585,607,635✔
938
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
585,559,712✔
939
    p = taosHashIterate(pColCmprObj, p);
585,558,521✔
940

941
    uint8_t l1, l2, lvl;
585,765,597✔
942
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
585,774,899✔
943

944
    const char *l1str = columnEncodeStr(l1);
585,785,080✔
945
    const char *l2str = columnCompressStr(l2);
585,663,147✔
946
    const char *lvlstr = columnLevelStr(lvl);
585,624,400✔
947
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
585,591,403✔
948
  }
949
  return;
77,927,044✔
950
}
951

952
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
77,931,434✔
953
  int rc = 0;
77,931,434✔
954

955
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
77,931,434✔
956
  if (pColCmprObj == NULL) {
77,930,806✔
957
    pColCmprObj = NULL;
×
958
    return TSDB_CODE_OUT_OF_MEMORY;
×
959
  }
960

961
  void      *pData = NULL;
77,930,806✔
962
  int        nData = 0;
77,934,300✔
963
  SMetaEntry e = {0};
77,936,024✔
964
  SDecoder   dc = {0};
77,935,902✔
965

966
  *ppColCmprObj = NULL;
77,935,124✔
967

968
  metaRLock(pMeta);
77,927,329✔
969
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
77,937,841✔
970
  if (rc < 0) {
77,933,335✔
971
    taosHashCleanup(pColCmprObj);
14,550✔
972
    metaULock(pMeta);
14,550✔
973
    return TSDB_CODE_FAILED;
14,550✔
974
  }
975
  int64_t version = ((SUidIdxVal *)pData)[0].version;
77,918,785✔
976
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
77,914,497✔
977
  if (rc < 0) {
77,912,620✔
978
    metaULock(pMeta);
×
979
    taosHashCleanup(pColCmprObj);
×
980
    metaError("failed to get table entry");
×
981
    return rc;
×
982
  }
983

984
  tDecoderInit(&dc, pData, nData);
77,912,620✔
985
  rc = metaDecodeEntry(&dc, &e);
77,913,301✔
986
  if (rc < 0) {
77,903,721✔
987
    tDecoderClear(&dc);
×
988
    tdbFree(pData);
×
989
    metaULock(pMeta);
×
990
    taosHashCleanup(pColCmprObj);
×
991
    return rc;
×
992
  }
993
  if (withExtSchema(e.type)) {
77,903,721✔
994
    SColCmprWrapper *p = &e.colCmpr;
77,914,378✔
995
    for (int32_t i = 0; i < p->nCols; i++) {
663,631,870✔
996
      SColCmpr *pCmpr = &p->pColCmpr[i];
585,721,965✔
997
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
585,708,758✔
998
      if (rc < 0) {
585,717,492✔
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);
77,885,564✔
1014
  tdbFree(pData);
77,922,002✔
1015
  metaULock(pMeta);
77,915,304✔
1016

1017
  *ppColCmprObj = pColCmprObj;
77,907,903✔
1018
  colCompressDebug(pColCmprObj);
77,911,045✔
1019

1020
  return 0;
77,927,044✔
1021
}
1022
// refactor later
1023
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
3,928,997✔
1024
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
3,928,997✔
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