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

taosdata / TDengine / #4775

01 Oct 2025 04:06AM UTC coverage: 58.544% (+0.2%) from 58.357%
#4775

push

travis-ci

web-flow
Merge pull request #33171 from taosdata/merge/3.3.6tomain

merge: from 3.3.6 to main branch

139180 of 302743 branches covered (45.97%)

Branch coverage included in aggregate %.

15 of 20 new or added lines in 2 files covered. (75.0%)

1305 existing lines in 71 files now uncovered.

210415 of 294403 relevant lines covered (71.47%)

17710526.47 hits per line

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

66.44
/source/dnode/vnode/src/meta/metaEntry.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
#include "osMemPool.h"
18
#include "osMemory.h"
19
#include "tencode.h"
20
#include "tmsg.h"
21

22
static bool schemasHasTypeMod(const SSchema *pSchema, int32_t nCols) {
20,603,073✔
23
  for (int32_t i = 0; i < nCols; i++) {
488,378,987✔
24
    if (HAS_TYPE_MOD(pSchema + i)) {
467,869,746✔
25
      return true;
93,832✔
26
    }
27
  }
28
  return false;
20,509,241✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
600,566✔
32
  if (pME->pExtSchemas) {
600,566✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
28,791✔
34
    bool                  hasTypeMods = false;
28,791✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
28,791✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
28,569✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
222!
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
224✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
28,793✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
5,621,963✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
11,186,346!
46
    }
47
  }
48
  return 0;
600,565✔
49
}
50

51
int meteEncodeColRefEntry(SEncoder *pCoder, const SMetaEntry *pME) {
1,944✔
52
  const SColRefWrapper *pw = &pME->colRef;
1,944✔
53
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
3,888!
54
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
3,888!
55
  uTrace("encode cols:%d", pw->nCols);
1,944!
56

57
  for (int32_t i = 0; i < pw->nCols; i++) {
18,036✔
58
    SColRef *p = &pw->pColRef[i];
16,092✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
32,184!
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
32,184!
61
    if (p->hasRef) {
16,092✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
18,492!
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
18,492!
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
18,492!
65
    }
66
  }
67
  return 0;
1,944✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
20,338,203✔
71
  bool hasExtSchema = false;
20,338,203✔
72
  SSchemaWrapper* pSchWrapper = NULL;
20,338,203✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
20,338,203!
74
    pSchWrapper = &pME->stbEntry.schemaRow;
20,381,635✔
UNCOV
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
×
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
3,371✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
20,385,006✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
20,485,950!
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
69,494!
84
    if (pME->pExtSchemas == NULL) {
69,513!
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
7,494,331✔
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
14,849,596!
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
20,416,456✔
93
  }
94

95
  return 0;
20,486,009✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
130,684✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
130,684✔
100
  bool                  hasTypeMods = false;
130,684✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
130,684✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
90,770✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
39,914!
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
39,968✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
130,738✔
109

110
  if (hasTypeMods) {
130,797✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
128!
112
    if (ret != NULL) {
118!
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
118✔
114
    }
115
    return ret;
118✔
116
  }
117
  return NULL;
130,669✔
118
}
119

