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

taosdata / TDengine / #4886

16 Dec 2025 01:13AM UTC coverage: 65.292% (+0.03%) from 65.258%
#4886

push

travis-ci

web-flow
fix: compile error (#33938)

178718 of 273721 relevant lines covered (65.29%)

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

50
    SColCmpr *pCol = p + nCols;
3,311,173✔
51
    pCol->id = pSchema->colId;
3,311,173✔
52
    pCol->alg = compress;
3,311,173✔
53
    pWp->nCols = nCols + 1;
3,311,173✔
54
    pWp->version = ver;
3,311,173✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
33,872,479✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
33,872,479✔
58
      if (pOCmpr->id == pSchema->colId) {
33,872,479✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
59,477✔
60
        if (left) {
59,477✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
38,029✔
62
        }
63
        nCols--;
59,477✔
64
        break;
59,477✔
65
      }
66
    }
67
    pWp->nCols = nCols;
59,477✔
68
    pWp->version = ver;
59,477✔
69
  }
70
  return 0;
3,370,650✔
71
}
72

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
90,981✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
90,981✔
92
  if (dropColId == newColNum) {
9,851✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
3,787✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
6,064✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
6,064✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
37,822✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
36,312✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
1,510✔
104
  return 0;
1,510✔
105
}
106

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

116
    SColRef *pCol = p + nCols;
60,836✔
117
    if (NULL == pColRef) {
60,836✔
118
      pCol->hasRef = false;
5,917✔
119
      pCol->id = pSchema->colId;
5,917✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
54,919✔
122
      pCol->id = pSchema->colId;
54,919✔
123
      if (pCol->hasRef) {
54,919✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
22,341✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
22,341✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
22,341✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
60,836✔
130
    pWp->version++;
60,836✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
31,978,838✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
31,978,838✔
134
      if (pOColRef->id == pSchema->colId) {
31,978,838✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
35,478✔
136
        if (left) {
35,478✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
33,152✔
138
        }
139
        nCols--;
35,478✔
140
        break;
35,478✔
141
      }
142
    }
143
    pWp->nCols = nCols;
35,478✔
144
    pWp->version++;
35,478✔
145
  }
146
  return 0;
96,314✔
147
}
148

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

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

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

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

171
  return 0;
21,906,364✔
172
}
173

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

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

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

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

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
196,766,032✔
211
    SColRef* pColRef = &pRef->pColRef[i];
196,165,806✔
212
    if (pColRef->hasRef) {
196,165,806✔
213
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
130,245,653✔
214
      if (code) {
130,245,653✔
215
        goto _return;
×
216
      }
217
    }
218
  }
219

220
  if (taosHashGetSize(pColRefHash) > 1000) {
600,226✔
221
    code = TSDB_CODE_VTABLE_TOO_MANY_REFERENCE;
972✔
222
    goto _return;
972✔
223
  }
224

225

226

227
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
599,254✔
228
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
599,254✔
229
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
599,254✔
230
    pMetaRsp->tuid = pEntry->uid;
337,296✔
231
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
261,958✔
232
    pMetaRsp->tuid = pEntry->uid;
261,958✔
233
    pMetaRsp->suid = pEntry->ctbEntry.suid;
261,958✔
234
  }
235

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

241
  taosHashCleanup(pColRefHash);
599,254✔
242
  return code;
599,254✔
243
_return:
972✔
244
  taosHashCleanup(pColRefHash);
972✔
245
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
972✔
246
  taosMemoryFreeClear(pMetaRsp->pSchemas);
972✔
247
  taosMemoryFreeClear(pMetaRsp->pColRefs);
972✔
248
  return code;
972✔
249
}
250

251
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
327,407✔
252
  int32_t code = 0;
327,407✔
253

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

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

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

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

277
  int16_t nCols = taosArrayGetSize(pTagVals);
