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

taosdata / TDengine / #4670

14 Aug 2025 01:04PM UTC coverage: 59.704% (+0.1%) from 59.572%
#4670

push

travis-ci

web-flow
fix(query): fix order by column check of union operator (#32524)

137028 of 292055 branches covered (46.92%)

Branch coverage included in aggregate %.

1 of 13 new or added lines in 1 file covered. (7.69%)

1859 existing lines in 71 files now uncovered.

207131 of 284385 relevant lines covered (72.83%)

14770392.73 hits per line

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

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

50
    SColCmpr *pCol = p + nCols;
4,727✔
51
    pCol->id = pSchema->colId;
4,727✔
52
    pCol->alg = compress;
4,727✔
53
    pWp->nCols = nCols + 1;
4,727✔
54
    pWp->version = ver;
4,727✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
41,927!
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
41,927✔
58
      if (pOCmpr->id == pSchema->colId) {
41,927✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
99✔
60
        if (left) {
99✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
60✔
62
        }
63
        nCols--;
99✔
64
        break;
99✔
65
      }
66
    }
67
    pWp->nCols = nCols;
99✔
68
    pWp->version = ver;
99✔
69
  }
70
  return 0;
4,826✔
71
}
72

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

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

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

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

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

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

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

169
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
18,017✔
170

171
  return 0;
18,017✔
172
}
173

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

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

193
    pMetaRsp->numOfColumns = pSchema->nCols;
490✔
194
    pMetaRsp->sversion = pSchema->version;
490✔
195
    memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
490✔
196
  }
197
  pMetaRsp->pColRefs = taosMemoryMalloc(pRef->nCols * sizeof(SColRef));
748!
198
  if (NULL == pMetaRsp->pColRefs) {
748!
199
    code = terrno;
×
200
    goto _return;
×
201
  }
202
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
748✔
203
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
748✔
204
  pMetaRsp->tuid = uid;
748✔
205
  pMetaRsp->tableType = tableType;
748✔
206
  pMetaRsp->virtualStb = false; // super table will never be processed here
748✔
207
  pMetaRsp->numOfColRefs = pRef->nCols;
748✔
208
  pMetaRsp->rversion = pRef->version;
748✔
209

210
  return code;
748✔
211
_return:
×
212
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
213
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
214
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
215
  return code;
×
216
}
217

218
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
761✔
219
  int32_t code = 0;
761✔
220

221
#ifdef USE_INVERTED_INDEX
222
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
761!
223
    return TSDB_CODE_INVALID_PARA;
×
224
  }
225
  void       *data = pCtbEntry->ctbEntry.pTags;
761✔
226
  const char *tagName = pSchema->name;
761✔
227

228
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
761✔
229
  tb_uid_t    tuid = pCtbEntry->uid;
761✔
230
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
761✔
231
  int32_t     nTagData = 0;
761✔
232

233
  SArray *pTagVals = NULL;
761✔
234
  code = tTagToValArray((const STag *)data, &pTagVals);
761✔
235
  if (code) {
761!
236
    return code;
×
237
  }
238

239
  SIndexMultiTerm *terms = indexMultiTermCreate();
761✔
240
  if (terms == NULL) {
761!
241
    return terrno;
×
242
  }
243

244
  int16_t nCols = taosArrayGetSize(pTagVals);
761✔
245
  for (int i = 0; i < nCols; i++) {
1,795✔
246
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
1,034✔
247
    char     type = pTagVal->type;
1,034✔
248

249
    char   *key = pTagVal->pKey;
1,034✔
250
    int32_t nKey = strlen(key);
1,034✔
251

252
    SIndexTerm *term = NULL;
1,034✔
253
    if (type == TSDB_DATA_TYPE_NULL) {
1,034✔
254
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
24✔
255
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
1,010✔
256
      if (pTagVal->nData > 0) {
904✔
257
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
888!
258
        if (val == NULL) {
888!
259
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
260
        }
261
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
888✔
262
        if (len < 0) {
888!
263
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
264
        }
265
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
888✔
266
        type = TSDB_DATA_TYPE_VARCHAR;
888✔
267
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
888✔
268
        taosMemoryFree(val);
888!
269
      } else if (pTagVal->nData == 0) {
16!
270
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
16✔
271
      }
272
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
106✔
273
      double val = *(double *)(&pTagVal->i64);
82✔
274
      int    len = sizeof(val);
82✔
275
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
82✔
276
    } else if (type == TSDB_DATA_TYPE_BOOL) {
24!
277
      int val = *(int *)(&pTagVal->i64);
24✔
278
      int len = sizeof(val);
24✔
279
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
24✔
280
    }
281

282
    if (term != NULL) {
1,034!
283
      int32_t ret = indexMultiTermAdd(terms, term);
1,034✔
284
      if (ret < 0) {
1,034!
285
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
286
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
287
      }
288
    } else {
289
      code = terrno;
×
290
      goto _exception;
×
291
    }
292
  }
