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

taosdata / TDengine / #4881

14 Dec 2025 03:48AM UTC coverage: 60.617% (+0.5%) from 60.092%
#4881

push

travis-ci

web-flow
test: update coverage workflow time (#33918)

156854 of 258761 relevant lines covered (60.62%)

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

50
    SColCmpr *pCol = p + nCols;
2,235,849✔
51
    pCol->id = pSchema->colId;
2,235,849✔
52
    pCol->alg = compress;
2,235,849✔
53
    pWp->nCols = nCols + 1;
2,235,849✔
54
    pWp->version = ver;
2,235,849✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
4,848,847✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
4,848,847✔
58
      if (pOCmpr->id == pSchema->colId) {
4,848,847✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
29,191✔
60
        if (left) {
29,191✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
15,136✔
62
        }
63
        nCols--;
29,191✔
64
        break;
29,191✔
65
      }
66
    }
67
    pWp->nCols = nCols;
29,191✔
68
    pWp->version = ver;
29,191✔
69
  }
70
  return 0;
2,265,040✔
71
}
72

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
48,834✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
48,834✔
92
  if (dropColId == newColNum) {
6,849✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
2,633✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
4,216✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
4,216✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
26,298✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
25,248✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
1,050✔
104
  return 0;
1,050✔
105
}
106

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

116
    SColRef *pCol = p + nCols;
39,550✔
117
    if (NULL == pColRef) {
39,550✔
118
      pCol->hasRef = false;
3,848✔
119
      pCol->id = pSchema->colId;
3,848✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
35,702✔
122
      pCol->id = pSchema->colId;
35,702✔
123
      if (pCol->hasRef) {
35,702✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
14,784✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
14,784✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
14,784✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
39,550✔
130
    pWp->version++;
39,550✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
87,325✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
87,325✔
134
      if (pOColRef->id == pSchema->colId) {
87,325✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
22,239✔
136
        if (left) {
22,239✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
21,343✔
138
        }
139
        nCols--;
22,239✔
140
        break;
22,239✔
141
      }
142
    }
143
    pWp->nCols = nCols;
22,239✔
144
    pWp->version++;
22,239✔
145
  }
146
  return 0;
61,789✔
147
}
148

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

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

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

169
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
3,521,230✔
170

171
  return 0;
3,521,230✔
172
}
173

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

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

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

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

216
  return code;
