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

taosdata / TDengine / #4837

07 Nov 2025 09:40AM UTC coverage: 58.963% (+0.2%) from 58.728%
#4837

push

travis-ci

DuanKuanJun
coverity: cases_other.task add -R -Q2 -Q3 -Q4

150245 of 324452 branches covered (46.31%)

Branch coverage included in aggregate %.

200054 of 269646 relevant lines covered (74.19%)

317833830.25 hits per line

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

65.8
/source/libs/command/src/command.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 "command.h"
17
#include "catalog.h"
18
#include "commandInt.h"
19
#include "decimal.h"
20
#include "scheduler.h"
21
#include "systable.h"
22
#include "taosdef.h"
23
#include "tdatablock.h"
24
#include "tdataformat.h"
25
#include "tglobal.h"
26
#include "tgrant.h"
27

28
#define COL_DATA_SET_VAL_AND_CHECK(pCol, rows, buf, isNull) \
29
  do {                                                      \
30
    int _code = colDataSetVal(pCol, rows, buf, isNull);     \
31
    if (TSDB_CODE_SUCCESS != _code) {                       \
32
      terrno = _code;                                       \
33
      return _code;                                         \
34
    }                                                       \
35
  } while (0)
36

37
extern SConfig* tsCfg;
38

39
static int32_t buildRetrieveTableRsp(SSDataBlock* pBlock, int32_t numOfCols, SRetrieveTableRsp** pRsp) {
28,611,713✔
40
  if (NULL == pBlock || NULL == pRsp) {
28,611,713!
41
    return TSDB_CODE_INVALID_PARA;
×
42
  }
43
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
28,611,713✔
44
  size_t rspSize = sizeof(SRetrieveTableRsp) + dataEncodeBufSize + PAYLOAD_PREFIX_LEN;
28,609,513✔
45
  *pRsp = taosMemoryCalloc(1, rspSize);
28,609,513!
46
  if (NULL == *pRsp) {
28,611,713!
47
    return terrno;
×
48
  }
49

50
  (*pRsp)->useconds = 0;
28,611,713✔
51
  (*pRsp)->completed = 1;
28,611,713✔
52
  (*pRsp)->precision = 0;
28,611,713✔
53
  (*pRsp)->compressed = 0;
28,611,713✔
54

55
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
28,611,713✔
56
  (*pRsp)->numOfCols = htonl(numOfCols);
28,611,713✔
57

58
  int32_t len = 0;
28,611,713✔
59
  if (pBlock->info.rows > 0) {
28,611,713✔
60
    len = blockEncode(pBlock, (*pRsp)->data + PAYLOAD_PREFIX_LEN, dataEncodeBufSize, numOfCols);
28,600,983✔
61
    if (len < 0) {
28,600,983!
62
      taosMemoryFree(*pRsp);
×
63
      *pRsp = NULL;
×
64
      return terrno;
×
65
    }
66
    SET_PAYLOAD_LEN((*pRsp)->data, len, len);
28,600,983✔
67
  }
68

69
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
28,611,713✔
70
  (*pRsp)->payloadLen = htonl(payloadLen);
28,611,713✔
71
  (*pRsp)->compLen = htonl(payloadLen);
28,611,713✔
72

73
  return TSDB_CODE_SUCCESS;
28,611,713✔
74
}
75

76
static int32_t getSchemaBytes(const SSchema* pSchema) {
558,947,546✔
77
  switch (pSchema->type) {
558,947,546✔
78
    case TSDB_DATA_TYPE_BINARY:
29,480,750✔
79
    case TSDB_DATA_TYPE_VARBINARY:
80
    case TSDB_DATA_TYPE_GEOMETRY:
81
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
29,480,750✔
82
    case TSDB_DATA_TYPE_NCHAR:
38,550,442✔
83
    case TSDB_DATA_TYPE_JSON:
84
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
38,550,442✔
85
    default:
490,916,354✔
86
      return pSchema->bytes;
490,916,354✔
87
  }
88
}
89

90
static const char* expandIdentifier(const char* name, char* output) {
9,554,110✔
91
  if (NULL == name) return "";
9,554,110!
92
  bool containsEscapeChar = false;
9,554,110✔
93
  for (const char* p = name; *p != '\0'; ++p) {
43,339,069✔
94
    if (*p == TS_ESCAPE_CHAR) {
34,180,689✔
95
      containsEscapeChar = true;
395,730✔
96
      break;
395,730✔
97
    }
98
  }
99
  if (!containsEscapeChar) return name;
9,554,110✔
100
  if (NULL == output) return "";
395,730!
101
  char* out_ptr = output;
395,730✔
102
  for (const char* src = name; *src != '\0'; ++src) {
2,435,938✔
103
    if (*src == TS_ESCAPE_CHAR) {
2,040,208✔
104
      *out_ptr++ = TS_ESCAPE_CHAR;
993,722✔
105
      *out_ptr++ = TS_ESCAPE_CHAR;
993,722✔
106
    } else {
107
      *out_ptr++ = *src;
1,046,486✔
108
    }
109
  }
110
  *out_ptr = '\0';
395,730✔
111
  return output;
395,730✔
112
}
113

114
static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) {
4,689,008✔
115
  QRY_PARAM_CHECK(pOutput);
4,689,008!
116

117
  SSDataBlock* pBlock = NULL;
4,689,008✔
118
  int32_t      code = createDataBlock(&pBlock);
4,689,008✔
119
  if (code) {
4,689,008!
120
    return code;
×
121
  }
122

123
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_FIELD_LEN, 1);
4,689,008✔
124
  code = blockDataAppendColInfo(pBlock, &infoData);
4,689,008✔
125
  if (TSDB_CODE_SUCCESS == code) {
4,689,008!
126
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_TYPE_LEN, 2);
4,689,008✔
127
    code = blockDataAppendColInfo(pBlock, &infoData);
4,689,008✔
128
  }
129
  if (TSDB_CODE_SUCCESS == code) {
4,689,008!
130
    infoData = createColumnInfoData(TSDB_DATA_TYPE_INT, tDataTypes[TSDB_DATA_TYPE_INT].bytes, 3);
4,689,008✔
131
    code = blockDataAppendColInfo(pBlock, &infoData);
4,689,008✔
132
  }
133
  if (TSDB_CODE_SUCCESS == code) {
4,689,008!
134
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_NOTE_LEN, 4);
4,689,008✔
135
    code = blockDataAppendColInfo(pBlock, &infoData);
4,689,008✔
136
  }
137
  if (TSDB_CODE_SUCCESS == code) {
4,689,008!
138
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 5);
4,689,008✔
139
    code = blockDataAppendColInfo(pBlock, &infoData);
4,689,008✔
140
  }
141
  if (TSDB_CODE_SUCCESS == code) {
4,689,008!
142
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 6);
4,689,008✔
143
    code = blockDataAppendColInfo(pBlock, &infoData);