293
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
761✔
294
  indexMultiTermDestroy(terms);
761✔
295

296
  taosArrayDestroy(pTagVals);
761✔
297
  return code;
761✔
298
_exception:
×
299
  indexMultiTermDestroy(terms);
×
300
  taosArrayDestroy(pTagVals);
×
301
#endif
302
  return code;
×
303
}
304
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
205✔
305
int32_t code = 0;
205✔
306
#ifdef USE_INVERTED_INDEX
307
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
205!
308
    return TSDB_CODE_INVALID_PARA;
×
309
  }
310
  void       *data = pCtbEntry->ctbEntry.pTags;
205✔
311
  const char *tagName = pSchema->name;
205✔
312

313
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
205✔
314
  tb_uid_t    tuid = pCtbEntry->uid;
205✔
315
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
205✔
316
  int32_t     nTagData = 0;
205✔
317

318
  SArray *pTagVals = NULL;
205✔
319
  code = tTagToValArray((const STag *)data, &pTagVals);
205✔
320
  if (code) {
205!
321
    return code;
×
322
  }
323

324
  SIndexMultiTerm *terms = indexMultiTermCreate();
205✔
325
  if (terms == NULL) {
205!
326
    return terrno;
×
327
  }
328

329
  int16_t nCols = taosArrayGetSize(pTagVals);
205✔
330
  for (int i = 0; i < nCols; i++) {
589✔
331
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
384✔
332
    char     type = pTagVal->type;
384✔
333

334
    char   *key = pTagVal->pKey;
384✔
335
    int32_t nKey = strlen(key);
384✔
336

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

388
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
63✔
389
  int32_t code = 0;
63✔
390
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
63!
391

392
  int64_t    nCtbDropped = 0;
63✔
393
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
63✔
394
  if (suidHash == NULL) {
63!
395
    return terrno;
×
396
  }
397

398
  metaWLock(pMeta);
63✔
399
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
5,335✔
400
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
5,272✔
401
    tb_uid_t suid = 0;
5,272✔
402
    int8_t   sysTbl = 0;
5,272✔
403
    int      type;
404
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
5,272✔
405
    if (code) return code;
5,272!
406
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
5,272!
407
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
5,264✔
408
      if (pVal) {
5,264✔
409
        nCtbDropped = *pVal + 1;
5,203✔
410
      } else {
411
        nCtbDropped = 1;
61✔
412
      }
413
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
5,264✔
414
      if (code) return code;
5,264!
415
    }
416
    /*
417
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
418
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
419
    }
420
    */
421
    metaDebug("batch drop table:%" PRId64, uid);
5,272!
422
  }
423
  metaULock(pMeta);
63✔
424

425
  // update timeseries
426
  void   *pCtbDropped = NULL;
63✔
427
  int32_t iter = 0;
63✔
428
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
124✔
429
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
61✔
430
    int32_t      nCols = 0;
61✔
431
    int8_t       flags = 0;
61✔
432
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
61✔
433
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
61!
434
      if (!TABLE_IS_VIRTUAL(flags)) {
61!
435
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
61✔
436
      }
437
    }
438
  }
439
  tSimpleHashCleanup(suidHash);
63✔
440

441
  pMeta->changed = true;
63✔
442
  return 0;
63✔
443
}
444

445
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
76✔
446
  int32_t code = 0;
76✔
447
  // 1, tranverse table's
448
  // 2, validate table name using vnodeValidateTableHash
449
  // 3, push invalidated table's uid into uidList
450

