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

taosdata / TDengine / #4946

02 Feb 2026 09:27AM UTC coverage: 66.932% (+0.06%) from 66.87%
#4946

push

travis-ci

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

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

413 existing lines in 119 files now uncovered.

205724 of 307364 relevant lines covered (66.93%)

126528878.24 hits per line

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

79.69
/source/dnode/vnode/src/meta/metaTable.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "meta.h"
17

18
extern SDmNotifyHandle dmNotifyHdl;
19

20
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
21
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
22
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
23
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
24
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
25
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
26
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
27
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
28
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
29
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
30
int32_t metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
31

32
int32_t    metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
33
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs);
34
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl);
35
void       metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey);
36
// opt ins_tables query
37
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
38
static int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
39

40
int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress) {
3,127,198✔
41
  int32_t nCols = pWp->nCols;
3,127,198✔
42
  int32_t ver = pWp->version;
3,127,198✔
43
  if (add) {
3,127,198✔
44
    SColCmpr *p = taosMemoryRealloc(pWp->pColCmpr, sizeof(SColCmpr) * (nCols + 1));
3,062,487✔
45
    if (p == NULL) {
3,062,487✔
46
      return terrno;
×
47
    }
48
    pWp->pColCmpr = p;
3,062,487✔
49

50
    SColCmpr *pCol = p + nCols;
3,062,487✔
51
    pCol->id = pSchema->colId;
3,062,487✔
52
    pCol->alg = compress;
3,062,487✔
53
    pWp->nCols = nCols + 1;
3,062,487✔
54
    pWp->version = ver;
3,062,487✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
30,326,833✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
30,326,833✔
58
      if (pOCmpr->id == pSchema->colId) {
30,326,833✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
64,711✔
60
        if (left) {
64,711✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
43,110✔
62
        }
63
        nCols--;
64,711✔
64
        break;
64,711✔
65
      }
66
    }
67
    pWp->nCols = nCols;
64,711✔
68
    pWp->version = ver;
64,711✔
69
  }
70
  return 0;
3,127,198✔
71
}
72

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
93,238✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
93,238✔
92
  if (dropColId == newColNum) {
13,567✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
5,411✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
8,156✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
8,156✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
59,126✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
56,460✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
2,666✔
104
  return 0;
2,666✔
105
}
106

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

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

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

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

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

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

171
  return 0;
9,972,364✔
172
}
173

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

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

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

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

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
208,619,658✔
211
    SColRef* pColRef = &pRef->pColRef[i];
208,030,197✔
212
    if (pColRef->hasRef) {
208,030,197✔
213
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
138,007,710✔
214
      if (code) {
138,007,710✔
215
        goto _return;
×
216
      }
217
    }
218
  }
219

220
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
589,461✔
221
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
589,461✔
222
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
589,461✔
223
    pMetaRsp->tuid = pEntry->uid;
333,878✔
224
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
255,583✔
225
    pMetaRsp->tuid = pEntry->uid;
255,583✔
226
    pMetaRsp->suid = pEntry->ctbEntry.suid;
255,583✔
227
  }
228

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

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

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

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

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

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

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

270
  int16_t nCols = taosArrayGetSize(pTagVals);
