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

taosdata / TDengine / #4852

14 Nov 2025 08:06AM UTC coverage: 63.812% (+0.06%) from 63.754%
#4852

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

355 of 675 new or added lines in 18 files covered. (52.59%)

3142 existing lines in 128 files now uncovered.

149263 of 233910 relevant lines covered (63.81%)

118719500.98 hits per line

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

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

50
    SColCmpr *pCol = p + nCols;
3,191,447✔
51
    pCol->id = pSchema->colId;
3,191,447✔
52
    pCol->alg = compress;
3,191,447✔
53
    pWp->nCols = nCols + 1;
3,191,447✔
54
    pWp->version = ver;
3,191,447✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
3,922,296✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
3,922,296✔
58
      if (pOCmpr->id == pSchema->colId) {
3,922,296✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
50,677✔
60
        if (left) {
50,677✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
28,063✔
62
        }
63
        nCols--;
50,677✔
64
        break;
50,677✔
65
      }
66
    }
67
    pWp->nCols = nCols;
50,677✔
68
    pWp->version = ver;
50,677✔
69
  }
70
  return 0;
3,242,124✔
71
}
72

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

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

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

116
    SColRef *pCol = p + nCols;
58,374✔
117
    if (NULL == pColRef) {
58,374✔
118
      pCol->hasRef = false;
5,797✔
119
      pCol->id = pSchema->colId;
5,797✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
52,577✔
122
      pCol->id = pSchema->colId;
52,577✔
123
      if (pCol->hasRef) {
52,577✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
21,549✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
21,549✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
21,549✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
58,374✔
130
    pWp->version++;
58,374✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
130,160✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
130,160✔
134
      if (pOColRef->id == pSchema->colId) {
130,160✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
33,374✔
136
        if (left) {
33,374✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
32,068✔
138
        }
139
        nCols--;
33,374✔
140
        break;
33,374✔
141
      }
142
    }
143
    pWp->nCols = nCols;
33,374✔
144
    pWp->version++;
33,374✔
145
  }
146
  return 0;
91,748✔
147
}
148

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

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

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

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

171
  return 0;
6,107,071✔
172
}
173

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

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

193
    pMetaRsp->numOfColumns = pSchema->nCols;
393,490✔
194
    pMetaRsp->sversion = pSchema->version;
393,490✔
195
    memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
393,490✔
196
  }
197
  pMetaRsp->pColRefs = taosMemoryMalloc(pRef->nCols * sizeof(SColRef));
600,155✔
198
  if (NULL == pMetaRsp->pColRefs) {
600,155✔
199
    code = terrno;
×
200
    goto _return;
×
201
  }
202
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
600,155✔
203
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
600,155✔
204
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
600,155✔
205
    pMetaRsp->tuid = pEntry->uid;
335,099✔
206
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
265,056✔
207
    pMetaRsp->tuid = pEntry->uid;
265,056✔
208
    pMetaRsp->suid = pEntry->ctbEntry.suid;
265,056✔
209
  }
210

211
  pMetaRsp->tableType = tableType;
600,155✔
212
  pMetaRsp->virtualStb = false; // super table will never be processed here
600,155✔
213
  pMetaRsp->numOfColRefs = pRef->nCols;
600,155✔
214
  pMetaRsp->rversion = pRef->version;
600,155✔
215

216
  return code;
600,155✔
217
_return:
×
218
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
219
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
220
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
221
  return code;
×
222
}
223

224
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
326,320✔
225
  int32_t code = 0;
326,320✔
226

227
#ifdef USE_INVERTED_INDEX
228
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
326,320✔
229
    return TSDB_CODE_INVALID_PARA;
×
230
  }
231
  void       *data = pCtbEntry->ctbEntry.pTags;
326,320✔
232
  const char *tagName = pSchema->name;
326,320✔
233

234
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
326,320✔
235
  tb_uid_t    tuid = pCtbEntry->uid;
326,320✔
236
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
326,320✔
237
  int32_t     nTagData = 0;
326,320✔
238

239
  SArray *pTagVals = NULL;
326,320✔
240
  code = tTagToValArray((const STag *)data, &pTagVals);
326,320✔
241
  if (code) {
326,320✔
242
    return code;
×
243
  }
244

245
  SIndexMultiTerm *terms = indexMultiTermCreate();
326,320✔
246
  if (terms == NULL) {
326,320✔
247
    return terrno;
×
248
  }
