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

taosdata / TDengine / #4541

19 Jul 2025 01:13AM UTC coverage: 56.753% (-1.6%) from 58.31%
#4541

push

travis-ci

web-flow
fix: subquery memleak (#32024)

124299 of 282344 branches covered (44.02%)

Branch coverage included in aggregate %.

181106 of 255787 relevant lines covered (70.8%)

24937406.43 hits per line

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

68.56
/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) {
10,874✔
40
  if (NULL == pBlock || NULL == pRsp) {
10,874!
41
    return TSDB_CODE_INVALID_PARA;
×
42
  }
43
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
10,875✔
44
  size_t rspSize = sizeof(SRetrieveTableRsp) + dataEncodeBufSize + PAYLOAD_PREFIX_LEN;
10,875✔
45
  *pRsp = taosMemoryCalloc(1, rspSize);
10,875!
46
  if (NULL == *pRsp) {
10,875!
47
    return terrno;
×
48
  }
49

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

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

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

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

73
  return TSDB_CODE_SUCCESS;
10,873✔
74
}
75

76
static int32_t getSchemaBytes(const SSchema* pSchema) {
183,018✔
77
  switch (pSchema->type) {
183,018✔
78
    case TSDB_DATA_TYPE_BINARY:
13,929✔
79
    case TSDB_DATA_TYPE_VARBINARY:
80
    case TSDB_DATA_TYPE_GEOMETRY:
81
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
13,929✔
82
    case TSDB_DATA_TYPE_NCHAR:
13,550✔
83
    case TSDB_DATA_TYPE_JSON:
84
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
13,550✔
85
    default:
155,539✔
86
      return pSchema->bytes;
155,539✔
87
  }
88
}
89

90
static const char* expandIdentifier(const char* name, char* output) {
3,077✔
91
  if (NULL == name) return "";
3,077!
92
  bool containsEscapeChar = false;
3,077✔
93
  for (const char* p = name; *p != '\0'; ++p) {
13,387✔
94
    if (*p == TS_ESCAPE_CHAR) {
10,670✔
95
      containsEscapeChar = true;
360✔
96
      break;
360✔
97
    }
98
  }
99
  if (!containsEscapeChar) return name;
3,077✔
100
  if (NULL == output) return "";
360!
101
  char* out_ptr = output;
360✔
102
  for (const char* src = name; *src != '\0'; ++src) {
2,216✔
103
    if (*src == TS_ESCAPE_CHAR) {
1,856✔
104
      *out_ptr++ = TS_ESCAPE_CHAR;
904✔
105
      *out_ptr++ = TS_ESCAPE_CHAR;
904✔
106
    } else {
107
      *out_ptr++ = *src;
952✔
108
    }
109
  }
110
  *out_ptr = '\0';
360✔
111
  return output;
360✔
112
}
113

114
static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) {
3,463✔
115
  QRY_PARAM_CHECK(pOutput);
3,463!
116

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

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

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

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

189
  if (hasRefCol(pMeta->tableType)) {
3,464✔
190
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
4✔
191
  }
192

193
  int32_t fillTagCol = 0;
3,464✔
194
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
3,464✔
195
  for (int32_t i = 0; i < numOfRows; ++i) {
186,480✔
196
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
183,015!
197
      continue;
×
198
    }
199
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
183,019✔
200
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
183,019!
201

202
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
183,019!
203
      uint8_t prec = 0, scale = 0;
1,727✔
204
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
1,727✔
205
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
1,727✔
206
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
1,727✔
207
      varDataSetLen(buf, len);
1,727✔
208
    } else {
209
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
181,292✔
210
    }
211
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
183,019!
212
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
183,019✔
213
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
183,018!
214
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
183,017✔
215
      if (i >= pMeta->tableInfo.numOfColumns) {
182,957✔
216
        STR_TO_VARSTR(buf, "TAG");
13,067✔
217
        fillTagCol = 1;
13,067✔
218
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
169,890✔
219
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
4✔
220
      } else {
221
        STR_TO_VARSTR(buf, "");
169,886✔
222
      }
223
    } else {
224
      STR_TO_VARSTR(buf, "VIEW COL");
60✔
225
    }