256,787✔
271
  for (int i = 0; i < nCols; i++) {
624,492✔
272
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
367,705✔
273
    char     type = pTagVal->type;
367,705✔
274

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

278
    SIndexTerm *term = NULL;
367,705✔
279
    if (type == TSDB_DATA_TYPE_NULL) {
367,705✔
280
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,700✔
281
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
365,005✔
282
      if (pTagVal->nData > 0) {
347,474✔
283
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
345,674✔
284
        if (val == NULL) {
345,674✔
285
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
286
        }
287
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
345,674✔
288
        if (len < 0) {
345,674✔
289
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
290
        }
291
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
345,674✔
292
        type = TSDB_DATA_TYPE_VARCHAR;
345,674✔
293
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
345,674✔
294
        taosMemoryFree(val);
345,674✔
295
      } else if (pTagVal->nData == 0) {
1,800✔
296
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,800✔
297
      }
298
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
17,531✔
299
      double val = *(double *)(&pTagVal->i64);
13,931✔
300
      int    len = sizeof(val);
13,931✔
301
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
13,931✔
302
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,600✔
303
      int val = *(int *)(&pTagVal->i64);
3,600✔
304
      int len = sizeof(val);
3,600✔
305
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,600✔
306
    }
307

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

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

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

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

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

355
  int16_t nCols = taosArrayGetSize(pTagVals);
111,654✔
356
  for (int i = 0; i < nCols; i++) {
320,137✔
357
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
208,483✔
358
    char     type = pTagVal->type;
208,483✔
359

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

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

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

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

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

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

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

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

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

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

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

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

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

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

526
  return 0;
23,744✔
527
}
528

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

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

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

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

549
end:
18,106✔
550
  taosArrayDestroy(tbUids);
23,744✔
551

552
  return code;
23,744✔
553
}
554

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

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

560
  metaULock(pMeta);
9,888,712✔
561

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

566
  return ret;
9,896,592✔
567
}
568

569
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
78,256✔
570
  int64_t btime;
571
  if (pME->type == TSDB_CHILD_TABLE) {
78,256✔
572
    btime = pME->ctbEntry.btime;
72,767✔
573
  } else if (pME->type == TSDB_NORMAL_TABLE) {
5,489✔
574
    btime = pME->ntbEntry.btime;
5,489✔
575
  } else {
576
    return TSDB_CODE_FAILED;
×
577
  }
578

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

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

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

597
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
78,256✔
598
  if (pME->type == TSDB_CHILD_TABLE) {
78,256✔
599
    ctx.ttlDays = pME->ctbEntry.ttlDays;
72,767✔
600
  } else {
601
    ctx.ttlDays = pME->ntbEntry.ttlDays;
5,489✔
602
  }
603

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

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

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

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

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

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

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

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

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

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

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

687
            const void *pTagData = NULL;
72,767✔
688
            int32_t     nTagData = 0;
72,767✔
689

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

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

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

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

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

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

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

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

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

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

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

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

820
  return 0;
78,256✔
821
}
822

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

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

839
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
11,665,066✔
840
  pMeta->changed = true;
11,665,066✔
841
  switch (pReq->action) {
11,665,066✔
842
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,419,347✔
843
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
844
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
845
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,419,347✔
846
    case TSDB_ALTER_TABLE_DROP_COLUMN:
93,238✔
847
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
93,238✔
848
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
472,270✔
849
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
472,270✔
850
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
43,408✔
851
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
43,408✔
852
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,478,761✔
853
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,478,761✔
854
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,185✔
855
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,185✔
856
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
24,124✔
857
      return metaUpdateTableOptions2(pMeta, version, pReq);
24,124✔
858
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5,830✔
859
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5,830✔
860
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
71,855✔
861
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
71,855✔
862
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
53,048✔
863
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
53,048✔
864
    default:
×
865
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
866
      break;
867
  }
868
}
869

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

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

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

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

883
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
700,741,565✔
884
  if (!tsTtlChangeOnWrite) return 0;
700,741,565✔
885

886
  metaWLock(pMeta);
19,805,823✔
887
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
19,784,970✔
888
  metaULock(pMeta);
19,784,970✔
889
  return ret;
19,784,970✔
890
}
891

892
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
81,435,643✔
893
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
894
  if (IS_VAR_DATA_TYPE(type)) {
81,435,643✔
895
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
9,459,815✔
896
  } else {
897
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
71,975,828✔
898
  }
899

900
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
81,457,423✔
901
  if (*ppTagIdxKey == NULL) {
81,453,800✔
902
    return terrno;
×
903
  }
904

905
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
81,443,493✔
906
  (*ppTagIdxKey)->cid = cid;
81,451,705✔
907
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
81,453,915✔
908
  (*ppTagIdxKey)->type = type;
81,464,362✔
909

910
  // refactor