249

250
  int16_t nCols = taosArrayGetSize(pTagVals);
326,320✔
251
  for (int i = 0; i < nCols; i++) {
784,911✔
252
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
458,591✔
253
    char     type = pTagVal->type;
458,591✔
254

255
    char   *key = pTagVal->pKey;
458,591✔
256
    int32_t nKey = strlen(key);
458,591✔
257

258
    SIndexTerm *term = NULL;
458,591✔
259
    if (type == TSDB_DATA_TYPE_NULL) {
458,591✔
260
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
3,228✔
261
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
455,363✔
262
      if (pTagVal->nData > 0) {
441,784✔
263
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
439,632✔
264
        if (val == NULL) {
439,632✔
265
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
266
        }
267
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
439,632✔
268
        if (len < 0) {
439,632✔
269
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
270
        }
271
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
439,632✔
272
        type = TSDB_DATA_TYPE_VARCHAR;
439,632✔
273
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
439,632✔
274
        taosMemoryFree(val);
439,632✔
275
      } else if (pTagVal->nData == 0) {
2,152✔
276
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
2,152✔
277
      }
278
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
13,579✔
279
      double val = *(double *)(&pTagVal->i64);
10,351✔
280
      int    len = sizeof(val);
10,351✔
281
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
10,351✔
282
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,228✔
283
      int val = *(int *)(&pTagVal->i64);
3,228✔
284
      int len = sizeof(val);
3,228✔
285
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,228✔
286
    }
287

288
    if (term != NULL) {
458,591✔
289
      int32_t ret = indexMultiTermAdd(terms, term);
458,591✔
290
      if (ret < 0) {
458,591✔
291
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
292
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
293
      }
294
    } else {
295
      code = terrno;
×
296
      goto _exception;
×
297
    }
298
  }
299
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
326,320✔
300
  indexMultiTermDestroy(terms);
326,320✔
301

302
  taosArrayDestroy(pTagVals);
326,320✔
303
  return code;
326,320✔
304
_exception:
×
305
  indexMultiTermDestroy(terms);
×
306
  taosArrayDestroy(pTagVals);
×
307
#endif
308
  return code;
×
309
}
310
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
120,270✔
311
int32_t code = 0;
120,270✔
312
#ifdef USE_INVERTED_INDEX
313
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
120,270✔
314
    return TSDB_CODE_INVALID_PARA;
×
315
  }
316
  void       *data = pCtbEntry->ctbEntry.pTags;
120,270✔
317
  const char *tagName = pSchema->name;
120,270✔
318

319
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
120,270✔
320
  tb_uid_t    tuid = pCtbEntry->uid;
120,270✔
321
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
120,270✔
322
  int32_t     nTagData = 0;
120,270✔
323

324
  SArray *pTagVals = NULL;
120,270✔
325
  code = tTagToValArray((const STag *)data, &pTagVals);
120,270✔
326
  if (code) {
120,270✔
327
    return code;
×
328
  }
329

330
  SIndexMultiTerm *terms = indexMultiTermCreate();
120,270✔
331
  if (terms == NULL) {
120,270✔
332
    return terrno;
×
333
  }
334

335
  int16_t nCols = taosArrayGetSize(pTagVals);
120,270✔
336
  for (int i = 0; i < nCols; i++) {
344,730✔
337
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
224,460✔
338
    char     type = pTagVal->type;
224,460✔
339

340
    char   *key = pTagVal->pKey;
224,460✔
341
    int32_t nKey = strlen(key);
224,460✔
342

343
    SIndexTerm *term = NULL;
224,460✔
344
    if (type == TSDB_DATA_TYPE_NULL) {
224,460✔
345
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
346
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
224,460✔
347
      if (pTagVal->nData > 0) {
221,770✔
348
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
221,770✔
349
        if (val == NULL) {
221,770✔
350
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
351
        }
352
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
221,770✔
353
        if (len < 0) {
221,770✔
354
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
355
        }
356
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
221,770✔
357
        type = TSDB_DATA_TYPE_VARCHAR;
221,770✔
358
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
221,770✔
359
        taosMemoryFree(val);
221,770✔
360
      } else if (pTagVal->nData == 0) {
×
361
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
362
      }
363
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
2,690✔
364
      double val = *(double *)(&pTagVal->i64);
1,614✔
365
      int    len = sizeof(val);
1,614✔
366
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
1,614✔
367
    } else if (type == TSDB_DATA_TYPE_BOOL) {
1,076✔
368
      int val = *(int *)(&pTagVal->i64);
1,076✔
369
      int len = sizeof(val);
1,076✔
370
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
1,076✔
371
    }
372
    if (term != NULL) {
224,460✔
373
      int32_t ret = indexMultiTermAdd(terms, term);
224,460✔
374
      if (ret < 0) {
224,460✔
375
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
376
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
377
      }
378
    } else {
379
      code = terrno;
×
380
      goto _exception;
×
381
    }
382
  }
