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

taosdata / TDengine / #4929

16 Jan 2026 02:32AM UTC coverage: 66.723% (+0.02%) from 66.708%
#4929

push

travis-ci

web-flow
enh: interp supports using non-null prev/next values to fill (#34236)

281 of 327 new or added lines in 11 files covered. (85.93%)

539 existing lines in 127 files now uncovered.

203225 of 304580 relevant lines covered (66.72%)

128590777.46 hits per line

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

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

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

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
82,452✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
82,452✔
92
  if (dropColId == newColNum) {
9,392✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
3,608✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
5,784✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
5,784✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
35,968✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
34,536✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
1,432✔
104
  return 0;
1,432✔
105
}
106

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

116
    SColRef *pCol = p + nCols;
56,220✔
117
    if (NULL == pColRef) {
56,220✔
118
      pCol->hasRef = false;
5,412✔
119
      pCol->id = pSchema->colId;
5,412✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
50,808✔
122
      pCol->id = pSchema->colId;
50,808✔
123
      if (pCol->hasRef) {
50,808✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
20,724✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
20,724✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
20,724✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
56,220✔
130
    pWp->version++;
56,220✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
34,785,439✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
34,785,439✔
134
      if (pOColRef->id == pSchema->colId) {
34,785,439✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
32,686✔
136
        if (left) {
32,686✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
30,372✔
138
        }
139
        nCols--;
32,686✔
140
        break;
32,686✔
141
      }
142
    }
143
    pWp->nCols = nCols;
32,686✔
144
    pWp->version++;
32,686✔
145
  }
146
  return 0;
88,906✔
147
}
148

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

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

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

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

171
  return 0;
9,082,191✔
172
}
173

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

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

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

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

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
214,175,540✔
211
    SColRef* pColRef = &pRef->pColRef[i];
213,578,301✔
212
    if (pColRef->hasRef) {
213,578,301✔
213
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
141,707,061✔
214
      if (code) {
141,707,061✔
215
        goto _return;
×
216
      }
217
    }
218
  }
219

220
  if (taosHashGetSize(pColRefHash) > 2000) {
597,239✔
221
    code = TSDB_CODE_VTABLE_TOO_MANY_REFERENCE;
1,058✔
222
    goto _return;
1,058✔
223
  }
224

225
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
596,181✔
226
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
596,181✔
227
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
596,181✔
228
    pMetaRsp->tuid = pEntry->uid;
338,403✔
229
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
257,778✔
230
    pMetaRsp->tuid = pEntry->uid;
257,778✔
231
    pMetaRsp->suid = pEntry->ctbEntry.suid;
257,778✔
232
  }
233

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

239
  taosHashCleanup(pColRefHash);
596,181✔
240
  return code;
596,181✔
241
_return:
1,058✔
242
  taosHashCleanup(pColRefHash);
1,058✔
243
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
1,058✔
244
  taosMemoryFreeClear(pMetaRsp->pSchemas);
1,058✔
245
  taosMemoryFreeClear(pMetaRsp->pColRefs);
1,058✔
246
  return code;
1,058✔
247
}
248

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

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

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

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

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

275
  int16_t nCols = taosArrayGetSize(pTagVals);
254,826✔
276
  for (int i = 0; i < nCols; i++) {
622,957✔
277
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
368,131✔
278
    char     type = pTagVal->type;
368,131✔
279

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

283
    SIndexTerm *term = NULL;
368,131✔
284
    if (type == TSDB_DATA_TYPE_NULL) {
368,131✔
285
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,724✔
286
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
365,407✔
287
      if (pTagVal->nData > 0) {
347,762✔
288
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
345,946✔
289
        if (val == NULL) {
345,946✔
290
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
291
        }
292
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
345,946✔
293
        if (len < 0) {
345,946✔
294
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
295
        }
296
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
345,946✔
297
        type = TSDB_DATA_TYPE_VARCHAR;
345,946✔
298
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
345,946✔
299
        taosMemoryFree(val);
345,946✔
300
      } else if (pTagVal->nData == 0) {
1,816✔
301
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,816✔
302
      }
303
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
17,645✔
304
      double val = *(double *)(&pTagVal->i64);
14,013✔
305
      int    len = sizeof(val);
14,013✔
306
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
14,013✔
307
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,632✔
308
      int val = *(int *)(&pTagVal->i64);
3,632✔
309
      int len = sizeof(val);
3,632✔
310
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,632✔
311
    }
312

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

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

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

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

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

360
  int16_t nCols = taosArrayGetSize(pTagVals);
113,364✔
361
  for (int i = 0; i < nCols; i++) {
325,040✔
362
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
211,676✔
363
    char     type = pTagVal->type;
211,676✔
364

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

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

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

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

429
  metaWLock(pMeta);
17,411✔
430
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
97,888✔
431
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
80,477✔
432
    tb_uid_t suid = 0;
80,477✔
433
    int8_t   sysTbl = 0;
80,477✔
434
    int      type;
80,477✔
435
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
80,477✔
436
    if (code) return code;
80,477✔
437
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
80,477✔
438
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
77,565✔
439
      if (pVal) {
77,565✔
440
        nCtbDropped = *pVal + 1;
60,436✔
441
      } else {
442
        nCtbDropped = 1;
17,129✔
443
      }
444
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
77,565✔
445
      if (code) return code;
77,565✔
446
    }
447
    /*
448
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
449
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
450
    }
451
    */
452
    metaDebug("batch drop table:%" PRId64, uid);
80,477✔
453
  }
