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

taosdata / TDengine / #4940

27 Jan 2026 10:23AM UTC coverage: 66.832% (-0.1%) from 66.931%
#4940

push

travis-ci

web-flow
fix: asan invalid write issue (#34400)

7 of 8 new or added lines in 2 files covered. (87.5%)

822 existing lines in 141 files now uncovered.

204293 of 305680 relevant lines covered (66.83%)

124534723.83 hits per line

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

81.31
/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) {
1,017,703,707✔
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;
30,641,021✔
26
    }
27
  }
28
  return false;
985,059,371✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
179,564,561✔
32
  if (pME->pExtSchemas) {
179,564,561✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
12,274,756✔
34
    bool                  hasTypeMods = false;
12,274,756✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
12,274,756✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
12,161,470✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
110,898✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
110,898✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
12,274,660✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
926,109,636✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
1,827,811,289✔
46
    }
47
  }
48
  return 0;
179,614,513✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
11,888,898✔
58
    SColRef *p = &pw->pColRef[i];
10,720,246✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
21,440,492✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
21,440,492✔
61
    if (p->hasRef) {
10,720,246✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
11,664,532✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
11,664,532✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
11,664,532✔
65
    }
66
  }
67
  return 0;
1,168,652✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
966,300,883✔
71
  bool hasExtSchema = false;
966,300,883✔
72
  SSchemaWrapper* pSchWrapper = NULL;
966,300,883✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
966,300,883✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
778,716,988✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
187,817,249✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
187,920,010✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
966,826,548✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
967,033,814✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
42,530,817✔
84
    if (pME->pExtSchemas == NULL) {
21,264,788✔
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
1,248,887,192✔
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
2,147,483,647✔
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
945,766,524✔
93
  }
94

95
  return 0;
967,094,022✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
39,253,983✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
39,253,983✔
100
  bool                  hasTypeMods = false;
39,253,983✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
39,253,983✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
30,720,084✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
8,549,219✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
8,554,919✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
39,274,847✔
109

110
  if (hasTypeMods) {
39,253,680✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
38,839✔
112
    if (ret != NULL) {
38,839✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
38,839✔
114
    }
115
    return ret;
38,839✔
116
  }
117
  return NULL;
39,214,841✔
118
}
119

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

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

131
  *rsmaSchema = (SSchemaRsma *)taosMemoryMalloc(sizeof(SSchemaRsma));
26,460✔
132
  if (*rsmaSchema == NULL) {
26,460✔
133
    return terrno;
×
134
  }
135

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
26,460✔
151
  int32_t    i = 0, j = 0;
26,460✔
152
  for (i = 0; i < nCols; ++i) {
203,595✔
153
    while (j < pParam->nFuncs) {
321,930✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
309,435✔
155
        pFuncIds[i] = pParam->funcIds[j];
144,795✔
156
        break;
144,795✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
164,640✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
32,340✔
160
        break;
32,340✔
161
      }
162
      ++j;
132,300✔
163
    }