383
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
120,270✔
384
  indexMultiTermDestroy(terms);
120,270✔
385
  taosArrayDestroy(pTagVals);
120,270✔
386
  return code;
120,270✔
387
_exception:
×
388
  indexMultiTermDestroy(terms);
×
389
  taosArrayDestroy(pTagVals);
×
390
#endif
391
  return code;
×
392
}
393

394
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
13,535✔
395
  int32_t code = 0;
13,535✔
396
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
13,535✔
397

398
  int64_t    nCtbDropped = 0;
13,535✔
399
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
13,535✔
400
  if (suidHash == NULL) {
13,535✔
401
    return terrno;
×
402
  }
403

404
  metaWLock(pMeta);
13,535✔
405
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
40,485✔
406
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
26,950✔
407
    tb_uid_t suid = 0;
26,950✔
408
    int8_t   sysTbl = 0;
26,950✔
409
    int      type;
26,950✔
410
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
26,950✔
411
    if (code) return code;
26,950✔
412
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
26,950✔
413
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
23,456✔
414
      if (pVal) {
23,456✔
415
        nCtbDropped = *pVal + 1;
10,627✔
416
      } else {
417
        nCtbDropped = 1;
12,829✔
418
      }
419
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
23,456✔
420
      if (code) return code;
23,456✔
421
    }
422
    /*
423
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
424
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
425
    }
426
    */
427
    metaDebug("batch drop table:%" PRId64, uid);
26,950✔
428
  }
429
  metaULock(pMeta);
13,535✔
430

431
  // update timeseries
432
  void   *pCtbDropped = NULL;
13,535✔
433
  int32_t iter = 0;
13,535✔
434
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
26,364✔
435
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
12,829✔
436
    int32_t      nCols = 0;
12,829✔
437
    int8_t       flags = 0;
12,829✔
438
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
12,829✔
439
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
12,829✔
440
      if (!TABLE_IS_VIRTUAL(flags)) {
12,829✔
441
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
12,829✔
442
      }
443
    }
444
  }
445
  tSimpleHashCleanup(suidHash);
13,535✔
446

447
  pMeta->changed = true;
13,535✔
448
  return 0;
13,535✔
449
}
450

451
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
20,323✔
452
  int32_t code = 0;
20,323✔
453
  // 1, tranverse table's
454
  // 2, validate table name using vnodeValidateTableHash
455
  // 3, push invalidated table's uid into uidList
456

457
  TBC *pCur;
20,323✔
458
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
20,323✔
459
  if (code < 0) {
20,323✔
460
    return code;
×
461
  }
462

463
  code = tdbTbcMoveToFirst(pCur);
20,323✔
464
  if (code) {
20,323✔
465
    tdbTbcClose(pCur);
×
466
    return code;
×
467
  }
468

469
  void *pData = NULL, *pKey = NULL;
20,323✔
470
  int   nData = 0, nKey = 0;
20,323✔
471

472
  while (1) {
68,674✔
473
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
88,997✔
474
    if (ret < 0) {
88,997✔
475
      break;
20,323✔
476
    }
477

478
    SMetaEntry me = {0};
68,674✔
479
    SDecoder   dc = {0};
68,674✔
480
    tDecoderInit(&dc, pData, nData);
68,674✔
481
    code = metaDecodeEntry(&dc, &me);
68,674✔
482
    if (code < 0) {
68,674✔
483
      tDecoderClear(&dc);
×
484
      return code;
×
485
    }
486

487
    if (me.type != TSDB_SUPER_TABLE) {
68,674✔
488
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
53,884✔
489
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
53,884✔
490
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
53,884✔
491
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
53,884✔
492
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
53,884✔
493
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
53,884✔
494
        if (taosArrayPush(uidList, &me.uid) == NULL) {
26,950✔
495
          code = terrno;
×
496
          break;
×
497
        }
498
      }
499
    }