454
  metaULock(pMeta);
17,411✔
455

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

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

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

482
  TBC *pCur;
24,340✔
483
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
24,340✔
484
  if (code < 0) {
24,340✔
485
    return code;
×
486
  }
487

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

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

497
  while (1) {
179,577✔
498
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
203,917✔
499
    if (ret < 0) {
203,917✔
500
      break;
24,340✔
501
    }
502

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

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

531
  return 0;
24,340✔
532
}
533

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

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

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

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

554
end:
17,411✔
555
  taosArrayDestroy(tbUids);
24,340✔
556

557
  return code;
24,340✔
558
}
559

560
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
9,675,689✔
561
  metaRLock(pMeta);
9,675,689✔
562

563
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
9,689,763✔
564

565
  metaULock(pMeta);
9,670,541✔
566

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

571
  return ret;
9,678,275✔
572
}
573

574
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
80,477✔
575
  int64_t btime;
576
  if (pME->type == TSDB_CHILD_TABLE) {
80,477✔
577
    btime = pME->ctbEntry.btime;
77,565✔
578
  } else if (pME->type == TSDB_NORMAL_TABLE) {
2,912✔
579
    btime = pME->ntbEntry.btime;
2,912✔
580
  } else {
581
    return TSDB_CODE_FAILED;
×
582
  }
583

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

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

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

602
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
80,477✔
603
  if (pME->type == TSDB_CHILD_TABLE) {
80,477✔
604
    ctx.ttlDays = pME->ctbEntry.ttlDays;
77,565✔
605
  } else {
606
    ctx.ttlDays = pME->ntbEntry.ttlDays;
2,912✔
607
  }
608

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

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

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

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

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

644
  if (type) *type = e.type;
80,477✔
645

646
  if (e.type == TSDB_CHILD_TABLE) {
80,477✔
647
    if (pSuid) *pSuid = e.ctbEntry.suid;
77,565✔
648
    void *tData = NULL;
77,565✔
649
    int   tLen = 0;
77,565✔
650

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

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

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

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

692
            const void *pTagData = NULL;
77,565✔
693
            int32_t     nTagData = 0;
77,565✔
694

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

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

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

744
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
80,477✔
745
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
80,477✔
746

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

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

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

777
    --pMeta->pVnode->config.vndStats.numOfNTables;
2,912✔
778
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
2,912✔
779

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

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

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

822
  tDecoderClear(&dc);
80,477✔
823
  tdbFree(pData);
80,477✔
824

825
  return 0;
80,477✔
826
}
827

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

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

844
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
11,829,852✔
845
  pMeta->changed = true;
11,829,852✔
846
  switch (pReq->action) {
11,829,852✔
847
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,465,404✔
848
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
849
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
850
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,465,404✔
851
    case TSDB_ALTER_TABLE_DROP_COLUMN:
82,452✔
852
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
82,452✔
853
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
477,921✔
854
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
477,921✔
855
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
44,102✔
856
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
44,102✔
857
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,598,943✔
858
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,598,943✔
859
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,285✔
860
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,285✔
861
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
24,570✔
862
      return metaUpdateTableOptions2(pMeta, version, pReq);
24,570✔
863
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5,963✔
864
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5,963✔
865
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
73,188✔
866
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
73,188✔
867
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
54,024✔
868
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
54,024✔
869
    default:
×
870
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
871
      break;
872
  }
873
}
874

875
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
20,179,695✔
876
  if (!tsTtlChangeOnWrite) return 0;
20,179,695✔
877

878
  if (changeTimeMs <= 0) {
20,179,695✔
879
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
880
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
881
  }
882

883
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
20,179,695✔
884

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

888
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
714,754,786✔
889
  if (!tsTtlChangeOnWrite) return 0;
714,754,786✔
890

891
  metaWLock(pMeta);
20,196,363✔
892
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
20,179,695✔
893
  metaULock(pMeta);
20,179,695✔
894
  return ret;
20,179,695✔
895
}
896

897
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
80,248,968✔
898
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
899
  if (IS_VAR_DATA_TYPE(type)) {
80,248,968✔
900
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
9,712,067✔
901
  } else {
902
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
70,536,901✔
903
  }