4,689,008✔
144
  }
145
  if (TSDB_CODE_SUCCESS == code) {
4,689,008!
146
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 7);
4,689,008✔
147
    code = blockDataAppendColInfo(pBlock, &infoData);
4,689,008✔
148
  }
149
  if (TSDB_CODE_SUCCESS == code) {
4,689,008!
150
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COL_REF_LEN, 8);
4,689,008✔
151
    code = blockDataAppendColInfo(pBlock, &infoData);
4,689,008✔
152
  }
153
  if (TSDB_CODE_SUCCESS == code) {
4,689,008!
154
    *pOutput = pBlock;
4,689,008✔
155
  } else {
156
    (void)blockDataDestroy(pBlock);
×
157
  }
158
  return code;
4,689,008✔
159
}
160

161
static int32_t setDescResultIntoDataBlock(bool sysInfoUser, SSDataBlock* pBlock, int32_t numOfRows, STableMeta* pMeta,
4,689,008✔
162
                                          int8_t biMode) {
163
  int32_t blockCap = (biMode != 0) ? numOfRows + 1 : numOfRows;
4,689,008!
164
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, blockCap));
4,689,008!
165
  pBlock->info.rows = 0;
4,689,008✔
166

167
  // field
168
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
4,689,008✔
169
  // Type
170
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
4,689,008✔
171
  // Length
172
  SColumnInfoData* pCol3 = taosArrayGet(pBlock->pDataBlock, 2);
4,689,008✔
173
  // Note
174
  SColumnInfoData* pCol4 = taosArrayGet(pBlock->pDataBlock, 3);
4,689,008✔
175
  // encode
176
  SColumnInfoData* pCol5 = NULL;
4,689,008✔
177
  // compress
178
  SColumnInfoData* pCol6 = NULL;
4,689,008✔
179
  // level
180
  SColumnInfoData* pCol7 = NULL;
4,689,008✔
181
  // colref
182
  SColumnInfoData* pCol8 = NULL;
4,689,008✔
183
  if (withExtSchema(pMeta->tableType)) {
4,689,008✔
184
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
4,468,257✔
185
    pCol6 = taosArrayGet(pBlock->pDataBlock, 5);
4,468,257✔
186
    pCol7 = taosArrayGet(pBlock->pDataBlock, 6);
4,468,257✔
187
  }
188

189
  if (hasRefCol(pMeta->tableType)) {
4,689,008✔
190
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
50,004✔
191
  }
192

193
  int32_t fillTagCol = 0;
4,689,008✔
194
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
4,689,008✔
195
  for (int32_t i = 0; i < numOfRows; ++i) {
563,634,556✔
196
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
558,945,548!
197
      continue;
×
198
    }
199
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
558,945,548!
200
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
558,945,548!
201

202
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
558,945,447!
203
      uint8_t prec = 0, scale = 0;
1,763,292✔
204
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
1,763,292✔
205
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
1,763,292✔
206
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
1,763,292✔
207
      varDataSetLen(buf, len);
1,763,292✔
208
    } else {
209
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
557,182,155!
210
    }
211
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
558,945,447!
212
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
558,947,546✔
213
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
558,947,546!
214
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
558,947,647✔
215
      if (i >= pMeta->tableInfo.numOfColumns) {
558,555,167✔
216
        STR_TO_VARSTR(buf, "TAG");
35,772,418!
217
        fillTagCol = 1;
35,772,418✔
218
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
522,782,749✔
219
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
111,237!
220
      } else {
221
        STR_TO_VARSTR(buf, "");
522,671,512!
222
      }
223
    } else {
224
      STR_TO_VARSTR(buf, "VIEW COL");
392,480!
225
    }
226
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
558,947,647!
227
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
558,947,748!
228
      if (i < pMeta->tableInfo.numOfColumns) {
557,492,767✔
229
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
521,786,889!
230
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
521,786,687!
231
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
521,786,889!
232
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
521,784,689!
233
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
521,786,788!
234
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
521,784,588!
235
      } else {
236
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
35,705,878!
237
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
35,705,878!
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
35,705,878!
239
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
35,705,878!
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
35,705,878!
241
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
35,705,878!
242
      }
243
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
1,454,981!
244
      if (i < pMeta->numOfColRefs) {
357,108✔
245
        if (pMeta->colRef[i].hasRef) {
290,568!
246
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
145,482✔
247
          strcat(refColName, pMeta->colRef[i].refDbName);
145,482!
248
          strcat(refColName, ".");
145,482!
249
          strcat(refColName, pMeta->colRef[i].refTableName);
145,482!
250
          strcat(refColName, ".");
145,482!
251
          strcat(refColName, pMeta->colRef[i].refColName);
145,482!
252
          STR_TO_VARSTR(buf, refColName);
145,482!
253
        } else {
254
          STR_TO_VARSTR(buf, "");
145,086!
255
        }
256
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
290,568!
257
      } else {
258
        STR_TO_VARSTR(buf, "");
66,540!
259
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
66,540!
260
      }
261
    }
262

263
    fillTagCol = 0;
558,947,748✔
264

265
    ++(pBlock->info.rows);
558,947,748✔
266
  }
267
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
4,689,008!
268
    STR_TO_VARSTR(buf, "tbname");
×
269
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
×
270
    STR_TO_VARSTR(buf, "VARCHAR");
×
271
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
×
272
    int32_t bytes = TSDB_TABLE_NAME_LEN - 1;
×
273
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
×
274
    STR_TO_VARSTR(buf, "TAG");
×
275
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
×
276
    ++(pBlock->info.rows);
×
277
  }
278
  if (pBlock->info.rows <= 0) {
4,689,008!
279
    qError("no permission to view any columns");
×
280
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
281
  }
282
  return TSDB_CODE_SUCCESS;
4,689,008✔
283
}
284

285
static int32_t execDescribe(bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode) {
4,689,008✔
286
  SDescribeStmt* pDesc = (SDescribeStmt*)pStmt;
4,689,008✔
287
  if (NULL == pDesc || NULL == pDesc->pMeta) {
4,689,008!
288
    return TSDB_CODE_INVALID_PARA;
×
289
  }
290
  int32_t numOfRows = TABLE_TOTAL_COL_NUM(pDesc->pMeta);
4,689,008✔
291

292
  SSDataBlock* pBlock = NULL;
4,689,008✔
293
  int32_t      code = buildDescResultDataBlock(&pBlock);
4,689,008✔
294
  if (TSDB_CODE_SUCCESS == code) {
4,689,008!
295
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
4,689,008✔
296
  }
297
  if (TSDB_CODE_SUCCESS == code) {
4,689,008!
298
    if (pDesc->pMeta) {
4,689,008!
299
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
4,689,008!
300
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
4,468,257✔
301
      } else if (hasRefCol(pDesc->pMeta->tableType) && pDesc->pMeta->colRef) {
220,751!
302
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
50,004✔
303
      } else {
304
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
170,747✔
305
      }
306
    } else {
307
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
308
    }
309
  }