451
  TBC *pCur;
452
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
76✔
453
  if (code < 0) {
76!
454
    return code;
×
455
  }
456

457
  code = tdbTbcMoveToFirst(pCur);
76✔
458
  if (code) {
76!
459
    tdbTbcClose(pCur);
×
460
    return code;
×
461
  }
462

463
  void *pData = NULL, *pKey = NULL;
76✔
464
  int   nData = 0, nKey = 0;
76✔
465

466
  while (1) {
10,610✔
467
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
10,686✔
468
    if (ret < 0) {
10,686✔
469
      break;
76✔
470
    }
471

472
    SMetaEntry me = {0};
10,610✔
473
    SDecoder   dc = {0};
10,610✔
474
    tDecoderInit(&dc, pData, nData);
10,610✔
475
    code = metaDecodeEntry(&dc, &me);
10,610✔
476
    if (code < 0) {
10,610!
477
      tDecoderClear(&dc);
×
478
      return code;
×
479
    }
480

481
    if (me.type != TSDB_SUPER_TABLE) {
10,610✔
482
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
483
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
10,544✔
484
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
10,544✔
485
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
10,544!
486
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
10,544✔
487
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
10,544!
488
        if (taosArrayPush(uidList, &me.uid) == NULL) {
5,272!
489
          code = terrno;
×
490
          break;
×
491
        }
492
      }
493
    }
494
    tDecoderClear(&dc);
10,610✔
495
  }
496
  tdbFree(pData);
76✔
497
  tdbFree(pKey);
76✔
498
  tdbTbcClose(pCur);
76✔
499

500
  return 0;
76✔
501
}
502

503
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
76✔
504
  int32_t code = 0;
76✔
505

506
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
76✔
507
  if (tbUids == NULL) {
76!
508
    return terrno;
×
509
  }
510

511
  code = metaFilterTableByHash(pMeta, tbUids);
76✔
512
  if (code != 0) {
76!
513
    goto end;
×
514
  }
515
  if (TARRAY_SIZE(tbUids) == 0) {
76✔
516
    goto end;
13✔
517
  }
518

519
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
63!
520
  code = metaDropTables(pMeta, tbUids);
63✔
521
  if (code) goto end;
63!
522

523
end:
63✔
524
  taosArrayDestroy(tbUids);
76✔
525

526
  return code;
76✔
527
}
528

529
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
94,703✔
530
  metaRLock(pMeta);
94,703✔
531

532
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
94,832✔
533

534
  metaULock(pMeta);
94,442✔
535

536
  if (ret != 0) {
94,765!
537
    metaError("ttl failed to find expired table, ret:%d", ret);
×
538
  }
539

540
  return ret;
94,782✔
541
}
542

543
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
5,272✔
544
  int64_t btime;
545
  if (pME->type == TSDB_CHILD_TABLE) {
5,272✔
546
    btime = pME->ctbEntry.btime;
5,264✔
547
  } else if (pME->type == TSDB_NORMAL_TABLE) {
8!
548
    btime = pME->ntbEntry.btime;
8✔
549
  } else {
550
    return TSDB_CODE_FAILED;
×
551
  }
552

553
  btimeKey->btime = btime;
5,272✔
554
  btimeKey->uid = pME->uid;
5,272✔
555
  return 0;
5,272✔
556
}
557

558
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
8✔
559
  if (pME->type == TSDB_NORMAL_TABLE) {
8!
560
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
8✔
561
    ncolKey->uid = pME->uid;
8✔
562
  } else {
563
    return TSDB_CODE_FAILED;
×
564
  }
565
  return 0;
8✔
566
}
567

568
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
5,272✔
569
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
5,272!
570

571
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
5,272✔
572
  if (pME->type == TSDB_CHILD_TABLE) {
5,272✔
573
    ctx.ttlDays = pME->ctbEntry.ttlDays;
5,264✔
574
  } else {
575
    ctx.ttlDays = pME->ntbEntry.ttlDays;
8✔
576
  }
577

578
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
5,272✔
579
  if (ret < 0) {
5,272!
580
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
581
              pME->uid, tstrerror(ret));
582
  }
583
  return;
5,272✔
584
}
585

586
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
5,272✔
587
  void      *pData = NULL;
