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

taosdata / TDengine / #4955

09 Feb 2026 01:16AM UTC coverage: 66.884% (+0.008%) from 66.876%
#4955

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

205800 of 307696 relevant lines covered (66.88%)

127581826.31 hits per line

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

81.52
/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,054,197,642✔
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;
27,923,960✔
26
    }
27
  }
28
  return false;
1,021,804,604✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
189,652,224✔
32
  if (pME->pExtSchemas) {
189,652,224✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
12,150,650✔
34
    bool                  hasTypeMods = false;
12,150,650✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
12,150,650✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
12,052,428✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
92,514✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
92,514✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
12,135,519✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,307,412,927✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
189,727,759✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
429,020,140✔
58
    SColRef *p = &pw->pColRef[i];
427,756,744✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
855,513,488✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
855,513,488✔
61
    if (p->hasRef) {
427,756,744✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
567,468,348✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
567,468,348✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
567,468,348✔
65
    }
66
  }
67
  return 0;
1,263,396✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
998,436,498✔
71
  bool hasExtSchema = false;
998,436,498✔
72
  SSchemaWrapper* pSchWrapper = NULL;
998,436,498✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
998,436,498✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
743,988,358✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
254,515,784✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
254,699,297✔
77
  } else {
78
    return 0;
12✔
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
998,590,698✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
998,478,537✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
37,428,394✔
84
    if (pME->pExtSchemas == NULL) {
18,703,291✔
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
1,543,827,575✔
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
2,147,483,647✔
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
979,755,686✔
93
  }
94

95
  return 0;
998,545,263✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
43,780,551✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
43,780,551✔
100
  bool                  hasTypeMods = false;
43,780,551✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
43,780,551✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
32,074,906✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
11,737,724✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
11,752,130✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
43,834,115✔
109

110
  if (hasTypeMods) {
43,765,205✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
39,182✔
112
    if (ret != NULL) {
39,182✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
39,182✔
114
    }
115
    return ret;
39,182✔
116
  }
117
  return NULL;
43,726,023✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
26,280✔
151
  int32_t    i = 0, j = 0;
26,280✔
152
  for (i = 0; i < nCols; ++i) {
202,210✔
153
    while (j < pParam->nFuncs) {
319,740✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
307,330✔
155
        pFuncIds[i] = pParam->funcIds[j];
143,810✔
156
        break;
143,810✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
163,520✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
32,120✔
160
        break;
32,120✔
161
      }
162
      ++j;
131,400✔
163
    }
164
    if (j >= pParam->nFuncs) {
188,340✔
165
      for (; i < nCols; ++i) {
24,820✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
12,410✔
167
      }
168
      break;
12,410✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
26,280✔
172

173
  return 0;
26,280✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
36,358,870✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
36,358,870✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
72,717,632✔
179
  if (pWrapper->nCols == 0) {
36,353,440✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
72,708,705✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
36,356,440✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
72,711,577✔
186
  if (pWrapper->pColRef == NULL) {
36,357,501✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
821,589,165✔
191
    SColRef *p = &pWrapper->pColRef[i];
785,182,206✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
1,570,477,353✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
1,570,497,434✔
194
    if (p->hasRef) {
785,262,712✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
431,200,983✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
431,207,319✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
431,216,879✔
198
    }
199
  }
200
  return 0;
36,372,611✔
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) {
646,660✔
220
  if (pSrc->nCols > 0) {
646,660✔
221
    pDst->nCols = pSrc->nCols;
646,660✔
222
    pDst->version = pSrc->version;
646,660✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
646,660✔
224
    if (NULL == pDst->pColRef) {
646,660✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
646,660✔
228
  }
229
  return 0;
646,660✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
45,421,439✔
233
  int32_t code = 0;
45,421,439✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
90,918,547✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
90,960,077✔
236
  uTrace("encode cols:%d", pw->nCols);
45,462,969✔
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;
45,683,549✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
45,359,992✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
45,359,992✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
45,479,814✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
998,598,762✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
998,598,762✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,997,140,785✔
252
  if (pWrapper->nCols == 0) {
997,736,539✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,996,959,586✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
998,924,571✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,997,377,708✔
259
  if (pWrapper->pColCmpr == NULL) {
997,879,977✔
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;
1,000,136,136✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
227,608✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,147,438✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
919,806✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
919,806✔
287
    pColCmpr->id = pColSchema->colId;
919,806✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
919,806✔
289
  }
290
  return 0;
227,632✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
177,407,398✔
294
  if (pSrc->nCols > 0) {
177,407,398✔
295
    pDst->nCols = pSrc->nCols;
161,163,414✔
296
    pDst->version = pSrc->version;
161,153,232✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
161,119,644✔
298
    if (NULL == pDst->pColCmpr) {
161,096,153✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
161,117,064✔
302
  }
303
  return 0;
177,401,769✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
177,952,755✔
307
  if (pColRef) {
177,952,755✔
308
    taosMemoryFreeClear(pColRef->pColRef);
177,981,706✔
309
  }
310
}
177,996,344✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
177,965,605✔
313
  if (pCmpr) {
177,965,605✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
177,989,930✔
315
  }
316
}
178,023,283✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
194,155,898✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
194,155,898✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
388,448,894✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
388,291,749✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
388,184,226✔
323

324
  if (pME->type > 0) {
194,105,356✔
325
    if (pME->name == NULL) {
189,616,871✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
379,132,146✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
189,509,577✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
50,240,371✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
50,493,032✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
50,525,189✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
25,196,446✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
73,730✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
164,425,822✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
286,709,627✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
286,720,658✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
286,712,520✔
342
      if (pME->ctbEntry.commentLen > 0) {
143,356,080✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
28,076✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
286,671,874✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
143,327,390✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
21,101,564✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
42,203,128✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
42,203,128✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
42,203,128✔
351
      if (pME->ntbEntry.commentLen > 0) {
21,101,564✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
38,700✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
42,203,128✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
42,203,128✔
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) {
189,650,505✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,443,820✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
188,299,987✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
25,193,414✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
163,116,942✔
368
        if (pME->colCmpr.nCols != 0) {
20,427,240✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
20,199,608✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
227,632✔
372
          SColCmprWrapper colCmprs = {0};
227,632✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
227,632✔
374
          if (code != 0) {
227,632✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
227,632✔
379
          taosMemoryFree(colCmprs.pColCmpr);
227,632✔
380
          TAOS_CHECK_RETURN(code);
227,632✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
189,660,679✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
194,134,608✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
50,199,349✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
49,965,475✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
168,921,991✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
40,854,480✔
391
  }
392

393
  tEndEncode(pCoder);
193,937,998✔
394
  return 0;
193,958,848✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,808,268,529✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,808,268,529✔
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,810,429,348✔
404
    tEndDecode(pCoder);
325,960✔
405
    return 0;
325,960✔
406
  }
407

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

411
    if (pME->type == TSDB_SUPER_TABLE) {
1,808,696,168✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,488,363,781✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,489,495,241✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,489,484,487✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
744,510,214✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
278,860✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
1,063,791,833✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,604,938,453✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,605,469,854✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,604,929,427✔
422
      if (pME->ctbEntry.commentLen > 0) {
802,196,397✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
48,572✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,604,715,601✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
802,646,250✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
262,152,040✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
524,927,507✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
525,107,978✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
524,879,685✔
431
      if (pME->ntbEntry.commentLen > 0) {
262,320,909✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
71,486✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
524,526,207✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
524,839,779✔
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,808,500,391✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
744,919,211✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
744,724,585✔
449

450
        if (pME->colCmpr.nCols == 0) {
745,057,950✔
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,063,744,786✔
458
      if (!tDecodeIsEnd(pCoder)) {
254,880,366✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
254,895,657✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
254,895,867✔
461
        if (pME->colCmpr.nCols == 0) {
254,969,217✔
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);
×
466
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
467
      }
468
      TABLE_SET_COL_COMPRESSED(pME->flags);
254,812,635✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
809,022,358✔
470
      if (!tDecodeIsEnd(pCoder)) {
36,362,224✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
36,361,132✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
36,361,132✔
473
      } else {
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
1,098✔
475
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
1,098✔
476
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
477
        }
478
      }
479
    }
480
    if (!tDecodeIsEnd(pCoder)) {
1,808,766,274✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
999,112,982✔
482
    } else {
483
      pME->pExtSchemas = NULL;
809,653,292✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
1,808,001,303✔
487
    if (!tDecodeIsEnd(pCoder)) {
742,919,199✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,487,644,514✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
743,574,434✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,487,559,319✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,063,633,000✔
494
    if (!tDecodeIsEnd(pCoder)) {
254,689,523✔
495
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.ownerId));
509,516,978✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
1,807,980,178✔
501
  return 0;
1,807,545,196✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,808,279,009✔
505

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

511
  pDst->nCols = pSrc->nCols;
314,612,452✔
512
  pDst->version = pSrc->version;
314,595,172✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
314,614,939✔
514
  if (pDst->pSchema == NULL) {
314,407,906✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
314,448,418✔
518
  return TSDB_CODE_SUCCESS;
314,634,132✔
519
}
520

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

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

549
  return TSDB_CODE_SUCCESS;
40,880✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
314,529,870✔
553
  if (pSchema) {
314,529,870✔
554
    taosMemoryFreeClear(pSchema->pSchema);
314,562,524✔
555
  }
556
}
314,539,520✔
557

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

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
178,068,370✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
178,068,370✔
573
    return;
72,569✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
178,022,486✔
577

578
  if ((*ppEntry)->type < 0) {
178,029,725✔
579
    taosMemoryFreeClear(*ppEntry);
×
580
    return;
×
581
  }
582

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
177,962,810✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
153,000,877✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
152,994,456✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
153,000,457✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
63,510✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
25,016,323✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
16,465,866✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
16,464,356✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,555,114✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,555,114✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,555,114✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
178,027,085✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
178,007,156✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
178,019,947✔
601

602
  taosMemoryFreeClear(*ppEntry);
177,959,500✔
603
  return;
177,976,233✔
604
}
605

606
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
178,008,021✔
607
  int32_t code = TSDB_CODE_SUCCESS;
178,008,021✔
608

609
  if (NULL == pEntry || NULL == ppEntry) {
178,008,021✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
178,069,133✔
614
  if (NULL == *ppEntry) {
177,926,805✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
177,966,901✔
619
  (*ppEntry)->type = pEntry->type;
177,996,668✔
620
  (*ppEntry)->uid = pEntry->uid;
177,999,851✔
621

622
  if (pEntry->type < 0) {
178,025,950✔
623
    return TSDB_CODE_SUCCESS;
×
624
  }
625

626
  if (pEntry->name) {
177,999,364✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
178,017,114✔
628
    if (NULL == (*ppEntry)->name) {
178,045,466✔
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
178,013,038✔
636
    (*ppEntry)->flags = pEntry->flags;
152,955,627✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
153,018,014✔
639
    if (code) {
153,031,904✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
153,031,904✔
645
    if (code) {
153,074,588✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
153,074,588✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
43,070✔
651
      if (code) {
40,880✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
153,074,367✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
153,051,071✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
25,020,415✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
16,465,301✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
16,466,531✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
16,465,899✔
662

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

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

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

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,555,114✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,555,114✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
11,993✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
11,993✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
11,993✔
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) {
178,079,384✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
650,978✔
714
    if (code) {
646,660✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
177,366,052✔
720
    if (code) {
177,392,545✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
178,039,205✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
9,633,812✔
727
    if (!(*ppEntry)->pExtSchemas) {
9,623,557✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
9,621,176✔
733
  }
734

735
  return code;
177,999,858✔
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