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

taosdata / TDengine / #4953

09 Feb 2026 01:16AM UTC coverage: 66.877% (+0.008%) from 66.869%
#4953

push

travis-ci

web-flow
docs: add support for recording STMT to CSV files (#34276)

* docs: add support for recording STMT to CSV files

* docs: update version for STMT recording feature in CSV files

205779 of 307696 relevant lines covered (66.88%)

126028030.19 hits per line

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

80.35
/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 "osMemory.h"
21
#include "osString.h"
22
#include "scheduler.h"
23
#include "systable.h"
24
#include "taosdef.h"
25
#include "tdatablock.h"
26
#include "tdataformat.h"
27
#include "tglobal.h"
28
#include "tgrant.h"
29

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

39
extern SConfig* tsCfg;
40

41
static int32_t buildRetrieveTableRsp(SSDataBlock* pBlock, int32_t numOfCols, SRetrieveTableRsp** pRsp) {
3,005,487✔
42
  if (NULL == pBlock || NULL == pRsp) {
3,005,487✔
43
    return TSDB_CODE_INVALID_PARA;
×
44
  }
45
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
3,005,487✔
46
  size_t rspSize = sizeof(SRetrieveTableRsp) + dataEncodeBufSize + PAYLOAD_PREFIX_LEN;
3,005,451✔
47
  *pRsp = taosMemoryCalloc(1, rspSize);
3,005,451✔
48
  if (NULL == *pRsp) {
3,005,487✔
49
    return terrno;
×
50
  }
51

52
  (*pRsp)->useconds = 0;
3,005,487✔
53
  (*pRsp)->completed = 1;
3,005,487✔
54
  (*pRsp)->precision = 0;
3,005,487✔
55
  (*pRsp)->compressed = 0;
3,005,487✔
56

57
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
3,005,487✔
58
  (*pRsp)->numOfCols = htonl(numOfCols);
3,005,487✔
59

60
  int32_t len = 0;
3,005,487✔
61
  if (pBlock->info.rows > 0) {
3,005,487✔
62
    len = blockEncode(pBlock, (*pRsp)->data + PAYLOAD_PREFIX_LEN, dataEncodeBufSize, numOfCols);
3,005,261✔
63
    if (len < 0) {
3,005,227✔
64
      taosMemoryFree(*pRsp);
×
65
      *pRsp = NULL;
×
66
      return terrno;
×
67
    }
68
    SET_PAYLOAD_LEN((*pRsp)->data, len, len);
3,005,227✔
69
  }
70

71
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
3,005,487✔
72
  (*pRsp)->payloadLen = htonl(payloadLen);
3,005,487✔
73
  (*pRsp)->compLen = htonl(payloadLen);
3,005,453✔
74

75
  return TSDB_CODE_SUCCESS;
3,005,453✔
76
}
77

78
static int32_t getSchemaBytes(const SSchema* pSchema) {
58,346,300✔
79
  switch (pSchema->type) {
58,346,300✔
80
    case TSDB_DATA_TYPE_BINARY:
2,326,047✔
81
    case TSDB_DATA_TYPE_VARBINARY:
82
    case TSDB_DATA_TYPE_GEOMETRY:
83
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
2,326,047✔
84
    case TSDB_DATA_TYPE_NCHAR:
4,066,318✔
85
    case TSDB_DATA_TYPE_JSON:
86
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
4,066,318✔
87
    default:
51,953,935✔
88
      return pSchema->bytes;
51,953,935✔
89
  }
90
}
91

92
static const char* expandIdentifier(const char* name, char* output) {
579,760✔
93
  if (NULL == name) return "";
579,760✔
94
  bool containsEscapeChar = false;
579,760✔
95
  for (const char* p = name; *p != '\0'; ++p) {
4,274,755✔
96
    if (*p == TS_ESCAPE_CHAR) {
3,740,805✔
97
      containsEscapeChar = true;
45,810✔
98
      break;
45,810✔
99
    }
100
  }
101
  if (!containsEscapeChar) return name;
579,760✔
102
  if (NULL == output) return "";
45,810✔
103
  char* out_ptr = output;
45,810✔
104
  for (const char* src = name; *src != '\0'; ++src) {
281,986✔
105
    if (*src == TS_ESCAPE_CHAR) {
236,176✔
106
      *out_ptr++ = TS_ESCAPE_CHAR;
115,034✔
107
      *out_ptr++ = TS_ESCAPE_CHAR;
115,034✔
108
    } else {
109
      *out_ptr++ = *src;
121,142✔
110
    }
111
  }
112
  *out_ptr = '\0';
45,810✔
113
  return output;
45,810✔
114
}
115

116
static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) {
411,846✔
117
  QRY_PARAM_CHECK(pOutput);
411,846✔
118

119
  SSDataBlock* pBlock = NULL;
411,880✔
120
  int32_t      code = createDataBlock(&pBlock);
411,880✔
121
  if (code) {
411,880✔
122
    return code;
×
123
  }
124

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

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

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

191
  if (hasRefCol(pMeta->tableType)) {
411,880✔
192
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
2,849✔
193
  }
194

195
  int32_t fillTagCol = 0;
411,880✔
196
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
411,880✔
197
  for (int32_t i = 0; i < numOfRows; ++i) {
58,758,144✔
198
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
58,346,264✔
199
      continue;
×
200
    }
201
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
58,346,264✔
202
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
58,346,264✔
203

204
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
58,346,264✔
205
      uint8_t prec = 0, scale = 0;
249,045✔
206
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
249,045✔
207
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
249,045✔
208
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
249,045✔
209
      varDataSetLen(buf, len);
249,045✔
210
    } else {
211
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
58,097,219✔
212
    }
213
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
58,346,264✔
214
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
58,346,300✔
215
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
58,346,300✔
216
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
58,346,300✔
217
      if (i >= pMeta->tableInfo.numOfColumns) {
58,317,460✔
218
        STR_TO_VARSTR(buf, "TAG");
2,837,944✔
219
        fillTagCol = 1;
2,837,944✔
220
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
55,479,516✔
221
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
10,176✔
222
      } else {
223
        STR_TO_VARSTR(buf, "");
55,469,340✔
224
      }