226
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
183,017!
227
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
183,017!
228
      if (i < pMeta->tableInfo.numOfColumns) {
182,689✔
229
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
169,635✔
230
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
169,636!
231
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
169,637✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
169,636!
233
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
169,635✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
169,635!
235
      } else {
236
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
13,054!
237
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
13,054!
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
13,054!
239
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
13,054!
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
13,054!
241
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
13,054✔
242
      }
243
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
328!
244
      if (i < pMeta->numOfColRefs) {
92✔
245
        if (pMeta->colRef[i].hasRef) {
80✔
246
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
30✔
247
          strcat(refColName, pMeta->colRef[i].refDbName);
30✔
248
          strcat(refColName, ".");
30✔
249
          strcat(refColName, pMeta->colRef[i].refTableName);
30✔
250
          strcat(refColName, ".");
30✔
251
          strcat(refColName, pMeta->colRef[i].refColName);
30✔
252
          STR_TO_VARSTR(buf, refColName);
30✔
253
        } else {
254
          STR_TO_VARSTR(buf, "");
50✔
255
        }
256
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
80!
257
      } else {
258
        STR_TO_VARSTR(buf, "");
12✔
259
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
12!
260
      }
261
    }
262

263
    fillTagCol = 0;
183,016✔
264

265
    ++(pBlock->info.rows);
183,016✔
266
  }
267
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
3,465!
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) {
3,465!
279
    qError("no permission to view any columns");
×
280
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
281
  }
282
  return TSDB_CODE_SUCCESS;
3,465✔
283
}
284

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

292
  SSDataBlock* pBlock = NULL;
3,463✔
293
  int32_t      code = buildDescResultDataBlock(&pBlock);
3,463✔
294
  if (TSDB_CODE_SUCCESS == code) {
3,464!
295
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
3,464✔
296
  }
297
  if (TSDB_CODE_SUCCESS == code) {
3,464!
298
    if (pDesc->pMeta) {
3,464!
299
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
3,464!
300
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
3,423✔
301
      } else if (hasRefCol(pDesc->pMeta->tableType) && pDesc->pMeta->colRef) {
41!
302
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
4✔
303
      } else {
304
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
37✔
305
      }
306
    } else {
307
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
308
    }
309
  }
310
  (void)blockDataDestroy(pBlock);
3,462✔
311
  return code;
3,464✔
312
}
313

314
static int32_t execResetQueryCache() { return catalogClearCache(); }
7,828✔
315

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

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

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

332
  if (TSDB_CODE_SUCCESS == code) {
24!
333
    *pOutput = pBlock;
24✔
334
  } else {
335
    (void)blockDataDestroy(pBlock);
×
336
  }
337
  return code;