324,389✔
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) {
169,819✔
225
  int32_t code = 0;
169,819✔
226

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

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

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

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

250
  int16_t nCols = taosArrayGetSize(pTagVals);
169,819✔
251
  for (int i = 0; i < nCols; i++) {
404,785✔
252
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
234,966✔
253
    char     type = pTagVal->type;
234,966✔
254

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

258
    SIndexTerm *term = NULL;
234,966✔
259
    if (type == TSDB_DATA_TYPE_NULL) {
234,966✔
260
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
1,908✔
261
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
233,058✔
262
      if (pTagVal->nData > 0) {
224,526✔
263
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
223,254✔
264
        if (val == NULL) {
223,254✔
265
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
266
        }
267
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
223,254✔
268
        if (len < 0) {
223,254✔
269
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
270
        }
271
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
223,254✔
272
        type = TSDB_DATA_TYPE_VARCHAR;
223,254✔
273
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
223,254✔
274
        taosMemoryFree(val);
223,254✔
275
      } else if (pTagVal->nData == 0) {
1,272✔
276
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,272✔
277
      }
278
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
8,532✔
279
      double val = *(double *)(&pTagVal->i64);
5,988✔
280
      int    len = sizeof(val);
5,988✔
281
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
5,988✔
282
    } else if (type == TSDB_DATA_TYPE_BOOL) {
2,544✔
283
      int val = *(int *)(&pTagVal->i64);
2,544✔
284
      int len = sizeof(val);
2,544✔
285
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
2,544✔
286
    }
287

288
    if (term != NULL) {
234,966✔
289
      int32_t ret = indexMultiTermAdd(terms, term);
234,966✔
290
      if (ret < 0) {
234,966✔
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);
169,819✔
300
  indexMultiTermDestroy(terms);
169,819✔
301

302
  taosArrayDestroy(pTagVals);
169,819✔
303
  return code;
169,819✔
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) {
78,942✔
311
int32_t code = 0;
78,942✔
312
#ifdef USE_INVERTED_INDEX
313
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
78,942✔
314
    return TSDB_CODE_INVALID_PARA;
×
315
  }
316
  void       *data = pCtbEntry->ctbEntry.pTags;
78,942✔
317
  const char *tagName = pSchema->name;
78,942✔
318

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

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

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

335
  int16_t nCols = taosArrayGetSize(pTagVals);
78,942✔
336
  for (int i = 0; i < nCols; i++) {
226,312✔
337
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
147,370✔
338
    char     type = pTagVal->type;
147,370✔
339

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

343
    SIndexTerm *term = NULL;
147,370✔
344
    if (type == TSDB_DATA_TYPE_NULL) {
147,370✔
345
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
346
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
147,370✔
347
      if (pTagVal->nData > 0) {
145,780✔
348
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
145,780✔
349
        if (val == NULL) {
145,780✔
350
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
351
        }
352
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
145,780✔
353
        if (len < 0) {
145,780✔
354
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
355
        }
356
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
145,780✔
357
        type = TSDB_DATA_TYPE_VARCHAR;
145,780✔
358
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
145,780✔
359
        taosMemoryFree(val);
145,780✔
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) {
1,590✔
364
      double val = *(double *)(&pTagVal->i64);
954✔
365
      int    len = sizeof(val);
954✔
366
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
954✔
367
    } else if (type == TSDB_DATA_TYPE_BOOL) {
636✔
368
      int val = *(int *)(&pTagVal->i64);
636✔
369
      int len = sizeof(val);
636✔
370
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
636✔
371
    }
372
    if (term != NULL) {
147,370✔
373
      int32_t ret = indexMultiTermAdd(terms, term);
147,370✔
374
      if (ret < 0) {
147,370✔
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);
78,942✔
384
  indexMultiTermDestroy(terms);
78,942✔
385
  taosArrayDestroy(pTagVals);
78,942✔
386
  return code;
78,942✔
387
_exception:
×
388
  indexMultiTermDestroy(terms);
×
389
  taosArrayDestroy(pTagVals);
×
390
#endif
391
  return code;
×
392
}
393

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

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

404
  metaWLock(pMeta);
8,761✔
405
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
22,951✔
406
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
14,190✔
407
    tb_uid_t suid = 0;
14,190✔
408
    int8_t   sysTbl = 0;
14,190✔
409
    int      type;
14,190✔
410
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
14,190✔
411
    if (code) return code;
14,190✔
412
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
14,190✔
413
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
10,308✔
414
      if (pVal) {
10,308✔
415
        nCtbDropped = *pVal + 1;
3,489✔
416
      } else {
417
        nCtbDropped = 1;
6,819✔
418
      }
419
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
10,308✔
420
      if (code) return code;
10,308✔
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);
14,190✔
428
  }
429
  metaULock(pMeta);
8,761✔
430

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

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

451
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
12,674✔
452
  int32_t code = 0;
12,674✔
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;
12,674✔
458
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
12,674✔
459
  if (code < 0) {
12,674✔
460
    return code;
×
461
  }
462

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

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

472
  while (1) {
37,171✔
473
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
49,845✔
474
    if (ret < 0) {
49,845✔
475
      break;
12,674✔
476
    }
477

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

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

506
  return 0;
12,674✔
507
}
508

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

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

517
  code = metaFilterTableByHash(pMeta, tbUids);
12,674✔
518
  if (code != 0) {
12,674✔
519
    goto end;
×
520
  }
521
  if (TARRAY_SIZE(tbUids) == 0) {
12,674✔
522
    goto end;
3,913✔
523
  }
524

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

529
end:
8,761✔
530
  taosArrayDestroy(tbUids);
12,674✔
531

532
  return code;
12,674✔
533
}
534

535
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
3,289,458✔
536
  metaRLock(pMeta);
3,289,458✔
537

538
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
3,295,632✔
539

540
  metaULock(pMeta);
3,288,729✔
541

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

546
  return ret;
3,291,778✔
547
}
548

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

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

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

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

577
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
14,190✔
578
  if (pME->type == TSDB_CHILD_TABLE) {
14,190✔
579
    ctx.ttlDays = pME->ctbEntry.ttlDays;
10,308✔
580
  } else {
581
    ctx.ttlDays = pME->ntbEntry.ttlDays;
3,882✔
582
  }
583

584
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
14,190✔
585
  if (ret < 0) {
14,190✔
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;
14,190✔
590
}
591

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

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

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

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

619
  if (type) *type = e.type;
14,190✔
620