225
    } else {
226
      STR_TO_VARSTR(buf, "VIEW COL");
28,840✔
227
    }
228
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
58,346,300✔
229
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
58,346,300✔
230
      if (i < pMeta->tableInfo.numOfColumns) {
58,263,439✔
231
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
55,430,805✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
55,430,733✔
233
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
55,430,735✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
55,430,805✔
235
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
55,430,805✔
236
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
55,430,701✔
237
      } else {
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,832,634✔
239
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
2,832,532✔
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,832,634✔
241
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
2,832,634✔
242
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,832,634✔
243
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
2,832,634✔
244
      }
245
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
82,861✔
246
      if (i < pMeta->numOfColRefs) {
35,575✔
247
        if (pMeta->colRef[i].hasRef) {
30,265✔
248
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
13,041✔
249
          strcat(refColName, pMeta->colRef[i].refDbName);
13,041✔
250
          strcat(refColName, ".");
13,041✔
251
          strcat(refColName, pMeta->colRef[i].refTableName);
13,041✔
252
          strcat(refColName, ".");
13,041✔
253
          strcat(refColName, pMeta->colRef[i].refColName);
13,041✔
254
          STR_TO_VARSTR(buf, refColName);
13,041✔
255
        } else {
256
          STR_TO_VARSTR(buf, "");
17,224✔
257
        }
258
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
30,265✔
259
      } else {
260
        STR_TO_VARSTR(buf, "");
5,310✔
261
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
5,310✔
262
      }
263
    }
264

265
    fillTagCol = 0;
58,346,264✔
266

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

287
static int32_t execDescribe(bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode) {
411,810✔
288
  SDescribeStmt* pDesc = (SDescribeStmt*)pStmt;
411,810✔
289
  if (NULL == pDesc || NULL == pDesc->pMeta) {
411,810✔
290
    return TSDB_CODE_INVALID_PARA;
×
291
  }
292
  int32_t numOfRows = TABLE_TOTAL_COL_NUM(pDesc->pMeta);
411,880✔
293

294
  SSDataBlock* pBlock = NULL;
411,880✔
295
  int32_t      code = buildDescResultDataBlock(&pBlock);
411,880✔
296
  if (TSDB_CODE_SUCCESS == code) {
411,880✔
297
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
411,880✔
298
  }
299
  if (TSDB_CODE_SUCCESS == code) {
411,880✔
300
    if (pDesc->pMeta) {
411,880✔
301
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
411,880✔
302
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
399,045✔
303
      } else if (hasRefCol(pDesc->pMeta->tableType) && pDesc->pMeta->colRef) {
12,835✔
304
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
2,849✔
305
      } else {
306
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
9,986✔
307
      }
308
    } else {
309
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
310
    }
311
  }
312
  (void)blockDataDestroy(pBlock);
411,880✔
313
  return code;
411,880✔
314
}
315

316
static int32_t execResetQueryCache() { return catalogClearCache(); }
2,015,133✔
317

318
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
62,041✔
319
  QRY_PARAM_CHECK(pOutput);
62,041✔
320

321
  SSDataBlock* pBlock = NULL;
62,041✔
322
  int32_t      code = createDataBlock(&pBlock);
62,041✔
323
  if (code) {
62,041✔
324
    return code;
×
325
  }
326

327
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_COLS, 1);
62,041✔
328
  code = blockDataAppendColInfo(pBlock, &infoData);
62,041✔
329
  if (TSDB_CODE_SUCCESS == code) {
62,041✔
330
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_FIELD2_LEN, 2);
62,041✔
331
    code = blockDataAppendColInfo(pBlock, &infoData);
62,041✔
332
  }
333

334
  if (TSDB_CODE_SUCCESS == code) {
62,041✔
335
    *pOutput = pBlock;
62,041✔
336
  } else {
337
    (void)blockDataDestroy(pBlock);
×
338
  }
339
  return code;
62,041✔
340
}
341

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

364
  return v;
×
365
}
366

367
static int32_t buildRetension(SArray* pRetension, char** ppRetentions) {
62,041✔
368
  size_t size = taosArrayGetSize(pRetension);
62,041✔
369
  if (size == 0) {
62,041✔
370
    *ppRetentions = NULL;
62,041✔
371
    return TSDB_CODE_SUCCESS;
62,041✔
372
  }
373

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

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

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

396
  *ppRetentions = p1;
×
397
  return TSDB_CODE_SUCCESS;
×
398
}
399

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

416
static const char* encryptAlgorithmStr(int8_t encryptAlgorithm, char* algorithmsId) {
61,695✔
417
  if (algorithmsId[0] != '\0') {
61,695✔
418
    return algorithmsId;
×
419
  } else if (encryptAlgorithm != 0) {
61,695✔
420
    switch (encryptAlgorithm) {
×
421
      case TSDB_ENCRYPT_ALGO_NONE:
×
422
        return TSDB_ENCRYPT_ALGO_NONE_STR;
×
423
      case TSDB_ENCRYPT_ALGO_SM4:
×
424
        return TSDB_ENCRYPT_ALGO_SM4_STR;
×
425
      default:
×
426
        break;
×
427
    }
428
    return TSDB_CACHE_MODEL_NONE_STR;
×
429
  }
430

431
  return TSDB_CACHE_MODEL_NONE_STR;
61,695✔
432
}
433

434
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
4,052,979✔
435
  if (buffer == NULL || bufSize <= 0) {
4,052,979✔
436
    return 0;
×
437
  }
438
  int32_t len = 0;
4,052,979✔
439
  if (timeInMinutes % 1440 == 0) {
4,052,979✔
440
    int32_t days = timeInMinutes / 1440;
4,041,590✔
441
    len = tsnprintf(buffer, bufSize, "%dd", days);
4,041,590✔
442
  } else if (timeInMinutes % 60 == 0) {
11,389✔
443
    int32_t hours = timeInMinutes / 60;
1,954✔
444
    len = tsnprintf(buffer, bufSize, "%dh", hours);
1,954✔
445
  } else {
446
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
9,435✔
447
  }
