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

taosdata / TDengine / #4818

20 Oct 2025 02:09AM UTC coverage: 61.04% (-0.1%) from 61.141%
#4818

push

travis-ci

web-flow
Merge e6f7b1ad7 into 7e74ade39

155148 of 324487 branches covered (47.81%)

Branch coverage included in aggregate %.

152 of 185 new or added lines in 22 files covered. (82.16%)

4700 existing lines in 117 files now uncovered.

207487 of 269610 relevant lines covered (76.96%)

127870039.14 hits per line

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

66.27
/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) {
680,442,579✔
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;
32,897,574✔
26
    }
27
  }
28
  return false;
646,472,414✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
156,816,652✔
32
  if (pME->pExtSchemas) {
156,816,652✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
7,104,829✔
34
    bool                  hasTypeMods = false;
7,104,829✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
7,104,829✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
6,979,781✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
119,798!
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
119,798✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
7,098,932✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
910,194,935✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
1,806,194,332!
46
    }
47
  }
48
  return 0;
156,885,118✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
14,065,524✔
58
    SColRef *p = &pw->pColRef[i];
12,614,226✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
25,228,452!
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
25,228,452!
61
    if (p->hasRef) {
12,614,226!
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
14,188,760!
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
14,188,760!
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
14,188,760!
65
    }
66
  }
67
  return 0;
1,451,298✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
656,320,200✔
71
  bool hasExtSchema = false;
656,320,200✔
72
  SSchemaWrapper* pSchWrapper = NULL;
656,320,200✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
656,320,200✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
656,166,443✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
358,905!
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
358,905✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
656,567,298✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
656,651,013✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
52,524,144!
84
    if (pME->pExtSchemas == NULL) {
26,261,863!
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
1,446,819,631!
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
2,147,483,647!
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
630,387,699✔
93
  }
94

95
  return 0;
656,485,409✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
17,045,606✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
17,045,606✔
100
  bool                  hasTypeMods = false;
17,045,606✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
17,045,606✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
15,029,550✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
2,027,863!
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
2,030,052✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
17,060,311✔
109

110
  if (hasTypeMods) {
17,038,930✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
45,697!
112
    if (ret != NULL) {
45,697!
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
45,697!
114
    }
115
    return ret;
45,697✔
116
  }
117
  return NULL;
16,993,233✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
17,136✔
151
  int32_t    i = 0, j = 0;
17,136✔
152
  for (i = 0; i < nCols; ++i) {
132,552✔
153
    while (j < pParam->nFuncs) {
210,672✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
202,104✔
155
        pFuncIds[i] = pParam->funcIds[j];
94,248✔
156
        break;
94,248✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
107,856✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
21,168✔
160
        break;
21,168✔
161
      }
162
      ++j;
86,688✔
163
    }
