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

taosdata / TDengine / #4946

02 Feb 2026 09:27AM UTC coverage: 66.932% (+0.06%) from 66.87%
#4946

push

travis-ci

web-flow
enh: [6690002267] Optimize virtual table query with plenty of columns. (#34341)

527 of 634 new or added lines in 23 files covered. (83.12%)

413 existing lines in 119 files now uncovered.

205724 of 307364 relevant lines covered (66.93%)

126528878.24 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,013,240,002✔
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,161,228✔
26
    }
27
  }
28
  return false;
984,412,281✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
185,183,024✔
32
  if (pME->pExtSchemas) {
185,183,024✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
11,711,829✔
34
    bool                  hasTypeMods = false;
11,711,829✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
11,711,829✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
11,602,497✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
108,216✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
108,216✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
11,711,484✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,223,459,486✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
185,212,977✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
417,784,708✔
58
    SColRef *p = &pw->pColRef[i];
416,537,184✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
833,074,368✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
833,074,368✔
61
    if (p->hasRef) {
416,537,184✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
552,530,464✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
552,530,464✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
552,530,464✔
65
    }
66
  }
67
  return 0;
1,247,524✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
959,593,614✔
71
  bool hasExtSchema = false;
959,593,614✔
72
  SSchemaWrapper* pSchWrapper = NULL;
959,593,614✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
959,593,614✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
713,151,360✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
246,636,375✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
246,742,990✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
959,932,719✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
960,026,637✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
40,729,370✔
84
    if (pME->pExtSchemas == NULL) {
20,360,209✔
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
1,483,505,641✔
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
2,147,483,647✔
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
939,658,183✔
93
  }
94

95
  return 0;
959,978,570✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
41,966,807✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
41,966,807✔
100
  bool                  hasTypeMods = false;
41,966,807✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
41,966,807✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
30,606,488✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
11,389,340✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
11,392,190✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
42,005,343✔
109

110
  if (hasTypeMods) {
41,938,850✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
38,974✔
112
    if (ret != NULL) {
38,974✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
38,974✔
114
    }
115
    return ret;
38,974✔
116
  }
117
  return NULL;
41,899,876✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
25,848✔
151
  int32_t    i = 0, j = 0;
25,848✔
152
  for (i = 0; i < nCols; ++i) {
198,886✔
153
    while (j < pParam->nFuncs) {
314,484✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
302,278✔
155
        pFuncIds[i] = pParam->funcIds[j];
141,446✔
156
        break;
141,446✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
160,832✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
31,592✔
160
        break;
31,592✔
161
      }
162
      ++j;
129,240✔
163
    }