448
  return len;
4,052,979✔
449
}
450

451
static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, char* dbFName, SDbCfgInfo* pCfg) {
62,041✔
452
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
62,041✔
453
  pBlock->info.rows = 1;
62,041✔
454

455
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
62,041✔
456
  char             buf1[SHOW_CREATE_DB_RESULT_FIELD1_LEN] = {0};
62,041✔
457
  STR_TO_VARSTR(buf1, dbName);
62,041✔
458
  COL_DATA_SET_VAL_AND_CHECK(pCol1, 0, buf1, false);
62,041✔
459

460
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
62,041✔
461
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
62,041✔
462
  int32_t          len = 0;
62,041✔
463
  char*            prec = NULL;
62,041✔
464
  switch (pCfg->precision) {
62,041✔
465
    case TSDB_TIME_PRECISION_MILLI:
62,041✔
466
      prec = TSDB_TIME_PRECISION_MILLI_STR;
62,041✔
467
      break;
62,041✔
468
    case TSDB_TIME_PRECISION_MICRO:
×
469
      prec = TSDB_TIME_PRECISION_MICRO_STR;
×
470
      break;
×
471
    case TSDB_TIME_PRECISION_NANO:
×
472
      prec = TSDB_TIME_PRECISION_NANO_STR;
×
473
      break;
×
474
    default:
×
475
      prec = "none";
×
476
      break;
×
477
  }
478

479
  char* pRetentions = NULL;
62,041✔
480
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
62,041✔
481
  int32_t dbFNameLen = strlen(dbFName);
62,041✔
482
  int32_t hashPrefix = 0;
62,041✔
483
  if (pCfg->hashPrefix > 0) {
62,041✔
484
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
1,440✔
485
  } else if (pCfg->hashPrefix < 0) {
60,601✔
486
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
2,160✔
487
  }
488
  char durationStr[128] = {0};
62,041✔
489
  char keep0Str[128] = {0};
62,041✔
490
  char keep1Str[128] = {0};
62,041✔
491
  char keep2Str[128] = {0};
62,041✔
492
  char compactIntervalStr[13] = {0};
62,041✔
493
  char compactStartTimeStr[13] = {0};
62,041✔
494
  char compactEndTimeStr[13] = {0};
62,041✔
495

496
  int32_t lenDuration = formatDurationOrKeep(durationStr, sizeof(durationStr), pCfg->daysPerFile);
62,041✔
497
  int32_t lenKeep0 = formatDurationOrKeep(keep0Str, sizeof(keep0Str), pCfg->daysToKeep0);
62,041✔
498
  int32_t lenKeep1 = formatDurationOrKeep(keep1Str, sizeof(keep1Str), pCfg->daysToKeep1);
62,041✔
499
  int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pCfg->daysToKeep2);
62,041✔
500
  UNUSED(formatDurationOrKeep(compactIntervalStr, sizeof(compactIntervalStr), pCfg->compactInterval));
62,041✔
501
  UNUSED(formatDurationOrKeep(compactStartTimeStr, sizeof(compactStartTimeStr), pCfg->compactStartTime));
62,041✔
502
  UNUSED(formatDurationOrKeep(compactEndTimeStr, sizeof(compactEndTimeStr), pCfg->compactEndTime));
62,041✔
503

504
  if (IS_SYS_DBNAME(dbName)) {
62,041✔
505
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
346✔
506
                     "CREATE DATABASE `%s`", dbName);
346✔
507
  } else {
508
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
370,170✔
509
                     "CREATE DATABASE `%s` BUFFER %d CACHESIZE %d CACHEMODEL '%s' COMP %d DURATION %s "
510
                     "WAL_FSYNC_PERIOD %d MAXROWS %d MINROWS %d STT_TRIGGER %d KEEP %s,%s,%s PAGES %d PAGESIZE %d "
511
                     "PRECISION '%s' REPLICA %d "
512
                     "WAL_LEVEL %d VGROUPS %d SINGLE_STABLE %d TABLE_PREFIX %d TABLE_SUFFIX %d TSDB_PAGESIZE %d "
513
                     "WAL_RETENTION_PERIOD %d WAL_RETENTION_SIZE %" PRId64
514
                     " KEEP_TIME_OFFSET %d ENCRYPT_ALGORITHM '%s' SS_CHUNKPAGES %d SS_KEEPLOCAL %dm SS_COMPACT %d "
515
                     "COMPACT_INTERVAL %s COMPACT_TIME_RANGE %s,%s COMPACT_TIME_OFFSET %" PRIi8 "h IS_AUDIT %d",
516
                     dbName, pCfg->buffer, pCfg->cacheSize, cacheModelStr(pCfg->cacheLast), pCfg->compression,
61,695✔
517
                     durationStr, pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str,
61,695✔
518
                     keep1Str, keep2Str, pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel,
61,695✔
519
                     pCfg->numOfVgroups, 1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize,
61,695✔
520
                     pCfg->walRetentionPeriod, pCfg->walRetentionSize, pCfg->keepTimeOffset,
521
                     encryptAlgorithmStr(pCfg->encryptAlgr, pCfg->algorithmsId), pCfg->ssChunkSize, pCfg->ssKeepLocal,
61,695✔
522
                     pCfg->ssCompact, compactIntervalStr, compactStartTimeStr, compactEndTimeStr,
61,695✔
523
                     pCfg->compactTimeOffset, pCfg->isAudit);
61,695✔
524

525
    if (pRetentions) {
61,695✔
526
      len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
527
                       " RETENTIONS %s", pRetentions);
528
    }
529
  }
530

531
  taosMemoryFree(pRetentions);
62,041✔
532

533
  (varDataLen(buf2)) = len;
62,041✔
534

535
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
62,041✔
536

537
  return TSDB_CODE_SUCCESS;
62,041✔
538
}
539

