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

taosdata / TDengine / #4996

19 Mar 2026 02:16AM UTC coverage: 72.069% (+0.07%) from 71.996%
#4996

push

travis-ci

web-flow
feat: SQL firewall black/white list (#34798)

461 of 618 new or added lines in 4 files covered. (74.6%)

380 existing lines in 128 files now uncovered.

245359 of 340448 relevant lines covered (72.07%)

135732617.17 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,240,684,504✔
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;
154,808,076✔
26
    }
27
  }
28
  return false;
1,081,047,217✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
203,745,951✔
32
  if (pME->pExtSchemas) {
203,745,951✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
12,409,325✔
34
    bool                  hasTypeMods = false;
12,409,325✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
12,409,325✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
12,301,560✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
116,824✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
116,824✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
12,423,569✔
43

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

51
int meteEncodeColRefEntry(SEncoder *pCoder, const SMetaEntry *pME) {
1,843,018✔
52
  const SColRefWrapper *pw = &pME->colRef;
1,843,018✔
53
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
3,686,036✔
54
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
3,686,036✔
55
  uTrace("encode cols:%d", pw->nCols);
1,843,018✔
56

57
  for (int32_t i = 0; i < pw->nCols; i++) {
439,595,352✔
58
    SColRef *p = &pw->pColRef[i];
437,752,334✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
875,504,668✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
875,504,668✔
61
    if (p->hasRef) {
437,752,334✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
580,177,476✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
580,177,476✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
580,177,476✔
65
    }
66
  }
67
  return 0;
1,843,018✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
1,182,839,875✔
71
  bool hasExtSchema = false;
1,182,839,875✔
72
  SSchemaWrapper* pSchWrapper = NULL;
1,182,839,875✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
1,182,839,875✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
905,759,148✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
277,045,656✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
277,146,634✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
1,182,782,235✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
1,182,913,560✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
290,989,120✔
84
    if (pME->pExtSchemas == NULL) {
145,498,012✔
85
      return terrno;
×
86
    }
87

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

95
  return 0;
1,182,949,771✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
45,762,586✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
45,762,586✔
100
  bool                  hasTypeMods = false;
45,762,586✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
45,762,586✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
33,469,429✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
12,316,852✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
12,323,628✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
45,792,265✔
109

110
  if (hasTypeMods) {
45,754,937✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
27,790✔
112
    if (ret != NULL) {
27,790✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
27,790✔
114
    }
115
    return ret;
28,060✔
116
  }
117
  return NULL;
45,727,147✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
27,491✔
151
  int32_t    i = 0, j = 0;
27,491✔
152
  for (i = 0; i < nCols; ++i) {
208,040✔
153
    while (j < pParam->nFuncs) {
328,406✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
315,032✔
155
        pFuncIds[i] = pParam->funcIds[j];
147,114✔
156
        break;
147,114✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
167,918✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
33,435✔
160
        break;
33,435✔
161
      }
162
      ++j;
134,483✔
163
    }
164
    if (j >= pParam->nFuncs) {
193,923✔
165
      for (; i < nCols; ++i) {
26,748✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
13,374✔
167
      }
168
      break;
13,374✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
27,491✔
172

173
  return 0;
27,491✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
55,173,324✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
55,173,324✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
110,352,692✔
179
  if (pWrapper->nCols == 0) {
55,177,623✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
110,347,149✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
55,174,410✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
110,337,856✔
186
  if (pWrapper->pColRef == NULL) {
55,167,135✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
939,508,871✔
191
    SColRef *p = &pWrapper->pColRef[i];
884,300,431✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
1,768,722,004✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
1,768,799,844✔
194
    if (p->hasRef) {
884,428,536✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
484,661,697✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
484,691,880✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
484,685,977✔
198
    }
199
  }
200
  return 0;
55,181,753✔
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) {
1,238,808✔
220
  if (pSrc->nCols > 0) {
1,238,808✔
221
    pDst->nCols = pSrc->nCols;
1,238,808✔
222
    pDst->version = pSrc->version;
1,238,808✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
1,238,808✔
224
    if (NULL == pDst->pColRef) {
1,238,196✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
1,238,196✔
228
  }
229
  return 0;
1,238,808✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
47,585,167✔
233
  int32_t code = 0;
47,585,167✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
95,166,993✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
95,186,656✔
236
  uTrace("encode cols:%d", pw->nCols);
47,604,830✔
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;
47,648,563✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
47,413,377✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
47,413,377✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
47,429,862✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
1,182,682,635✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
1,182,682,635✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
2,147,483,647✔
252
  if (pWrapper->nCols == 0) {
1,182,847,186✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
2,147,483,647✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
1,183,007,619✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
2,147,483,647✔
259
  if (pWrapper->pColCmpr == NULL) {
1,183,023,478✔
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,183,462,994✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
240,438✔
273

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

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

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

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
200,290,287✔
294
  if (pSrc->nCols > 0) {
200,290,287✔
295
    pDst->nCols = pSrc->nCols;
173,678,621✔
296
    pDst->version = pSrc->version;
173,681,222✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
173,701,364✔
298
    if (NULL == pDst->pColCmpr) {
173,668,906✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
173,671,582✔
302
  }
303
  return 0;
200,311,414✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
201,514,544✔
307
  if (pColRef) {
201,514,544✔
308
    taosMemoryFreeClear(pColRef->pColRef);
201,530,109✔
309
  }
310
}
201,538,135✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
201,488,616✔
313
  if (pCmpr) {
201,488,616✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
201,531,664✔
315
  }
316
}
201,486,591✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
208,714,381✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
208,714,381✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
417,466,595✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
417,401,791✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
417,360,254✔
323

324
  if (pME->type > 0) {
208,685,471✔
325
    if (pME->name == NULL) {
203,755,573✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
407,426,937✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
203,723,267✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
52,445,742✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
52,551,468✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
52,533,026✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
26,224,301✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
110,654✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
177,496,693✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
310,787,215✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
310,786,177✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
310,779,861✔
342
      if (pME->ctbEntry.commentLen > 0) {
155,395,127✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
29,464✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
310,778,037✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
155,388,152✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
22,123,970✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
44,247,940✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
44,247,447✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
44,247,447✔
351
      if (pME->ntbEntry.commentLen > 0) {
22,123,970✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
18,680✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
44,247,447✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
44,247,447✔
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) {
203,758,106✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,936,066✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
201,864,615✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
26,197,705✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
175,742,856✔
368
        if (pME->colCmpr.nCols != 0) {
21,385,510✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
21,145,072✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
240,438✔
372
          SColCmprWrapper colCmprs = {0};
240,438✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
240,438✔
374
          if (code != 0) {
240,438✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
240,438✔
379
          taosMemoryFree(colCmprs.pColCmpr);
240,438✔
380
          TAOS_CHECK_RETURN(code);
240,438✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
203,759,174✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
208,662,861✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
52,457,445✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
52,406,760✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
182,421,272✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
42,771,020✔
391
  }
392

393
  tEndEncode(pCoder);
208,669,495✔
394
  return 0;
208,697,753✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
2,059,375,223✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
2,059,375,223✔
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) {
2,060,335,137✔
404
    tEndDecode(pCoder);
318,810✔
405
    return 0;
318,810✔
406
  }
407

408
  if (pME->type > 0) {
2,060,016,327✔
409
    TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->name));
2,147,483,647✔
410

411
    if (pME->type == TSDB_SUPER_TABLE) {
2,059,751,774✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,812,010,250✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,812,096,931✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,811,914,851✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
905,857,225✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
339,697✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
1,153,200,548✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,735,348,998✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,735,560,604✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,735,335,675✔
422
      if (pME->ctbEntry.commentLen > 0) {
867,551,904✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
51,008✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,735,306,035✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
867,781,450✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
285,930,444✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
572,180,550✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
572,321,085✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
572,239,192✔
431
      if (pME->ntbEntry.commentLen > 0) {
286,083,162✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
38,876✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
572,085,831✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
572,140,864✔
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) {
2,059,438,900✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
905,913,415✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
905,976,974✔
449

450
        if (pME->colCmpr.nCols == 0) {
906,112,245✔
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,153,178,552✔
458
      if (!tDecodeIsEnd(pCoder)) {
277,269,384✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
277,276,424✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
277,276,424✔
461
        if (pME->colCmpr.nCols == 0) {
277,264,326✔
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);
226✔
466
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
226✔
467
      }
468
      TABLE_SET_COL_COMPRESSED(pME->flags);
277,252,710✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
876,092,163✔
470
      if (!tDecodeIsEnd(pCoder)) {
55,176,793✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
55,176,982✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
55,176,982✔
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)) {
2,059,747,754✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
1,183,270,416✔
482
    } else {
483
      pME->pExtSchemas = NULL;
876,477,338✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
2,059,437,254✔
487
    if (!tDecodeIsEnd(pCoder)) {
905,704,725✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,811,850,289✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
905,757,910✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,811,678,209✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,153,057,399✔
494
    if (!tDecodeIsEnd(pCoder)) {
277,178,796✔
495
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.ownerId));
554,506,174✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
2,059,401,598✔
501
  return 0;
2,059,460,631✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
2,058,975,243✔
505

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

511
  pDst->nCols = pSrc->nCols;
339,340,305✔
512
  pDst->version = pSrc->version;
339,335,123✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
339,357,655✔
514
  if (pDst->pSchema == NULL) {
339,198,186✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
339,250,581✔
518
  return TSDB_CODE_SUCCESS;
339,357,144✔
519
}
520

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

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

549
  return TSDB_CODE_SUCCESS;
75,189✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
339,302,045✔
553
  if (pSchema) {
339,302,045✔
554
    taosMemoryFreeClear(pSchema->pSchema);
339,329,786✔
555
  }
556
}
339,261,229✔
557

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

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
201,593,095✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
201,593,095✔
573
    return;
78,213✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
201,527,009✔
577

578
  if ((*ppEntry)->type < 0) {
201,557,849✔
579
    taosMemoryFreeClear(*ppEntry);
×
580
    return;
×
581
  }
582

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
201,481,340✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
165,170,437✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
165,145,034✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
165,150,856✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
114,641✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
36,359,831✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
27,384,454✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
27,381,825✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,980,685✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,980,915✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,980,915✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
201,513,773✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
201,501,045✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
201,521,465✔
601

602
  taosMemoryFreeClear(*ppEntry);
201,492,762✔
603
  return;
201,519,137✔
604
}
605

606
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
201,510,529✔
607
  int32_t code = TSDB_CODE_SUCCESS;
201,510,529✔
608

609
  if (NULL == pEntry || NULL == ppEntry) {
201,510,529✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
201,550,769✔
614
  if (NULL == *ppEntry) {
201,473,018✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
201,500,194✔
619
  (*ppEntry)->type = pEntry->type;
201,498,768✔
620
  (*ppEntry)->uid = pEntry->uid;
201,548,457✔
621

622
  if (pEntry->type < 0) {
201,544,524✔
623
    return TSDB_CODE_SUCCESS;
×
624
  }
625

626
  if (pEntry->name) {
201,538,757✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
201,539,689✔
628
    if (NULL == (*ppEntry)->name) {
201,537,670✔
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
201,504,905✔
636
    (*ppEntry)->flags = pEntry->flags;
165,166,744✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
165,159,183✔
639
    if (code) {
165,182,319✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
165,182,319✔
645
    if (code) {
165,204,104✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
165,204,104✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
75,189✔
651
      if (code) {
75,189✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
165,204,687✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
165,188,939✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
36,362,090✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
27,381,151✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
27,383,286✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
27,384,486✔
662

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

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

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

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,980,915✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,980,915✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
6,006✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
6,006✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
6,006✔
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) {
201,554,632✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
1,236,107✔
714
    if (code) {
1,238,808✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
200,282,679✔
720
    if (code) {
200,310,590✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
201,549,398✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
9,794,436✔
727
    if (!(*ppEntry)->pExtSchemas) {
9,793,204✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
9,790,691✔
733
  }
734

735
  return code;
201,522,303✔
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