24✔
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) {
24✔
366
  size_t size = taosArrayGetSize(pRetension);
24✔
367
  if (size == 0) {
24!
368
    *ppRetentions = NULL;
24✔
369
    return TSDB_CODE_SUCCESS;
24✔
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) {
22✔
399
  switch (cacheModel) {
22!
400
    case TSDB_CACHE_MODEL_NONE:
22✔
401
      return TSDB_CACHE_MODEL_NONE_STR;
22✔
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) {
22✔
415
  switch (encryptAlgorithm) {
22!
416
    case TSDB_ENCRYPT_ALGO_NONE:
22✔
417
      return TSDB_ENCRYPT_ALGO_NONE_STR;
22✔
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) {
9,261,134✔
427
  if (buffer == NULL || bufSize <= 0) {
9,261,134!
428
    return 0;
×
429
  }
430
  int32_t len = 0;
9,268,655✔
431
  if (timeInMinutes % 1440 == 0) {
9,268,655!
432
    int32_t days = timeInMinutes / 1440;
9,274,313✔
433
    len = tsnprintf(buffer, bufSize, "%dd", days);
9,274,313✔
434
  } else if (timeInMinutes % 60 == 0) {
×
435
    int32_t hours = timeInMinutes / 60;
3✔
436
    len = tsnprintf(buffer, bufSize, "%dh", hours);
3✔
437
  } else {
438
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
×
439
  }
440
  return len;
9,329,254✔
441
}
442

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

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

452
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
24✔
453
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
24✔
454
  int32_t          len = 0;
24✔
455
  char*            prec = NULL;
24✔
456
  switch (pCfg->precision) {
24!
457
    case TSDB_TIME_PRECISION_MILLI:
24✔
458
      prec = TSDB_TIME_PRECISION_MILLI_STR;
24✔
459
      break;
24✔
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;
24✔
472
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
24!
473
  int32_t dbFNameLen = strlen(dbFName);
24✔
474
  int32_t hashPrefix = 0;
24✔
475
  if (pCfg->hashPrefix > 0) {
24✔
476
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
2✔
477
  } else if (pCfg->hashPrefix < 0) {
22✔
478
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
3✔
479
  }
480
  char durationStr[128] = {0};
24✔
481
  char keep0Str[128] = {0};
24✔
482
  char keep1Str[128] = {0};
24✔
483
  char keep2Str[128] = {0};
24✔
484
  char compactIntervalStr[13] = {0};
24✔
485
  char compactStartTimeStr[13] = {0};
24✔
486
  char compactEndTimeStr[13] = {0};
24✔
487

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

496
  if (IS_SYS_DBNAME(dbName)) {
24!
497
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
2✔
498
                     "CREATE DATABASE `%s`", dbName);
2✔
499
  } else {
500
    len +=
22✔
501
        tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
22✔
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,
22✔
510
                  pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str, keep1Str, keep2Str,
22✔
511
                  pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel, pCfg->numOfVgroups,
22✔
512
                  1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize, pCfg->walRetentionPeriod,
22✔
513
                  pCfg->walRetentionSize, pCfg->keepTimeOffset, encryptAlgorithmStr(pCfg->encryptAlgorithm),
22✔
514
                  pCfg->ssChunkSize, pCfg->ssKeepLocal, pCfg->ssCompact, compactIntervalStr, compactStartTimeStr,
22✔
515
                  compactEndTimeStr, pCfg->compactTimeOffset);
22✔
516

517
    if (pRetentions) {
22!
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);
24!
524

525
  (varDataLen(buf2)) = len;
24✔
526

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

529
  return TSDB_CODE_SUCCESS;
24✔
530
}
531

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

545
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
334✔
546
  QRY_PARAM_CHECK(pOutput);
334!
547

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

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

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

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

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

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

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

593
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
215✔
594
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
215✔
595
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
1,887✔
596
    SSchema* pSchema = pCfg->pSchemas + i;
1,672✔
597
    SColRef* pRef = pCfg->pColRefs + i;
1,672✔
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];
602
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
1,672✔
603
    int typeLen = strlen(type);
1,672✔
604
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
1,672✔
605
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
1,529✔
606
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
164✔
607
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
1,508✔
608
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
59✔
609
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
59✔
610
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
1,449✔
611
      uint8_t precision, scale;
612
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
120✔
613
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
120✔
614
    }
615

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

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

634
    if (!(pSchema->flags & COL_IS_KEY)) {
1,672✔
635
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,670✔
636
                        "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
1,670✔
637
    } else {
638
      char* pk = "COMPOSITE KEY";
2✔
639
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2!
640
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
2✔
641
    }
642
  }
643
}
215✔
644

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

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

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

666
  }
667
}
10✔
668

669
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
134✔
670
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
134✔
671
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
866✔
672
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
732✔
673
    char     type[32];
674
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
732✔
675
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
732✔
676
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
625✔
677
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
124✔
678
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
124✔
679
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
608✔
680
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
32✔
681
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
32✔
682
    }
683

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

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

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

703
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
89!
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)) {
89✔
709
    char* pJson = NULL;
4✔
710
    parseTagDatatoJson(pTag, &pJson, charsetCxt);
4✔
711
    if (NULL == pJson) {
4!
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),
4✔
716
                      "%s", pJson);
717
    taosMemoryFree(pJson);
4!
718

719
    return TSDB_CODE_SUCCESS;
4✔
720
  }
721

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

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