540
static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt, SRetrieveTableRsp** pRsp) {
62,041✔
541
  SSDataBlock* pBlock = NULL;
62,041✔
542
  int32_t      code = buildCreateDBResultDataBlock(&pBlock);
62,041✔
543
  if (TSDB_CODE_SUCCESS == code) {
62,041✔
544
    code = setCreateDBResultIntoDataBlock(pBlock, pStmt->dbName, pStmt->dbFName, pStmt->pCfg);
62,041✔
545
  }
546
  if (TSDB_CODE_SUCCESS == code) {
62,041✔
547
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_DB_RESULT_COLS, pRsp);
62,041✔
548
  }
549
  (void)blockDataDestroy(pBlock);
62,041✔
550
  return code;
62,041✔
551
}
552

553
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
81,427✔
554
  QRY_PARAM_CHECK(pOutput);
81,427✔
555

556
  SSDataBlock* pBlock = NULL;
81,427✔
557
  int32_t      code = createDataBlock(&pBlock);
81,427✔
558
  if (code) {
81,427✔
559
    return code;
×
560
  }
561

562
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD1_LEN, 1);
81,427✔
563
  code = blockDataAppendColInfo(pBlock, &infoData);
81,427✔
564
  if (TSDB_CODE_SUCCESS == code) {
81,427✔
565
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD2_LEN, 2);
81,427✔
566
    code = blockDataAppendColInfo(pBlock, &infoData);
81,427✔
567
  }
568

569
  if (TSDB_CODE_SUCCESS == code) {
81,427✔
570
    *pOutput = pBlock;
81,427✔
571
  } else {
572
    (void)blockDataDestroy(pBlock);
×
573
  }
574
  return code;
81,427✔
575
}
576

577
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
5,798✔
578
  QRY_PARAM_CHECK(pOutput);
5,798✔
579

580
  SSDataBlock* pBlock = NULL;
5,798✔
581
  int32_t      code = createDataBlock(&pBlock);
5,798✔
582
  if (code) {
5,798✔
583
    return code;
×
584
  }
585

586
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD1_LEN, 1);
5,798✔
587
  code = blockDataAppendColInfo(pBlock, &infoData);
5,798✔
588
  if (TSDB_CODE_SUCCESS == code) {
5,798✔
589
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD2_LEN, 2);
5,798✔
590
    code = blockDataAppendColInfo(pBlock, &infoData);
5,798✔
591
  }
592

593
  if (TSDB_CODE_SUCCESS == code) {
5,798✔
594
    *pOutput = pBlock;
5,798✔
595
  } else {
596
    (void)blockDataDestroy(pBlock);
×
597
  }
598
  return code;
5,798✔
599
}
600

601
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
39,158✔
602
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
39,158✔
603
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
250,150✔
604
    SSchema* pSchema = pCfg->pSchemas + i;
210,992✔
605
    SColRef* pRef = pCfg->pColRefs + i;
210,992✔
606
#define LTYPE_LEN                                    \
607
  (32 + 60 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN + \
608
   10)  // 60 byte for compress info, TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN for column ref
609
    char type[LTYPE_LEN];
210,992✔
610
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
210,992✔
611
    int typeLen = strlen(type);
210,992✔
612
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
210,992✔
613
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
185,781✔
614
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
26,577✔
615
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
184,415✔
616
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
8,099✔
617
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
8,099✔
618
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
176,316✔
619
      uint8_t precision, scale;
17,288✔
620
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
17,288✔
621
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
17,288✔
622
    }
623

624
    if (withExtSchema(pCfg->tableType) && pCfg->pSchemaExt && tsShowFullCreateTableColumn) {
210,992✔
625
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " ENCODE \'%s\'",
22,022✔
626
                           columnEncodeStr(COMPRESS_L1_TYPE_U32(pCfg->pSchemaExt[i].compress)));
22,022✔
627
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " COMPRESS \'%s\'",
22,022✔
628
                           columnCompressStr(COMPRESS_L2_TYPE_U32(pCfg->pSchemaExt[i].compress)));
22,022✔
629
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " LEVEL \'%s\'",
22,022✔
630
                           columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pCfg->pSchemaExt[i].compress)));
22,022✔
631
    }
632

633
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
210,992✔
634
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " FROM `%s`", pRef->refDbName);
23,160✔
635
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
23,160✔
636
      typeLen +=
23,160✔
637
          tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
23,160✔
638
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
23,160✔
639
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
23,160✔
640
    }
641

642
    if (!(pSchema->flags & COL_IS_KEY)) {
210,992✔
643
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
202,144✔
644
                        "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
202,144✔
645
    } else {
646
      char* pk = "COMPOSITE KEY";
8,848✔
647
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
8,848✔
648
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
8,848✔
649
    }
650
  }
651
}
39,158✔
652

653
static void appendColRefFields(char* buf, int32_t* len, STableCfg* pCfg) {
4,109✔
654
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
4,109✔
655
  bool firstRef = true;
4,109✔
656
  for (int32_t i = 1; i < pCfg->numOfColumns; ++i) {
54,464✔
657
    SSchema* pSchema = pCfg->pSchemas + i;
50,355✔
658
    SColRef* pRef = pCfg->pColRefs + i;
50,355✔
659
    char     type[TSDB_COL_NAME_LEN + 10 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN];
50,355✔
660
    int      typeLen = 0;
50,355✔
661

662
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
50,355✔
663
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
29,068✔
664
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
29,068✔
665
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
29,068✔
666
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
29,068✔
667
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
29,068✔
668
    } else {
669
      continue;
21,287✔
670
    }
671

672
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
29,068✔
673
                      "%s`%s` %s", (!firstRef ? ", " : " ("), expandIdentifier(pSchema->name, expandName), type);
29,068✔
674
    firstRef = false;
29,068✔
675
  }
676
  if (!firstRef) {
4,109✔
677
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
3,512✔
678
  }
679
}
4,109✔
680

681
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
22,826✔
682
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
22,826✔
683
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
68,953✔
684
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
46,127✔
685
    char     type[32];
46,127✔
686
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
46,127✔
687
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
46,127✔
688
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
37,447✔
689
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
8,738✔
690
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
8,738✔
691
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
37,389✔
692
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
1,305✔
693
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
1,305✔
694
    }