500
    tDecoderClear(&dc);
68,674✔
501
  }
502
  tdbFree(pData);
20,323✔
503
  tdbFree(pKey);
20,323✔
504
  tdbTbcClose(pCur);
20,323✔
505

506
  return 0;
20,323✔
507
}
508

509
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
20,323✔
510
  int32_t code = 0;
20,323✔
511

512
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
20,323✔
513
  if (tbUids == NULL) {
20,323✔
514
    return terrno;
×
515
  }
516

517
  code = metaFilterTableByHash(pMeta, tbUids);
20,323✔
518
  if (code != 0) {
20,323✔
519
    goto end;
×
520
  }
521
  if (TARRAY_SIZE(tbUids) == 0) {
20,323✔
522
    goto end;
6,788✔
523
  }
524

525
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
13,535✔
526
  code = metaDropTables(pMeta, tbUids);
13,535✔
527
  if (code) goto end;
13,535✔
528

529
end:
13,535✔
530
  taosArrayDestroy(tbUids);
20,323✔
531

532
  return code;
20,323✔
533
}
534

535
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
9,012,646✔
536
  metaRLock(pMeta);
9,012,646✔
537

538
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
9,025,436✔
539

540
  metaULock(pMeta);
9,001,965✔
541

542
  if (ret != 0) {
9,013,531✔
543
    metaError("ttl failed to find expired table, ret:%d", ret);
×
544
  }
545

546
  return ret;
9,011,602✔
547
}
548

549
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
26,950✔
550
  int64_t btime;
551
  if (pME->type == TSDB_CHILD_TABLE) {
26,950✔
552
    btime = pME->ctbEntry.btime;
23,456✔
553
  } else if (pME->type == TSDB_NORMAL_TABLE) {
3,494✔
554
    btime = pME->ntbEntry.btime;
3,494✔
555
  } else {
556
    return TSDB_CODE_FAILED;
×
557
  }
558

559
  btimeKey->btime = btime;
26,950✔
560
  btimeKey->uid = pME->uid;
26,950✔
561
  return 0;
26,950✔
562
}
563

564
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
3,494✔
565
  if (pME->type == TSDB_NORMAL_TABLE) {
3,494✔
566
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
3,494✔
567
    ncolKey->uid = pME->uid;
3,494✔
568
  } else {
569
    return TSDB_CODE_FAILED;
×
570
  }
571
  return 0;
3,494✔
572
}
573

574
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
26,950✔
575
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
26,950✔
576

577
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
26,950✔
578
  if (pME->type == TSDB_CHILD_TABLE) {
26,950✔
579
    ctx.ttlDays = pME->ctbEntry.ttlDays;
23,456✔
580
  } else {
581
    ctx.ttlDays = pME->ntbEntry.ttlDays;
3,494✔
582
  }
583

584
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
26,950✔
585
  if (ret < 0) {
26,950✔
586
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
587
              pME->uid, tstrerror(ret));
588
  }
589
  return;
26,950✔
590
}
591

592
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
26,950✔
593
  void      *pData = NULL;
26,950✔
594
  int        nData = 0;
26,950✔
595
  int        rc = 0;
26,950✔
596
  SMetaEntry e = {0};
26,950✔
597
  SDecoder   dc = {0};
26,950✔
598
  int32_t    ret = 0;
26,950✔
599

600
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
26,950✔
601
  if (rc < 0) {
26,950✔
602
    return rc;
×
603
  }
604
  int64_t version = ((SUidIdxVal *)pData)[0].version;
26,950✔
605

606
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
26,950✔
607
  if (rc < 0) {
26,950✔
608
    tdbFree(pData);
×
609
    return rc;
×
610
  }
611

612
  tDecoderInit(&dc, pData, nData);
26,950✔
613
  rc = metaDecodeEntry(&dc, &e);
26,950✔
614
  if (rc < 0) {
26,950✔
615
    tDecoderClear(&dc);
×
616
    return rc;
×
617
  }
618

619
  if (type) *type = e.type;
26,950✔
620