310
  (void)blockDataDestroy(pBlock);
4,689,008✔
311
  return code;
4,689,008✔
312
}
313

314
static int32_t execResetQueryCache() { return catalogClearCache(); }
16,444,874✔
315

316
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
423,506✔
317
  QRY_PARAM_CHECK(pOutput);
423,506!
318

319
  SSDataBlock* pBlock = NULL;
423,506✔
320
  int32_t      code = createDataBlock(&pBlock);
423,506✔
321
  if (code) {
423,506!
322
    return code;
×
323
  }
324

325
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_COLS, 1);
423,506✔
326
  code = blockDataAppendColInfo(pBlock, &infoData);
423,506✔
327
  if (TSDB_CODE_SUCCESS == code) {
423,506!
328
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_FIELD2_LEN, 2);
423,506✔
329
    code = blockDataAppendColInfo(pBlock, &infoData);
423,506✔
330
  }
331

332
  if (TSDB_CODE_SUCCESS == code) {
423,506!
333
    *pOutput = pBlock;
423,506✔
334
  } else {
335
    (void)blockDataDestroy(pBlock);
×
336
  }
337
  return code;
423,506✔
338
}
339

340
int64_t getValOfDiffPrecision(int8_t unit, int64_t val) {
×
341
  int64_t v = 0;
×
342
  switch (unit) {
×
343
    case 's':
×
344
      v = val / 1000;
×
345
      break;
×
346
    case 'm':
×
347
      v = val / tsTickPerMin[TSDB_TIME_PRECISION_MILLI];
×
348
      break;
×
349
    case 'h':
×
350
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 60);
×
351
      break;
×
352
    case 'd':
×
353
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60);
×
354
      break;
×
355
    case 'w':
×
356
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60 * 7);
×
357
      break;
×
358
    default:
×
359
      break;
×
360
  }
361

362
  return v;
×
363
}
364

365
static int32_t buildRetension(SArray* pRetension, char** ppRetentions) {
423,506✔
366
  size_t size = taosArrayGetSize(pRetension);
423,506✔
367
  if (size == 0) {
423,506!
368
    *ppRetentions = NULL;
423,506✔
369
    return TSDB_CODE_SUCCESS;
423,506✔
370
  }
371

372
  const int lMaxLen = 128;
×
373
  char*     p1 = taosMemoryCalloc(1, lMaxLen);
×
374
  if (NULL == p1) {
×
375
    return terrno;
×
376
  }
377
  int32_t len = 0;
×
378

379
  for (int32_t i = 0; i < size; ++i) {
×
380
    SRetention* p = TARRAY_GET_ELEM(pRetension, i);
×
381
    int64_t     v1 = getValOfDiffPrecision(p->freqUnit, p->freq);
×
382
    int64_t     v2 = getValOfDiffPrecision(p->keepUnit, p->keep);
×
383
    if (i == 0) {
×
384
      len += tsnprintf(p1 + len, lMaxLen - len, "-:%" PRId64 "%c", v2, p->keepUnit);
×
385
    } else {
386
      len += tsnprintf(p1 + len, lMaxLen - len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit);
×
387
    }
388

389
    if (i < size - 1) {
×
390
      len += tsnprintf(p1 + len, lMaxLen - len, ",");
×
391
    }
392
  }
393

394
  *ppRetentions = p1;
×
395
  return TSDB_CODE_SUCCESS;
×
396
}
397

398
static const char* cacheModelStr(int8_t cacheModel) {
416,388✔
399
  switch (cacheModel) {
416,388!
400
    case TSDB_CACHE_MODEL_NONE:
416,388✔
401
      return TSDB_CACHE_MODEL_NONE_STR;
416,388✔
402
    case TSDB_CACHE_MODEL_LAST_ROW:
×
403
      return TSDB_CACHE_MODEL_LAST_ROW_STR;
×
404
    case TSDB_CACHE_MODEL_LAST_VALUE:
×
405
      return TSDB_CACHE_MODEL_LAST_VALUE_STR;
×
406
    case TSDB_CACHE_MODEL_BOTH:
×
407
      return TSDB_CACHE_MODEL_BOTH_STR;
×
408
    default:
×
409
      break;
×
410
  }
411
  return TSDB_CACHE_MODEL_NONE_STR;
×
412
}
413

414
static const char* encryptAlgorithmStr(int8_t encryptAlgorithm) {
416,388✔
415
  switch (encryptAlgorithm) {
416,388!
416
    case TSDB_ENCRYPT_ALGO_NONE:
416,388✔
417
      return TSDB_ENCRYPT_ALGO_NONE_STR;
416,388✔
418
    case TSDB_ENCRYPT_ALGO_SM4:
×
419
      return TSDB_ENCRYPT_ALGO_SM4_STR;
×
420
    default:
×
421
      break;
×
422
  }
423
  return TSDB_CACHE_MODEL_NONE_STR;
×
424
}
425

426
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
51,220,344✔
427
  if (buffer == NULL || bufSize <= 0) {
51,220,344!
428
    return 0;
×
429
  }
430
  int32_t len = 0;
51,220,344✔
431
  if (timeInMinutes % 1440 == 0) {
51,220,344✔
432
    int32_t days = timeInMinutes / 1440;
51,142,247✔
433
    len = tsnprintf(buffer, bufSize, "%dd", days);
51,142,247✔
434
  } else if (timeInMinutes % 60 == 0) {
78,097✔
435
    int32_t hours = timeInMinutes / 60;
19,298✔
436
    len = tsnprintf(buffer, bufSize, "%dh", hours);
19,298✔
437
  } else {
438
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
58,799✔
439
  }
440
  return len;
51,220,344✔
441
}
442