695

696
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
46,127✔
697
                      "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
46,127✔
698
  }
699
}
22,826✔
700

701
static void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
35,615✔
702
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
35,615✔
703
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
98,972✔
704
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
63,357✔
705
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
63,357✔
706
                      "%s`%s`", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName));
63,357✔
707
  }
708
}
35,615✔
709

710
static int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg, void* charsetCxt) {
35,615✔
711
  int32_t code = TSDB_CODE_SUCCESS;
35,615✔
712
  SArray* pTagVals = NULL;
35,615✔
713
  STag*   pTag = (STag*)pCfg->pTags;
35,615✔
714

715
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
35,615✔
716
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
717
    return TSDB_CODE_APP_ERROR;
×
718
  }
719

720
  if (tTagIsJson(pTag)) {
35,615✔
721
    char* pJson = NULL;
2,310✔
722
    parseTagDatatoJson(pTag, &pJson, charsetCxt);
2,310✔
723
    if (NULL == pJson) {
2,310✔
724
      qError("failed to parse tag to json, pJson is NULL");
×
725
      return terrno;
×
726
    }
727
    char* escapedJson = taosMemCalloc(sizeof(char), strlen(pJson) * 2 + 1);  // taosMemoryAlloc(strlen(pJson) * 2 + 1);
2,310✔
728
    if (escapedJson) {
2,310✔
729
      char* writer = escapedJson;
2,310✔
730
      for (char* reader = pJson; *reader; ++reader) {
46,200✔
731
        if (*reader == '\'') {
43,890✔
732
          *writer++ = '\'';  // Escape single quote by doubling it
462✔
733
        }
734
        *writer++ = *reader;
43,890✔
735
      }
736
      *writer = '\0';
2,310✔
737

738
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,310✔
739
                        "'%s'", escapedJson);
740
      taosMemoryFree(escapedJson);
2,310✔
741
    } else {
742
      taosMemoryFree(pJson);
×
743
      return terrno;
×
744
    }
745
    taosMemoryFree(pJson);
2,310✔
746

747
    return TSDB_CODE_SUCCESS;
2,310✔
748
  }
749

750
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
33,305✔
751
  int16_t valueNum = taosArrayGetSize(pTagVals);
33,305✔
752
  int32_t num = 0;
33,305✔
753
  int32_t j = 0;
33,305✔
754
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
94,352✔
755
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
61,047✔
756
    if (i > 0) {
61,047✔
757
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
27,742✔
758
                        ", ");
759
    }
760

761
    if (j >= valueNum) {
61,047✔
762
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
15,060✔
763
                        "NULL");
764
      continue;
15,060✔
765
    }
766

767
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
45,987✔
768
    if (pSchema->colId > pTagVal->cid) {
45,987✔
769
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
770
      code = TSDB_CODE_APP_ERROR;
×
771
      TAOS_CHECK_ERRNO(code);
×
772
    } else if (pSchema->colId == pTagVal->cid) {
45,987✔
773
      char    type = pTagVal->type;
44,491✔
774
      int32_t tlen = 0;
44,491✔
775

776
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
44,491✔
777
      if (leftSize <= 0) {
44,491✔
778
        qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
×
779
        code = TSDB_CODE_APP_ERROR;
×
780
        TAOS_CHECK_ERRNO(code);
×
781
      }
782
      if (IS_VAR_DATA_TYPE(type)) {
44,491✔
783
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
11,883✔
784
        TAOS_CHECK_ERRNO(code);
11,883✔
785
      } else {
786
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
32,608✔
787
                               &tlen);
788
        TAOS_CHECK_ERRNO(code);
32,608✔
789
      }
790
      *len += tlen;
44,491✔
791
      j++;
44,491✔
792
    } else {
793
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,496✔
794
                        "NULL");
795
    }
796
  }
797
_exit:
33,305✔
798
  taosArrayDestroy(pTagVals);
33,305✔
799

800
  return code;
33,305✔
801
}
802

803
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
67,191✔
804
  if (pCfg->commentLen > 0) {
67,191✔
805
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
806
                      " COMMENT '%s'", pCfg->pComment);
807
  } else if (0 == pCfg->commentLen) {
67,191✔
808
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,560✔
809
                      " COMMENT ''");
810
  }
811

812
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
67,191✔
813
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
814
                      " WATERMARK %" PRId64 "a", pCfg->watermark1);
815
    if (pCfg->watermark2 > 0) {
×
816
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
817
                        ", %" PRId64 "a", pCfg->watermark2);
818
    }
819
  }
820

821
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
67,191✔
822
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
823
                      " MAX_DELAY %" PRId64 "a", pCfg->delay1);
824
    if (pCfg->delay2 > 0) {
×
825
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
826
                        ", %" PRId64 "a", pCfg->delay2);
827
    }
828
  }
829

830
  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
67,191✔
831
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
67,191✔
832
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
833
                      " ROLLUP(");
834
    for (int32_t i = 0; i < funcNum; ++i) {
×
835
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
×
836
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
837
                        "%s%s", ((i > 0) ? ", " : ""), pFunc);
838
    }
839
    *len +=
×
840
        snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
×
841
  }
842

843
  if (pCfg->ttl > 0) {
67,191✔
844
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
845
                      " TTL %d", pCfg->ttl);
846
  }
847

848
  if (pCfg->keep > 0) {
67,191✔
849
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,598✔
850
                      " KEEP %dm", pCfg->keep);
851
  }
852

853
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
67,191✔
854
    int32_t nSma = 0;
35,685✔
855
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
201,882✔
856
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
166,197✔
857
        ++nSma;
166,197✔
858
      }
859
    }
860

861
    if (nSma < pCfg->numOfColumns && nSma > 0) {
35,685✔
862
      bool smaOn = false;
×
863
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
864
                        " SMA(");
865
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
866
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
867
          if (smaOn) {
×
868
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
869
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ",`%s`",
×
870
                              (pCfg->pSchemas + i)->name);