904

905
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
80,249,265✔
906
  if (*ppTagIdxKey == NULL) {
80,249,619✔
907
    return terrno;
×
908
  }
909

910
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
80,248,491✔
911
  (*ppTagIdxKey)->cid = cid;
80,242,723✔
912
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
80,251,810✔
913
  (*ppTagIdxKey)->type = type;
80,249,344✔
914

915
  // refactor
916
  if (IS_VAR_DATA_TYPE(type)) {
80,243,531✔
917
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
9,737,789✔
918
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
9,714,806✔
919
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
9,716,527✔
920
  } else {
921
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
70,505,742✔
922
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
70,507,275✔
923
  }
924

925
  return 0;
80,240,108✔
926
}
927

928
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
77,192,500✔
929
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
77,192,500✔
930
}
77,192,201✔
931

932
static void colCompressDebug(SHashObj *pColCmprObj) {
88,804,255✔
933
  void *p = taosHashIterate(pColCmprObj, NULL);
88,804,255✔
934
  while (p) {
756,555,015✔
935
    uint32_t cmprAlg = *(uint32_t *)p;
667,720,998✔
936
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
667,677,652✔
937
    p = taosHashIterate(pColCmprObj, p);
667,687,400✔
938

939
    uint8_t l1, l2, lvl;
667,919,846✔
940
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
667,930,665✔
941

942
    const char *l1str = columnEncodeStr(l1);
667,908,441✔
943
    const char *l2str = columnCompressStr(l2);
667,750,513✔
944
    const char *lvlstr = columnLevelStr(lvl);
667,715,409✔
945
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
667,718,310✔
946
  }
947
  return;
88,834,017✔
948
}
949

950
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
88,856,750✔
951
  int rc = 0;
88,856,750✔
952

953
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
88,856,750✔
954
  if (pColCmprObj == NULL) {
88,855,216✔
955
    pColCmprObj = NULL;
×
956
    return TSDB_CODE_OUT_OF_MEMORY;
×
957
  }
958

959
  void      *pData = NULL;
88,855,216✔
960
  int        nData = 0;
88,861,292✔
961
  SMetaEntry e = {0};
88,862,941✔
962
  SDecoder   dc = {0};
88,862,175✔
963

964
  *ppColCmprObj = NULL;
88,855,305✔
965

966
  metaRLock(pMeta);
88,854,858✔
967
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
88,870,731✔
968
  if (rc < 0) {
88,861,060✔
969
    taosHashCleanup(pColCmprObj);
41,320✔
970
    metaULock(pMeta);
41,320✔
971
    return TSDB_CODE_FAILED;
41,320✔
972
  }
973
  int64_t version = ((SUidIdxVal *)pData)[0].version;
88,819,740✔
974
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
88,814,643✔
975
  if (rc < 0) {
88,814,411✔
976
    metaULock(pMeta);
×
977
    taosHashCleanup(pColCmprObj);
×
978
    metaError("failed to get table entry");
×
979
    return rc;
×
980
  }
981

982
  tDecoderInit(&dc, pData, nData);
88,814,411✔
983
  rc = metaDecodeEntry(&dc, &e);
88,820,403✔
984
  if (rc < 0) {
88,792,389✔
985
    tDecoderClear(&dc);
×
986
    tdbFree(pData);
×
987
    metaULock(pMeta);
×
988
    taosHashCleanup(pColCmprObj);
×
989
    return rc;
×
990
  }
991
  if (withExtSchema(e.type)) {
88,792,389✔
992
    SColCmprWrapper *p = &e.colCmpr;
88,816,444✔
993
    for (int32_t i = 0; i < p->nCols; i++) {
756,651,416✔
994
      SColCmpr *pCmpr = &p->pColCmpr[i];
667,826,968✔
995
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
667,840,752✔
996
      if (rc < 0) {
667,834,972✔
997
        tDecoderClear(&dc);
×
998
        tdbFree(pData);
×
999
        metaULock(pMeta);
×
1000
        taosHashCleanup(pColCmprObj);
×
1001
        return rc;
×
1002
      }
1003
    }
1004
  } else {
UNCOV
1005
    tDecoderClear(&dc);
×
1006
    tdbFree(pData);
×
1007
    metaULock(pMeta);
×
1008
    taosHashCleanup(pColCmprObj);
×
1009
    return 0;
×
1010
  }
1011
  tDecoderClear(&dc);
88,776,251✔
1012
  tdbFree(pData);
88,823,052✔
1013
  metaULock(pMeta);
88,811,472✔
1014

1015
  *ppColCmprObj = pColCmprObj;
88,805,334✔
1016
  colCompressDebug(pColCmprObj);
88,810,715✔
1017

1018
  return 0;
88,833,718✔
1019
}
1020
// refactor later
1021
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
3,930,462✔
1022
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
3,930,462✔
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