327,407✔
278
  for (int i = 0; i < nCols; i++) {
791,094✔
279
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
463,687✔
280
    char     type = pTagVal->type;
463,687✔
281

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

285
    SIndexTerm *term = NULL;
463,687✔
286
    if (type == TSDB_DATA_TYPE_NULL) {
463,687✔
287
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
3,264✔
288
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
460,423✔
289
      if (pTagVal->nData > 0) {
445,148✔
290
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
442,972✔
291
        if (val == NULL) {
442,972✔
292
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
293
        }
294
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
442,972✔
295
        if (len < 0) {
442,972✔
296
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
297
        }
298
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
442,972✔
299
        type = TSDB_DATA_TYPE_VARCHAR;
442,972✔
300
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
442,972✔
301
        taosMemoryFree(val);
442,972✔
302
      } else if (pTagVal->nData == 0) {
2,176✔
303
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
2,176✔
304
      }
305
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
15,275✔
306
      double val = *(double *)(&pTagVal->i64);
10,923✔
307
      int    len = sizeof(val);
10,923✔
308
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
10,923✔
309
    } else if (type == TSDB_DATA_TYPE_BOOL) {
4,352✔
310
      int val = *(int *)(&pTagVal->i64);
4,352✔
311
      int len = sizeof(val);
4,352✔
312
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
4,352✔
313
    }
314

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

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

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

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

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

362
  int16_t nCols = taosArrayGetSize(pTagVals);
123,685✔
363
  for (int i = 0; i < nCols; i++) {
354,518✔
364
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
230,833✔
365
    char     type = pTagVal->type;
230,833✔
366

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

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

421
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
15,672✔
422
  int32_t code = 0;
15,672✔
423
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
15,672✔
424

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

431
  metaWLock(pMeta);
15,672✔
432
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
43,299✔
433
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
27,627✔
434
    tb_uid_t suid = 0;
27,627✔
435
    int8_t   sysTbl = 0;
27,627✔
436
    int      type;
27,627✔
437
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
27,627✔
438
    if (code) return code;
27,627✔
439
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
27,627✔
440
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
21,987✔
441
      if (pVal) {
21,987✔
442
        nCtbDropped = *pVal + 1;
9,145✔
443
      } else {
444
        nCtbDropped = 1;
12,842✔
445
      }
446
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
21,987✔
447
      if (code) return code;
21,987✔
448
    }
449
    /*
450
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
451
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
452
    }
453
    */
454
    metaDebug("batch drop table:%" PRId64, uid);
27,627✔
455
  }
456
  metaULock(pMeta);
15,672✔
457

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

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

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

484
  TBC *pCur;
20,446✔
485
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
20,446✔
486
  if (code < 0) {
20,446✔
487
    return code;
×
488
  }
489

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

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

499
  while (1) {
70,074✔
500
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
90,520✔
501
    if (ret < 0) {
90,520✔
502
      break;
20,446✔
503
    }
504

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

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

533
  return 0;
20,446✔
534
}
535

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

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

544
  code = metaFilterTableByHash(pMeta, tbUids);
20,446✔
545
  if (code != 0) {
20,446✔
546
    goto end;
×
547
  }
548
  if (TARRAY_SIZE(tbUids) == 0) {
20,446✔
549
    goto end;
4,774✔
550
  }
551

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

556
end:
15,672✔
557
  taosArrayDestroy(tbUids);
20,446✔
558

559
  return code;
20,446✔
560
}
561

562
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
7,440,792✔
563
  metaRLock(pMeta);
7,440,792✔
564

565
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
7,451,336✔
566

567
  metaULock(pMeta);
7,442,271✔
568

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

573
  return ret;
7,437,874✔
574
}
575

576
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
27,627✔
577
  int64_t btime;
578
  if (pME->type == TSDB_CHILD_TABLE) {
27,627✔
579
    btime = pME->ctbEntry.btime;
21,987✔
580
  } else if (pME->type == TSDB_NORMAL_TABLE) {
5,640✔
581
    btime = pME->ntbEntry.btime;
5,640✔
582
  } else {
583
    return TSDB_CODE_FAILED;
×
584
  }