×
871
          } else {
872
            smaOn = true;
×
873
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
874
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), "`%s`",
×
875
                              (pCfg->pSchemas + i)->name);
×
876
          }
877
        }
878
      }
879
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, ")");
×
880
    }
881
  }
882

883
  if (pCfg->virtualStb) {
67,191✔
884
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,693✔
885
                      " VIRTUAL %d", pCfg->virtualStb);
1,693✔
886
  }
887
}
67,191✔
888

889
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
79,963✔
890
                                              void* charsetCxt) {
891
  int32_t code = TSDB_CODE_SUCCESS;
79,963✔
892
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
79,963✔
893
  pBlock->info.rows = 1;
79,963✔
894

895
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
79,963✔
896
  char             buf1[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1)] = {0};
79,963✔
897
  STR_TO_VARSTR(buf1, tbName);
79,963✔
898
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
79,963✔
899

900
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
79,963✔
901
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
79,963✔
902
  if (NULL == buf2) {
79,963✔
903
    QRY_ERR_RET(terrno);
×
904
  }
905

906
  int32_t len = 0;
79,963✔
907

908
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
79,963✔
909
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
22,826✔
910
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
911
    appendColumnFields(buf2, &len, pCfg);
22,826✔
912
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
22,826✔
913
                     ") TAGS (");
914
    appendTagFields(buf2, &len, pCfg);
22,826✔
915
    len +=
22,826✔
916
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
22,826✔
917
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
22,826✔
918
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
57,137✔
919
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
31,506✔
920
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
921
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
31,506✔
922
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
31,506✔
923
    appendTagNameFields(buf2, &len, pCfg);
31,506✔
924
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
31,506✔
925
                     ") TAGS (");
926
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
31,506✔
927
    TAOS_CHECK_ERRNO(code);
31,506✔
928
    len +=
31,506✔
929
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
31,506✔
930
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
31,506✔
931
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
25,631✔
932
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
12,859✔
933
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
934
    appendColumnFields(buf2, &len, pCfg);
12,859✔
935
    len +=
12,859✔
936
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
12,859✔
937
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
12,859✔
938
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
12,772✔
939
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
3,473✔
940
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
941
    appendColumnFields(buf2, &len, pCfg);
3,473✔
942
    len +=
3,473✔
943
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
3,473✔
944
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
9,299✔
945
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
4,109✔
946
                     "CREATE VTABLE `%s`", expandIdentifier(tbName, buf1));
947
    appendColRefFields(buf2, &len, pCfg);
4,109✔
948
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
4,109✔
949
                    " USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
4,109✔
950
    appendTagNameFields(buf2, &len, pCfg);
4,109✔
951
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
4,109✔
952
                     ") TAGS (");
953
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
4,109✔
954
    TAOS_CHECK_ERRNO(code);
4,109✔
955
    len +=
4,109✔
956
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
4,109✔
957
  }
958

959
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
79,963✔
960

961
  code = colDataSetVal(pCol2, 0, buf2, false);
79,963✔
962
  TAOS_CHECK_ERRNO(code);
79,963✔
963

964
_exit:
79,963✔
965
  taosMemoryFree(buf2);
79,963✔
966

967
  return code;
79,963✔
968
}
969

970
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
5,798✔
971
  int32_t code = 0;
5,798✔
972
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
5,798✔
973
  pBlock->info.rows = 1;
5,798✔
974

975
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
5,798✔
976
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
5,798✔
977
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
5,798✔
978
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
5,798✔
979
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
5,798✔
980

981
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
5,798✔
982
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
5,798✔
983
  if (NULL == buf2) {
5,798✔
984
    return terrno;
×
985
  }
986

987
  SViewMeta* pMeta = pStmt->pViewMeta;
5,798✔
988
  if (NULL == pMeta) {
5,798✔
989
    qError("exception: view meta is null");
×
990
    taosMemoryFree(buf2);
×
991
    return TSDB_CODE_APP_ERROR;
×
992
  }
993
  snprintf(varDataVal(buf2), SHOW_CREATE_VIEW_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE VIEW `%s`.`%s` AS %s",
5,798✔
994
           pStmt->dbName, pStmt->viewName, pMeta->querySql);
5,798✔
995
  int32_t len = strlen(varDataVal(buf2));
5,798✔
996
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
5,798✔
997
  code = colDataSetVal(pCol2, 0, buf2, false);
5,798✔
998
  taosMemoryFree(buf2);
5,798✔
999

1000
  return code;
5,798✔
1001
}
1002

1003
extern const char* fmGetFuncName(int32_t funcId);
1004
static int32_t setCreateRsmaResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateRsmaStmt* pStmt) {
1,464✔
1005
  int32_t       code = 0, lino = 0;
1,464✔
1006
  char*         buf2 = NULL;
1,464✔
1007
  SRsmaInfoRsp* pMeta = pStmt->pRsmaMeta;
1,464✔
1008

1009
  if (pMeta->nFuncs != pMeta->nColNames) {
1,464✔
1010
    qError("exception: rsma meta is invalid, nFuncs:%d != nColNames:%d", pMeta->nFuncs, pMeta->nColNames);
×
1011
    return TSDB_CODE_APP_ERROR;
×
1012
  }
1013

1014
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, 1));
1,464✔
1015
  pBlock->info.rows = 1;
1,464✔
1016

1017
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
1,464✔
1018
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1] = {0};
1,464✔
1019
  snprintf(varDataVal(buf1), TSDB_TABLE_FNAME_LEN + 4, "`%s`", expandIdentifier(pStmt->rsmaName, buf1));
1,464✔
1020
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
1,464✔
1021
  TAOS_CHECK_EXIT(colDataSetVal(pCol1, 0, buf1, false));
1,464✔
1022

1023
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
1,464✔
1024
  if (!(buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN))) {
1,464✔
1025
    return terrno;
×
1026
  }
1027

1028
  SName name = {0};