120
int32_t metaGetRsmaSchema(const SMetaEntry *pME, SSchemaRsma **rsmaSchema) {
22✔
121
  if (!rsmaSchema) return 0;
22!
122
  if ((pME->type != TSDB_SUPER_TABLE) || !TABLE_IS_ROLLUP(pME->flags)) {  // only support super table
22!
123
    *rsmaSchema = NULL;
×
124
    return 0;
×
125
  }
126

127
  const SRSmaParam *pParam = &pME->stbEntry.rsmaParam;
22✔
128
  const SSchema    *pSchema = pME->stbEntry.schemaRow.pSchema;
22✔
129
  int32_t           nCols = pME->stbEntry.schemaRow.nCols;
22✔
130

131
  *rsmaSchema = (SSchemaRsma *)taosMemoryMalloc(sizeof(SSchemaRsma));
22!
132
  if (*rsmaSchema == NULL) {
22!
133
    return terrno;
×
134
  }
135

136
  (*rsmaSchema)->funcIds = taosMemoryMalloc(sizeof(func_id_t) * nCols);
22!
137
  if ((*rsmaSchema)->funcIds == NULL) {
22!
138
    taosMemoryFree(*rsmaSchema);
×
139
    *rsmaSchema = NULL;
×
140
    return terrno;
×
141
  }
142

143
  (void)snprintf((*rsmaSchema)->tbName, TSDB_TABLE_NAME_LEN, "%s", pME->name);
22✔
144
  (*rsmaSchema)->tbUid = pME->uid;
22✔
145
  (*rsmaSchema)->tbType = pME->type;
22✔
146
  (*rsmaSchema)->interval[0] = pParam->interval[0];
22✔
147
  (*rsmaSchema)->interval[1] = pParam->interval[1];
22✔
148
  (*rsmaSchema)->nFuncs = nCols;
22✔
149

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
22✔
151
  int32_t    i = 0, j = 0;
22✔
152
  for (i = 0; i < nCols; ++i) {
192✔
153
    while (j < pParam->nFuncs) {
316✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
302✔
155
        pFuncIds[i] = pParam->funcIds[j];
140✔
156
        break;
140✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
162✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
30✔
160
        break;
30✔
161
      }
162
      ++j;
132✔
163
    }
164
    if (j >= pParam->nFuncs) {
184✔
165
      for (; i < nCols; ++i) {
28✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
14✔
167
      }
168
      break;
14✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
22✔
172

173
  return 0;
22✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
15,737✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
15,737✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
31,474!
179
  if (pWrapper->nCols == 0) {
15,737!
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
31,475!
184
  uDebug("decode cols:%d", pWrapper->nCols);
15,738✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
15,738!
186
  if (pWrapper->pColRef == NULL) {
15,743!
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
228,588✔
191
    SColRef *p = &pWrapper->pColRef[i];
212,857✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
425,669!
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
425,627!
194
    if (p->hasRef) {
212,815✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
122,410!
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
122,475!
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
122,504✔
198
    }
199
  }
200
  return 0;
15,731✔
201
}
202

203
static FORCE_INLINE int32_t metatInitDefaultSColRefWrapper(SDecoder *pDecoder, SColRefWrapper *pRef,
204
                                                            SSchemaWrapper *pSchema) {
205
  pRef->nCols = pSchema->nCols;
×
206
  if ((pRef->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pRef->nCols * sizeof(SColRef))) == NULL) {
×
207
    return terrno;
×
208
  }
209

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
×
211
    SColRef  *pColRef = &pRef->pColRef[i];
×
212
    SSchema  *pColSchema = &pSchema->pSchema[i];
×
213
    pColRef->id = pColSchema->colId;
×
214
    pColRef->hasRef = false;
×
215
  }
216
  return 0;
×
217
}
218