585

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

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

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

604
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
27,627✔
605
  if (pME->type == TSDB_CHILD_TABLE) {
27,627✔
606
    ctx.ttlDays = pME->ctbEntry.ttlDays;
21,987✔
607
  } else {
608
    ctx.ttlDays = pME->ntbEntry.ttlDays;
5,640✔
609
  }
610

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

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

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

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

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

646
  if (type) *type = e.type;
27,627✔
647

648
  if (e.type == TSDB_CHILD_TABLE) {
27,627✔
649
    if (pSuid) *pSuid = e.ctbEntry.suid;
21,987✔
650
    void *tData = NULL;
21,987✔
651
    int   tLen = 0;
21,987✔
652

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

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

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

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

694
            const void *pTagData = NULL;
21,987✔
695
            int32_t     nTagData = 0;
21,987✔
696

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

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

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

746
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
27,627✔
747
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
27,627✔
748

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

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

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

779
    --pMeta->pVnode->config.vndStats.numOfNTables;
5,640✔
780
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
5,640✔
781

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

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

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

824
  tDecoderClear(&dc);
27,627✔
825
  tdbFree(pData);
27,627✔
826

827
  return 0;
27,627✔
828
}
829

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

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

846
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
12,608,155✔
847
  pMeta->changed = true;
12,608,155✔
848
  switch (pReq->action) {
12,608,155✔
849
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,692,410✔
850
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
851
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
852
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,692,410✔
853
    case TSDB_ALTER_TABLE_DROP_COLUMN:
90,981✔
854
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
90,981✔
855
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
500,708✔
856
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
500,708✔
857
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
45,364✔
858
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
45,364✔
859
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
8,109,611✔
860
      return metaUpdateTableTagValue(pMeta, version, pReq);
8,109,611✔
861
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,480✔
862
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,480✔
863
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
17,662✔
864
      return metaUpdateTableOptions2(pMeta, version, pReq);
17,662✔
865
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
6,632✔
866
      return metaUpdateTableColCompress2(pMeta, version, pReq);
6,632✔
867
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
82,057✔
868
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
82,057✔
869
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
59,250✔
870
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
59,250✔
871
    default:
×
872
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
873
      break;
874
  }
875
}
876

877
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
1,176,997✔
878
  if (!tsTtlChangeOnWrite) return 0;
1,176,997✔
879

880
  if (changeTimeMs <= 0) {
1,176,997✔
881
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
882
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
883
  }
884

885
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
1,176,997✔
886

887
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
1,176,997✔
888
}
889

890
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
593,304,238✔
891
  if (!tsTtlChangeOnWrite) return 0;
593,304,238✔
892

893
  metaWLock(pMeta);
1,185,670✔
894
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
1,176,997✔
895
  metaULock(pMeta);
1,176,997✔
896
  return ret;
1,176,997✔
897
}
898

899
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
81,332,576✔
900
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
901
  if (IS_VAR_DATA_TYPE(type)) {
81,332,576✔
902
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
5,762,839✔
903
  } else {
904
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
75,569,737✔
905
  }
906

907
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
81,336,257✔
908
  if (*ppTagIdxKey == NULL) {
81,332,876✔
909
    return terrno;
×
910
  }
911

912
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
81,325,519✔
913
  (*ppTagIdxKey)->cid = cid;
81,333,617✔
914
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
81,340,041✔
915
  (*ppTagIdxKey)->type = type;
81,331,677✔
916

917
  // refactor
918
  if (IS_VAR_DATA_TYPE(type)) {
81,335,095✔
919
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
5,768,674✔
920
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
5,766,133✔
921
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
5,767,015✔
922
  } else {
923
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
75,566,421✔
924
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
75,562,695✔
925
  }
926

927
  return 0;
81,329,594✔
928
}
929

930
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
77,835,893✔
931
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
77,835,893✔
932
}
77,835,405✔
933