1,464✔
1029
  TAOS_CHECK_EXIT(tNameFromString(&name, pMeta->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
1,464✔
1030

1031
  int32_t len = 0;
1,464✔
1032
  len += tsnprintf(varDataVal(buf2), SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
2,928✔
1033
                       "CREATE RSMA `%s` ON `%s`.`%s` FUNCTION(", expandIdentifier(pStmt->rsmaName, buf1),
1,464✔
1034
                       expandIdentifier(name.dbname, buf1), expandIdentifier(name.tname, buf1));
1035
  for (int32_t i = 0; i < pMeta->nFuncs; ++i) {
10,980✔
1036
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
19,032✔
1037
                         "%s%s(`%s`)", (i > 0) ? "," : "", fmGetFuncName(pMeta->funcIds[i]),
9,516✔
1038
                         expandIdentifier(*(char**)TARRAY_GET_ELEM(pMeta->colNames, i), buf1));
9,516✔
1039
  }
1040
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,464✔
1041
                       ") INTERVAL(%d%c", pMeta->interval[0], pMeta->intervalUnit);
1,464✔
1042
  if (pMeta->interval[1] > 0) {
1,464✔
1043
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,464✔
1044
                         ",%d%c", pMeta->interval[1], pMeta->intervalUnit);
1,464✔
1045
  }
1046
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,464✔
1047
  varDataLen(buf2) = len;
1,464✔
1048
  code = colDataSetVal(pCol2, 0, buf2, false);
1,464✔
1049
_exit:
1,464✔
1050
  taosMemoryFree(buf2);
1,464✔
1051
  return code;
1,464✔
1052
}
1053

1054
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
73,007✔
1055
  SSDataBlock* pBlock = NULL;
73,007✔
1056
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
73,007✔
1057
  if (TSDB_CODE_SUCCESS == code) {
73,007✔
1058
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
73,007✔
1059
  }
1060
  if (TSDB_CODE_SUCCESS == code) {
73,007✔
1061
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
73,007✔
1062
  }
1063
  (void)blockDataDestroy(pBlock);
73,007✔
1064
  return code;
73,007✔
1065
}
1066

1067
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
6,956✔
1068
  SSDataBlock* pBlock = NULL;
6,956✔
1069
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
6,956✔
1070
  if (TSDB_CODE_SUCCESS == code) {
6,956✔
1071
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
6,956✔
1072
  }
1073
  if (TSDB_CODE_SUCCESS == code) {
6,956✔
1074
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
6,956✔
1075
  }
1076
  (void)blockDataDestroy(pBlock);
6,956✔
1077
  return code;
6,956✔
1078
}
1079

1080
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
1,116✔
1081
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
1,116✔
1082
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
1,116✔
1083
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
173✔
1084
    return terrno;
173✔
1085
  }
1086

1087
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
943✔
1088
}
1089

1090
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
26,808✔
1091
  int32_t code = 0;
26,808✔
1092

1093
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
26,808✔
1094
    taosResetLog();
47✔
1095
    cfgDumpCfg(tsCfg, 0, false);
47✔
1096
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
26,761✔
1097
    int32_t tmp = 0;
2,448✔
1098
    code = taosStr2int32(value, &tmp);
2,448✔
1099
    if (code) {
2,448✔
1100
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1101
      return code;
×
1102
    }
1103
    code = schedulerUpdatePolicy(tmp);
2,448✔
1104
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
24,313✔
1105
    int32_t tmp = 0;
×
1106
    code = taosStr2int32(value, &tmp);
×
1107
    if (code) {
×
1108
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1109
      return code;
×
1110
    }
1111
    code = schedulerEnableReSchedule(tmp != 0);
×
1112
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
24,313✔
1113
    code = ctgdHandleDbgCommand(value);
×
1114
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
24,313✔
1115
    code = taosMemoryDbgInit();
×
1116
    if (code) {
×
1117
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
1118
      return code;
×
1119
    }
1120
    tsAsyncLog = false;
×
1121
    qInfo("memory dbg enabled");
×
1122
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
24,313✔
1123
    code = taosMemoryDbgInitRestore();
×
1124
    if (code) {
×
1125
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
1126
      return code;
×
1127
    }
1128
    qInfo("memory dbg disabled");
×
1129
  } else {
1130
    goto _return;
24,313✔
1131
  }
1132

1133
  *processed = true;
2,495✔
1134

1135
_return:
26,808✔
1136

1137
  if (code) {
26,808✔
1138
    terrno = code;
×
1139
  }
1140

1141
  return code;
26,808✔
1142
}
1143

1144
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
26,808✔
1145
  bool processed = false;
26,808✔
1146

1147
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
26,808✔
1148
    return terrno;
×
1149
  }
1150

1151
  if (processed) {
26,808✔
1152
    goto _return;
2,495✔
1153
  }
1154

1155
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
24,313✔
1156
    return terrno;
1,593✔
1157
  }
1158

1159
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
22,720✔
1160
    return terrno;
×
1161
  }
1162

1163
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
22,720✔
1164

1165
_return:
25,215✔
1166

1167
  return TSDB_CODE_SUCCESS;
25,215✔
1168
}
1169

1170
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
12,039✔
1171
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
12,039✔
1172
  if (NULL == pBlock) {
12,039✔
1173
    return terrno;
×
1174
  }
1175

1176
  pBlock->info.hasVarCol = true;
12,039✔
1177

1178
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
12,039✔
1179
  if (NULL == pBlock->pDataBlock) {
12,039✔
1180
    taosMemoryFree(pBlock);
×
1181
    return terrno;
×
1182
  }
1183

1184
  SColumnInfoData infoData = {0};
12,039✔
1185

1186
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
12,039✔
1187
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
12,039✔
1188
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
24,078✔
1189
    goto _exit;
×
1190
  }
1191

1192
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
12,039✔
1193
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
12,039✔
1194
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
24,078✔
1195
    goto _exit;
×
1196
  }
1197

1198
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
12,039✔
1199
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
12,039✔
1200
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
24,078✔
1201
    goto _exit;
×
1202
  }
1203

1204
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
12,039✔
1205
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
12,039✔
1206
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
24,078✔
1207
    goto _exit;
×
1208
  }
1209

1210
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
12,039✔
1211
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
12,039✔
1212
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
24,078✔
1213
    goto _exit;