5,272✔
588
  int        nData = 0;
5,272✔
589
  int        rc = 0;
5,272✔
590
  SMetaEntry e = {0};
5,272✔
591
  SDecoder   dc = {0};
5,272✔
592
  int32_t    ret = 0;
5,272✔
593

594
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
5,272✔
595
  if (rc < 0) {
5,272!
596
    return rc;
×
597
  }
598
  int64_t version = ((SUidIdxVal *)pData)[0].version;
5,272✔
599

600
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
5,272✔
601
  if (rc < 0) {
5,272!
602
    tdbFree(pData);
×
603
    return rc;
×
604
  }
605

606
  tDecoderInit(&dc, pData, nData);
5,272✔
607
  rc = metaDecodeEntry(&dc, &e);
5,272✔
608
  if (rc < 0) {
5,272!
609
    tDecoderClear(&dc);
×
610
    return rc;
×
611
  }
612

613
  if (type) *type = e.type;
5,272!
614

615
  if (e.type == TSDB_CHILD_TABLE) {
5,272✔
616
    if (pSuid) *pSuid = e.ctbEntry.suid;
5,264!
617
    void *tData = NULL;
5,264✔
618
    int   tLen = 0;
5,264✔
619

620
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
5,264!
621
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
5,264✔
622
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
5,264!
623
        SDecoder   tdc = {0};
5,264✔
624
        SMetaEntry stbEntry = {0};
5,264✔
625

626
        tDecoderInit(&tdc, tData, tLen);
5,264✔
627
        ret = metaDecodeEntry(&tdc, &stbEntry);
5,264✔
628
        if (ret < 0) {
5,264!
629
          tDecoderClear(&tdc);
×
630
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
631
                    e.ctbEntry.suid, tstrerror(ret));
632
          return ret;
×
633
        }
634

635
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
5,264!
636

637
        SSchema        *pTagColumn = NULL;
5,264✔
638
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
5,264✔
639
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
5,264!
640
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
641
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
642
          if (ret < 0) {
×
643
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
644
                      e.name, e.uid, tstrerror(ret));
645
          }
646
        } else {
647
          for (int i = 0; i < pTagSchema->nCols; i++) {
11,418✔
648
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
6,154✔
649
            if (!IS_IDX_ON(pTagColumn)) continue;
6,154✔
650
            STagIdxKey *pTagIdxKey = NULL;
5,264✔
651
            int32_t     nTagIdxKey;
652

653
            const void *pTagData = NULL;
5,264✔
654
            int32_t     nTagData = 0;
5,264✔
655

656
            STagVal tagVal = {.cid = pTagColumn->colId};
5,264✔
657
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
5,264!
658
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
5,264!
659
                pTagData = tagVal.pData;
×
660
                nTagData = (int32_t)tagVal.nData;
×
661
              } else {
662
                pTagData = &(tagVal.i64);
5,264✔
663
                nTagData = tDataTypes[pTagColumn->type].bytes;
5,264✔
664
              }
665
            } else {
666
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
667
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
668
              }
669
            }
670

671
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
5,264!
672
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
673
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
5,264✔
674
              if (ret < 0) {
5,264!
675
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
676
                          e.name, e.uid, tstrerror(ret));
677
              }
678
            }
679
            metaDestroyTagIdxKey(pTagIdxKey);
5,264✔
680
            pTagIdxKey = NULL;
5,264✔
681
          }
682
        }
683
        tDecoderClear(&tdc);
5,264✔
684
      }
685
      tdbFree(tData);
5,264✔
686
    }
687
  }
688

689
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
5,272✔
690
  if (ret < 0) {
5,272!
691
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
692
              tstrerror(ret));
693
  }
694
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
5,272✔
695
  if (ret < 0) {
5,272!
696
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
697
              tstrerror(ret));
698
  }
699
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
5,272✔
700
  if (ret < 0) {
5,272!
701
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
702
              tstrerror(ret));
703
  }
704

705
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
5,272!
706
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
5,272✔
707

708
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
5,272!
709

710
  if (e.type == TSDB_CHILD_TABLE) {
5,272✔
711
    ret =
712
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
5,264✔
713
    if (ret < 0) {
5,264!
714
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
715
                tstrerror(ret));
716
    }
717