621
  if (e.type == TSDB_CHILD_TABLE) {
26,950✔
622
    if (pSuid) *pSuid = e.ctbEntry.suid;
23,456✔
623
    void *tData = NULL;
23,456✔
624
    int   tLen = 0;
23,456✔
625

626
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
23,456✔
627
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
23,456✔
628
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
23,456✔
629
        SDecoder   tdc = {0};
23,456✔
630
        SMetaEntry stbEntry = {0};
23,456✔
631

632
        tDecoderInit(&tdc, tData, tLen);
23,456✔
633
        ret = metaDecodeEntry(&tdc, &stbEntry);
23,456✔
634
        if (ret < 0) {
23,456✔
635
          tDecoderClear(&tdc);
×
636
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
637
                    e.ctbEntry.suid, tstrerror(ret));
638
          return ret;
×
639
        }
640

641
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
23,456✔
642
        
643
        ret = metaStableTagFilterCacheUpdateUid(
23,456✔
644
          pMeta, &e, &stbEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
645
        if (ret < 0) {
23,456✔
NEW
646
          metaError("vgId:%d, failed to update stable tag filter cache:%s "
×
647
            "uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
648
            e.ctbEntry.suid, tstrerror(ret));
649
        }
650

651
        SSchema        *pTagColumn = NULL;
23,456✔
652
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
23,456✔
653
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
23,456✔
654
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
UNCOV
655
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
UNCOV
656
          if (ret < 0) {
×
UNCOV
657
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
658
                      e.name, e.uid, tstrerror(ret));
659
          }
660
        } else {
661
          for (int i = 0; i < pTagSchema->nCols; i++) {
67,261✔
662
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
43,805✔
663
            if (!IS_IDX_ON(pTagColumn)) continue;
43,805✔
664
            STagIdxKey *pTagIdxKey = NULL;
23,456✔
665
            int32_t     nTagIdxKey;
23,456✔
666

667
            const void *pTagData = NULL;
23,456✔
668
            int32_t     nTagData = 0;
23,456✔
669

670
            STagVal tagVal = {.cid = pTagColumn->colId};
23,456✔
671
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
23,456✔
672
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
23,456✔
UNCOV
673
                pTagData = tagVal.pData;
×
UNCOV
674
                nTagData = (int32_t)tagVal.nData;
×
675
              } else {
676
                pTagData = &(tagVal.i64);
23,456✔
677
                nTagData = tDataTypes[pTagColumn->type].bytes;
23,456✔
678
              }
679
            } else {
UNCOV
680
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
681
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
682
              }
683
            }
684

685
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
23,456✔
686
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
687
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
23,456✔
688
              if (ret < 0) {
23,456✔
689
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
690
                          e.name, e.uid, tstrerror(ret));
691
              }
692
            }
693
            metaDestroyTagIdxKey(pTagIdxKey);
23,456✔
694
            pTagIdxKey = NULL;
23,456✔
695
          }
696
        }
697
        tDecoderClear(&tdc);
23,456✔
698
      }
699
      tdbFree(tData);
23,456✔
700
    }
701
  }
702

703
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
26,950✔
704
  if (ret < 0) {
26,950✔
UNCOV
705
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
706
              tstrerror(ret));
707
  }
708
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
26,950✔
709
  if (ret < 0) {
26,950✔
UNCOV
710
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
711
              tstrerror(ret));
712
  }
713
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
26,950✔
714
  if (ret < 0) {
26,950✔
UNCOV
715
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
716
              tstrerror(ret));
717
  }
718

719
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
26,950✔
720
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
26,950✔
721

722
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
26,950✔
723

724
  if (e.type == TSDB_CHILD_TABLE) {
26,950✔
725
    ret =
726
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
23,456✔
727
    if (ret < 0) {
23,456✔
UNCOV
728
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
729
                tstrerror(ret));
730
    }
731

732
    --pMeta->pVnode->config.vndStats.numOfCTables;
23,456✔
733
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
23,456✔
734
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
23,456✔
735
    if (ret < 0) {
23,456✔
736
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
737
                e.ctbEntry.suid, tstrerror(ret));
738
    }
739
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
23,456✔
740
    if (ret < 0) {
23,456✔
UNCOV
741
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
742
                e.ctbEntry.suid, tstrerror(ret));
743
    }
744
    /*
745
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
746
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
747
    }
748
    */
749
  } else if (e.type == TSDB_NORMAL_TABLE) {
3,494✔
750
    // drop schema.db (todo)
751

752
    --pMeta->pVnode->config.vndStats.numOfNTables;
3,494✔
753
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
3,494✔
754

755
    /*
756
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
757
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
758
    }
759
    */
UNCOV
760
  } else if (e.type == TSDB_SUPER_TABLE) {
×
UNCOV
761
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
UNCOV
762
    if (ret < 0) {
×
UNCOV
763
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
764
                tstrerror(ret));
765
    }