934
static void colCompressDebug(SHashObj *pColCmprObj) {
99,775,735✔
935
  void *p = taosHashIterate(pColCmprObj, NULL);
99,775,735✔
936
  while (p) {
739,236,597✔
937
    uint32_t cmprAlg = *(uint32_t *)p;
639,396,998✔
938
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
639,392,242✔
939
    p = taosHashIterate(pColCmprObj, p);
639,412,552✔
940

941
    uint8_t l1, l2, lvl;
639,504,326✔
942
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
639,522,684✔
943

944
    const char *l1str = columnEncodeStr(l1);
639,504,475✔
945
    const char *l2str = columnCompressStr(l2);
639,427,718✔
946
    const char *lvlstr = columnLevelStr(lvl);
639,453,072✔
947
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
639,466,386✔
948
  }
949
  return;
99,839,599✔
950
}
951

952
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
99,815,617✔
953
  int rc = 0;
99,815,617✔
954

955
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
99,815,617✔
956
  if (pColCmprObj == NULL) {
99,826,396✔
957
    pColCmprObj = NULL;
×
958
    return TSDB_CODE_OUT_OF_MEMORY;
×
959
  }
960

961
  void      *pData = NULL;
99,826,396✔
962
  int        nData = 0;
99,839,320✔
963
  SMetaEntry e = {0};
99,846,237✔
964
  SDecoder   dc = {0};
99,846,973✔
965

966
  *ppColCmprObj = NULL;
99,843,194✔
967

968
  metaRLock(pMeta);
99,828,905✔
969
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
99,876,347✔
970
  if (rc < 0) {
99,859,293✔
971
    taosHashCleanup(pColCmprObj);
42,945✔
972
    metaULock(pMeta);
42,945✔
973
    return TSDB_CODE_FAILED;
42,945✔
974
  }
975
  int64_t version = ((SUidIdxVal *)pData)[0].version;
99,816,348✔
976
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
99,819,495✔
977
  if (rc < 0) {
99,823,405✔
978
    metaULock(pMeta);
×
979
    taosHashCleanup(pColCmprObj);
×
980
    metaError("failed to get table entry");
×
981
    return rc;
×
982
  }
983

984
  tDecoderInit(&dc, pData, nData);
99,823,405✔
985
  rc = metaDecodeEntry(&dc, &e);
99,809,484✔
986
  if (rc < 0) {
99,784,378✔
987
    tDecoderClear(&dc);
×
988
    tdbFree(pData);
×
989
    metaULock(pMeta);
×
990
    taosHashCleanup(pColCmprObj);
×
991
    return rc;
×
992
  }
993
  if (withExtSchema(e.type)) {
99,784,378✔
994
    SColCmprWrapper *p = &e.colCmpr;
99,809,826✔
995
    for (int32_t i = 0; i < p->nCols; i++) {
739,283,479✔
996
      SColCmpr *pCmpr = &p->pColCmpr[i];
639,458,062✔
997
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
639,449,943✔
998
      if (rc < 0) {
639,473,653✔
999
        tDecoderClear(&dc);
×
1000
        tdbFree(pData);
×
1001
        metaULock(pMeta);
×
1002
        taosHashCleanup(pColCmprObj);
×
1003
        return rc;
×
1004
      }
1005
    }
1006
  } else {
1007
    tDecoderClear(&dc);
×
1008
    tdbFree(pData);
×
1009
    metaULock(pMeta);
×
1010
    taosHashCleanup(pColCmprObj);
×
1011
    return 0;
×
1012
  }
1013
  tDecoderClear(&dc);
99,781,985✔
1014
  tdbFree(pData);
99,828,215✔
1015
  metaULock(pMeta);
99,823,097✔
1016

1017
  *ppColCmprObj = pColCmprObj;
99,804,330✔
1018
  colCompressDebug(pColCmprObj);
99,819,973✔
1019

1020
  return 0;
99,840,130✔
1021
}
1022
// refactor later
1023
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
4,339,191✔
1024
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
4,339,191✔
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