911
  if (IS_VAR_DATA_TYPE(type)) {
81,454,473✔
912
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
9,501,146✔
913
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
9,483,734✔
914
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
9,483,784✔
915
  } else {
916
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
71,953,327✔
917
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
71,962,225✔
918
  }
919

920
  return 0;
81,448,226✔
921
}
922

923
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
78,454,284✔
924
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
78,454,284✔
925
}
78,451,791✔
926

927
static void colCompressDebug(SHashObj *pColCmprObj) {
94,249,526✔
928
  void *p = taosHashIterate(pColCmprObj, NULL);
94,249,526✔
929
  while (p) {
763,942,971✔
930
    uint32_t cmprAlg = *(uint32_t *)p;
669,665,588✔
931
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
669,707,503✔
932
    p = taosHashIterate(pColCmprObj, p);
669,678,070✔
933

934
    uint8_t l1, l2, lvl;
669,868,911✔
935
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
669,845,782✔
936

937
    const char *l1str = columnEncodeStr(l1);
669,849,006✔
938
    const char *l2str = columnCompressStr(l2);
669,763,183✔
939
    const char *lvlstr = columnLevelStr(lvl);
669,743,291✔
940
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
669,697,917✔
941
  }
942
  return;
94,277,383✔
943
}
944

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

948
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
94,306,798✔
949
  if (pColCmprObj == NULL) {
94,307,719✔
950
    pColCmprObj = NULL;
×
951
    return TSDB_CODE_OUT_OF_MEMORY;
×
952
  }
953

954
  void      *pData = NULL;
94,307,719✔
955
  int        nData = 0;
94,302,071✔
956
  SMetaEntry e = {0};
94,314,403✔
957
  SDecoder   dc = {0};
94,313,529✔
958

959
  *ppColCmprObj = NULL;
94,314,752✔
960

961
  metaRLock(pMeta);
94,313,880✔
962
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
94,319,998✔
963
  if (rc < 0) {
94,301,235✔
964
    taosHashCleanup(pColCmprObj);
43,044✔
965
    metaULock(pMeta);
43,044✔
966
    return TSDB_CODE_FAILED;
43,044✔
967
  }
968
  int64_t version = ((SUidIdxVal *)pData)[0].version;
94,258,191✔
969
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
94,266,805✔
970
  if (rc < 0) {
94,260,707✔
971
    metaULock(pMeta);
×
972
    taosHashCleanup(pColCmprObj);
×
973
    metaError("failed to get table entry");
×
974
    return rc;
×
975
  }
976

977
  tDecoderInit(&dc, pData, nData);
94,260,707✔
978
  rc = metaDecodeEntry(&dc, &e);
94,270,211✔
979
  if (rc < 0) {
94,229,260✔
980
    tDecoderClear(&dc);
×
981
    tdbFree(pData);
×
982
    metaULock(pMeta);
×
983
    taosHashCleanup(pColCmprObj);
×
984
    return rc;
×
985
  }
986
  if (withExtSchema(e.type)) {
94,229,260✔
987
    SColCmprWrapper *p = &e.colCmpr;
94,264,329✔
988
    for (int32_t i = 0; i < p->nCols; i++) {
764,023,705✔
989
      SColCmpr *pCmpr = &p->pColCmpr[i];
669,777,596✔
990
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
669,783,385✔
991
      if (rc < 0) {
669,759,376✔
992
        tDecoderClear(&dc);
×
993
        tdbFree(pData);
×
994
        metaULock(pMeta);
×
995
        taosHashCleanup(pColCmprObj);
×
996
        return rc;
×
997
      }
998
    }
999
  } else {
1000
    tDecoderClear(&dc);
×
1001
    tdbFree(pData);
×
1002
    metaULock(pMeta);
×
1003
    taosHashCleanup(pColCmprObj);
×
1004
    return 0;
×
1005
  }
1006
  tDecoderClear(&dc);
94,237,795✔
1007
  tdbFree(pData);
94,261,022✔
1008
  metaULock(pMeta);
94,262,534✔
1009

1010
  *ppColCmprObj = pColCmprObj;
94,257,052✔
1011
  colCompressDebug(pColCmprObj);
94,255,117✔
1012

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