766
    // drop schema.db (todo)
767

768
    ret = metaStatsCacheDrop(pMeta, uid);
×
769
    if (ret < 0) {
×
770
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
771
                tstrerror(ret));
772
    }
UNCOV
773
    ret = metaUidCacheClear(pMeta, uid);
×
UNCOV
774
    if (ret < 0) {
×
UNCOV
775
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
776
                tstrerror(ret));
777
    }
NEW
778
    ret = metaStableTagFilterCacheDropSTable(pMeta, uid);
×
NEW
779
    if (ret < 0) {
×
NEW
780
      metaError("vgId:%d, failed to clear stable tag filter cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
781
                e.uid, tstrerror(ret));
782
    }
UNCOV
783
    ret = metaTbGroupCacheClear(pMeta, uid);
×
UNCOV
784
    if (ret < 0) {
×
UNCOV
785
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
786
                e.uid, tstrerror(ret));
787
    }
788
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
789
  }
790

791
  ret = metaCacheDrop(pMeta, uid);
26,950✔
792
  if (ret < 0) {
26,950✔
793
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
26,950✔
794
              tstrerror(ret));
795
  }
796

797
  tDecoderClear(&dc);
26,950✔
798
  tdbFree(pData);
26,950✔
799

800
  return 0;
26,950✔
801
}
802

803
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
26,950✔
804
  SBtimeIdxKey btimeKey = {0};
26,950✔
805
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
26,950✔
UNCOV
806
    return 0;
×
807
  }
808
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
26,950✔
809
}
810

811
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
3,494✔
812
  SNcolIdxKey ncolKey = {0};
3,494✔
813
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
3,494✔
UNCOV
814
    return 0;
×
815
  }
816
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
3,494✔
817
}
818

819
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
12,159,863✔
820
  pMeta->changed = true;
12,159,863✔
821
  switch (pReq->action) {
12,159,863✔
822
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,571,678✔
823
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
824
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
825
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,571,678✔
826
    case TSDB_ALTER_TABLE_DROP_COLUMN:
81,328✔
827
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
81,328✔
828
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
485,695✔
829
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
485,695✔
830
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
45,558✔
831
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
45,558✔
832
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,812,303✔
833
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,812,303✔
834
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,370✔
835
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,370✔
836
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
17,416✔
837
      return metaUpdateTableOptions2(pMeta, version, pReq);
17,416✔
838
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
6,552✔
839
      return metaUpdateTableColCompress2(pMeta, version, pReq);
6,552✔
840
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
78,729✔
841
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
78,729✔
842
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
57,234✔
843
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
57,234✔
UNCOV
844
    default:
×
UNCOV
845
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
846
      break;
847
  }
848
}
849

850
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
1,122,359✔
851
  if (!tsTtlChangeOnWrite) return 0;
1,122,359✔
852

853
  if (changeTimeMs <= 0) {
1,122,359✔
UNCOV
854
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
UNCOV
855
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
856
  }
857

858
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
1,122,359✔
859

860
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
1,122,359✔
861
}
862

863
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
556,247,477✔
864
  if (!tsTtlChangeOnWrite) return 0;
556,247,477✔
865

866
  metaWLock(pMeta);
1,155,044✔
867
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
1,122,359✔
868
  metaULock(pMeta);
1,122,359✔
869
  return ret;
1,122,359✔
870
}
871

872
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
80,406,544✔
873
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
874
  if (IS_VAR_DATA_TYPE(type)) {
80,406,544✔
875
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
5,881,584✔
876
  } else {
877
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
74,524,960✔
878
  }
879

880
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
80,407,744✔
881
  if (*ppTagIdxKey == NULL) {
80,398,424✔
UNCOV
882
    return terrno;
×
883
  }
884

885
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
80,405,148✔
886
  (*ppTagIdxKey)->cid = cid;
80,403,434✔
887
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
80,407,193✔
888
  (*ppTagIdxKey)->type = type;
80,403,511✔
889

890
  // refactor
891
  if (IS_VAR_DATA_TYPE(type)) {
80,403,486✔
892
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
5,893,124✔
893
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
5,881,403✔
894
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
5,882,543✔
895
  } else {
896
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
74,510,362✔
897
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
74,511,793✔
898
  }