621
  if (e.type == TSDB_CHILD_TABLE) {
14,190✔
622
    if (pSuid) *pSuid = e.ctbEntry.suid;
10,308✔
623
    void *tData = NULL;
10,308✔
624
    int   tLen = 0;
10,308✔
625

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

632
        tDecoderInit(&tdc, tData, tLen);
10,308✔
633
        ret = metaDecodeEntry(&tdc, &stbEntry);
10,308✔
634
        if (ret < 0) {
10,308✔
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;
10,308✔
642
        
643
        ret = metaStableTagFilterCacheUpdateUid(
10,308✔
644
          pMeta, &e, &stbEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
645
        if (ret < 0) {
10,308✔
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;
10,308✔
652
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
10,308✔
653
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
10,308✔
654
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
655
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
656
          if (ret < 0) {
×
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++) {
20,616✔
662
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
10,308✔
663
            if (!IS_IDX_ON(pTagColumn)) continue;
10,308✔
664
            STagIdxKey *pTagIdxKey = NULL;
10,308✔
665
            int32_t     nTagIdxKey;
10,308✔
666

667
            const void *pTagData = NULL;
10,308✔
668
            int32_t     nTagData = 0;
10,308✔
669

670
            STagVal tagVal = {.cid = pTagColumn->colId};
10,308✔
671
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
10,308✔
672
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
10,308✔
673
                pTagData = tagVal.pData;
×
674
                nTagData = (int32_t)tagVal.nData;
×
675
              } else {
676
                pTagData = &(tagVal.i64);
10,308✔
677
                nTagData = tDataTypes[pTagColumn->type].bytes;
10,308✔
678
              }
679
            } else {
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,
10,308✔
686
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
687
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
10,308✔
688
              if (ret < 0) {
10,308✔
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);
10,308✔
694
            pTagIdxKey = NULL;
10,308✔
695
          }
696
        }
697
        tDecoderClear(&tdc);
10,308✔
698
      }
699
      tdbFree(tData);
10,308✔
700
    }
701
  }
702

703
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
14,190✔
704
  if (ret < 0) {
14,190✔
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);
14,190✔
709
  if (ret < 0) {
14,190✔
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);
14,190✔
714
  if (ret < 0) {
14,190✔
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);
14,190✔
720
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
14,190✔
721

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

724
  if (e.type == TSDB_CHILD_TABLE) {
14,190✔
725
    ret =
726
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
10,308✔
727
    if (ret < 0) {
10,308✔
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;
10,308✔
733
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
10,308✔
734
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
10,308✔
735
    if (ret < 0) {
10,308✔
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);
10,308✔
740
    if (ret < 0) {
10,308✔
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,882✔
750
    // drop schema.db (todo)
751

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

755
    /*
756
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
757
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
758
    }
759
    */
760
  } else if (e.type == TSDB_SUPER_TABLE) {
×
761
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
762
    if (ret < 0) {
×
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
    }
773
    ret = metaUidCacheClear(pMeta, uid);
×
774
    if (ret < 0) {
×
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
    }
778
    ret = metaStableTagFilterCacheDropSTable(pMeta, uid);
×
779
    if (ret < 0) {
×
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
    }
783
    ret = metaTbGroupCacheClear(pMeta, uid);
×
784
    if (ret < 0) {
×
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);
14,190✔
792
  if (ret < 0) {
14,190✔
793
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
14,190✔
794
              tstrerror(ret));
795
  }
796

797
  tDecoderClear(&dc);
14,190✔
798
  tdbFree(pData);
14,190✔
799

800
  return 0;
14,190✔
801
}
802

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

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

819
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
8,532,413✔
820
  pMeta->changed = true;
8,532,413✔
821
  switch (pReq->action) {
8,532,413✔
822
    case TSDB_ALTER_TABLE_ADD_COLUMN:
2,478,079✔
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);
2,478,079✔
826
    case TSDB_ALTER_TABLE_DROP_COLUMN:
48,834✔
827
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
48,834✔
828
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
333,756✔
829
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
333,756✔
830
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
24,730✔
831
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
24,730✔
832
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
5,542,550✔
833
      return metaUpdateTableTagValue(pMeta, version, pReq);
5,542,550✔
834
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
2,340✔
835
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
2,340✔
836
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
11,032✔
837
      return metaUpdateTableOptions2(pMeta, version, pReq);
11,032✔
838
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
4,152✔
839
      return metaUpdateTableColCompress2(pMeta, version, pReq);
4,152✔
840
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
49,388✔
841
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
49,388✔
842
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
37,552✔
843
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
37,552✔
844
    default:
×
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) {
694,570✔
851
  if (!tsTtlChangeOnWrite) return 0;
694,570✔
852

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

858
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
694,570✔
859

860
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
694,570✔
861
}
862

863
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
303,485,644✔
864
  if (!tsTtlChangeOnWrite) return 0;
303,485,644✔
865

866
  metaWLock(pMeta);
698,274✔
867
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
694,570✔
868
  metaULock(pMeta);
694,570✔
869
  return ret;
694,570✔
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,
44,744,298✔
873
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
874
  if (IS_VAR_DATA_TYPE(type)) {
44,744,298✔
875
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
2,780,965✔
876
  } else {
877
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
41,963,333✔
878
  }
879