739
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
105✔
740
    if (pSchema->colId > pTagVal->cid) {
105!
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) {
105✔
745
      char    type = pTagVal->type;
103✔
746
      int32_t tlen = 0;
103✔
747

748
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
103✔
749
      if (leftSize <= 0) {
103!
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)) {
103!
755
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
18✔
756
        TAOS_CHECK_ERRNO(code);
18!
757
      } else {
758
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
85✔
759
                               &tlen);
760
        TAOS_CHECK_ERRNO(code);
85!
761
      }
762
      *len += tlen;
103✔
763
      j++;
103✔
764
    } else {
765
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2✔
766
                        "NULL");
767
    }
768
  }
769
_exit:
85✔
770
  taosArrayDestroy(pTagVals);
85✔
771

772
  return code;
85✔
773
}
774

775
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
284✔
776
  if (pCfg->commentLen > 0) {
284!
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) {
284!
780
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
781
                      " COMMENT ''");
782
  }
783

784
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
284!
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) {
284!
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);
284✔
803
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
284!
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) {
284!
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) {
284✔
821
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
4✔
822
                      " KEEP %dm", pCfg->keep);
823
  }
824

825
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
284✔
826
    int32_t nSma = 0;
205✔
827
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
1,813✔
828
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
1,608!
829
        ++nSma;
1,608✔
830
      }
831
    }
832

833
    if (nSma < pCfg->numOfColumns && nSma > 0) {
205!
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) {
284✔
856
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
8✔
857
                      " VIRTUAL %d", pCfg->virtualStb);
8✔
858
  }
859
}
284✔
860

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

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

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

878
  int32_t len = 0;
334✔
879

880
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
334✔
881
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
134✔
882
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
883
    appendColumnFields(buf2, &len, pCfg);
134✔
884
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
134✔
885
                     ") TAGS (");
886
    appendTagFields(buf2, &len, pCfg);
134✔
887
    len +=
134✔
888
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
134✔
889
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
134✔
890
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
200✔
891
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
79✔
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),
79✔
894
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
79✔
895
    appendTagNameFields(buf2, &len, pCfg);
79✔
896
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
79✔
897
                     ") TAGS (");
898
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
79✔
899
    TAOS_CHECK_ERRNO(code);
79!
900
    len +=
79✔
901
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
79✔
902
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
79✔
903
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
121✔
904
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
71✔
905
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
906
    appendColumnFields(buf2, &len, pCfg);
71✔
907
    len +=
71✔
908
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
71✔
909
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
71✔
910
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
50✔
911
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
10✔
912
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
913
    appendColumnFields(buf2, &len, pCfg);
10✔
914
    len +=
10✔
915
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
10✔
916
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
40✔
917
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
10✔
918
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
919
    appendColRefFields(buf2, &len, pCfg);
10✔
920
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
20✔
921
                    ") USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
10✔
922
    appendTagNameFields(buf2, &len, pCfg);
10✔
923
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
10✔
924
                     ") TAGS (");
925
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
10✔
926
    TAOS_CHECK_ERRNO(code);
10!
927
    len +=
10✔
928
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
10✔
929
  }
930

931
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
334✔
932

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

936
_exit:
334✔
937
  taosMemoryFree(buf2);
334!
938

939
  return code;
334✔
940
}
941

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

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

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

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

971
  return code;
15✔
972
}
973

974
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
318✔
975
  SSDataBlock* pBlock = NULL;
318✔
976
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
318✔
977
  if (TSDB_CODE_SUCCESS == code) {
318!
978
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
318✔
979
  }
980
  if (TSDB_CODE_SUCCESS == code) {
318!
981
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
318✔
982
  }
983
  (void)blockDataDestroy(pBlock);
318✔
984
  return code;
318✔
985
}
986

987
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
16✔
988
  SSDataBlock* pBlock = NULL;
16✔
989
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
16✔
990
  if (TSDB_CODE_SUCCESS == code) {
16!
991
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
16✔
992
  }
993
  if (TSDB_CODE_SUCCESS == code) {
16!
994
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
16✔
995
  }
996
  (void)blockDataDestroy(pBlock);
16✔
997
  return code;
16✔
998
}
999