×
1214
  }
1215

1216
  *pOutput = pBlock;
12,039✔
1217

1218
_exit:
12,039✔
1219
  if (terrno != TSDB_CODE_SUCCESS) {
12,039✔
1220
    taosArrayDestroy(pBlock->pDataBlock);
×
1221
    taosMemoryFree(pBlock);
×
1222
  }
1223
  return terrno;
12,039✔
1224
}
1225

1226
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
12,039✔
1227
  SSDataBlock* pBlock = NULL;
12,039✔
1228
  char*        likePattern = NULL;
12,039✔
1229
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
12,039✔
1230
  if (TSDB_CODE_SUCCESS == code) {
12,039✔
1231
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
12,039✔
1232
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
2,559✔
1233
    }
1234
  }
1235
  if (TSDB_CODE_SUCCESS == code) {
12,039✔
1236
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
12,039✔
1237
  }
1238
  if (TSDB_CODE_SUCCESS == code) {
12,039✔
1239
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
12,039✔
1240
  }
1241
  (void)blockDataDestroy(pBlock);
12,039✔
1242
  return code;
12,039✔
1243
}
1244

1245
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
2,432,302✔
1246
  QRY_PARAM_CHECK(pOutput);
2,432,302✔
1247

1248
  SSDataBlock* pBlock = NULL;
2,432,302✔
1249
  int32_t      code = createDataBlock(&pBlock);
2,432,302✔
1250
  if (code) {
2,432,302✔
1251
    return code;
×
1252
  }
1253

1254
  SNode* pProj = NULL;
2,432,302✔
1255
  FOREACH(pProj, pProjects) {
6,208,085✔
1256
    SExprNode*      pExpr = (SExprNode*)pProj;
3,775,783✔
1257
    SColumnInfoData infoData = {0};
3,775,783✔
1258
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
3,775,783✔
1259
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1260
      infoData.info.bytes = 0;
×
1261
    } else {
1262
      infoData.info.type = pExpr->resType.type;
3,775,783✔
1263
      infoData.info.bytes = pExpr->resType.bytes;
3,775,783✔
1264
      infoData.info.precision = pExpr->resType.precision;
3,775,783✔
1265
      infoData.info.scale = pExpr->resType.scale;
3,775,783✔
1266
    }
1267
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
3,775,783✔
1268
  }
1269

1270
  *pOutput = pBlock;
2,432,302✔
1271
  return code;
2,432,302✔
1272
}
1273

1274
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
2,432,302✔
1275
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
2,432,302✔
1276

1277
  int32_t index = 0;
2,432,302✔
1278
  SNode*  pProj = NULL;
2,432,302✔
1279
  FOREACH(pProj, pProjects) {
6,208,085✔
1280
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
3,775,783✔
1281
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1282
    } else {
1283
      if (((SValueNode*)pProj)->isNull) {
3,775,783✔
1284
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
80,737✔
1285
      } else {
1286
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
3,695,046✔
1287
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1288
      }
1289
    }
1290
  }
1291

1292
  pBlock->info.rows = 1;
2,432,302✔
1293
  return TSDB_CODE_SUCCESS;
2,432,302✔
1294
}
1295

1296
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
2,432,302✔
1297
  SSDataBlock* pBlock = NULL;
2,432,302✔
1298
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
2,432,302✔
1299
  if (TSDB_CODE_SUCCESS == code) {
2,432,302✔
1300
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
2,432,302✔
1301
  }
1302
  if (TSDB_CODE_SUCCESS == code) {
2,432,302✔
1303
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
2,432,302✔
1304
  }
1305
  (void)blockDataDestroy(pBlock);
2,432,302✔
1306
  return code;
2,432,302✔
1307
}
1308

1309
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
5,798✔
1310
  SSDataBlock* pBlock = NULL;
5,798✔
1311
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
5,798✔
1312
  if (TSDB_CODE_SUCCESS == code) {
5,798✔
1313
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
5,798✔
1314
  }
1315
  if (TSDB_CODE_SUCCESS == code) {
5,798✔
1316
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
5,798✔
1317
  }
1318
  (void)blockDataDestroy(pBlock);
5,798✔
1319
  return code;
5,798✔
1320
}
1321

1322
static int32_t execShowCreateRsma(SShowCreateRsmaStmt* pStmt, SRetrieveTableRsp** pRsp) {
1,464✔
1323
  SSDataBlock* pBlock = NULL;
1,464✔
1324
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
1,464✔
1325
  if (TSDB_CODE_SUCCESS == code) {
1,464✔
1326
    code = setCreateRsmaResultIntoDataBlock(pBlock, pStmt);
1,464✔
1327
  }
1328
  if (TSDB_CODE_SUCCESS == code) {
1,464✔
1329
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
1,464✔
1330
  }
1331
  (void)blockDataDestroy(pBlock);
1,464✔
1332
  return code;
1,464✔
1333
}
1334

1335
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
5,047,531✔
1336
                     void* charsetCxt) {
1337
  switch (nodeType(pStmt)) {
5,047,531✔
1338
    case QUERY_NODE_DESCRIBE_STMT:
411,880✔
1339
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
411,880✔
1340
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
2,015,133✔
1341
      return execResetQueryCache();
2,015,133✔
1342
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
62,041✔
1343
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
62,041✔
1344
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
72,064✔
1345
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
72,064✔
1346
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
6,956✔
1347
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
6,956✔
1348
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
1,116✔
1349
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,116✔
1350
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
5,798✔
1351
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
5,798✔
1352
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
1,464✔
1353
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
1,464✔
1354
    case QUERY_NODE_ALTER_LOCAL_STMT:
26,808✔
1355
      return execAlterLocal((SAlterLocalStmt*)pStmt);
26,808✔
1356
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
12,039✔
1357
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
12,039✔
1358
    case QUERY_NODE_SELECT_STMT:
2,432,302✔
1359
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
2,432,302✔
1360
    default:
×
1361
      break;
×
1362
  }
1363
  return TSDB_CODE_FAILED;
×
1364
}
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