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

taosdata / TDengine / #4939

26 Jan 2026 07:26AM UTC coverage: 66.931% (+0.1%) from 66.8%
#4939

push

travis-ci

web-flow
fix: interp func order (#34402)

* fix: interp func order

* fix: iterp sort

204592 of 305676 relevant lines covered (66.93%)

126181350.71 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,063,783,689✔
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;
29,192,869✔
26
    }
27
  }
28
  return false;
1,031,732,923✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
180,344,312✔
32
  if (pME->pExtSchemas) {
180,344,312✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
11,650,792✔
34
    bool                  hasTypeMods = false;
11,650,792✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
11,650,792✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
11,543,748✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
108,040✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
108,040✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
11,642,520✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,233,483,617✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
180,367,783✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
347,243,490✔
58
    SColRef *p = &pw->pColRef[i];
346,027,052✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
692,054,104✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
692,054,104✔
61
    if (p->hasRef) {
346,027,052✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
413,968,364✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
413,968,364✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
413,968,364✔
65
    }
66
  }
67
  return 0;
1,216,438✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
1,009,789,014✔
71
  bool hasExtSchema = false;
1,009,789,014✔
72
  SSchemaWrapper* pSchWrapper = NULL;
1,009,789,014✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
1,009,789,014✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
763,029,724✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
246,888,562✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
247,024,573✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
1,010,186,569✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
1,010,335,802✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
40,900,850✔
84
    if (pME->pExtSchemas == NULL) {
20,450,234✔
85
      return terrno;
×
86
    }
87

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

95
  return 0;
1,010,303,198✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
42,367,720✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
42,367,720✔
100
  bool                  hasTypeMods = false;
42,367,720✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
42,367,720✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
31,016,604✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
11,387,217✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
11,401,222✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
42,406,804✔
109

110
  if (hasTypeMods) {
42,363,886✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
38,060✔
112
    if (ret != NULL) {
38,060✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
38,060✔
114
    }
115
    return ret;
38,060✔
116
  }
117
  return NULL;
42,325,826✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
25,668✔
151
  int32_t    i = 0, j = 0;
25,668✔
152
  for (i = 0; i < nCols; ++i) {
197,501✔
153
    while (j < pParam->nFuncs) {
312,294✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
300,173✔
155
        pFuncIds[i] = pParam->funcIds[j];
140,461✔
156
        break;
140,461✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
159,712✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
31,372✔
160
        break;
31,372✔
161
      }
162
      ++j;
128,340✔
163
    }