1000
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
3✔
1001
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
3✔
1002
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
3✔
1003
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
1✔
1004
    return terrno;
1✔
1005
  }
1006

1007
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
2✔
1008
}
1009

1010
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
649✔
1011
  int32_t code = 0;
649✔
1012

1013
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
649✔
1014
    taosResetLog();
1✔
1015
    cfgDumpCfg(tsCfg, 0, false);
1✔
1016
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
648✔
1017
    int32_t tmp = 0;
40✔
1018
    code = taosStr2int32(value, &tmp);
40✔
1019
    if (code) {
40!
1020
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1021
      return code;
×
1022
    }
1023
    code = schedulerUpdatePolicy(tmp);
40✔
1024
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
608!
1025
    int32_t tmp = 0;
×
1026
    code = taosStr2int32(value, &tmp);
×
1027
    if (code) {
×
1028
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1029
      return code;
×
1030
    }
1031
    code = schedulerEnableReSchedule(tmp != 0);
×
1032
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
608!
1033
    code = ctgdHandleDbgCommand(value);
×
1034
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
608!
1035
    code = taosMemoryDbgInit();
×
1036
    if (code) {
×
1037
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
1038
      return code;
×
1039
    }
1040
    tsAsyncLog = false;
×
1041
    qInfo("memory dbg enabled");
×
1042
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
608!
1043
    code = taosMemoryDbgInitRestore();
×
1044
    if (code) {
×
1045
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
1046
      return code;
×
1047
    }
1048
    qInfo("memory dbg disabled");
×
1049
  } else {
1050
    goto _return;
608✔
1051
  }
1052

1053
  *processed = true;
41✔
1054

1055
_return:
649✔
1056

1057
  if (code) {
649!
1058
    terrno = code;
×
1059
  }
1060

1061
  return code;
649✔
1062
}
1063

1064
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
649✔
1065
  bool processed = false;
649✔
1066

1067
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
649!
1068
    return terrno;
×
1069
  }
1070

1071
  if (processed) {
649✔
1072
    goto _return;
41✔
1073
  }
1074

1075
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
608✔
1076
    return terrno;
7✔
1077
  }
1078

1079
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
601!
1080
    return terrno;
×
1081
  }
1082

1083
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
601!
1084

1085
_return:
601✔
1086

1087
  return TSDB_CODE_SUCCESS;
642✔
1088
}
1089

1090
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
572✔
1091
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
572!
1092
  if (NULL == pBlock) {
572!
1093
    return terrno;
×
1094
  }
1095

1096
  pBlock->info.hasVarCol = true;
572✔
1097

1098
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
572✔
1099
  if (NULL == pBlock->pDataBlock) {
572!
1100
    taosMemoryFree(pBlock);
×
1101
    return terrno;
×
1102
  }
1103

1104
  SColumnInfoData infoData = {0};
572✔
1105

1106
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
572✔
1107
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
572✔
1108
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,144!
1109
    goto _exit;
×
1110
  }
1111

1112
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
572✔
1113
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
572✔
1114
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,144!
1115
    goto _exit;
×
1116
  }
1117

1118
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
572✔
1119
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
572✔
1120
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,144!
1121
    goto _exit;
×
1122
  }
1123

1124
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
572✔
1125
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
572✔
1126
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,144!
1127
    goto _exit;
×
1128
  }
1129

1130
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
572✔
1131
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
572✔
1132
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,144!
1133
    goto _exit;
×
1134
  }
1135

1136
  *pOutput = pBlock;
572✔
1137

1138
_exit:
572✔
1139
  if (terrno != TSDB_CODE_SUCCESS) {
572!
1140
    taosArrayDestroy(pBlock->pDataBlock);
×
1141
    taosMemoryFree(pBlock);
×
1142
  }
1143
  return terrno;
572✔
1144
}
1145

1146
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
572✔
1147
  SSDataBlock* pBlock = NULL;
572✔
1148
  char*        likePattern = NULL;
572✔
1149
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
572✔
1150
  if (TSDB_CODE_SUCCESS == code) {
572!
1151
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
572✔
1152
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
7✔
1153
    }
1154
  }