219
static int32_t metaCloneColRef(const SColRefWrapper*pSrc, SColRefWrapper *pDst) {
1,037✔
220
  if (pSrc->nCols > 0) {
1,037!
221
    pDst->nCols = pSrc->nCols;
1,037✔
222
    pDst->version = pSrc->version;
1,037✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
1,037!
224
    if (NULL == pDst->pColRef) {
1,037!
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
1,037✔
228
  }
229
  return 0;
1,037✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
133,789✔
233
  int32_t code = 0;
133,789✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
267,578!
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
267,578!
236
  uTrace("encode cols:%d", pw->nCols);
133,789✔
237

238
  for (int32_t i = 0; i < pw->nCols; i++) {
24,400,458✔
239
    SColCmpr *p = &pw->pColCmpr[i];
24,266,687✔
240
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
48,533,374!
241
    TAOS_CHECK_RETURN(tEncodeU32(pCoder, p->alg));
48,533,374!
242
  }
243
  return code;
133,771✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
133,411✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
133,411✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
133,411✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
22,799,103✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
22,799,103✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
45,497,826!
252
  if (pWrapper->nCols == 0) {
22,698,723!
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
45,380,396!
257
  uDebug("dencode cols:%d", pWrapper->nCols);
22,681,673✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
22,681,673✔
259
  if (pWrapper->pColCmpr == NULL) {
22,825,294!
260
    return terrno;
×
261
  }
262

263
  for (int i = 0; i < pWrapper->nCols; i++) {
507,203,468✔
264
    SColCmpr *p = &pWrapper->pColCmpr[i];
485,567,229✔
265
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
967,098,670!
266
    TAOS_CHECK_RETURN(tDecodeU32(pDecoder, &p->alg));
965,909,615!
267
  }
268
  return 0;
21,636,239✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
UNCOV
272
  pCmpr->nCols = pSchema->nCols;
×
273

UNCOV
274
  if (pDecoder == NULL) {
×
275
    pCmpr->pColCmpr = taosMemoryCalloc(1, pCmpr->nCols * sizeof(SColCmpr));
412!
276
  } else {
277
    pCmpr->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pCmpr->nCols * sizeof(SColCmpr));
×
278
  }
279

280
  if (pCmpr->pColCmpr == NULL) {
412!
281
    return terrno;
×
282
  }
283

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
2,072!
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
1,660✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
1,660✔
287
    pColCmpr->id = pColSchema->colId;
1,660✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
1,660✔
289
  }
290
  return 0;
412✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
532,204✔
294
  if (pSrc->nCols > 0) {
532,204✔
295
    pDst->nCols = pSrc->nCols;
484,668✔
296
    pDst->version = pSrc->version;
484,668✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
484,668!
298
    if (NULL == pDst->pColCmpr) {
484,831!
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
484,831✔
302
  }
303
  return 0;
532,367✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
533,432✔
307
  if (pColRef) {
533,432!
308
    taosMemoryFreeClear(pColRef->pColRef);
533,435!
309
  }
310
}
533,432✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
533,315✔
313
  if (pCmpr) {
533,315!
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
533,347!
315
  }
316
}
533,388✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
639,089✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
639,089✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
1,279,620!
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
1,279,620!
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
1,279,620!
323

324
  if (pME->type > 0) {
639,810✔
325
    if (pME->name == NULL) {
601,437!
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
1,202,874!
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
601,437✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
196,080!
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
196,080!
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
196,080!
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
98,040✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
48!
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
503,397✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
933,310!
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
933,310!
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
933,310!
342
      if (pME->ctbEntry.commentLen > 0) {
466,655✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
156!
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
933,310!
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
466,655!
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
36,742!
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
73,484!
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
73,484!
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
73,484!
351
      if (pME->ntbEntry.commentLen > 0) {
36,742✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
156!
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
73,484!
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
73,484!
356
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
357
      TAOS_CHECK_RETURN(tEncodeTSma(pCoder, pME->smaEntry.tsma));
×
358
    } else {
359
      metaError("meta/entry: invalide table type: %" PRId8 " encode failed.", pME->type);
×
360
      return TSDB_CODE_INVALID_PARA;
×
361
    }
362
    if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
601,335✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,866!
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
599,469!
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
98,092✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
501,377✔
368
        if (pME->colCmpr.nCols != 0) {
35,750✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
35,338!
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
412!
372
          SColCmprWrapper colCmprs = {0};
412✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
412!
374
          if (code != 0) {
412!
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
412✔
379
          taosMemoryFree(colCmprs.pColCmpr);
412!
380
          TAOS_CHECK_RETURN(code);
412!
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
601,211!
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
639,705✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
196,096!
388
  }
389

390
  tEndEncode(pCoder);
639,705✔
391
  return 0;
639,678✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
32,663,306✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
32,663,306!
396
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->version));
65,129,244!
397
  TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->type));
64,661,107!
398
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->uid));
64,430,463!
399

400
  if (headerOnly) {
32,190,433✔
401
    tEndDecode(pCoder);
90,252✔
402
    return 0;
90,252✔
403
  }
404

405
  if (pME->type > 0) {
32,100,181!
406
    TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->name));
64,819,619!
407