164
    if (j >= pParam->nFuncs) {
183,954✔
165
      for (; i < nCols; ++i) {
24,242✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
12,121✔
167
      }
168
      break;
12,121✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
25,668✔
172

173
  return 0;
25,668✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
35,352,591✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
35,352,591✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
70,702,862✔
179
  if (pWrapper->nCols == 0) {
35,349,826✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
70,702,453✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
35,353,863✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
70,701,116✔
186
  if (pWrapper->pColRef == NULL) {
35,349,910✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
798,326,337✔
191
    SColRef *p = &pWrapper->pColRef[i];
762,949,013✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
1,525,992,449✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
1,526,044,422✔
194
    if (p->hasRef) {
763,044,475✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
420,613,129✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
420,605,833✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
420,602,080✔
198
    }
199
  }
200
  return 0;
35,355,956✔
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) {
638,184✔
220
  if (pSrc->nCols > 0) {
638,184✔
221
    pDst->nCols = pSrc->nCols;
638,184✔
222
    pDst->version = pSrc->version;
638,184✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
638,184✔
224
    if (NULL == pDst->pColRef) {
638,184✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
638,184✔
228
  }
229
  return 0;
638,184✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
41,250,588✔
233
  int32_t code = 0;
41,250,588✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
82,509,767✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
82,546,050✔
236
  uTrace("encode cols:%d", pw->nCols);
41,286,871✔
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;
41,334,524✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
41,091,978✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
41,091,978✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
41,140,793✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
1,009,972,834✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
1,009,972,834✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
2,020,346,548✔
252
  if (pWrapper->nCols == 0) {
1,009,936,748✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
2,019,888,331✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
1,010,029,030✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,943,023,410✔
259
  if (pWrapper->pColCmpr == NULL) {
1,010,273,540✔
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,010,918,052✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
204,792✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,047,800✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
842,634✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
842,634✔
287
    pColCmpr->id = pColSchema->colId;
842,634✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
842,634✔
289
  }
290
  return 0;
205,166✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
171,589,995✔
294
  if (pSrc->nCols > 0) {
171,589,995✔
295
    pDst->nCols = pSrc->nCols;
155,493,344✔
296
    pDst->version = pSrc->version;
155,493,500✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
155,482,629✔
298
    if (NULL == pDst->pColCmpr) {
155,466,103✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
155,448,070✔
302
  }
303
  return 0;
171,581,236✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
172,207,370✔
307
  if (pColRef) {
172,207,370✔
308
    taosMemoryFreeClear(pColRef->pColRef);
172,221,125✔
309
  }
310
}
172,223,408✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
172,199,819✔
313
  if (pCmpr) {
172,199,819✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
172,219,688✔
315
  }
316
}
172,206,219✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
184,771,766✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
184,771,766✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
369,544,147✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
369,520,107✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
369,543,970✔
323

324
  if (pME->type > 0) {
184,771,348✔
325
    if (pME->name == NULL) {
180,346,098✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
360,739,927✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
180,386,579✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
47,519,492✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
47,583,853✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
47,585,957✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
23,754,683✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
72,013✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
156,561,190✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
276,770,475✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
276,791,213✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
276,806,490✔
342
      if (pME->ctbEntry.commentLen > 0) {
138,401,650✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
27,528✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
276,769,502✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
138,377,859✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
18,191,422✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
36,382,132✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
36,381,420✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
36,381,420✔
351
      if (pME->ntbEntry.commentLen > 0) {
18,190,710✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
37,848✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
36,381,420✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
36,382,132✔
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) {
180,338,620✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,331,135✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
179,049,123✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
23,783,672✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
155,390,972✔
368
        if (pME->colCmpr.nCols != 0) {
17,538,634✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
17,333,468✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
205,166✔
372
          SColCmprWrapper colCmprs = {0};
205,166✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
205,166✔
374
          if (code != 0) {
205,166✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
205,166✔
379
          taosMemoryFree(colCmprs.pColCmpr);
205,166✔
380
          TAOS_CHECK_RETURN(code);
205,166✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
180,377,769✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
184,774,929✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
47,519,031✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
47,483,218✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
160,967,195✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
35,077,268✔
391
  }
392

393
  tEndEncode(pCoder);
184,728,227✔
394
  return 0;
184,760,904✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,799,195,195✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,799,195,195✔
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,800,600,819✔
404
    tEndDecode(pCoder);
314,900✔
405
    return 0;
314,900✔
406
  }
407

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

411
    if (pME->type == TSDB_SUPER_TABLE) {
1,799,821,002✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,526,797,131✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,527,067,814✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,527,133,719✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
763,481,294✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
273,079✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
1,036,055,460✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,564,113,331✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,564,246,383✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,564,105,022✔
422
      if (pME->ctbEntry.commentLen > 0) {
781,988,607✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
47,664✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,563,995,062✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
782,123,215✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
254,356,292✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
509,001,490✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
509,137,326✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
509,015,962✔
431
      if (pME->ntbEntry.commentLen > 0) {
254,439,342✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
69,984✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
508,818,823✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
508,899,378✔
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,799,463,872✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
763,377,115✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
763,386,455✔
449

450
        if (pME->colCmpr.nCols == 0) {
763,561,525✔
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,035,953,671✔
458
      if (!tDecodeIsEnd(pCoder)) {
247,180,989✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
247,205,184✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
247,205,387✔
461
        if (pME->colCmpr.nCols == 0) {
247,081,942✔
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);
3,200✔
466
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
467
      }
468
      TABLE_SET_COL_COMPRESSED(pME->flags);
247,128,566✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
788,952,186✔
470
      if (!tDecodeIsEnd(pCoder)) {
35,352,049✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
35,353,045✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
35,353,045✔
473
      } else {
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
475
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
1✔
476
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
477
        }
478
      }
479
    }
480
    if (!tDecodeIsEnd(pCoder)) {
1,799,787,971✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
1,010,530,912✔
482
    } else {
483
      pME->pExtSchemas = NULL;
789,257,059✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
1,799,300,494✔
487
    if (!tDecodeIsEnd(pCoder)) {
762,954,268✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,526,369,254✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
762,904,867✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,526,140,850✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,035,992,811✔
494
    if (!tDecodeIsEnd(pCoder)) {
247,100,286✔
495
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->ntbEntry.ownerId));
494,131,872✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
1,799,205,376✔
501
  return 0;
1,799,250,538✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,799,415,601✔
505

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

511
  pDst->nCols = pSrc->nCols;
303,414,806✔
512
  pDst->version = pSrc->version;
303,360,776✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
303,413,662✔
514
  if (pDst->pSchema == NULL) {
303,294,219✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
303,340,685✔
518
  return TSDB_CODE_SUCCESS;
303,420,531✔
519
}
520

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

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

549
  return TSDB_CODE_SUCCESS;
42,067✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
303,363,076✔
553
  if (pSchema) {
303,363,076✔
554
    taosMemoryFreeClear(pSchema->pSchema);
303,382,541✔
555
  }
556
}
303,368,408✔
557

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

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
172,277,302✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
172,277,302✔
573
    return;
71,539✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
172,216,620✔
577

578
  if ((*ppEntry)->type < 0) {
172,235,824✔
579
    taosMemoryFreeClear(*ppEntry);
×
580
    return;
×
581
  }
582

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
172,174,966✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
147,488,096✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
147,468,795✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
147,473,184✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
63,457✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
24,723,419✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
16,312,730✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
16,312,730✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,411,112✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,411,112✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,411,112✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
172,189,873✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
172,186,885✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
172,207,665✔
601

602
  taosMemoryFreeClear(*ppEntry);
172,169,011✔
603
  return;
172,206,557✔
604
}
605

606
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
172,192,686✔
607
  int32_t code = TSDB_CODE_SUCCESS;
172,192,686✔
608

609
  if (NULL == pEntry || NULL == ppEntry) {
172,192,686✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
172,229,737✔
614
  if (NULL == *ppEntry) {
172,169,221✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
172,182,414✔
619
  (*ppEntry)->type = pEntry->type;
172,186,211✔
620
  (*ppEntry)->uid = pEntry->uid;
172,210,539✔
621

622
  if (pEntry->type < 0) {
172,219,552✔
623
    return TSDB_CODE_SUCCESS;
×
624
  }
625

626
  if (pEntry->name) {
172,213,214✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
172,213,834✔
628
    if (NULL == (*ppEntry)->name) {
172,230,739✔
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
172,226,612✔
636
    (*ppEntry)->flags = pEntry->flags;
147,469,792✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
147,496,662✔
639
    if (code) {
147,506,169✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
147,506,169✔
645
    if (code) {
147,518,596✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
147,518,596✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
42,067✔
651
      if (code) {
42,067✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
147,518,278✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
147,512,502✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
24,723,555✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
16,312,443✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
16,312,220✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
16,312,037✔
662

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

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

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

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,411,112✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,411,112✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
11,730✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
11,730✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
11,730✔
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) {
172,245,564✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
645,578✔
714
    if (code) {
638,184✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
171,570,640✔
720
    if (code) {
171,571,993✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
172,210,177✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
9,157,402✔
727
    if (!(*ppEntry)->pExtSchemas) {
9,152,113✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
9,154,902✔
733
  }
734

735
  return code;
172,212,856✔
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