443
static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, char* dbFName, SDbCfgInfo* pCfg) {
423,506✔
444
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
423,506!
445
  pBlock->info.rows = 1;
423,506✔
446

447
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
423,506✔
448
  char             buf1[SHOW_CREATE_DB_RESULT_FIELD1_LEN] = {0};
423,506✔
449
  STR_TO_VARSTR(buf1, dbName);
423,506!
450
  COL_DATA_SET_VAL_AND_CHECK(pCol1, 0, buf1, false);
423,506!
451

452
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
423,506✔
453
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
423,506✔
454
  int32_t          len = 0;
423,506✔
455
  char*            prec = NULL;
423,506✔
456
  switch (pCfg->precision) {
423,506!
457
    case TSDB_TIME_PRECISION_MILLI:
423,506✔
458
      prec = TSDB_TIME_PRECISION_MILLI_STR;
423,506✔
459
      break;
423,506✔
460
    case TSDB_TIME_PRECISION_MICRO:
×
461
      prec = TSDB_TIME_PRECISION_MICRO_STR;
×
462
      break;
×
463
    case TSDB_TIME_PRECISION_NANO:
×
464
      prec = TSDB_TIME_PRECISION_NANO_STR;
×
465
      break;
×
466
    default:
×
467
      prec = "none";
×
468
      break;
×
469
  }
470

471
  char* pRetentions = NULL;
423,506✔
472
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
423,506!
473
  int32_t dbFNameLen = strlen(dbFName);
423,506!
474
  int32_t hashPrefix = 0;
423,506✔
475
  if (pCfg->hashPrefix > 0) {
423,506✔
476
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
8,682✔
477
  } else if (pCfg->hashPrefix < 0) {
414,824✔
478
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
13,023✔
479
  }
480
  char durationStr[128] = {0};
423,506✔
481
  char keep0Str[128] = {0};
423,506✔
482
  char keep1Str[128] = {0};
423,506✔
483
  char keep2Str[128] = {0};
423,506✔
484
  char compactIntervalStr[13] = {0};
423,506✔
485
  char compactStartTimeStr[13] = {0};
423,506✔
486
  char compactEndTimeStr[13] = {0};
423,506✔
487

488
  int32_t lenDuration = formatDurationOrKeep(durationStr, sizeof(durationStr), pCfg->daysPerFile);
423,506✔
489
  int32_t lenKeep0 = formatDurationOrKeep(keep0Str, sizeof(keep0Str), pCfg->daysToKeep0);
423,506✔
490
  int32_t lenKeep1 = formatDurationOrKeep(keep1Str, sizeof(keep1Str), pCfg->daysToKeep1);
423,506✔
491
  int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pCfg->daysToKeep2);
423,506✔
492
  UNUSED(formatDurationOrKeep(compactIntervalStr, sizeof(compactIntervalStr), pCfg->compactInterval));
423,506✔
493
  UNUSED(formatDurationOrKeep(compactStartTimeStr, sizeof(compactStartTimeStr), pCfg->compactStartTime));
423,506✔
494
  UNUSED(formatDurationOrKeep(compactEndTimeStr, sizeof(compactEndTimeStr), pCfg->compactEndTime));
423,506✔
495

496
  if (IS_SYS_DBNAME(dbName)) {
423,506!
497
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
7,118✔
498
                     "CREATE DATABASE `%s`", dbName);
7,118✔
499
  } else {
500
    len +=
416,388✔
501
        tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
2,498,328✔
502
                  "CREATE DATABASE `%s` BUFFER %d CACHESIZE %d CACHEMODEL '%s' COMP %d DURATION %s "
503
                  "WAL_FSYNC_PERIOD %d MAXROWS %d MINROWS %d STT_TRIGGER %d KEEP %s,%s,%s PAGES %d PAGESIZE %d "
504
                  "PRECISION '%s' REPLICA %d "
505
                  "WAL_LEVEL %d VGROUPS %d SINGLE_STABLE %d TABLE_PREFIX %d TABLE_SUFFIX %d TSDB_PAGESIZE %d "
506
                  "WAL_RETENTION_PERIOD %d WAL_RETENTION_SIZE %" PRId64
507
                  " KEEP_TIME_OFFSET %d ENCRYPT_ALGORITHM '%s' SS_CHUNKPAGES %d SS_KEEPLOCAL %dm SS_COMPACT %d "
508
                  "COMPACT_INTERVAL %s COMPACT_TIME_RANGE %s,%s COMPACT_TIME_OFFSET %"PRIi8 "h",
509
                  dbName, pCfg->buffer, pCfg->cacheSize, cacheModelStr(pCfg->cacheLast), pCfg->compression, durationStr,
416,388✔
510
                  pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str, keep1Str, keep2Str,
416,388✔
511
                  pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel, pCfg->numOfVgroups,
416,388✔
512
                  1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize, pCfg->walRetentionPeriod,
416,388✔
513
                  pCfg->walRetentionSize, pCfg->keepTimeOffset, encryptAlgorithmStr(pCfg->encryptAlgorithm),
416,388✔
514
                  pCfg->ssChunkSize, pCfg->ssKeepLocal, pCfg->ssCompact, compactIntervalStr, compactStartTimeStr,
416,388✔
515
                  compactEndTimeStr, pCfg->compactTimeOffset);
416,388✔
516

517
    if (pRetentions) {
416,388!
518
      len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
519
                       " RETENTIONS %s", pRetentions);
520
    }
521
  }
522

523
  taosMemoryFree(pRetentions);
423,506!
524

525
  (varDataLen(buf2)) = len;
423,506✔
526

527
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
423,506!
528

529
  return TSDB_CODE_SUCCESS;
423,506✔
530
}
531

532
static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt, SRetrieveTableRsp** pRsp) {
423,506✔
533
  SSDataBlock* pBlock = NULL;
423,506✔
534
  int32_t      code = buildCreateDBResultDataBlock(&pBlock);
423,506✔
535
  if (TSDB_CODE_SUCCESS == code) {
423,506!
536
    code = setCreateDBResultIntoDataBlock(pBlock, pStmt->dbName, pStmt->dbFName, pStmt->pCfg);
423,506✔
537
  }
538
  if (TSDB_CODE_SUCCESS == code) {
423,506!
539
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_DB_RESULT_COLS, pRsp);
423,506✔
540
  }
541
  (void)blockDataDestroy(pBlock);
423,506✔
542
  return code;
423,506✔
543
}
544

545
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
1,112,513✔
546
  QRY_PARAM_CHECK(pOutput);
1,112,513!
547

548
  SSDataBlock* pBlock = NULL;
1,112,513✔
549
  int32_t      code = createDataBlock(&pBlock);
1,112,513✔
550
  if (code) {
1,112,513!
551
    return code;
×
552
  }
553

554
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD1_LEN, 1);
1,112,513✔
555
  code = blockDataAppendColInfo(pBlock, &infoData);
1,112,513✔
556
  if (TSDB_CODE_SUCCESS == code) {
1,112,513!
557
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD2_LEN, 2);
1,112,513✔
558
    code = blockDataAppendColInfo(pBlock, &infoData);
1,112,513✔
559
  }
560

561
  if (TSDB_CODE_SUCCESS == code) {
1,112,513!
562
    *pOutput = pBlock;
1,112,513✔
563
  } else {
564
    (void)blockDataDestroy(pBlock);
×
565
  }
566
  return code;
1,112,513✔
567
}
568

569
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
78,592✔
570
  QRY_PARAM_CHECK(pOutput);
78,592!
571

572
  SSDataBlock* pBlock = NULL;
78,592✔
573
  int32_t      code = createDataBlock(&pBlock);
78,592✔
574
  if (code) {
78,592!
575
    return code;
×
576
  }
577