899

900
  return 0;
80,396,807✔
901
}
902

903
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
76,675,699✔
904
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
76,675,699✔
905
}
76,675,748✔
906

907
static void colCompressDebug(SHashObj *pColCmprObj) {
70,059,979✔
908
  void *p = taosHashIterate(pColCmprObj, NULL);
70,059,979✔
909
  while (p) {
620,384,800✔
910
    uint32_t cmprAlg = *(uint32_t *)p;
550,307,771✔
911
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
550,287,542✔
912
    p = taosHashIterate(pColCmprObj, p);
550,283,177✔
913

914
    uint8_t l1, l2, lvl;
550,275,718✔
915
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
550,387,056✔
916

917
    const char *l1str = columnEncodeStr(l1);
550,377,980✔
918
    const char *l2str = columnCompressStr(l2);
550,329,465✔
919
    const char *lvlstr = columnLevelStr(lvl);
550,316,634✔
920
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
550,319,559✔
921
  }
922
  return;
70,077,029✔
923
}
924

925
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
70,113,902✔
926
  int rc = 0;
70,113,902✔
927

928
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
70,113,902✔
929
  if (pColCmprObj == NULL) {
70,112,843✔
UNCOV
930
    pColCmprObj = NULL;
×
UNCOV
931
    return TSDB_CODE_OUT_OF_MEMORY;
×
932
  }
933

934
  void      *pData = NULL;
70,112,843✔
935
  int        nData = 0;
70,114,766✔
936
  SMetaEntry e = {0};
70,115,534✔
937
  SDecoder   dc = {0};
70,116,082✔
938

939
  *ppColCmprObj = NULL;
70,115,018✔
940

941
  metaRLock(pMeta);
70,111,996✔
942
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
70,116,501✔
943
  if (rc < 0) {
70,112,543✔
944
    taosHashCleanup(pColCmprObj);
41,700✔
945
    metaULock(pMeta);
41,700✔
946
    return TSDB_CODE_FAILED;
41,700✔
947
  }
948
  int64_t version = ((SUidIdxVal *)pData)[0].version;
70,070,843✔
949
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
70,070,790✔
950
  if (rc < 0) {
70,065,882✔
UNCOV
951
    metaULock(pMeta);
×
UNCOV
952
    taosHashCleanup(pColCmprObj);
×
UNCOV
953
    metaError("failed to get table entry");
×
UNCOV
954
    return rc;
×
955
  }
956

957
  tDecoderInit(&dc, pData, nData);
70,065,882✔
958
  rc = metaDecodeEntry(&dc, &e);
70,073,594✔
959
  if (rc < 0) {
70,056,117✔
UNCOV
960
    tDecoderClear(&dc);
×
UNCOV
961
    tdbFree(pData);
×
UNCOV
962
    metaULock(pMeta);
×
UNCOV
963
    taosHashCleanup(pColCmprObj);
×
964
    return rc;
×
965
  }
966
  if (withExtSchema(e.type)) {
70,056,117✔
967
    SColCmprWrapper *p = &e.colCmpr;
70,071,495✔
968
    for (int32_t i = 0; i < p->nCols; i++) {
620,424,032✔
969
      SColCmpr *pCmpr = &p->pColCmpr[i];
550,346,504✔
970
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
550,334,519✔
971
      if (rc < 0) {
550,352,537✔
UNCOV
972
        tDecoderClear(&dc);
×
973
        tdbFree(pData);
×
974
        metaULock(pMeta);
×
975
        taosHashCleanup(pColCmprObj);
×
976
        return rc;
×
977
      }
978
    }
979
  } else {
UNCOV
980
    tDecoderClear(&dc);
×
UNCOV
981
    tdbFree(pData);
×
UNCOV
982
    metaULock(pMeta);
×
UNCOV
983
    taosHashCleanup(pColCmprObj);
×
UNCOV
984
    return 0;
×
985
  }
986
  tDecoderClear(&dc);
70,056,790✔
987
  tdbFree(pData);
70,074,801✔
988
  metaULock(pMeta);
70,067,350✔
989

990
  *ppColCmprObj = pColCmprObj;
70,063,830✔
991
  colCompressDebug(pColCmprObj);
70,064,508✔
992

993
  return 0;
70,077,266✔
994
}
995
// refactor later
996
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
4,433,740✔
997
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
4,446,187✔
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