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

taosdata / TDengine / #4947

02 Feb 2026 09:27AM UTC coverage: 66.872% (-0.06%) from 66.932%
#4947

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%)

3610 existing lines in 126 files now uncovered.

205539 of 307364 relevant lines covered (66.87%)

125933663.91 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,933,320✔
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,029,470✔
26
    }
27
  }
28
  return false;
985,249,951✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
184,903,494✔
32
  if (pME->pExtSchemas) {
184,903,494✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
11,742,734✔
34
    bool                  hasTypeMods = false;
11,742,734✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
11,742,734✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
11,645,036✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
89,584✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
89,584✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
11,733,959✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,248,856,745✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
184,935,845✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
413,771,424✔
58
    SColRef *p = &pw->pColRef[i];
412,527,114✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
825,054,228✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
825,054,228✔
61
    if (p->hasRef) {
412,527,114✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
547,217,588✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
547,217,588✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
547,217,588✔
65
    }
66
  }
67
  return 0;
1,244,310✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
959,690,730✔
71
  bool hasExtSchema = false;
959,690,730✔
72
  SSchemaWrapper* pSchWrapper = NULL;
959,690,730✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
959,690,730✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
727,268,151✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
232,586,821✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
232,715,133✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
960,038,895✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
960,068,937✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
36,414,514✔
84
    if (pME->pExtSchemas == NULL) {
18,200,360✔
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
1,485,096,618✔
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
2,147,483,647✔
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
941,862,991✔
93
  }
94

95
  return 0;
960,064,115✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
42,546,388✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
42,546,388✔
100
  bool                  hasTypeMods = false;
42,546,388✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
42,546,388✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
31,121,914✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
11,440,044✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
11,444,620✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
42,563,737✔
109

110
  if (hasTypeMods) {
42,522,642✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
38,576✔
112
    if (ret != NULL) {
38,576✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
38,576✔
114
    }
115
    return ret;
38,576✔
116
  }
117
  return NULL;
