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

taosdata / TDengine / #4794

15 Oct 2025 11:06AM UTC coverage: 60.905% (-0.001%) from 60.906%
#4794

push

travis-ci

web-flow
Merge c54016657 into 7e74ade39

154617 of 324341 branches covered (47.67%)

Branch coverage included in aggregate %.

54 of 65 new or added lines in 13 files covered. (83.08%)

2506 existing lines in 114 files now uncovered.

207077 of 269525 relevant lines covered (76.83%)

125143530.79 hits per line

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

65.59
/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) {
637,987,617✔
23
  for (int32_t i = 0; i < nCols; i++) {
2,147,483,647✔
24
    if (HAS_TYPE_MOD(pSchema + i)) {
2,147,483,647✔
25
      return true;
31,240,229✔
26
    }
27
  }
28
  return false;
603,766,696✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
154,182,878✔
32
  if (pME->pExtSchemas) {
154,182,878✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
6,759,782✔
34
    bool                  hasTypeMods = false;
6,759,782✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
6,759,782✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
6,641,459✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
115,002!
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
115,002✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
6,754,845✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
984,602,571✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
1,955,651,791!
46
    }
47
  }
48
  return 0;
154,258,383✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
13,930,752✔
58
    SColRef *p = &pw->pColRef[i];
12,501,020✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
25,002,040!
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
25,002,040!
61
    if (p->hasRef) {
12,501,020!
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
13,988,268!
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
13,988,268!
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
13,988,268!
65
    }
66
  }
67
  return 0;
1,429,732✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
614,389,209✔
71
  bool hasExtSchema = false;
614,389,209✔
72
  SSchemaWrapper* pSchWrapper = NULL;
614,389,209✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
614,389,209✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
614,321,723✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
207,148!
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
207,148✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
614,531,005✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
614,611,519✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
49,844,637!
84
    if (pME->pExtSchemas == NULL) {
24,920,347!
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
1,501,695,033!
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
2,147,483,647!
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
589,687,081✔
93
  }
94

95
  return 0;
614,549,966✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
16,863,907✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
16,863,907✔
100
  bool                  hasTypeMods = false;
16,863,907✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
16,863,907✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
14,869,789✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,998,533!
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
2,000,433✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
16,870,829✔
109

110
  if (hasTypeMods) {
16,862,557✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
45,145!
112
    if (ret != NULL) {
45,145!
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
45,145!
114
    }
115
    return ret;
45,145✔
116
  }
117
  return NULL;
16,817,412✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
16,898✔
151
  int32_t    i = 0, j = 0;
16,898✔
152
  for (i = 0; i < nCols; ++i) {
130,711✔
153
    while (j < pParam->nFuncs) {
207,746✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
199,297✔
155
        pFuncIds[i] = pParam->funcIds[j];
92,939✔
156
        break;
92,939✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
106,358✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
20,874✔
160
        break;
20,874✔
161
      }
162
      ++j;
85,484✔
163
    }
