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

taosdata / TDengine / #4963

09 Feb 2026 01:16AM UTC coverage: 66.867% (-0.005%) from 66.872%
#4963

push

travis-ci

web-flow
docs: add support for recording STMT to CSV files (#34276)

* docs: add support for recording STMT to CSV files

* docs: update version for STMT recording feature in CSV files

205748 of 307696 relevant lines covered (66.87%)

127038261.06 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,055,370,227✔
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,847,840✔
26
    }
27
  }
28
  return false;
1,020,432,144✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
192,653,379✔
32
  if (pME->pExtSchemas) {
192,653,379✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
12,152,099✔
34
    bool                  hasTypeMods = false;
12,152,099✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
12,152,099✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
12,043,128✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
115,428✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
115,428✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
12,154,581✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,344,015,104✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
192,687,622✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
427,516,464✔
58
    SColRef *p = &pw->pColRef[i];
426,253,684✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
852,507,368✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
852,507,368✔
61
    if (p->hasRef) {
426,253,684✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
565,460,872✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
565,460,872✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
565,460,872✔
65
    }
66
  }
67
  return 0;
1,262,780✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
998,262,952✔
71
  bool hasExtSchema = false;
998,262,952✔
72
  SSchemaWrapper* pSchWrapper = NULL;
998,262,952✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
998,262,952✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
753,464,933✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
244,831,207✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
244,900,800✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
998,303,665✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
998,297,439✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
43,368,163✔
84
    if (pME->pExtSchemas == NULL) {
21,677,917✔
85
      return terrno;
×
86
    }
87

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

95
  return 0;
998,294,260✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
45,026,649✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
45,026,649✔
100
  bool                  hasTypeMods = false;
45,026,649✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
45,026,649✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
32,869,201✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
12,166,734✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
12,172,520✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
45,044,526✔
109

110
  if (hasTypeMods) {
45,018,520✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
40,397✔
112
    if (ret != NULL) {
40,397✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
40,397✔
114
    }
115
    return ret;
40,397✔
116
  }
117
  return NULL;
44,978,123✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
26,928✔
151
  int32_t    i = 0, j = 0;
26,928✔
152
  for (i = 0; i < nCols; ++i) {
207,196✔
153
    while (j < pParam->nFuncs) {
327,624✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
314,908✔
155
        pFuncIds[i] = pParam->funcIds[j];
147,356✔
156
        break;
147,356✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
167,552✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
32,912✔
160
        break;
32,912✔
161
      }
162
      ++j;
134,640✔
163
    }