578
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD1_LEN, 1);
78,592✔
579
  code = blockDataAppendColInfo(pBlock, &infoData);
78,592✔
580
  if (TSDB_CODE_SUCCESS == code) {
78,592!
581
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD2_LEN, 2);
78,592✔
582
    code = blockDataAppendColInfo(pBlock, &infoData);
78,592✔
583
  }
584

585
  if (TSDB_CODE_SUCCESS == code) {
78,592!
586
    *pOutput = pBlock;
78,592✔
587
  } else {
588
    (void)blockDataDestroy(pBlock);
×
589
  }
590
  return code;
78,592✔
591
}
592

593
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
700,377✔
594
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
700,377✔
595
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
5,799,686✔
596
    SSchema* pSchema = pCfg->pSchemas + i;
5,099,309✔
597
    SColRef* pRef = pCfg->pColRefs + i;
5,099,309✔
598
#define LTYPE_LEN                                    \
599
  (32 + 60 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN + \
600
   10)  // 60 byte for compress info, TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN for column ref
601
    char type[LTYPE_LEN];
5,099,309✔
602
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
5,099,309✔
603
    int typeLen = strlen(type);
5,099,309✔
604
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
5,099,309✔
605
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
4,658,817✔
606
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
519,139✔
607
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
4,580,170✔
608
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
185,078✔
609
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
185,078✔
610
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
4,395,092✔
611
      uint8_t precision, scale;
122,544✔
612
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
122,544✔
613
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
122,544✔
614
    }
615

616
    if (withExtSchema(pCfg->tableType) && pCfg->pSchemaExt && tsShowFullCreateTableColumn) {
5,099,309!
617
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " ENCODE \'%s\'",
166,038✔
618
                           columnEncodeStr(COMPRESS_L1_TYPE_U32(pCfg->pSchemaExt[i].compress)));
166,038✔
619
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " COMPRESS \'%s\'",
166,038✔
620
                           columnCompressStr(COMPRESS_L2_TYPE_U32(pCfg->pSchemaExt[i].compress)));
166,038✔
621
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " LEVEL \'%s\'",
166,038✔
622
                           columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pCfg->pSchemaExt[i].compress)));
166,038✔
623
    }
624

625
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
5,099,309!
626
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " FROM `%s`", pRef->refDbName);
58,928✔
627
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
58,928✔
628
      typeLen +=
58,928✔
629
          tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
58,928✔
630
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
58,928✔
631
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
58,928✔
632
    }
633

634
    if (!(pSchema->flags & COL_IS_KEY)) {
5,099,309✔
635
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
5,026,497✔
636
                        "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
5,026,497✔
637
    } else {
638
      char* pk = "COMPOSITE KEY";
72,812✔
639
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
72,812!
640
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
72,812✔
641
    }
642
  }
643
}
700,377✔
644

645
static void appendColRefFields(char* buf, int32_t* len, STableCfg* pCfg) {
12,928✔
646
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
12,928✔
647
  for (int32_t i = 1; i < pCfg->numOfColumns; ++i) {
109,062✔
648
    SSchema* pSchema = pCfg->pSchemas + i;
96,134✔
649
    SColRef* pRef = pCfg->pColRefs + i;
96,134✔
650
    char     type[TSDB_COL_NAME_LEN + 10 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN];
96,134✔
651
    int      typeLen = 0;
96,134✔
652

653
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
96,134!
654
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
38,258✔
655
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
38,258✔
656
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
38,258✔
657
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
38,258✔
658
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
38,258✔
659
    } else {
660
      continue;
57,876✔
661
    }
662

663
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
38,258✔
664
                      "%s`%s` %s", ((i > 1) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
38,258✔
665

666
  }
667
}
12,928✔
668

669
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
424,623✔
670
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
424,623✔
671
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
2,824,698✔
672
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
2,400,075✔
673
    char     type[32];
2,400,075✔
674
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
2,400,075✔
675
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
2,400,075✔
676
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
2,079,187✔
677
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
384,593✔
678
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
384,593✔
679
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
2,015,482✔
680
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
107,535✔
681
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
107,535✔
682
    }
683

684
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,400,075✔
685
                      "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
2,400,075✔
686
  }
687
}
424,623✔
688

689
static void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
295,026✔
690
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
295,026✔
691
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
718,123✔
692
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
423,097✔
693
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
423,097✔
694
                      "%s`%s`", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName));
423,097✔
695
  }
696
}
295,026✔
697

698
static int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg, void* charsetCxt) {
295,026✔
699
  int32_t code = TSDB_CODE_SUCCESS;
295,026✔
700
  SArray* pTagVals = NULL;
295,026✔
701
  STag*   pTag = (STag*)pCfg->pTags;
295,026✔
702

703
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
295,026!
704
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
705
    return TSDB_CODE_APP_ERROR;
×
706
  }
707

708
  if (tTagIsJson(pTag)) {
295,026✔
709
    char* pJson = NULL;
5,332✔
710
    parseTagDatatoJson(pTag, &pJson, charsetCxt);
5,332✔
711
    if (NULL == pJson) {
5,332!
712
      qError("failed to parse tag to json, pJson is NULL");
×
713
      return terrno;
×
714
    }
715
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
5,332✔
716
                      "%s", pJson);
717
    taosMemoryFree(pJson);
5,332!
718

719
    return TSDB_CODE_SUCCESS;
5,332✔
720
  }
721

722
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
289,694!
723
  int16_t valueNum = taosArrayGetSize(pTagVals);
289,694✔
724
  int32_t num = 0;
289,694✔
725
  int32_t j = 0;
289,694✔
726
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
707,459✔
727
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
417,765✔
728
    if (i > 0) {
417,765✔
729
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
128,071✔
730
                        ", ");
731
    }
732

733
    if (j >= valueNum) {
417,765✔
734
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
91,317✔
735
                        "NULL");
736
      continue;
91,317✔
737
    }
738

739
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
326,448✔
740
    if (pSchema->colId > pTagVal->cid) {
326,448!
741
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
742
      code = TSDB_CODE_APP_ERROR;
×
743
      TAOS_CHECK_ERRNO(code);
×
744
    } else if (pSchema->colId == pTagVal->cid) {
326,448✔
745
      char    type = pTagVal->type;
317,674✔
746
      int32_t tlen = 0;
317,674✔
747

748
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
317,674✔
749
      if (leftSize <= 0) {
317,674!
750
        qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
×
751
        code = TSDB_CODE_APP_ERROR;
×
752
        TAOS_CHECK_ERRNO(code);
×
753
      }
754
      if (IS_VAR_DATA_TYPE(type)) {
317,674!
755
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
50,198✔
756
        TAOS_CHECK_ERRNO(code);
50,198!
757
      } else {
758
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
267,476✔
759
                               &tlen);
760
        TAOS_CHECK_ERRNO(code);
267,476!
761
      }
762
      *len += tlen;