42,484,066✔
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) {
32,022,999✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
32,022,999✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
64,049,130✔
179
  if (pWrapper->nCols == 0) {
32,023,746✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
64,047,559✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
32,025,223✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
64,046,690✔
186
  if (pWrapper->pColRef == NULL) {
32,022,742✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
743,248,349✔
191
    SColRef *p = &pWrapper->pColRef[i];
711,200,747✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
1,422,461,734✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
1,422,486,299✔
194
    if (p->hasRef) {
711,263,893✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
394,138,849✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
394,143,706✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
394,146,644✔
198
    }
199
  }
200
  return 0;
32,026,622✔
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,703✔
220
  if (pSrc->nCols > 0) {
646,703✔
221
    pDst->nCols = pSrc->nCols;
646,703✔
222
    pDst->version = pSrc->version;
646,703✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
646,703✔
224
    if (NULL == pDst->pColRef) {
646,703✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
646,703✔
228
  }
229
  return 0;
646,703✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
43,908,442✔
233
  int32_t code = 0;
43,908,442✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
87,808,636✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
87,824,069✔
236
  uTrace("encode cols:%d", pw->nCols);
43,923,875✔
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;
43,978,275✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
43,726,768✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
43,726,768✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
43,780,274✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
959,770,908✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
959,770,908✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,919,991,572✔
252
  if (pWrapper->nCols == 0) {
959,867,220✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,919,520,236✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
959,800,496✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,919,762,584✔
259
  if (pWrapper->pColCmpr == NULL) {
959,986,781✔
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,369,202✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
214,930✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,086,764✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
871,834✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
871,834✔
287
    pColCmpr->id = pColSchema->colId;
871,834✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
871,834✔
289
  }
290
  return 0;
214,930✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
173,718,852✔
294
  if (pSrc->nCols > 0) {
173,718,852✔
295
    pDst->nCols = pSrc->nCols;
157,525,762✔
296
    pDst->version = pSrc->version;
157,526,372✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
157,499,355✔
298
    if (NULL == pDst->pColCmpr) {
157,496,988✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
157,493,062✔
302
  }
303
  return 0;
173,718,024✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
174,354,276✔
307
  if (pColRef) {
174,354,276✔
308
    taosMemoryFreeClear(pColRef->pColRef);
174,367,899✔
309
  }
310
}
174,369,800✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
174,351,496✔
313
  if (pCmpr) {
174,351,496✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
174,363,270✔
315
  }
316
}
174,373,864✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
189,390,803✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
189,390,803✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
378,771,020✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
378,773,586✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
378,800,753✔
323

324
  if (pME->type > 0) {
189,398,176✔
325
    if (pME->name == NULL) {
184,928,553✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
369,840,493✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
184,935,335✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
48,250,466✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
48,303,738✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
48,316,564✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
24,124,950✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
72,518✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
160,758,888✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
280,572,750✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
280,587,269✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
280,598,468✔
342
      if (pME->ctbEntry.commentLen > 0) {
140,302,903✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
27,852✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
280,573,187✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
140,283,002✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
20,492,844✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
40,985,688✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
40,985,688✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
40,985,688✔
351
      if (pME->ntbEntry.commentLen > 0) {
20,492,844✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
38,460✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
40,985,688✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
40,985,688✔
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) {
184,909,457✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,353,549✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
183,576,073✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
24,146,860✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
159,569,497✔
368
        if (pME->colCmpr.nCols != 0) {
19,824,706✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
19,609,776✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
214,930✔
372
          SColCmprWrapper colCmprs = {0};
214,930✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
214,930✔
374
          if (code != 0) {
214,930✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
214,930✔
379
          taosMemoryFree(colCmprs.pColCmpr);
214,930✔
380
          TAOS_CHECK_RETURN(code);
214,930✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
184,938,292✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
189,386,580✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
48,274,837✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
48,186,297✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
165,224,589✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
39,649,412✔
391
  }
392

393
  tEndEncode(pCoder);
189,337,739✔
394
  return 0;
189,371,110✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,727,036,496✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,727,036,496✔
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,727,767,561✔
404
    tEndDecode(pCoder);
346,708✔
405
    return 0;
346,708✔
406
  }
407

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

411
    if (pME->type == TSDB_SUPER_TABLE) {
1,727,228,176✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,454,847,056✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,454,989,461✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,454,787,524✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
727,344,077✔
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) {
999,462,175✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,520,284,322✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,520,314,084✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,520,098,025✔
422
      if (pME->ctbEntry.commentLen > 0) {
759,937,523✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
48,214✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,520,154,070✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
760,159,859✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
239,556,192✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
479,286,627✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
479,395,033✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
479,320,644✔
431
      if (pME->ntbEntry.commentLen > 0) {
239,616,826✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
71,068✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
479,151,040✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
479,340,150✔
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,727,058,809✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
727,458,968✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
727,427,796✔
449

450
        if (pME->colCmpr.nCols == 0) {
727,314,843✔
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) {
999,410,459✔
458
      if (!tDecodeIsEnd(pCoder)) {
232,655,419✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
232,704,040✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
232,704,040✔
461
        if (pME->colCmpr.nCols == 0) {
232,668,385✔
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);
232,730,764✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
766,742,448✔
470
      if (!tDecodeIsEnd(pCoder)) {
32,025,341✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
32,026,345✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
32,025,627✔
473
      } else {
UNCOV
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
475
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
476
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
477
        }
478
      }
479
    }
480
    if (!tDecodeIsEnd(pCoder)) {
1,726,971,138✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
960,091,586✔
482
    } else {
483
      pME->pExtSchemas = NULL;
766,879,552✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
1,726,935,392✔
487
    if (!tDecodeIsEnd(pCoder)) {
727,120,673✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,454,361,422✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
727,201,225✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,454,376,738✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
999,252,684✔
494
    if (!tDecodeIsEnd(pCoder)) {
232,737,350✔
495
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.ownerId));
465,476,722✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
1,726,477,075✔
501
  return 0;
1,726,510,280✔
502
}
503

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

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

511
  pDst->nCols = pSrc->nCols;
307,484,239✔
512
  pDst->version = pSrc->version;
307,477,471✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
307,469,040✔
514
  if (pDst->pSchema == NULL) {
307,350,408✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
307,368,160✔
518
  return TSDB_CODE_SUCCESS;
307,479,946✔
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));
42,362✔
537

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

549
  return TSDB_CODE_SUCCESS;
42,362✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
307,400,009✔
553
  if (pSchema) {
307,400,009✔
554
    taosMemoryFreeClear(pSchema->pSchema);
307,415,430✔
555
  }
556
}
307,425,815✔
557

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

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

576
  taosMemoryFreeClear((*ppEntry)->name);
174,358,847✔
577

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

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
174,352,265✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
149,510,524✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
149,490,500✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
149,492,173✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
63,902✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
24,846,800✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
16,411,762✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
16,411,944✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,435,653✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,435,653✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,435,653✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
174,358,121✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
174,328,033✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
174,333,069✔
601

602
  taosMemoryFreeClear(*ppEntry);
174,316,124✔
603
  return;
174,325,425✔
604
}
605

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

609
  if (NULL == pEntry || NULL == ppEntry) {
174,335,646✔
610
    return TSDB_CODE_INVALID_PARA;
91✔
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
174,353,938✔
614
  if (NULL == *ppEntry) {
174,275,254✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
174,331,862✔
619
  (*ppEntry)->type = pEntry->type;
174,360,013✔
620
  (*ppEntry)->uid = pEntry->uid;
174,347,796✔
621

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

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

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
174,376,251✔
636
    (*ppEntry)->flags = pEntry->flags;
149,532,729✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
149,506,665✔
639
    if (code) {
149,521,007✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
149,521,007✔
645
    if (code) {
149,530,572✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
149,530,572✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
42,362✔
651
      if (code) {
42,362✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
149,520,412✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
149,493,130✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
24,847,597✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
16,411,944✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
16,411,944✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
16,411,944✔
662

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

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

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

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,435,653✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,435,653✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
11,918✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
11,918✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
11,918✔
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,358,388✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
643,553✔
714
    if (code) {
646,703✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
173,716,501✔
720
    if (code) {
173,697,717✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
174,344,420✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
9,210,762✔
727
    if (!(*ppEntry)->pExtSchemas) {
9,209,616✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
9,208,377✔
733
  }
734

735
  return code;
174,362,788✔
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