408
    if (pME->type == TSDB_SUPER_TABLE) {
32,610,623✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
40,792,202!
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
39,516,270!
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
38,110,989!
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
18,932,664✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
227!
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
12,156,366✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
19,564,391!
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
19,520,304!
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
19,510,425!
419
      if (pME->ctbEntry.commentLen > 0) {
9,762,659✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
308!
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
19,521,366!
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
9,758,707!
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
2,364,513!
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
4,725,387!
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
4,720,059!
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
4,718,168!
428
      if (pME->ntbEntry.commentLen > 0) {
2,358,983✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
350!
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
4,718,784!
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
4,667,978!
433
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
434
      pME->smaEntry.tsma = tDecoderMalloc(pCoder, sizeof(STSma));
×
435
      if (!pME->smaEntry.tsma) {
×
436
        return terrno;
×
437
      }
438
      TAOS_CHECK_RETURN(tDecodeTSma(pCoder, pME->smaEntry.tsma, true));
×
439
    } else {
440
      metaError("meta/entry: invalide table type: %" PRId8 " decode failed.", pME->type);
×
441
      return TSDB_CODE_INVALID_PARA;
×
442
    }
443
    if (pME->type == TSDB_SUPER_TABLE) {
32,618,746✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
20,441,500!
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
20,456,346!
446

447
        if (pME->colCmpr.nCols == 0) {
20,352,065!
448
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
449
        }
450
      } else {
451
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
452
        TABLE_SET_COL_COMPRESSED(pME->flags);
×
453
      }
454
    } else if (pME->type == TSDB_NORMAL_TABLE) {
12,177,246✔
455
      if (!tDecodeIsEnd(pCoder)) {
2,359,881!
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
2,360,203✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
2,360,204✔
458
        if (pME->colCmpr.nCols == 0) {
2,356,225!
459
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
460
        }
461
      } else {
462
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
463
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
464
      }
465
      TABLE_SET_COL_COMPRESSED(pME->flags);
2,356,225✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
9,817,365✔
467
      if (!tDecodeIsEnd(pCoder)) {
15,737!
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
15,738✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
15,738!
470
      } else {
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
472
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
473
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
474
        }
475
      }
476
    }