718
    --pMeta->pVnode->config.vndStats.numOfCTables;
5,264✔
719
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
5,264✔
720
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
5,264✔
721
    if (ret < 0) {
5,264!
722
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
723
                e.ctbEntry.suid, tstrerror(ret));
724
    }
725
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
5,264✔
726
    if (ret < 0) {
5,264!
727
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
728
                e.ctbEntry.suid, tstrerror(ret));
729
    }
730
    /*
731
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
732
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
733
    }
734
    */
735
  } else if (e.type == TSDB_NORMAL_TABLE) {
8!
736
    // drop schema.db (todo)
737

738
    --pMeta->pVnode->config.vndStats.numOfNTables;
8✔
739
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
8✔
740

741
    /*
742
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
743
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
744
    }
745
    */
746
  } else if (e.type == TSDB_SUPER_TABLE) {
×
747
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
748
    if (ret < 0) {
×
749
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
750
                tstrerror(ret));
751
    }
752
    // drop schema.db (todo)
753

754
    ret = metaStatsCacheDrop(pMeta, uid);
×
755
    if (ret < 0) {
×
756
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
757
                tstrerror(ret));
758
    }
759
    ret = metaUidCacheClear(pMeta, uid);
×
760
    if (ret < 0) {
×
761
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
762
                tstrerror(ret));
763
    }
764
    ret = metaTbGroupCacheClear(pMeta, uid);
×
765
    if (ret < 0) {
×
766
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
767
                e.uid, tstrerror(ret));
768
    }
769
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
770
  }
771

772
  ret = metaCacheDrop(pMeta, uid);
5,272✔
773
  if (ret < 0) {
5,272!
774
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
5,272!
775
              tstrerror(ret));
776
  }
777

778
  tDecoderClear(&dc);
5,272✔
779
  tdbFree(pData);
5,272✔
780

781
  return 0;
5,272✔
782
}
783

784
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
5,272✔
785
  SBtimeIdxKey btimeKey = {0};
5,272✔
786
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
5,272!
787
    return 0;
×
788
  }
789
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
5,272✔
790
}
791

792
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
8✔
793
  SNcolIdxKey ncolKey = {0};
8✔
794
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
8!
795
    return 0;
×
796
  }
797
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
8✔
798
}
799

800
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
19,752✔
801
  pMeta->changed = true;
19,752✔
802
  switch (pReq->action) {
19,752!
803
    case TSDB_ALTER_TABLE_ADD_COLUMN:
5,315✔
804
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
805
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
806
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
5,315✔
807
    case TSDB_ALTER_TABLE_DROP_COLUMN:
154✔
808
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
154✔
809
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
788✔
810
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
788✔
811
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
86✔
812
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
86✔
813
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
13,170✔
814
      return metaUpdateTableTagValue(pMeta, version, pReq);
13,170✔
815
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
5✔
816
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
5✔
817
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
94✔
818
      return metaUpdateTableOptions2(pMeta, version, pReq);
94✔
819
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
16✔
820
      return metaUpdateTableColCompress2(pMeta, version, pReq);
16✔
821
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
80✔
822
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
80✔
823
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
44✔
824
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
44✔
825
    default:
×
826
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
827
      break;
828
  }
829
}
830

831
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
2,060✔
832
  if (!tsTtlChangeOnWrite) return 0;
2,060!
833

834
  if (changeTimeMs <= 0) {
2,060!
835
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
836
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
837
  }
838

839
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
2,060✔
840

841
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
2,060✔
842
}
843

844
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
13,874,376✔
845
  if (!tsTtlChangeOnWrite) return 0;
13,874,376✔
846

847
  metaWLock(pMeta);
2,049✔
848
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
2,060✔
849
  metaULock(pMeta);
2,060✔
850
  return ret;
2,060✔
851
}
852

853
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
289,020✔
854
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
855
  if (IS_VAR_DATA_TYPE(type)) {
289,020!
856
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
38,994✔
857
  } else {
858
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
250,026✔
859
  }
860

861
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
289,020!
862
  if (*ppTagIdxKey == NULL) {
289,062!
863
    return terrno;
×
864
  }
865

866
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
289,062✔
867
  (*ppTagIdxKey)->cid = cid;