164
    if (j >= pParam->nFuncs) {
122,262✔
165
      for (; i < nCols; ++i) {
16,898✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
8,449✔
167
      }
168
      break;
8,449✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
16,898✔
172

173
  return 0;
16,898✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
11,325,288✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
11,325,288✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
22,651,341!
179
  if (pWrapper->nCols == 0) {
11,325,288!
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
22,652,024!
184
  uDebug("decode cols:%d", pWrapper->nCols);
11,326,818✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
22,653,263!
186
  if (pWrapper->pColRef == NULL) {
11,327,020!
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
175,808,133✔
191
    SColRef *p = &pWrapper->pColRef[i];
164,474,834✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
328,976,616!
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
328,983,107!
194
    if (p->hasRef) {
164,493,728!
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
93,489,687!
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
93,488,569!
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
93,492,778!
198
    }
199
  }
200
  return 0;
11,328,713✔
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) {
810,923✔
220
  if (pSrc->nCols > 0) {
810,923!
221
    pDst->nCols = pSrc->nCols;
810,923✔
222
    pDst->version = pSrc->version;
810,923✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
810,923!
224
    if (NULL == pDst->pColRef) {
810,923!
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
810,923!
228
  }
229
  return 0;
810,923✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
35,855,719✔
233
  int32_t code = 0;
35,855,719✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
71,672,682!
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
71,693,509!
236
  uTrace("encode cols:%d", pw->nCols);
35,876,546✔
237

238
  for (int32_t i = 0; i < pw->nCols; i++) {
2,147,483,647✔
239
    SColCmpr *p = &pw->pColCmpr[i];
2,147,483,647✔
240
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
2,147,483,647!
241
    TAOS_CHECK_RETURN(tEncodeU32(pCoder, p->alg));
2,147,483,647!
242
  }
243
  return code;
35,931,154✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
35,735,757✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
35,735,757✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
35,752,350✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
766,787,716✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
766,787,716✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,533,933,103!
252
  if (pWrapper->nCols == 0) {
766,897,881!
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,533,658,878!
257
  uDebug("dencode cols:%d", pWrapper->nCols);
766,913,980✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,533,808,382✔
259
  if (pWrapper->pColCmpr == NULL) {
767,003,741!
260
    return terrno;
×
261
  }
262

263
  for (int i = 0; i < pWrapper->nCols; i++) {
2,147,483,647✔
264
    SColCmpr *p = &pWrapper->pColCmpr[i];
2,147,483,647✔
265
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
2,147,483,647!
266
    TAOS_CHECK_RETURN(tDecodeU32(pDecoder, &p->alg));
2,147,483,647!
267
  }
268
  return 0;
767,173,044✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
198,320✔
273

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

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

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

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
160,965,849✔
294
  if (pSrc->nCols > 0) {
160,965,849✔
295
    pDst->nCols = pSrc->nCols;
146,108,416✔
296
    pDst->version = pSrc->version;
146,112,065✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
146,114,984!
298
    if (NULL == pDst->pColCmpr) {
146,078,556!
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
146,086,855!
302
  }
303
  return 0;
160,975,465✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
161,766,426✔
307
  if (pColRef) {
161,766,426!
308
    taosMemoryFreeClear(pColRef->pColRef);
161,774,206!
309
  }
310
}
161,781,254✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
161,769,712✔
313
  if (pCmpr) {
161,769,712✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
161,762,941!
315
  }
316
}
161,770,880✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
174,125,495✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
174,125,495!
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
348,308,270!
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
348,257,683!
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
348,228,396!
323

324
  if (pME->type > 0) {
174,128,459✔
325
    if (pME->name == NULL) {
154,222,358!
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
308,435,252!
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
154,245,089✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
44,911,992!
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
44,955,809!
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
44,980,834!
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
22,465,831✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
47,215!
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
131,749,878✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
235,123,652!
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
235,127,311!
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
235,133,248!
342
      if (pME->ctbEntry.commentLen > 0) {
117,568,568✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
19,044!
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
235,126,098!
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
117,562,213!
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
14,192,944!
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
28,385,888!
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
28,385,888!
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
28,385,888!
351
      if (pME->ntbEntry.commentLen > 0) {
14,192,944✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
17,300!
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
28,385,888!
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
28,385,888!
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) {
154,221,608✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,493,149!
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
152,779,385✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
22,492,015✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
130,324,873✔
368
        if (pME->colCmpr.nCols != 0) {
13,432,630✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
13,234,310!
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
198,320!
372
          SColCmprWrapper colCmprs = {0};
198,320✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
198,320!
374
          if (code != 0) {
198,320!
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
198,320✔
379
          taosMemoryFree(colCmprs.pColCmpr);
198,320!
380
          TAOS_CHECK_RETURN(code);
198,320!
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
154,242,490!
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
174,144,348✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
44,950,754!
388
  }
389

390
  tEndEncode(pCoder);
174,094,291✔
391
  return 0;
174,043,774✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,208,991,713✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,208,991,713!
396
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->version));
2,147,483,647!
397
  TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->type));
2,147,483,647!
398
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->uid));
2,147,483,647!
399

400
  if (headerOnly) {
1,209,603,600✔
401
    tEndDecode(pCoder);
66,142✔
402
    return 0;
66,142✔
403
  }
404

405
  if (pME->type > 0) {
1,209,537,458✔
406
    TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->name));
2,147,483,647!
407

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,209,438,607✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,228,913,458!
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,228,987,158!
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,228,758,578!
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
614,319,345✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
181,405!
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
594,609,945✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
877,640,517!
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
877,813,912!
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
877,616,858!
419
      if (pME->ctbEntry.commentLen > 0) {
438,709,142✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
27,758!
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
877,611,756!
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
438,908,712!
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
156,065,607!
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
312,187,071!
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
312,207,348!
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
312,188,462!
428
      if (pME->ntbEntry.commentLen > 0) {
156,081,808✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
26,888!
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
312,151,687!
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
312,182,632!
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) {
1,209,196,436✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
614,462,888!
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
614,457,608!
446

447
        if (pME->colCmpr.nCols == 0) {
614,333,599!
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) {
594,571,891✔
455
      if (!tDecodeIsEnd(pCoder)) {
152,596,183!
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
152,602,745✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
152,602,745✔
458
        if (pME->colCmpr.nCols == 0) {
152,590,764!
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);
152,598,036✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
441,974,117✔
467
      if (!tDecodeIsEnd(pCoder)) {
11,326,514!
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
11,327,279✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
11,327,279!
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)) {
1,209,215,640✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
614,598,889!
479
    } else {
480
      pME->pExtSchemas = NULL;
594,616,751✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,209,073,941✔
484
    if (!tDecodeIsEnd(pCoder)) {
614,281,707!
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,228,642,594!
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,209,208,805✔
491
  return 0;
1,209,195,447✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,209,049,625✔
495

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

501
  pDst->nCols = pSrc->nCols;
283,457,710✔
502
  pDst->version = pSrc->version;
283,454,530✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
283,439,349!
504
  if (pDst->pSchema == NULL) {
283,380,501!
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
283,389,288!
508
  return TSDB_CODE_SUCCESS;
283,463,505✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
27,335✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
283,434,207✔
543
  if (pSchema) {
283,434,207!
544
    taosMemoryFreeClear(pSchema->pSchema);
283,444,730!
545
  }
546
}
283,449,188✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
161,864,497✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
161,864,497!
563
    return;
91,689✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
161,778,973!
567

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

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
161,767,806✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
136,800,025✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
136,792,099✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
136,801,179✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
41,251✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
24,970,983✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
15,130,622!
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
15,130,637!
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
9,840,361!
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
9,840,361✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
9,840,361!
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
161,771,707✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
161,760,044!
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
161,755,591✔
591

592
  taosMemoryFreeClear(*ppEntry);
161,741,692!
593
  return;
161,754,288✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
161,753,726✔
597
  int32_t code = TSDB_CODE_SUCCESS;
161,753,726✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
161,753,726!
UNCOV
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
161,788,247!
604
  if (NULL == *ppEntry) {
161,735,151!
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
161,743,576✔
609
  (*ppEntry)->type = pEntry->type;
161,765,477✔
610
  (*ppEntry)->uid = pEntry->uid;
161,759,796✔
611

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

616
  if (pEntry->name) {
161,765,609!
617
    (*ppEntry)->name = tstrdup(pEntry->name);
161,777,862✔
618
    if (NULL == (*ppEntry)->name) {
161,780,499!
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
161,776,732✔
626
    (*ppEntry)->flags = pEntry->flags;
136,805,526✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
136,790,586✔
629
    if (code) {
136,806,291!
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
136,806,291✔
635
    if (code) {
136,819,739!
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
136,819,739✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
27,335✔
641
      if (code) {
27,335!
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
136,819,873✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
24,971,725✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
15,131,364✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
15,131,364✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
15,131,364✔
651

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

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

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
9,840,361✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
9,840,361✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
5,440!
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
5,440!
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
5,440!
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) {
161,779,107✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
837,550✔
702
    if (code) {
810,923!
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
160,942,053✔
708
    if (code) {
160,973,291!
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
161,784,214!
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
7,379,000!
715
    if (!(*ppEntry)->pExtSchemas) {
7,369,711!
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
7,371,337!
721
  }
722

723
  return code;
161,771,954✔
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