1155
  if (TSDB_CODE_SUCCESS == code) {
572!
1156
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
572✔
1157
  }
1158
  if (TSDB_CODE_SUCCESS == code) {
572!
1159
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
572✔
1160
  }
1161
  (void)blockDataDestroy(pBlock);
572✔
1162
  return code;
572✔
1163
}
1164

1165
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
6,466✔
1166
  QRY_PARAM_CHECK(pOutput);
6,466!
1167

1168
  SSDataBlock* pBlock = NULL;
6,466✔
1169
  int32_t      code = createDataBlock(&pBlock);
6,466✔
1170
  if (code) {
6,466!
1171
    return code;
×
1172
  }
1173

1174
  SNode* pProj = NULL;
6,466✔
1175
  FOREACH(pProj, pProjects) {
16,969!
1176
    SExprNode*      pExpr = (SExprNode*)pProj;
10,503✔
1177
    SColumnInfoData infoData = {0};
10,503✔
1178
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
10,503!
1179
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1180
      infoData.info.bytes = 0;
×
1181
    } else {
1182
      infoData.info.type = pExpr->resType.type;
10,503✔
1183
      infoData.info.bytes = pExpr->resType.bytes;
10,503✔
1184
      infoData.info.precision = pExpr->resType.precision;
10,503✔
1185
      infoData.info.scale = pExpr->resType.scale;
10,503✔
1186
    }
1187
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
10,503!
1188
  }
1189

1190
  *pOutput = pBlock;
6,466✔
1191
  return code;
6,466✔
1192
}
1193

1194
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
6,466✔
1195
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
6,466!
1196

1197
  int32_t index = 0;
6,466✔
1198
  SNode*  pProj = NULL;
6,466✔
1199
  FOREACH(pProj, pProjects) {
16,969!
1200
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
10,503!
1201
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1202
    } else {
1203
      if (((SValueNode*)pProj)->isNull) {
10,503✔
1204
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
141!
1205
      } else {
1206
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
10,362!
1207
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1208
      }
1209
    }
1210
  }
1211

1212
  pBlock->info.rows = 1;
6,466✔
1213
  return TSDB_CODE_SUCCESS;
6,466✔
1214
}
1215

1216
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
6,466✔
1217
  SSDataBlock* pBlock = NULL;
6,466✔
1218
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
6,466✔
1219
  if (TSDB_CODE_SUCCESS == code) {
6,466!
1220
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
6,466✔
1221
  }
1222
  if (TSDB_CODE_SUCCESS == code) {
6,466!
1223
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
6,466!
1224
  }
1225
  (void)blockDataDestroy(pBlock);
6,466✔
1226
  return code;
6,466✔
1227
}
1228

1229
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
15✔
1230
  SSDataBlock* pBlock = NULL;
15✔
1231
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
15✔
1232
  if (TSDB_CODE_SUCCESS == code) {
15!
1233
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
15✔
1234
  }
1235
  if (TSDB_CODE_SUCCESS == code) {
15!
1236
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
15✔
1237
  }
1238
  (void)blockDataDestroy(pBlock);
15✔
1239
  return code;
15✔
1240
}
1241

1242
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
19,352✔
1243
                     void* charsetCxt) {
1244
  switch (nodeType(pStmt)) {
19,352!
1245
    case QUERY_NODE_DESCRIBE_STMT:
3,463✔
1246
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
3,463✔
1247
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
7,828✔
1248
      return execResetQueryCache();
7,828✔
1249
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
24✔
1250
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
24✔
1251
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
316✔
1252
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
316✔
1253
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
16✔
1254
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
16✔
1255
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
3✔
1256
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
3✔
1257
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
15✔
1258
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
15✔
1259
    case QUERY_NODE_ALTER_LOCAL_STMT:
649✔
1260
      return execAlterLocal((SAlterLocalStmt*)pStmt);
649✔
1261
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
572✔
1262
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
572✔
1263
    case QUERY_NODE_SELECT_STMT:
6,466✔
1264
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
6,466✔
1265
    default:
×
1266
      break;
×
1267
  }
1268
  return TSDB_CODE_FAILED;
×
1269
}
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