477
    if (!tDecodeIsEnd(pCoder)) {
32,372,970✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
20,349,230!
479
    } else {
480
      pME->pExtSchemas = NULL;
12,023,740✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
32,394,996✔
484
    if (!tDecodeIsEnd(pCoder)) {
20,479,755!
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
40,856,438!
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
32,288,434✔
491
  return 0;
32,436,321✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
32,558,903✔
495

496
static int32_t metaCloneSchema(const SSchemaWrapper *pSrc, SSchemaWrapper *pDst) {
955,806✔
497
  if (pSrc == NULL || pDst == NULL) {
955,806!
498
    return TSDB_CODE_INVALID_PARA;
×
499
  }
500

501
  pDst->nCols = pSrc->nCols;
955,881✔
502
  pDst->version = pSrc->version;
955,881✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
955,881!
504
  if (pDst->pSchema == NULL) {
955,861!
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
955,861✔
508
  return TSDB_CODE_SUCCESS;
955,861✔
509
}
510

511
static int32_t metaCloneRsmaParam(const SRSmaParam *pSrc, SRSmaParam *pDst) {
15✔
512
  if (pSrc == NULL || pDst == NULL) {
15!
513
    return TSDB_CODE_INVALID_PARA;
×
514
  }
515
  memcpy(pDst, pSrc, sizeof(SRSmaParam));
15✔
516
  pDst->name = tstrdup(pSrc->name);
15✔
517
  if (pDst->name == NULL) {
15!
518
    return terrno;
×
519
  }
520
  if (pSrc->nFuncs > 0) {
15!
521
    pDst->nFuncs = pSrc->nFuncs;
15✔
522
    pDst->funcColIds = (col_id_t *)taosMemoryMalloc(pSrc->nFuncs * sizeof(col_id_t));
15!
523
    if (pDst->funcColIds == NULL) {
15!
524
      return terrno;
×
525
    }
526
    memcpy(pDst->funcColIds, pSrc->funcColIds, pSrc->nFuncs * sizeof(col_id_t));
15✔
527

528
    pDst->funcIds = (func_id_t *)taosMemoryMalloc(pSrc->nFuncs * sizeof(func_id_t));
15!
529
    if (pDst->funcIds == NULL) {
15!
530
      return terrno;
×
531
    }
532
    memcpy(pDst->funcIds, pSrc->funcIds, pSrc->nFuncs * sizeof(func_id_t));
15✔
533
  } else {
534
    pDst->nFuncs = 0;
×
535
    pDst->funcColIds = NULL;
×
536
    pDst->funcIds = NULL;
×
537
  }
538

539
  return TSDB_CODE_SUCCESS;
15✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
955,875✔
543
  if (pSchema) {
955,875!
544
    taosMemoryFreeClear(pSchema->pSchema);
955,932!
545
  }
546
}
956,019✔
547

548
/**
549
 * @param type 0x01 free name
550
 */
551
void metaFreeRsmaParam(SRSmaParam *pParam, int8_t type) {
39✔
552
  if (pParam) {
39!
553
    if ((type & 0x01)) {
39!
554
      taosMemoryFreeClear(pParam->name);
39!
555
    }
556
    taosMemoryFreeClear(pParam->funcColIds);
39!
557
    taosMemoryFreeClear(pParam->funcIds);
39!
558
  }
559
}
39✔
560

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
533,416✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
533,416!
563
    return;
73✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
533,343!
567

568
  if ((*ppEntry)->type < 0) {
533,352!
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
533,352✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
470,694✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
470,735✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
470,735✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
39✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
62,658✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
47,915!
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
47,915!
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
14,743!
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
14,743✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
14,743!
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
533,393✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
533,414!
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
533,416✔
591

592
  taosMemoryFreeClear(*ppEntry);
533,426!
593
  return;
533,467✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
533,177✔
597
  int32_t code = TSDB_CODE_SUCCESS;
533,177✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
533,177!
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
533,220!
604
  if (NULL == *ppEntry) {
533,389!
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
533,389✔
609
  (*ppEntry)->type = pEntry->type;
533,389✔
610
  (*ppEntry)->uid = pEntry->uid;
533,389✔
611

612
  if (pEntry->type < 0) {
533,389!
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
533,389!
617
    (*ppEntry)->name = tstrdup(pEntry->name);
533,421✔
618
    if (NULL == (*ppEntry)->name) {
533,343!
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
533,327✔
626
    (*ppEntry)->flags = pEntry->flags;
470,670✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
470,670✔
629
    if (code) {
470,646!
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
470,646✔
635
    if (code) {
470,631!
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
470,631✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
15✔
641
      if (code) {
15!
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
470,638✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
62,657✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
47,914✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
47,914✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
47,914✔
651

652
    // comment
653
    (*ppEntry)->ctbEntry.commentLen = pEntry->ctbEntry.commentLen;
47,914✔
654
    if (pEntry->ctbEntry.commentLen > 0) {
47,914✔
655
      (*ppEntry)->ctbEntry.comment = taosMemoryMalloc(pEntry->ctbEntry.commentLen + 1);
45!
656
      if (NULL == (*ppEntry)->ctbEntry.comment) {
45!
657
        code = terrno;
×
658
        metaCloneEntryFree(ppEntry);
×
659
        return code;
×
660
      }
661
      memcpy((*ppEntry)->ctbEntry.comment, pEntry->ctbEntry.comment, pEntry->ctbEntry.commentLen + 1);
45✔
662
    }
663

664
    // tags
665
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
47,914✔
666
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
47,914!
667
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
47,915!
668
      code = terrno;
×
669
      metaCloneEntryFree(ppEntry);
×
670
      return code;
×
671
    }
672
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
47,915✔
673
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
14,743!
674
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
14,743✔
675
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
14,743✔
676
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
14,743✔
677

678
    // schema
679
    code = metaCloneSchema(&pEntry->ntbEntry.schemaRow, &(*ppEntry)->ntbEntry.schemaRow);
14,743✔
680
    if (code) {
14,743!
681
      metaCloneEntryFree(ppEntry);
×
682
      return code;
×
683
    }
684

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
14,743✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
14,743✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
49!
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
49!
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
49✔
695
    }
696
  } else {
697
    return TSDB_CODE_INVALID_PARA;
×
698
  }
699

700
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
533,296✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
1,085✔
702
    if (code) {
1,037!
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
532,211✔
708
    if (code) {
532,381✔
709
      metaCloneEntryFree(ppEntry);
44✔
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
533,374!
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
26,227!
715
    if (!(*ppEntry)->pExtSchemas) {
26,228!
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
26,228✔
721
  }
722

723
  return code;
533,375✔
724
}
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