164
    if (j >= pParam->nFuncs) {
123,984✔
165
      for (; i < nCols; ++i) {
17,136✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
8,568✔
167
      }
168
      break;
8,568✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
17,136✔
172

173
  return 0;
17,136✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
19,482,787✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
19,482,787✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
38,967,520!
179
  if (pWrapper->nCols == 0) {
19,483,555!
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
38,964,858!
184
  uDebug("decode cols:%d", pWrapper->nCols);
19,482,071✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
38,963,691!
186
  if (pWrapper->pColRef == NULL) {
19,481,620!
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
219,827,593✔
191
    SColRef *p = &pWrapper->pColRef[i];
200,336,829✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
400,703,071!
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
400,720,275!
194
    if (p->hasRef) {
200,365,550!
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
123,541,258!
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
123,537,296!
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
123,542,807!
198
    }
199
  }
200
  return 0;
19,483,965✔
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) {
809,814✔
220
  if (pSrc->nCols > 0) {
809,814!
221
    pDst->nCols = pSrc->nCols;
809,814✔
222
    pDst->version = pSrc->version;
809,814✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
809,814!
224
    if (NULL == pDst->pColRef) {
809,814!
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
809,814!
228
  }
229
  return 0;
809,814✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
36,692,247✔
233
  int32_t code = 0;
36,692,247✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
73,371,349!
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
73,377,476!
236
  uTrace("encode cols:%d", pw->nCols);
36,698,374✔
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;
36,749,890✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
36,418,999✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
36,418,999✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
36,492,289✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
823,674,765✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
823,674,765✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,647,578,296!
252
  if (pWrapper->nCols == 0) {
823,610,485!
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,647,397,663!
257
  uDebug("dencode cols:%d", pWrapper->nCols);
823,843,437✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,647,704,652✔
259
  if (pWrapper->pColCmpr == NULL) {
823,985,222!
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;
824,153,350✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
274,488✔
273

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

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

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

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
164,273,099✔
294
  if (pSrc->nCols > 0) {
164,273,099✔
295
    pDst->nCols = pSrc->nCols;
148,705,733✔
296
    pDst->version = pSrc->version;
148,709,987✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
148,710,013!
298
    if (NULL == pDst->pColCmpr) {
148,697,589!
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
148,696,926!
302
  }
303
  return 0;
164,279,973✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
165,080,306✔
307
  if (pColRef) {
165,080,306!
308
    taosMemoryFreeClear(pColRef->pColRef);
165,086,729!
309
  }
310
}
165,086,101✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
165,074,376✔
313
  if (pCmpr) {
165,074,376✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
165,071,576!
315
  }
316
}
165,081,685✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
177,644,357✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
177,644,357!
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
355,365,386!
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
355,319,811!
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
355,287,706!
323

324
  if (pME->type > 0) {
177,647,108✔
325
    if (pME->name == NULL) {
156,879,206!
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
313,715,925!
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
156,878,331✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
46,321,850!
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
46,375,230!
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
46,400,385!
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
23,172,456✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
46,368!
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
133,682,712✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
238,769,084!
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
238,775,449!
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
238,780,987!
342
      if (pME->ctbEntry.commentLen > 0) {
119,390,949✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
19,188!
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
238,776,495!
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
119,391,224!
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
14,307,392!
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
28,614,784!
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
28,614,784!
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
28,614,784!
351
      if (pME->ntbEntry.commentLen > 0) {
14,307,392✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
17,700!
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
28,614,784!
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
28,614,784!
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) {
156,855,354✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,512,305!
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
155,393,667✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
23,180,534✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
132,241,900✔
368
        if (pME->colCmpr.nCols != 0) {
13,548,436✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
13,273,948!
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
274,488!
372
          SColCmprWrapper colCmprs = {0};
274,488✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
274,488!
374
          if (code != 0) {
274,488!
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
274,488✔
379
          taosMemoryFree(colCmprs.pColCmpr);
274,488!
380
          TAOS_CHECK_RETURN(code);
274,488!
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
156,885,125!
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
177,641,285✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
46,333,223!
388
  }
389

390
  tEndEncode(pCoder);
177,631,945✔
391
  return 0;
177,568,605✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,301,869,024✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,301,869,024!
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,302,570,214✔
401
    tEndDecode(pCoder);
73,802✔
402
    return 0;
73,802✔
403
  }
404

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

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,302,374,154✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,312,684,226!
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,312,744,721!
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,312,454,974!
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
656,139,289✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
183,960!
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
645,683,121✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
949,892,207!
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
950,041,777!
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
949,829,068!
419
      if (pME->ctbEntry.commentLen > 0) {
474,813,926✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
28,026!
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
949,790,411!
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
475,014,461!
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
171,009,097!
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
342,080,306!
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
342,106,902!
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
342,086,905!
428
      if (pME->ntbEntry.commentLen > 0) {
171,029,577✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
27,612!
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
342,033,038!
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
342,082,496!
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,302,184,613✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
656,329,990!
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
656,311,208!
446

447
        if (pME->colCmpr.nCols == 0) {
656,236,798!
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) {
645,602,326✔
455
      if (!tDecodeIsEnd(pCoder)) {
167,665,329!
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
167,673,801✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
167,673,801!
458
        if (pME->colCmpr.nCols == 0) {
167,666,332!
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);
167,662,300✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
477,939,576✔
467
      if (!tDecodeIsEnd(pCoder)) {
19,483,711✔
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
19,483,178✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
19,484,777!
470
      } else {
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
533!
472
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
533!
473
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
474
        }
475
      }
476
    }
477
    if (!tDecodeIsEnd(pCoder)) {
1,302,201,905✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
656,682,909!
479
    } else {
480
      pME->pExtSchemas = NULL;
645,518,996✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,302,051,068✔
484
    if (!tDecodeIsEnd(pCoder)) {
656,180,604✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,312,433,637!
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,302,072,952✔
491
  return 0;
1,302,152,692✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,302,046,089✔
495

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

501
  pDst->nCols = pSrc->nCols;
288,597,628✔
502
  pDst->version = pSrc->version;
288,592,428✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
288,581,792!
504
  if (pDst->pSchema == NULL) {
288,539,886!
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
288,557,915!
508
  return TSDB_CODE_SUCCESS;
288,596,879✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
27,720✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
288,586,559✔
543
  if (pSchema) {
288,586,559✔
544
    taosMemoryFreeClear(pSchema->pSchema);
288,586,340!
545
  }
546
}
288,590,381✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
165,164,951✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
165,164,951!
563
    return;
91,441✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
165,078,842!
567

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

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
165,065,238✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
139,339,282✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
139,343,808✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
139,343,809✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
41,328✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
25,734,866✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
15,836,820!
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
15,836,820!
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
9,898,937!
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
9,898,937✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
9,898,937!
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
165,078,966✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
165,076,457!
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
165,072,866✔
591

592
  taosMemoryFreeClear(*ppEntry);
165,065,859!
593
  return;
165,068,754✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
165,050,634✔
597
  int32_t code = TSDB_CODE_SUCCESS;
165,050,634✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
165,050,634!
UNCOV
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
165,089,105!
604
  if (NULL == *ppEntry) {
165,054,697!
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
165,059,938✔
609
  (*ppEntry)->type = pEntry->type;
165,071,202✔
610
  (*ppEntry)->uid = pEntry->uid;
165,064,570✔
611

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

616
  if (pEntry->name) {
165,071,950✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
165,085,305✔
618
    if (NULL == (*ppEntry)->name) {
165,083,512!
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
165,073,046✔
626
    (*ppEntry)->flags = pEntry->flags;
139,346,779✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
139,329,622✔
629
    if (code) {
139,347,767!
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
139,347,767✔
635
    if (code) {
139,353,884!
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
139,353,884✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
27,720✔
641
      if (code) {
27,720!
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
139,355,306✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
25,735,694✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
15,836,820✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
15,836,055✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
15,836,820✔
651

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

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

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
9,898,937✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
9,898,937✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
5,562!
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
5,562!
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
5,562!
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) {
165,085,400✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
823,335✔
702
    if (code) {
809,814!
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
164,262,689✔
708
    if (code) {
164,277,679!
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
165,087,493!
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
7,715,101!
715
    if (!(*ppEntry)->pExtSchemas) {
7,709,932!
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
7,710,877!
721
  }
722

723
  return code;
165,076,979✔
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