317,674✔
763
      j++;
317,674✔
764
    } else {
765
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
8,774✔
766
                        "NULL");
767
    }
768
  }
769
_exit:
289,694✔
770
  taosArrayDestroy(pTagVals);
289,694✔
771

772
  return code;
289,694✔
773
}
774

775
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
969,547✔
776
  if (pCfg->commentLen > 0) {
969,547!
777
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
778
                      " COMMENT '%s'", pCfg->pComment);
779
  } else if (0 == pCfg->commentLen) {
969,547✔
780
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
41,736✔
781
                      " COMMENT ''");
782
  }
783

784
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
969,547!
785
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
786
                      " WATERMARK %" PRId64 "a", pCfg->watermark1);
787
    if (pCfg->watermark2 > 0) {
×
788
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
789
                        ", %" PRId64 "a", pCfg->watermark2);
790
    }
791
  }
792

793
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
969,547!
794
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
795
                      " MAX_DELAY %" PRId64 "a", pCfg->delay1);
796
    if (pCfg->delay2 > 0) {
×
797
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
798
                        ", %" PRId64 "a", pCfg->delay2);
799
    }
800
  }
801

802
  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
969,547✔
803
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
969,547!
804
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
805
                      " ROLLUP(");
806
    for (int32_t i = 0; i < funcNum; ++i) {
×
807
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
×
808
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
809
                        "%s%s", ((i > 0) ? ", " : ""), pFunc);
810
    }
811
    *len +=
×
812
        snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
×
813
  }
814

815
  if (pCfg->ttl > 0) {
969,547!
816
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
817
                      " TTL %d", pCfg->ttl);
818
  }
819

820
  if (pCfg->keep > 0) {
969,547✔
821
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
16,738✔
822
                      " KEEP %dm", pCfg->keep);
823
  }
824

825
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
969,547✔
826
    int32_t nSma = 0;
687,449✔
827
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
5,677,696✔
828
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
4,990,247!
829
        ++nSma;
4,990,247✔
830
      }
831
    }
832

833
    if (nSma < pCfg->numOfColumns && nSma > 0) {
687,449!
834
      bool smaOn = false;
×
835
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
836
                        " SMA(");
837
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
838
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
839
          if (smaOn) {
×
840
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
841
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ",`%s`",
×
842
                              (pCfg->pSchemas + i)->name);
×
843
          } else {
844
            smaOn = true;
×
845
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
846
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), "`%s`",
×
847
                              (pCfg->pSchemas + i)->name);
×
848
          }
849
        }
850
      }
851
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, ")");
×
852
    }
853
  }
854

855
  if (pCfg->virtualStb) {
969,547✔
856
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
8,794✔
857
                      " VIRTUAL %d", pCfg->virtualStb);
8,794✔
858
  }
859
}
969,547✔
860

861
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
1,102,173✔
862
                                              void* charsetCxt) {
863
  int32_t code = TSDB_CODE_SUCCESS;
1,102,173✔
864
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
1,102,173!
865
  pBlock->info.rows = 1;
1,102,173✔
866

867
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
1,102,173✔
868
  char             buf1[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1)] = {0};
1,102,173✔
869
  STR_TO_VARSTR(buf1, tbName);
1,102,173!
870
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
1,102,173!
871

872
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
1,102,173✔
873
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
1,102,173!
874
  if (NULL == buf2) {
1,102,173!
875
    QRY_ERR_RET(terrno);
×
876
  }
877

878
  int32_t len = 0;
1,102,173✔
879

880
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
1,102,173✔
881
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
424,623✔
882
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
883
    appendColumnFields(buf2, &len, pCfg);
424,623✔
884
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
424,623✔
885
                     ") TAGS (");
886
    appendTagFields(buf2, &len, pCfg);
424,623✔
887
    len +=
424,623✔
888
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
424,623✔
889
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
424,623✔
890
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
677,550✔
891
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
282,098✔
892
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
893
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
282,098✔
894
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
282,098✔
895
    appendTagNameFields(buf2, &len, pCfg);
282,098✔
896
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
282,098✔
897
                     ") TAGS (");
898
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
282,098✔
899
    TAOS_CHECK_ERRNO(code);
282,098!
900
    len +=
282,098✔
901
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
282,098✔
902
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
282,098✔
903
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
395,452✔
904
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
262,826✔
905
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
906
    appendColumnFields(buf2, &len, pCfg);
262,826✔
907
    len +=
262,826✔
908
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
262,826✔
909
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
262,826✔
910
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
132,626✔
911
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
12,928✔
912
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
913
    appendColumnFields(buf2, &len, pCfg);
12,928✔
914
    len +=
12,928✔
915
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
12,928✔
916
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
119,698✔
917
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
12,928✔
918
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
919
    appendColRefFields(buf2, &len, pCfg);
12,928✔
920
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
12,928✔
921
                    ") USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
12,928✔
922
    appendTagNameFields(buf2, &len, pCfg);
12,928✔
923
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
12,928✔
924
                     ") TAGS (");
925
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
12,928✔
926
    TAOS_CHECK_ERRNO(code);
12,928!
927
    len +=
12,928✔
928
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
12,928✔
929
  }
930

931
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
1,102,173✔
932

933
  code = colDataSetVal(pCol2, 0, buf2, false);
1,102,173✔
934
  TAOS_CHECK_ERRNO(code);
1,102,173!
935

936
_exit:
1,102,173✔
937
  taosMemoryFree(buf2);
1,102,173!
938

939
  return code;
1,102,173✔
940
}
941

942
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
78,592✔
943
  int32_t code = 0;
78,592✔
944
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
78,592!
945
  pBlock->info.rows = 1;
78,592✔
946

947
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
78,592✔
948
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
78,592✔
949
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
78,592✔
950
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
78,592!
951
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
78,592!
952

953
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
78,592✔
954
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
78,592!
955
  if (NULL == buf2) {
78,592!
956
    return terrno;
×
957
  }
958

959
  SViewMeta* pMeta = pStmt->pViewMeta;
78,592✔
960
  if (NULL == pMeta) {
78,592!
961
    qError("exception: view meta is null");
×
962
    taosMemoryFree(buf2);
×
963
    return TSDB_CODE_APP_ERROR;
×
964
  }
965
  snprintf(varDataVal(buf2), SHOW_CREATE_VIEW_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE VIEW `%s`.`%s` AS %s",
78,592✔
966
           pStmt->dbName, pStmt->viewName, pMeta->querySql);
78,592✔
967
  int32_t len = strlen(varDataVal(buf2));
78,592!
968
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
78,592✔
969
  code = colDataSetVal(pCol2, 0, buf2, false);
78,592✔
970
  taosMemoryFree(buf2);
78,592!
971

972
  return code;
78,592✔
973
}
974