164
    if (j >= pParam->nFuncs) {
189,630✔
165
      for (; i < nCols; ++i) {
24,990✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
12,495✔
167
      }
168
      break;
12,495✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
26,460✔
172

173
  return 0;
26,460✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
17,879,520✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
17,879,520✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
35,767,395✔
179
  if (pWrapper->nCols == 0) {
17,883,139✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
35,762,067✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
17,884,231✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
35,765,143✔
186
  if (pWrapper->pColRef == NULL) {
17,882,088✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
254,375,753✔
191
    SColRef *p = &pWrapper->pColRef[i];
236,472,481✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
473,029,520✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
473,059,712✔
194
    if (p->hasRef) {
236,540,392✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
140,382,557✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
140,371,434✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
140,374,317✔
198
    }
199
  }
200
  return 0;
17,884,698✔
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) {
623,914✔
220
  if (pSrc->nCols > 0) {
623,914✔
221
    pDst->nCols = pSrc->nCols;
623,914✔
222
    pDst->version = pSrc->version;
623,914✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
623,914✔
224
    if (NULL == pDst->pColRef) {
623,914✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
623,914✔
228
  }
229
  return 0;
623,914✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
39,219,018✔
233
  int32_t code = 0;
39,219,018✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
78,485,463✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
78,573,278✔
236
  uTrace("encode cols:%d", pw->nCols);
39,306,833✔
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;
39,306,052✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
39,047,935✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
39,047,935✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
39,122,062✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
966,451,452✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
966,451,452✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,933,755,884✔
252
  if (pWrapper->nCols == 0) {
966,790,073✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,932,988,809✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
966,683,337✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,839,339,429✔
259
  if (pWrapper->pColCmpr == NULL) {
966,882,607✔
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;
967,634,560✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
207,122✔
273

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

280
  if (pCmpr->pColCmpr == NULL) {
207,496✔
281
    return terrno;
×
282
  }
283

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,057,772✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
850,490✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
850,276✔
287
    pColCmpr->id = pColSchema->colId;
850,276✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
850,276✔
289
  }
290
  return 0;
207,282✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
173,350,084✔
294
  if (pSrc->nCols > 0) {
173,350,084✔
295
    pDst->nCols = pSrc->nCols;
157,306,475✔
296
    pDst->version = pSrc->version;
157,311,015✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
157,306,663✔
298
    if (NULL == pDst->pColCmpr) {
157,284,836✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
157,279,871✔
302
  }
303
  return 0;
173,372,511✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
173,953,125✔
307
  if (pColRef) {
173,953,125✔
308
    taosMemoryFreeClear(pColRef->pColRef);
173,963,187✔
309
  }
310
}
173,976,743✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
173,971,600✔
313
  if (pCmpr) {
173,971,600✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
173,980,634✔
315
  }
316
}
173,989,498✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
183,965,643✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
183,965,643✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
368,001,886✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
368,026,456✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
368,007,126✔
323

324
  if (pME->type > 0) {
183,996,725✔
325
    if (pME->name == NULL) {
179,599,476✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
359,200,116✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
179,596,876✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
49,740,175✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
49,822,580✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
49,814,958✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
24,860,234✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
73,500✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
154,685,125✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
279,308,030✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
279,325,632✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
279,323,544✔
342
      if (pME->ctbEntry.commentLen > 0) {
139,659,783✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
27,584✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
279,300,209✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
139,653,673✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
15,046,714✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
30,093,428✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
30,093,428✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
30,093,428✔
351
      if (pME->ntbEntry.commentLen > 0) {
15,046,714✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
38,512✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
30,093,428✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
30,093,428✔
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) {
179,598,357✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,253,777✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
178,378,389✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
24,917,655✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
153,515,196✔
368
        if (pME->colCmpr.nCols != 0) {
14,414,916✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
14,207,610✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
207,116✔
372
          SColCmprWrapper colCmprs = {0};
207,496✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
207,496✔
374
          if (code != 0) {
207,282✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
207,282✔
379
          taosMemoryFree(colCmprs.pColCmpr);
207,496✔
380
          TAOS_CHECK_RETURN(code);
207,496✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
179,593,912✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
183,986,520✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
49,786,233✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
49,768,352✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
159,059,854✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
28,830,212✔
391
  }
392

393
  tEndEncode(pCoder);
183,958,504✔
394
  return 0;
183,904,446✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,718,838,695✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,718,838,695✔
399
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->version));
2,147,483,647✔
400
  TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->type));
2,147,483,647✔
401
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->uid));
2,147,483,647✔
402

403
  if (headerOnly) {
1,720,613,876✔
404
    tEndDecode(pCoder);
319,936✔
405
    return 0;
319,936✔
406
  }
407

408
  if (pME->type > 0) {
1,720,293,940✔
409
    TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->name));
2,147,483,647✔
410

411
    if (pME->type == TSDB_SUPER_TABLE) {
1,719,522,475✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,558,104,398✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,558,761,327✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,558,887,325✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
779,443,369✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
281,505✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
939,954,242✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,495,823,379✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,496,293,508✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,496,022,881✔
422
      if (pME->ctbEntry.commentLen > 0) {
747,880,888✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
47,780✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,495,776,958✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
748,115,246✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
192,511,574✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
385,180,662✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
385,314,250✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
385,248,527✔
431
      if (pME->ntbEntry.commentLen > 0) {
192,588,807✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
71,120✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
385,119,443✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
385,245,501✔
436
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
437
      pME->smaEntry.tsma = tDecoderMalloc(pCoder, sizeof(STSma));
×
438
      if (!pME->smaEntry.tsma) {
×
439
        return terrno;
×
440
      }
441
      TAOS_CHECK_RETURN(tDecodeTSma(pCoder, pME->smaEntry.tsma, true));
×
442
    } else {
443
      metaError("meta/entry: invalide table type: %" PRId8 " decode failed.", pME->type);
×
444
      return TSDB_CODE_INVALID_PARA;
×
445
    }
446
    if (pME->type == TSDB_SUPER_TABLE) {
1,718,868,301✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
779,244,971✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
779,207,967✔
449

450
        if (pME->colCmpr.nCols == 0) {
779,488,464✔
451
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
452
        }
453
      } else {
454
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
455
        TABLE_SET_COL_COMPRESSED(pME->flags);
×
456
      }
457
    } else if (pME->type == TSDB_NORMAL_TABLE) {
939,831,320✔
458
      if (!tDecodeIsEnd(pCoder)) {
187,990,582✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
187,995,776✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
187,996,493✔
461
        if (pME->colCmpr.nCols == 0) {
188,034,474✔
462
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
463
        }
464
      } else {
465
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
1,737✔
466
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
164✔
467
      }
468
      TABLE_SET_COL_COMPRESSED(pME->flags);
187,984,372✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
751,888,878✔
470
      if (!tDecodeIsEnd(pCoder)) {
17,883,559✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
17,884,298✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
17,884,298✔
473
      } else {
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
475
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
476
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
477
        }
478
      }
479
    }
480
    if (!tDecodeIsEnd(pCoder)) {
1,719,425,395✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
967,093,772✔
482
    } else {
483
      pME->pExtSchemas = NULL;
752,331,623✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
1,718,707,128✔
487
    if (!tDecodeIsEnd(pCoder)) {
778,152,667✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,557,636,739✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
778,606,201✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,557,556,867✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
939,830,911✔
494
    if (!tDecodeIsEnd(pCoder)) {
187,937,355✔
495
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->ntbEntry.ownerId));
375,851,450✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
1,718,749,061✔
501
  return 0;
1,717,862,369✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,718,813,326✔
505

506
static int32_t metaCloneSchema(const SSchemaWrapper *pSrc, SSchemaWrapper *pDst) {
307,068,679✔
507
  if (pSrc == NULL || pDst == NULL) {
307,068,679✔
508
    return TSDB_CODE_INVALID_PARA;
×
509
  }
510

511
  pDst->nCols = pSrc->nCols;
307,098,360✔
512
  pDst->version = pSrc->version;
307,103,264✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
307,110,264✔
514
  if (pDst->pSchema == NULL) {
307,014,346✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
307,032,989✔
518
  return TSDB_CODE_SUCCESS;
307,120,719✔
519
}
520

521
static int32_t metaCloneRsmaParam(const SRSmaParam *pSrc, SRSmaParam *pDst) {
43,365✔
522
  if (pSrc == NULL || pDst == NULL) {
43,365✔
523
    return TSDB_CODE_INVALID_PARA;
×
524
  }
525
  memcpy(pDst, pSrc, sizeof(SRSmaParam));
43,365✔
526
  pDst->name = tstrdup(pSrc->name);
43,365✔
527
  if (pDst->name == NULL) {
43,365✔
528
    return terrno;
×
529
  }
530
  if (pSrc->nFuncs > 0) {
43,365✔
531
    pDst->nFuncs = pSrc->nFuncs;
43,365✔
532
    pDst->funcColIds = (col_id_t *)taosMemoryMalloc(pSrc->nFuncs * sizeof(col_id_t));
43,365✔
533
    if (pDst->funcColIds == NULL) {
43,365✔
534
      return terrno;
×
535
    }
536
    memcpy(pDst->funcColIds, pSrc->funcColIds, pSrc->nFuncs * sizeof(col_id_t));
43,365✔
537

538
    pDst->funcIds = (func_id_t *)taosMemoryMalloc(pSrc->nFuncs * sizeof(func_id_t));
43,365✔
539
    if (pDst->funcIds == NULL) {
43,365✔
540
      return terrno;
×
541
    }
542
    memcpy(pDst->funcIds, pSrc->funcIds, pSrc->nFuncs * sizeof(func_id_t));
43,365✔
543
  } else {
544
    pDst->nFuncs = 0;
×
545
    pDst->funcColIds = NULL;
×
546
    pDst->funcIds = NULL;
×
547
  }
548

549
  return TSDB_CODE_SUCCESS;
43,365✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
307,062,830✔
553
  if (pSchema) {
307,062,830✔
554
    taosMemoryFreeClear(pSchema->pSchema);
307,084,364✔
555
  }
556
}
307,099,728✔
557

558
/**
559
 * @param type 0x01 free name
560
 */
561
void metaFreeRsmaParam(SRSmaParam *pParam, int8_t type) {
64,680✔
562
  if (pParam) {
64,680✔
563
    if ((type & 0x01)) {
64,680✔
564
      taosMemoryFreeClear(pParam->name);
64,680✔
565
    }
566
    taosMemoryFreeClear(pParam->funcColIds);
65,415✔
567
    taosMemoryFreeClear(pParam->funcIds);
64,680✔
568
  }
569
}
64,680✔
570

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
174,058,201✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
174,058,201✔
573
    return;
70,719✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
173,996,908✔
577

578
  if ((*ppEntry)->type < 0) {
173,992,338✔
579
    taosMemoryFreeClear(*ppEntry);
×
580
    return;
×
581
  }
582

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
173,968,295✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
149,380,954✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
149,369,137✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
149,377,192✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
64,680✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
24,618,643✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
16,257,979✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
16,256,978✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,362,293✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,362,293✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,362,293✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
173,994,487✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
173,979,629✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
173,989,275✔
601

602
  taosMemoryFreeClear(*ppEntry);
173,952,436✔
603
  return;
173,968,038✔
604
}
605

606
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
173,977,466✔
607
  int32_t code = TSDB_CODE_SUCCESS;
173,977,466✔
608

609
  if (NULL == pEntry || NULL == ppEntry) {
173,977,466✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
174,011,806✔
614
  if (NULL == *ppEntry) {
173,944,451✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
173,961,995✔
619
  (*ppEntry)->type = pEntry->type;
173,978,494✔
620
  (*ppEntry)->uid = pEntry->uid;
173,995,704✔
621

622
  if (pEntry->type < 0) {
173,978,267✔
623
    return TSDB_CODE_SUCCESS;
×
624
  }
625

626
  if (pEntry->name) {
173,985,115✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
173,977,039✔
628
    if (NULL == (*ppEntry)->name) {
173,991,833✔
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
173,969,493✔
636
    (*ppEntry)->flags = pEntry->flags;
149,340,954✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
149,370,853✔
639
    if (code) {
149,374,424✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
149,374,424✔
645
    if (code) {
149,395,149✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
149,395,149✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
43,365✔
651
      if (code) {
42,630✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
149,395,160✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
149,390,967✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
24,620,900✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
16,258,424✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
16,258,607✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
16,258,607✔
662

663
    // comment
664
    (*ppEntry)->ctbEntry.commentLen = pEntry->ctbEntry.commentLen;
16,258,607✔
665
    if (pEntry->ctbEntry.commentLen > 0) {
16,258,607✔
666
      (*ppEntry)->ctbEntry.comment = taosMemoryMalloc(pEntry->ctbEntry.commentLen + 1);
8,146✔
667
      if (NULL == (*ppEntry)->ctbEntry.comment) {
8,146✔
668
        code = terrno;
×
669
        metaCloneEntryFree(ppEntry);
×
670
        return code;
×
671
      }
672
      memcpy((*ppEntry)->ctbEntry.comment, pEntry->ctbEntry.comment, pEntry->ctbEntry.commentLen + 1);
8,146✔
673
    }
674

675
    // tags
676
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
16,258,607✔
677
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
16,258,607✔
678
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
16,258,607✔
679
      code = terrno;
×
680
      metaCloneEntryFree(ppEntry);
×
681
      return code;
×
682
    }
683
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
16,258,214✔
684
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
8,362,293✔
685
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
8,362,293✔
686
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
8,362,293✔
687
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
8,362,293✔
688
    (*ppEntry)->ntbEntry.ownerId = pEntry->ntbEntry.ownerId;
8,362,293✔
689

690
    // schema
691
    code = metaCloneSchema(&pEntry->ntbEntry.schemaRow, &(*ppEntry)->ntbEntry.schemaRow);
8,362,293✔
692
    if (code) {
8,362,293✔
693
      metaCloneEntryFree(ppEntry);
×
694
      return code;
×
695
    }
696

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,362,293✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,362,293✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
11,930✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
11,930✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
11,930✔
707
    }
708
  } else {
709
    return TSDB_CODE_INVALID_PARA;
×
710
  }
711

712
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
173,998,269✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
638,904✔
714
    if (code) {
623,914✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
173,346,687✔
720
    if (code) {
173,364,790✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
173,988,704✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
9,774,484✔
727
    if (!(*ppEntry)->pExtSchemas) {
9,772,757✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
9,773,121✔
733
  }
734

735
  return code;
173,956,629✔
736
}
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