164
    if (j >= pParam->nFuncs) {
192,984✔
165
      for (; i < nCols; ++i) {
25,432✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
12,716✔
167
      }
168
      break;
12,716✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
26,928✔
172

173
  return 0;
26,928✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
33,596,361✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
33,596,361✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
67,195,470✔
179
  if (pWrapper->nCols == 0) {
33,596,553✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
67,195,208✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
33,598,981✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
67,193,026✔
186
  if (pWrapper->pColRef == NULL) {
33,593,533✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
764,862,918✔
191
    SColRef *p = &pWrapper->pColRef[i];
731,244,980✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
1,462,547,587✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
1,462,586,453✔
194
    if (p->hasRef) {
731,319,692✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
405,128,771✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
405,121,181✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
405,130,908✔
198
    }
199
  }
200
  return 0;
33,601,161✔
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) {
636,975✔
220
  if (pSrc->nCols > 0) {
636,975✔
221
    pDst->nCols = pSrc->nCols;
636,975✔
222
    pDst->version = pSrc->version;
636,975✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
636,975✔
224
    if (NULL == pDst->pColRef) {
636,975✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
636,975✔
228
  }
229
  return 0;
636,975✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
46,017,883✔
233
  int32_t code = 0;
46,017,883✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
92,048,600✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
92,065,400✔
236
  uTrace("encode cols:%d", pw->nCols);
46,034,683✔
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;
46,159,904✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
45,863,796✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
45,863,796✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
45,925,744✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
998,332,487✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
998,332,487✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,996,637,615✔
252
  if (pWrapper->nCols == 0) {
998,061,447✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,996,545,326✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
998,408,049✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,996,737,604✔
259
  if (pWrapper->pColCmpr == NULL) {
998,163,991✔
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;
998,814,065✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
235,404✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,179,208✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
943,804✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
943,804✔
287
    pColCmpr->id = pColSchema->colId;
943,804✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
943,804✔
289
  }
290
  return 0;
235,404✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
180,329,590✔
294
  if (pSrc->nCols > 0) {
180,329,590✔
295
    pDst->nCols = pSrc->nCols;
163,726,896✔
296
    pDst->version = pSrc->version;
163,717,945✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
163,695,237✔
298
    if (NULL == pDst->pColCmpr) {
163,692,483✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
163,690,163✔
302
  }
303
  return 0;
180,322,201✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
180,882,231✔
307
  if (pColRef) {
180,882,231✔
308
    taosMemoryFreeClear(pColRef->pColRef);
180,896,586✔
309
  }
310
}
180,913,333✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
180,911,166✔
313
  if (pCmpr) {
180,911,166✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
180,924,810✔
315
  }
316
}
180,938,922✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
197,321,620✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
197,321,620✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
394,725,013✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
394,621,567✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
394,539,625✔
323

324
  if (pME->type > 0) {
197,271,739✔
325
    if (pME->name == NULL) {
192,631,726✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
385,185,248✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
192,554,271✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
50,691,312✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
50,825,923✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
50,864,555✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
25,401,990✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
77,044✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
167,227,713✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
291,718,363✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
291,714,760✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
291,694,827✔
342
      if (pME->ctbEntry.commentLen > 0) {
145,847,099✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
28,820✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
291,673,441✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
145,829,972✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
21,395,388✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
42,790,776✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
42,790,776✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
42,790,776✔
351
      if (pME->ntbEntry.commentLen > 0) {
21,395,388✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
39,812✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
42,790,776✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
42,790,776✔
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) {
192,635,898✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,356,809✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
191,329,157✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
25,380,080✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
165,951,628✔
368
        if (pME->colCmpr.nCols != 0) {
20,733,534✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
20,498,130✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
235,404✔
372
          SColCmprWrapper colCmprs = {0};
235,404✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
235,404✔
374
          if (code != 0) {
235,404✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
235,404✔
379
          taosMemoryFree(colCmprs.pColCmpr);
235,404✔
380
          TAOS_CHECK_RETURN(code);
235,404✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
192,646,385✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
197,310,601✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
50,681,180✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
50,504,791✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
171,883,001✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
41,467,068✔
391
  }
392

393
  tEndEncode(pCoder);
197,159,214✔
394
  return 0;
197,200,942✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,794,363,866✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,794,363,866✔
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,794,992,085✔
404
    tEndDecode(pCoder);
374,450✔
405
    return 0;
374,450✔
406
  }
407

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

411
    if (pME->type == TSDB_SUPER_TABLE) {
1,794,134,379✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,507,080,459✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,507,343,752✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,507,279,588✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
753,579,297✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
286,484✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
1,040,452,428✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,577,508,697✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,577,607,597✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,577,426,563✔
422
      if (pME->ctbEntry.commentLen > 0) {
788,622,171✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
49,992✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,577,377,578✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
788,776,437✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
251,920,088✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
504,083,835✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
504,183,832✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
504,080,311✔
431
      if (pME->ntbEntry.commentLen > 0) {
251,986,757✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
73,698✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
503,967,530✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
504,096,894✔
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,794,075,145✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
753,718,936✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
753,629,387✔
449

450
        if (pME->colCmpr.nCols == 0) {
753,754,542✔
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) {
1,040,381,255✔
458
      if (!tDecodeIsEnd(pCoder)) {
244,983,529✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
244,991,684✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
244,991,906✔
461
        if (pME->colCmpr.nCols == 0) {
245,004,440✔
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);
15✔
466
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
15✔
467
      }
468
      TABLE_SET_COL_COMPRESSED(pME->flags);
244,953,162✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
795,482,764✔
470
      if (!tDecodeIsEnd(pCoder)) {
33,597,645✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
33,597,957✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
33,597,957✔
473
      } else {
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
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,794,147,281✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
998,486,608✔
482
    } else {
483
      pME->pExtSchemas = NULL;
795,660,673✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
1,793,946,331✔
487
    if (!tDecodeIsEnd(pCoder)) {
753,133,305✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,506,819,121✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
753,325,955✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,506,745,087✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,040,340,695✔
494
    if (!tDecodeIsEnd(pCoder)) {
244,918,621✔
495
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.ownerId));
489,885,648✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
1,793,965,776✔
501
  return 0;
1,793,830,950✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,794,037,548✔
505

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

511
  pDst->nCols = pSrc->nCols;
319,578,303✔
512
  pDst->version = pSrc->version;
319,564,464✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
319,580,951✔
514
  if (pDst->pSchema == NULL) {
319,457,977✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
319,495,832✔
518
  return TSDB_CODE_SUCCESS;
319,600,808✔
519
}
520

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

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

549
  return TSDB_CODE_SUCCESS;
44,132✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
319,516,505✔
553
  if (pSchema) {
319,516,505✔
554
    taosMemoryFreeClear(pSchema->pSchema);
319,544,208✔
555
  }
556
}
319,537,265✔
557

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

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
180,989,982✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
180,989,982✔
573
    return;
70,157✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
180,935,705✔
577

578
  if ((*ppEntry)->type < 0) {
180,939,782✔
579
    taosMemoryFreeClear(*ppEntry);
×
580
    return;
×
581
  }
582

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
180,887,836✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
155,434,734✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
155,415,608✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
155,423,092✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
66,572✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
25,503,721✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
16,817,559✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
16,816,231✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,687,490✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,687,490✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,687,490✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
180,936,137✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
180,915,457✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
180,908,234✔
601

602
  taosMemoryFreeClear(*ppEntry);
180,884,053✔
603
  return;
180,899,050✔
604
}
605

606
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
180,936,365✔
607
  int32_t code = TSDB_CODE_SUCCESS;
180,936,365✔
608

609
  if (NULL == pEntry || NULL == ppEntry) {
180,936,365✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
180,971,231✔
614
  if (NULL == *ppEntry) {
180,884,794✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
180,923,507✔
619
  (*ppEntry)->type = pEntry->type;
180,923,819✔
620
  (*ppEntry)->uid = pEntry->uid;
180,916,947✔
621

622
  if (pEntry->type < 0) {
180,935,234✔
623
    return TSDB_CODE_SUCCESS;
×
624
  }
625

626
  if (pEntry->name) {
180,923,894✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
180,924,685✔
628
    if (NULL == (*ppEntry)->name) {
180,952,170✔
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
180,908,822✔
636
    (*ppEntry)->flags = pEntry->flags;
155,392,131✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
155,441,492✔
639
    if (code) {
155,447,476✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
155,447,476✔
645
    if (code) {
155,473,178✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
155,473,178✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
44,132✔
651
      if (code) {
44,132✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
155,474,121✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
155,451,681✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
25,505,049✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
16,817,559✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
16,817,559✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
16,817,559✔
662

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

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

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

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,687,490✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,687,490✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
12,337✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
12,337✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
12,337✔
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) {
180,966,221✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
638,686✔
714
    if (code) {
636,975✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
180,292,289✔
720
    if (code) {
180,312,689✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
180,949,664✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
9,617,611✔
727
    if (!(*ppEntry)->pExtSchemas) {
9,605,632✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
9,608,575✔
733
  }
734

735
  return code;
180,925,613✔
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