975
extern const char* fmGetFuncName(int32_t funcId);
976
static int32_t setCreateRsmaResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateRsmaStmt* pStmt) {
10,340✔
977
  int32_t       code = 0, lino = 0;
10,340✔
978
  char*         buf2 = NULL;
10,340✔
979
  SRsmaInfoRsp* pMeta = pStmt->pRsmaMeta;
10,340✔
980

981
  if (pMeta->nFuncs != pMeta->nColNames) {
10,340!
982
    qError("exception: rsma meta is invalid, nFuncs:%d != nColNames:%d", pMeta->nFuncs, pMeta->nColNames);
×
983
    return TSDB_CODE_APP_ERROR;
×
984
  }
985

986
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, 1));
10,340!
987
  pBlock->info.rows = 1;
10,340✔
988

989
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
10,340✔
990
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1] = {0};
10,340✔
991
  snprintf(varDataVal(buf1), TSDB_TABLE_FNAME_LEN + 4, "`%s`", expandIdentifier(pStmt->rsmaName, buf1));
10,340✔
992
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
10,340!
993
  TAOS_CHECK_EXIT(colDataSetVal(pCol1, 0, buf1, false));
10,340!
994

995
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
10,340✔
996
  if (!(buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN))) {
10,340!
997
    return terrno;
×
998
  }
999

1000
  SName name = {0};
10,340✔
1001
  TAOS_CHECK_EXIT(tNameFromString(&name, pMeta->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
10,340!
1002

1003
  int32_t len = 0;
10,340✔
1004
  len += tsnprintf(varDataVal(buf2), SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
20,680✔
1005
                       "CREATE RSMA `%s` ON `%s`.`%s` FUNCTION(", expandIdentifier(pStmt->rsmaName, buf1),
10,340✔
1006
                       expandIdentifier(name.dbname, buf1), expandIdentifier(name.tname, buf1));
1007
  for (int32_t i = 0; i < pMeta->nFuncs; ++i) {
77,550✔
1008
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
134,420✔
1009
                         "%s%s(`%s`)", (i > 0) ? "," : "", fmGetFuncName(pMeta->funcIds[i]),
67,210✔
1010
                         expandIdentifier(*(char**)TARRAY_GET_ELEM(pMeta->colNames, i), buf1));
67,210✔
1011
  }
1012
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
10,340✔
1013
                       ") INTERVAL(%d%c", pMeta->interval[0], pMeta->intervalUnit);
10,340✔
1014
  if (pMeta->interval[1] > 0) {
10,340!
1015
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
10,340✔
1016
                         ",%d%c", pMeta->interval[1], pMeta->intervalUnit);
10,340✔
1017
  }
1018
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
10,340✔
1019
  varDataLen(buf2) = len;
10,340✔
1020
  code = colDataSetVal(pCol2, 0, buf2, false);
10,340✔
1021
_exit:
10,340✔
1022
  taosMemoryFree(buf2);
10,340!
1023
  return code;
10,340✔
1024
}
1025

1026
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
1,080,714✔
1027
  SSDataBlock* pBlock = NULL;
1,080,714✔
1028
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
1,080,714✔
1029
  if (TSDB_CODE_SUCCESS == code) {
1,080,714!
1030
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
1,080,714✔
1031
  }
1032
  if (TSDB_CODE_SUCCESS == code) {
1,080,714!
1033
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
1,080,714✔
1034
  }
1035
  (void)blockDataDestroy(pBlock);
1,080,714✔
1036
  return code;
1,080,714✔
1037
}
1038

1039
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
21,459✔
1040
  SSDataBlock* pBlock = NULL;
21,459✔
1041
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
21,459✔
1042
  if (TSDB_CODE_SUCCESS == code) {
21,459!
1043
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
21,459✔
1044
  }
1045
  if (TSDB_CODE_SUCCESS == code) {
21,459!
1046
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
21,459✔
1047
  }
1048
  (void)blockDataDestroy(pBlock);
21,459✔
1049
  return code;
21,459✔
1050
}
1051

1052
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
10,677✔
1053
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
10,677✔
1054
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
10,677✔
1055
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
3,559✔
1056
    return terrno;
3,559✔
1057
  }
1058

1059
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
7,118✔
1060
}
1061

1062
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
2,218,646✔
1063
  int32_t code = 0;
2,218,646✔
1064

1065
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
2,218,646!
1066
    taosResetLog();
3,382✔
1067
    cfgDumpCfg(tsCfg, 0, false);
3,382✔
1068
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
2,215,264✔
1069
    int32_t tmp = 0;
25,670✔
1070
    code = taosStr2int32(value, &tmp);
25,670✔
1071
    if (code) {
25,670!
1072
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1073
      return code;
×
1074
    }
1075
    code = schedulerUpdatePolicy(tmp);
25,670✔
1076
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
2,189,594!
1077
    int32_t tmp = 0;
×
1078
    code = taosStr2int32(value, &tmp);
×
1079
    if (code) {
×
1080
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1081
      return code;
×
1082
    }
1083
    code = schedulerEnableReSchedule(tmp != 0);
×
1084
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
2,189,594!
1085
    code = ctgdHandleDbgCommand(value);
×
1086
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
2,189,594!
1087
    code = taosMemoryDbgInit();
×
1088
    if (code) {
×
1089
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
1090
      return code;
×
1091
    }
1092
    tsAsyncLog = false;
×
1093
    qInfo("memory dbg enabled");
×
1094
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
2,189,594!
1095
    code = taosMemoryDbgInitRestore();
×
1096
    if (code) {
×
1097
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
1098
      return code;
×
1099
    }
1100
    qInfo("memory dbg disabled");
×
1101
  } else {
1102
    goto _return;
2,189,594✔
1103
  }
1104

1105
  *processed = true;
29,052✔
1106

1107
_return:
2,218,646✔
1108

1109
  if (code) {
2,218,646!
1110
    terrno = code;
×
1111
  }
1112

1113
  return code;
2,218,646✔
1114
}
1115

1116
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
2,218,646✔
1117
  bool processed = false;
2,218,646✔
1118

1119
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
2,218,646!
1120
    return terrno;
×
1121
  }
1122

1123
  if (processed) {
2,218,646✔
1124
    goto _return;
29,052✔
1125
  }
1126

1127
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
2,189,594✔
1128
    return terrno;
45,141✔
1129
  }
1130

1131
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
2,144,453!
1132
    return terrno;
×
1133
  }
1134

1135
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
2,144,453!
1136

1137
_return:
2,172,523✔
1138

1139
  return TSDB_CODE_SUCCESS;
2,173,505✔
1140
}
1141

1142
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
727,010✔
1143
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
727,010!
1144
  if (NULL == pBlock) {
727,010!
1145
    return terrno;
×
1146
  }
1147

1148
  pBlock->info.hasVarCol = true;
727,010✔
1149