164
    if (j >= pParam->nFuncs) {
185,244✔
165
      for (; i < nCols; ++i) {
24,412✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
12,206✔
167
      }
168
      break;
12,206✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
25,848✔
172

173
  return 0;
25,848✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
35,431,883✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
35,431,883✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
70,872,308✔
179
  if (pWrapper->nCols == 0) {
35,436,194✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
70,874,999✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
35,437,500✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
70,872,765✔
186
  if (pWrapper->pColRef == NULL) {
35,437,267✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
802,713,510✔
191
    SColRef *p = &pWrapper->pColRef[i];
767,240,347✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
1,534,571,534✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
1,534,615,844✔
194
    if (p->hasRef) {
767,315,848✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
422,809,225✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
422,809,126✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
422,812,950✔
198
    }
199
  }
200
  return 0;
35,444,414✔
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) {
644,540✔
220
  if (pSrc->nCols > 0) {
644,540✔
221
    pDst->nCols = pSrc->nCols;
644,540✔
222
    pDst->version = pSrc->version;
644,540✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
644,540✔
224
    if (NULL == pDst->pColRef) {
644,540✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
644,540✔
228
  }
229
  return 0;
644,540✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
44,166,122✔
233
  int32_t code = 0;
44,166,122✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
88,301,068✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
88,298,426✔
236
  uTrace("encode cols:%d", pw->nCols);
44,163,480✔
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;
44,252,848✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
43,980,672✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
43,980,672✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
44,047,255✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
959,658,935✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
959,658,935✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,919,853,076✔
252
  if (pWrapper->nCols == 0) {
959,819,991✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,919,326,222✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
959,733,036✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,919,589,520✔
259
  if (pWrapper->pColCmpr == NULL) {
959,911,923✔
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;
960,330,146✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
212,968✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,081,206✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
868,238✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
868,238✔
287
    pColCmpr->id = pColSchema->colId;
868,238✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
868,238✔
289
  }
290
  return 0;
212,968✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
173,757,399✔
294
  if (pSrc->nCols > 0) {
173,757,399✔
295
    pDst->nCols = pSrc->nCols;
157,550,017✔
296
    pDst->version = pSrc->version;
157,544,650✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
157,521,597✔
298
    if (NULL == pDst->pColCmpr) {
157,512,421✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
157,512,856✔
302
  }
303
  return 0;
173,753,838✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
174,391,113✔
307
  if (pColRef) {
174,391,113✔
308
    taosMemoryFreeClear(pColRef->pColRef);
174,403,097✔
309
  }
310
}
174,401,491✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
174,385,583✔
313
  if (pCmpr) {
174,385,583✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
174,400,053✔
315
  }
316
}
174,401,582✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
189,667,755✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
189,667,755✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
379,321,457✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
379,318,725✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
379,346,526✔
323

324
  if (pME->type > 0) {
189,663,381✔
325
    if (pME->name == NULL) {
185,196,242✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
370,412,505✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
185,218,671✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
48,416,309✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
48,470,398✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
48,468,768✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
24,198,164✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
73,954✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
160,966,272✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
280,603,578✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
280,617,729✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
280,635,949✔
342
      if (pME->ctbEntry.commentLen > 0) {
140,319,912✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
27,836✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
280,618,018✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
140,307,178✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
20,673,038✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
41,345,673✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
41,345,673✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
41,345,874✔
351
      if (pME->ntbEntry.commentLen > 0) {
20,672,836✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
38,764✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
41,345,260✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
41,345,663✔
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) {
185,189,815✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,309,373✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
183,895,883✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
24,217,952✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
159,759,308✔
368
        if (pME->colCmpr.nCols != 0) {
20,005,268✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
19,792,300✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
212,968✔
372
          SColCmprWrapper colCmprs = {0};
212,757✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
212,968✔
374
          if (code != 0) {
212,968✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
212,968✔
379
          taosMemoryFree(colCmprs.pColCmpr);
212,968✔
380
          TAOS_CHECK_RETURN(code);
212,968✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
185,230,660✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
189,660,955✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
48,426,246✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
48,383,211✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
165,428,944✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
40,010,536✔
391
  }
392

393
  tEndEncode(pCoder);
189,643,597✔
394
  return 0;
189,641,824✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,706,997,256✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,706,997,256✔
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,707,789,019✔
404
    tEndDecode(pCoder);
345,714✔
405
    return 0;
345,714✔
406
  }
407

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

411
    if (pME->type == TSDB_SUPER_TABLE) {
1,707,270,947✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,426,686,695✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,426,821,747✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,426,559,311✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
713,220,608✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
273,558✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
993,408,773✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,479,390,585✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,479,444,478✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,479,206,638✔
422
      if (pME->ctbEntry.commentLen > 0) {
739,484,189✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
48,196✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,479,254,852✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
739,728,901✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
254,059,637✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
508,315,884✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
508,423,545✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
508,333,822✔
431
      if (pME->ntbEntry.commentLen > 0) {
254,116,827✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
71,578✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
508,192,697✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
508,354,436✔
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,706,922,128✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
713,379,536✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
713,346,463✔
449

450
        if (pME->colCmpr.nCols == 0) {
713,202,346✔
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) {
993,384,335✔
458
      if (!tDecodeIsEnd(pCoder)) {
246,709,521✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
246,753,847✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
246,753,847✔
461
        if (pME->colCmpr.nCols == 0) {
246,729,763✔
462
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
463
        }
464
      } else {
UNCOV
465
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
466
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
467
      }
468
      TABLE_SET_COL_COMPRESSED(pME->flags);
246,758,432✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
746,679,523✔
470
      if (!tDecodeIsEnd(pCoder)) {
35,440,779✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
35,440,172✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
35,440,172✔
473
      } else {
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
607✔
475
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
607✔
476
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
477
        }
478
      }
479
    }
480
    if (!tDecodeIsEnd(pCoder)) {
1,706,997,369✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
960,076,936✔
482
    } else {
483
      pME->pExtSchemas = NULL;
746,920,433✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
1,706,871,703✔
487
    if (!tDecodeIsEnd(pCoder)) {
713,064,019✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,426,162,117✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
713,064,109✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,426,211,911✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
993,313,097✔
494
    if (!tDecodeIsEnd(pCoder)) {
246,749,000✔
495
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.ownerId));
493,526,758✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
1,706,392,316✔
501
  return 0;
1,706,340,368✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,706,771,646✔
505

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

511
  pDst->nCols = pSrc->nCols;
307,449,474✔
512
  pDst->version = pSrc->version;
307,439,676✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
307,429,822✔
514
  if (pDst->pSchema == NULL) {
307,323,988✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
307,343,120✔
518
  return TSDB_CODE_SUCCESS;
307,452,183✔
519
}
520

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

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

549
  return TSDB_CODE_SUCCESS;
41,644✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
307,379,417✔
553
  if (pSchema) {
307,379,417✔
554
    taosMemoryFreeClear(pSchema->pSchema);
307,394,940✔
555
  }
556
}
307,403,956✔
557

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

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
174,469,011✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
174,469,011✔
573
    return;
72,242✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
174,400,378✔
577

578
  if ((*ppEntry)->type < 0) {
174,394,051✔
579
    taosMemoryFreeClear(*ppEntry);
×
580
    return;
×
581
  }
582

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
174,387,971✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
149,460,967✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
149,441,099✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
149,437,161✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
63,184✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
24,936,041✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
16,427,815✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
16,427,280✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,508,761✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,508,761✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,508,761✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
174,400,282✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
174,362,667✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
174,367,353✔
601

602
  taosMemoryFreeClear(*ppEntry);
174,353,526✔
603
  return;
174,358,096✔
604
}
605

606
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
174,372,143✔
607
  int32_t code = TSDB_CODE_SUCCESS;
174,372,143✔
608

609
  if (NULL == pEntry || NULL == ppEntry) {
174,372,143✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
174,398,376✔
614
  if (NULL == *ppEntry) {
174,333,917✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
174,356,672✔
619
  (*ppEntry)->type = pEntry->type;
174,392,433✔
620
  (*ppEntry)->uid = pEntry->uid;
174,387,449✔
621

622
  if (pEntry->type < 0) {
174,384,696✔
623
    return TSDB_CODE_SUCCESS;
×
624
  }
625

626
  if (pEntry->name) {
174,370,467✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
174,407,035✔
628
    if (NULL == (*ppEntry)->name) {
174,407,366✔
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
174,404,154✔
636
    (*ppEntry)->flags = pEntry->flags;
149,470,875✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
149,453,441✔
639
    if (code) {
149,466,953✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
149,466,953✔
645
    if (code) {
149,479,266✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
149,479,266✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
42,362✔
651
      if (code) {
41,644✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
149,462,755✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
149,471,136✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
24,936,312✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
16,427,551✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
16,427,551✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
16,427,551✔
662

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

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

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

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,508,761✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,508,761✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
12,009✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
12,009✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
12,009✔
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) {
174,397,426✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
653,795✔
714
    if (code) {
644,540✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
173,754,779✔
720
    if (code) {
173,740,643✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
174,385,183✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
9,199,476✔
727
    if (!(*ppEntry)->pExtSchemas) {
9,197,049✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
9,195,846✔
733
  }
734

735
  return code;
174,392,059✔
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