880
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
44,748,916✔
881
  if (*ppTagIdxKey == NULL) {
44,744,176✔
882
    return terrno;
×
883
  }
884

885
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
44,741,999✔
886
  (*ppTagIdxKey)->cid = cid;
44,747,369✔
887
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
44,741,096✔
888
  (*ppTagIdxKey)->type = type;
44,748,636✔
889

890
  // refactor
891
  if (IS_VAR_DATA_TYPE(type)) {
44,739,838✔
892
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
2,788,303✔
893
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
2,786,777✔
894
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
2,786,085✔
895
  } else {
896
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
41,951,535✔
897
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
41,954,712✔
898
  }
899

900
  return 0;
44,740,589✔
901
}
902

903
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
42,826,947✔
904
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
42,826,947✔
905
}
42,827,838✔
906

907
static void colCompressDebug(SHashObj *pColCmprObj) {
42,742,554✔
908
  void *p = taosHashIterate(pColCmprObj, NULL);
42,742,554✔
909
  while (p) {
338,921,727✔
910
    uint32_t cmprAlg = *(uint32_t *)p;
296,174,880✔
911
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
296,168,925✔
912
    p = taosHashIterate(pColCmprObj, p);
296,170,835✔
913

914
    uint8_t l1, l2, lvl;
296,210,201✔
915
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
296,202,175✔
916

917
    const char *l1str = columnEncodeStr(l1);
296,203,492✔
918
    const char *l2str = columnCompressStr(l2);
296,191,976✔
919
    const char *lvlstr = columnLevelStr(lvl);
296,181,825✔
920
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
296,178,931✔
921
  }
922
  return;
42,746,847✔
923
}
924

925
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
42,772,686✔
926
  int rc = 0;
42,772,686✔
927

928
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
42,772,686✔
929
  if (pColCmprObj == NULL) {
42,771,379✔
930
    pColCmprObj = NULL;
×
931
    return TSDB_CODE_OUT_OF_MEMORY;
×
932
  }
933

934
  void      *pData = NULL;
42,771,379✔
935
  int        nData = 0;
42,770,735✔
936
  SMetaEntry e = {0};
42,772,524✔
937
  SDecoder   dc = {0};
42,772,199✔
938

939
  *ppColCmprObj = NULL;
42,772,965✔
940

941
  metaRLock(pMeta);
42,772,068✔
942
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
42,771,828✔
943
  if (rc < 0) {
42,770,427✔
944
    taosHashCleanup(pColCmprObj);
25,361✔
945
    metaULock(pMeta);
25,361✔
946
    return TSDB_CODE_FAILED;
25,361✔
947
  }
948
  int64_t version = ((SUidIdxVal *)pData)[0].version;
42,745,066✔
949
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
42,746,413✔
950
  if (rc < 0) {
42,743,681✔
951
    metaULock(pMeta);
×
952
    taosHashCleanup(pColCmprObj);
×
953
    metaError("failed to get table entry");
×
954
    return rc;
×
955
  }
956

957
  tDecoderInit(&dc, pData, nData);
42,743,681✔
958
  rc = metaDecodeEntry(&dc, &e);
42,746,415✔
959
  if (rc < 0) {
42,738,566✔
960
    tDecoderClear(&dc);
×
961
    tdbFree(pData);
×
962
    metaULock(pMeta);
×
963
    taosHashCleanup(pColCmprObj);
×
964
    return rc;
×
965
  }
966
  if (withExtSchema(e.type)) {
42,738,566✔
967
    SColCmprWrapper *p = &e.colCmpr;
42,740,857✔
968
    for (int32_t i = 0; i < p->nCols; i++) {
338,944,296✔
969
      SColCmpr *pCmpr = &p->pColCmpr[i];
296,197,661✔
970
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
296,189,657✔
971
      if (rc < 0) {
296,203,439✔
972
        tDecoderClear(&dc);
×
973
        tdbFree(pData);
×
974
        metaULock(pMeta);
×
975
        taosHashCleanup(pColCmprObj);
×
976
        return rc;
×
977
      }
978
    }
979
  } else {
980
    tDecoderClear(&dc);
×
981
    tdbFree(pData);
×
982
    metaULock(pMeta);
×
983
    taosHashCleanup(pColCmprObj);
×
984
    return 0;
×
985
  }
986
  tDecoderClear(&dc);
42,742,484✔
987
  tdbFree(pData);
42,744,298✔
988
  metaULock(pMeta);
42,743,720✔
989

990
  *ppColCmprObj = pColCmprObj;
42,744,505✔
991
  colCompressDebug(pColCmprObj);
42,744,923✔
992

993
  return 0;
42,746,847✔
994
}
995
// refactor later
996
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
2,435,143✔
997
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
2,435,143✔
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