1150
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
727,010✔
1151
  if (NULL == pBlock->pDataBlock) {
727,010!
1152
    taosMemoryFree(pBlock);
×
1153
    return terrno;
×
1154
  }
1155

1156
  SColumnInfoData infoData = {0};
727,010✔
1157

1158
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
727,010✔
1159
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
727,010✔
1160
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,454,020!
1161
    goto _exit;
×
1162
  }
1163

1164
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
727,010✔
1165
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
727,010✔
1166
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,454,020!
1167
    goto _exit;
×
1168
  }
1169

1170
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
727,010✔
1171
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
727,010✔
1172
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,454,020!
1173
    goto _exit;
×
1174
  }
1175

1176
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
727,010✔
1177
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
727,010✔
1178
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,454,020!
1179
    goto _exit;
×
1180
  }
1181

1182
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
727,010✔
1183
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
727,010✔
1184
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,454,020!
1185
    goto _exit;
×
1186
  }
1187

1188
  *pOutput = pBlock;
727,010✔
1189

1190
_exit:
727,010✔
1191
  if (terrno != TSDB_CODE_SUCCESS) {
727,010!
1192
    taosArrayDestroy(pBlock->pDataBlock);
×
1193
    taosMemoryFree(pBlock);
×
1194
  }
1195
  return terrno;
727,010✔
1196
}
1197

1198
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
727,010✔
1199
  SSDataBlock* pBlock = NULL;
727,010✔
1200
  char*        likePattern = NULL;
727,010✔
1201
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
727,010✔
1202
  if (TSDB_CODE_SUCCESS == code) {
727,010!
1203
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
727,010✔
1204
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
45,988✔
1205
    }
1206
  }
1207
  if (TSDB_CODE_SUCCESS == code) {
727,010!
1208
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
727,010✔
1209
  }
1210
  if (TSDB_CODE_SUCCESS == code) {
727,010!
1211
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
727,010✔
1212
  }
1213
  (void)blockDataDestroy(pBlock);
727,010✔
1214
  return code;
727,010✔
1215
}
1216

1217
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
21,581,084✔
1218
  QRY_PARAM_CHECK(pOutput);
21,581,084!
1219

1220
  SSDataBlock* pBlock = NULL;
21,581,084✔
1221
  int32_t      code = createDataBlock(&pBlock);
21,581,084✔
1222
  if (code) {
21,581,084!
1223
    return code;
×
1224
  }
1225

1226
  SNode* pProj = NULL;
21,581,084✔
1227
  FOREACH(pProj, pProjects) {
52,552,894!
1228
    SExprNode*      pExpr = (SExprNode*)pProj;
30,971,810✔
1229
    SColumnInfoData infoData = {0};
30,971,810✔
1230
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
30,971,810!
1231
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1232
      infoData.info.bytes = 0;
×
1233
    } else {
1234
      infoData.info.type = pExpr->resType.type;
30,971,810✔
1235
      infoData.info.bytes = pExpr->resType.bytes;
30,971,810✔
1236
      infoData.info.precision = pExpr->resType.precision;
30,971,810✔
1237
      infoData.info.scale = pExpr->resType.scale;
30,971,810✔
1238
    }
1239
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
30,971,810!
1240
  }
1241

1242
  *pOutput = pBlock;
21,581,084✔
1243
  return code;
21,581,084✔
1244
}
1245

1246
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
21,581,084✔
1247
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
21,581,084!
1248

1249
  int32_t index = 0;
21,581,084✔
1250
  SNode*  pProj = NULL;
21,581,084✔
1251
  FOREACH(pProj, pProjects) {
52,552,894!
1252
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
30,971,810!
1253
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1254
    } else {
1255
      if (((SValueNode*)pProj)->isNull) {
30,971,810!
1256
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
777,472!
1257
      } else {
1258
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
30,194,338!
1259
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1260
      }
1261
    }
1262
  }
1263

1264
  pBlock->info.rows = 1;
21,581,084✔
1265
  return TSDB_CODE_SUCCESS;
21,581,084✔
1266
}
1267

1268
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
21,581,084✔
1269
  SSDataBlock* pBlock = NULL;
21,581,084✔
1270
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
21,581,084✔
1271
  if (TSDB_CODE_SUCCESS == code) {
21,581,084!
1272
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
21,581,084✔
1273
  }
1274
  if (TSDB_CODE_SUCCESS == code) {
21,581,084!
1275
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
21,581,084!
1276
  }
1277
  (void)blockDataDestroy(pBlock);
21,581,084✔
1278
  return code;
21,581,084✔
1279
}
1280

1281
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
78,592✔
1282
  SSDataBlock* pBlock = NULL;
78,592✔
1283
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
78,592✔
1284
  if (TSDB_CODE_SUCCESS == code) {
78,592!
1285
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
78,592✔
1286
  }
1287
  if (TSDB_CODE_SUCCESS == code) {
78,592!
1288
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
78,592✔
1289
  }
1290
  (void)blockDataDestroy(pBlock);
78,592✔
1291
  return code;
78,592✔
1292
}
1293

1294
static int32_t execShowCreateRsma(SShowCreateRsmaStmt* pStmt, SRetrieveTableRsp** pRsp) {
10,340✔
1295
  SSDataBlock* pBlock = NULL;
10,340✔
1296
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
10,340✔
1297
  if (TSDB_CODE_SUCCESS == code) {
10,340!
1298
    code = setCreateRsmaResultIntoDataBlock(pBlock, pStmt);
10,340✔
1299
  }
1300
  if (TSDB_CODE_SUCCESS == code) {
10,340!
1301
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
10,340✔
1302
  }
1303
  (void)blockDataDestroy(pBlock);
10,340✔
1304
  return code;
10,340✔
1305
}
1306

1307
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
47,278,792✔
1308
                     void* charsetCxt) {
1309
  switch (nodeType(pStmt)) {
47,278,792!
1310
    case QUERY_NODE_DESCRIBE_STMT:
4,689,008✔
1311
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
4,689,008✔
1312
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
16,444,874✔
1313
      return execResetQueryCache();
16,444,874✔
1314
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
423,506✔
1315
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
423,506✔
1316
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
1,073,596✔
1317
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,073,596✔
1318
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
21,459✔
1319
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
21,459✔
1320
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
10,677✔
1321
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
10,677✔
1322
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
78,592✔
1323
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
78,592✔
1324
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
10,340✔
1325
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
10,340✔
1326
    case QUERY_NODE_ALTER_LOCAL_STMT:
2,218,646✔
1327
      return execAlterLocal((SAlterLocalStmt*)pStmt);
2,218,646✔
1328
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
727,010✔
1329
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
727,010✔
1330
    case QUERY_NODE_SELECT_STMT:
21,581,084✔
1331
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
21,581,084✔
1332
    default:
×
1333
      break;
×
1334
  }
1335
  return TSDB_CODE_FAILED;
×
1336
}
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