289,062✔
868
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
289,062✔
869
  (*ppTagIdxKey)->type = type;
289,062✔
870

871
  // refactor
872
  if (IS_VAR_DATA_TYPE(type)) {
289,062!
873
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
39,064✔
874
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
39,064✔
875
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
39,064✔
876
  } else {
877
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
249,998✔
878
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
249,998✔
879
  }
880

881
  return 0;
289,062✔
882
}
883

884
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
274,514✔
885
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
274,514!
886
}
274,514✔
887

888
static void colCompressDebug(SHashObj *pColCmprObj) {
294,830✔
889
  void *p = taosHashIterate(pColCmprObj, NULL);
294,830✔
890
  while (p) {
2,412,477✔
891
    uint32_t cmprAlg = *(uint32_t *)p;
2,117,737✔
892
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
2,117,737✔
893
    p = taosHashIterate(pColCmprObj, p);
2,117,691✔
894

895
    uint8_t l1, l2, lvl;
896
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
2,118,126✔
897

898
    const char *l1str = columnEncodeStr(l1);
2,117,962✔
899
    const char *l2str = columnCompressStr(l2);
2,117,858✔
900
    const char *lvlstr = columnLevelStr(lvl);
2,117,695✔
901
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
2,117,632✔
902
  }
903
  return;
294,740✔
904
}
905

906
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
294,826✔
907
  int rc = 0;
294,826✔
908

909
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
294,826✔
910
  if (pColCmprObj == NULL) {
294,840!
911
    pColCmprObj = NULL;
×
912
    return TSDB_CODE_OUT_OF_MEMORY;
×
913
  }
914

915
  void      *pData = NULL;
294,840✔
916
  int        nData = 0;
294,840✔
917
  SMetaEntry e = {0};
294,840✔
918
  SDecoder   dc = {0};
294,840✔
919

920
  *ppColCmprObj = NULL;
294,840✔
921

922
  metaRLock(pMeta);
294,840✔
923
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
294,855✔
924
  if (rc < 0) {
294,822!
925
    taosHashCleanup(pColCmprObj);
×
926
    metaULock(pMeta);
×
927
    return TSDB_CODE_FAILED;
×
928
  }
929
  int64_t version = ((SUidIdxVal *)pData)[0].version;
294,822✔
930
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
294,822✔
931
  if (rc < 0) {
294,828!
932
    metaULock(pMeta);
×
933
    taosHashCleanup(pColCmprObj);
×
934
    metaError("failed to get table entry");
×
935
    return rc;
×
936
  }
937

938
  tDecoderInit(&dc, pData, nData);
294,828✔
939
  rc = metaDecodeEntry(&dc, &e);
294,822✔
940
  if (rc < 0) {
294,796!
941
    tDecoderClear(&dc);
×
942
    tdbFree(pData);
×
943
    metaULock(pMeta);
×
944
    taosHashCleanup(pColCmprObj);
×
945
    return rc;
×
946
  }
947
  if (withExtSchema(e.type)) {
294,796!
948
    SColCmprWrapper *p = &e.colCmpr;
294,797✔
949
    for (int32_t i = 0; i < p->nCols; i++) {
2,413,182✔
950
      SColCmpr *pCmpr = &p->pColCmpr[i];
2,118,199✔
951
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
2,118,199✔
952
      if (rc < 0) {
2,118,385!
953
        tDecoderClear(&dc);
×
954
        tdbFree(pData);
×
955
        metaULock(pMeta);
×
956
        taosHashCleanup(pColCmprObj);
×
957
        return rc;
×
958
      }
959
    }
960
  } else {
UNCOV
961
    tDecoderClear(&dc);
×
962
    tdbFree(pData);
×
963
    metaULock(pMeta);
×
964
    taosHashCleanup(pColCmprObj);
×
965
    return 0;
×
966
  }
967
  tDecoderClear(&dc);
294,983✔
968
  tdbFree(pData);
294,825✔
969
  metaULock(pMeta);
294,842✔
970

971
  *ppColCmprObj = pColCmprObj;
294,836✔
972
  colCompressDebug(pColCmprObj);
294,836✔
973

974
  return 0;
294,759✔
975
}
976
// refactor later
977
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
48,144✔
978
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
48,083✔
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