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

taosdata / TDengine / #4997

20 Mar 2026 06:10AM UTC coverage: 71.739% (-0.3%) from 72.069%
#4997

push

travis-ci

web-flow
feat: add query phase tracking for SHOW QUERIES (#34706)

148 of 183 new or added lines in 10 files covered. (80.87%)

9273 existing lines in 172 files now uncovered.

244572 of 340921 relevant lines covered (71.74%)

133392941.95 hits per line

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

80.82
/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,223,929✔
42
  if (NULL == pBlock || NULL == pRsp) {
3,223,929✔
43
    return TSDB_CODE_INVALID_PARA;
×
44
  }
45
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
3,223,929✔
46
  size_t rspSize = sizeof(SRetrieveTableRsp) + dataEncodeBufSize + PAYLOAD_PREFIX_LEN;
3,223,889✔
47
  *pRsp = taosMemoryCalloc(1, rspSize);
3,223,889✔
48
  if (NULL == *pRsp) {
3,223,929✔
49
    return terrno;
×
50
  }
51

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

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

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

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

75
  return TSDB_CODE_SUCCESS;
3,223,929✔
76
}
77

78
static int32_t getSchemaBytes(const SSchema* pSchema) {
60,626,466✔
79
  switch (pSchema->type) {
60,626,466✔
80
    case TSDB_DATA_TYPE_BINARY:
2,521,803✔
81
    case TSDB_DATA_TYPE_VARBINARY:
82
    case TSDB_DATA_TYPE_GEOMETRY:
83
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
2,521,803✔
84
    case TSDB_DATA_TYPE_NCHAR:
4,298,990✔
85
    case TSDB_DATA_TYPE_JSON:
86
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
4,298,990✔
87
    default:
53,805,673✔
88
      return pSchema->bytes;
53,805,673✔
89
  }
90
}
91

92
static const char* expandIdentifier(const char* name, char* output) {
632,136✔
93
  if (NULL == name) return "";
632,136✔
94
  bool containsEscapeChar = false;
632,136✔
95
  for (const char* p = name; *p != '\0'; ++p) {
4,590,056✔
96
    if (*p == TS_ESCAPE_CHAR) {
4,005,620✔
97
      containsEscapeChar = true;
47,700✔
98
      break;
47,700✔
99
    }
100
  }
101
  if (!containsEscapeChar) return name;
632,136✔
102
  if (NULL == output) return "";
47,700✔
103
  char* out_ptr = output;
47,700✔
104
  for (const char* src = name; *src != '\0'; ++src) {
293,620✔
105
    if (*src == TS_ESCAPE_CHAR) {
245,920✔
106
      *out_ptr++ = TS_ESCAPE_CHAR;
119,780✔
107
      *out_ptr++ = TS_ESCAPE_CHAR;
119,780✔
108
    } else {
109
      *out_ptr++ = *src;
126,140✔
110
    }
111
  }
112
  *out_ptr = '\0';
47,700✔
113
  return output;
47,700✔
114
}
115

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

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

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

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

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

191
  if (hasRefCol(pMeta->tableType)) {
437,332✔
192
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
3,196✔
193
  }
194

195
  int32_t fillTagCol = 0;
437,332✔
196
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
437,332✔
197
  for (int32_t i = 0; i < numOfRows; ++i) {
61,063,798✔
198
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
60,626,466✔
199
      continue;
×
200
    }
201
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
60,626,466✔
202
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
60,626,466✔
203

204
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
60,626,426✔
205
      uint8_t prec = 0, scale = 0;
260,193✔
206
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
260,193✔
207
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
260,193✔
208
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
260,193✔
209
      varDataSetLen(buf, len);
260,193✔
210
    } else {
211
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
60,366,273✔
212
    }
213
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
60,626,466✔
214
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
60,626,466✔
215
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
60,626,466✔
216
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
60,626,466✔
217
      if (i >= pMeta->tableInfo.numOfColumns) {
60,595,386✔
218
        STR_TO_VARSTR(buf, "TAG");
3,042,131✔
219
        fillTagCol = 1;
3,042,131✔
220
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
57,553,255✔
221
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
10,974✔
222
      } else {
223
        STR_TO_VARSTR(buf, "");
57,542,281✔
224
      }
225
    } else {
226
      STR_TO_VARSTR(buf, "VIEW COL");
31,080✔
227
    }
228
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
60,626,466✔
229
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
60,626,466✔
230
      if (i < pMeta->tableInfo.numOfColumns) {
60,538,031✔
231
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
57,501,660✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
57,501,620✔
233
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
57,501,660✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
57,501,580✔
235
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
57,501,660✔
236
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
57,501,580✔
237
      } else {
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,036,371✔
239
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
3,036,371✔
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,036,371✔
241
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
3,036,371✔
242
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,036,371✔
243
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
3,036,371✔
244
      }
245
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
88,435✔
246
      if (i < pMeta->numOfColRefs) {
38,044✔
247
        if (pMeta->colRef[i].hasRef) {
32,284✔
248
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
14,092✔
249

250
          TSlice strBuf = {0};
14,092✔
251
          sliceInit(&strBuf, refColName, sizeof(refColName));
14,092✔
252

253
          QRY_ERR_RET(sliceAppend(&strBuf, pMeta->colRef[i].refDbName, strlen(pMeta->colRef[i].refDbName)));
14,092✔
254
          QRY_ERR_RET(sliceAppend(&strBuf, ".", 1));
14,092✔
255
          QRY_ERR_RET(sliceAppend(&strBuf, pMeta->colRef[i].refTableName, strlen(pMeta->colRef[i].refTableName)));
14,092✔
256
          QRY_ERR_RET(sliceAppend(&strBuf, ".", 1));
14,092✔
257
          QRY_ERR_RET(sliceAppend(&strBuf, pMeta->colRef[i].refColName, strlen(pMeta->colRef[i].refColName)));
14,092✔
258
          STR_TO_VARSTR(buf, refColName);
14,092✔
259
        } else {
260
          STR_TO_VARSTR(buf, "");
18,192✔
261
        }
262
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
32,284✔
263
      } else {
264
        STR_TO_VARSTR(buf, "");
5,760✔
265
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
5,760✔
266
      }
267
    }
268

269
    fillTagCol = 0;
60,626,466✔
270

271
    ++(pBlock->info.rows);
60,626,466✔
272
  }
273
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
437,332✔
274
    STR_TO_VARSTR(buf, "tbname");
×
275
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
×
276
    STR_TO_VARSTR(buf, "VARCHAR");
×
277
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
×
278
    int32_t bytes = TSDB_TABLE_NAME_LEN - 1;
×
279
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
×
280
    STR_TO_VARSTR(buf, "TAG");
×
281
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
×
282
    ++(pBlock->info.rows);
×
283
  }
284
  if (pBlock->info.rows <= 0) {
437,332✔
285
    qError("no permission to view any columns");
×
286
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
287
  }
288
  return TSDB_CODE_SUCCESS;
437,332✔
289
}
290

291
static int32_t execDescribe(bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode) {
437,253✔
292
  SDescribeStmt* pDesc = (SDescribeStmt*)pStmt;
437,253✔
293
  if (NULL == pDesc || NULL == pDesc->pMeta) {
437,253✔
294
    return TSDB_CODE_INVALID_PARA;
×
295
  }
296
  int32_t numOfRows = TABLE_TOTAL_COL_NUM(pDesc->pMeta);
437,332✔
297

298
  SSDataBlock* pBlock = NULL;
437,332✔
299
  int32_t      code = buildDescResultDataBlock(&pBlock);
437,332✔
300
  if (TSDB_CODE_SUCCESS == code) {
437,332✔
301
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
437,332✔
302
  }
303
  if (TSDB_CODE_SUCCESS == code) {
437,332✔
304
    if (pDesc->pMeta) {
437,332✔
305
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
437,332✔
306
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
423,437✔
307
      } else if (hasRefCol(pDesc->pMeta->tableType) && pDesc->pMeta->colRef) {
13,895✔
308
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
3,196✔
309
      } else {
310
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
10,699✔
311
      }
312
    } else {
313
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
314
    }
315
  }
316
  (void)blockDataDestroy(pBlock);
437,332✔
317
  return code;
437,332✔
318
}
319

320
static int32_t execResetQueryCache() { return catalogClearCache(); }
2,124,313✔
321

322
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
73,755✔
323
  QRY_PARAM_CHECK(pOutput);
73,755✔
324

325
  SSDataBlock* pBlock = NULL;
73,755✔
326
  int32_t      code = createDataBlock(&pBlock);
73,755✔
327
  if (code) {
73,755✔
328
    return code;
×
329
  }
330

331
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_COLS, 1);
73,755✔
332
  code = blockDataAppendColInfo(pBlock, &infoData);
73,755✔
333
  if (TSDB_CODE_SUCCESS == code) {
73,755✔
334
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_FIELD2_LEN, 2);
73,755✔
335
    code = blockDataAppendColInfo(pBlock, &infoData);
73,755✔
336
  }
337

338
  if (TSDB_CODE_SUCCESS == code) {
73,755✔
339
    *pOutput = pBlock;
73,755✔
340
  } else {
341
    (void)blockDataDestroy(pBlock);
×
342
  }
343
  return code;
73,755✔
344
}
345

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

368
  return v;
×
369
}
370

371
static int32_t buildRetension(SArray* pRetension, char** ppRetentions) {
73,755✔
372
  size_t size = taosArrayGetSize(pRetension);
73,755✔
373
  if (size == 0) {
73,755✔
374
    *ppRetentions = NULL;
73,755✔
375
    return TSDB_CODE_SUCCESS;
73,755✔
376
  }
377

378
  const int lMaxLen = 128;
×
379
  char*     p1 = taosMemoryCalloc(1, lMaxLen);
×
380
  if (NULL == p1) {
×
381
    return terrno;
×
382
  }
383
  int32_t len = 0;
×
384

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

395
    if (i < size - 1) {
×
396
      len += snprintf(p1 + len, lMaxLen - len, ",");
×
397
    }
398
  }
399

400
  *ppRetentions = p1;
×
401
  return TSDB_CODE_SUCCESS;
×
402
}
403

404
static const char* cacheModelStr(int8_t cacheModel) {
73,387✔
405
  switch (cacheModel) {
73,387✔
406
    case TSDB_CACHE_MODEL_NONE:
73,387✔
407
      return TSDB_CACHE_MODEL_NONE_STR;
73,387✔
408
    case TSDB_CACHE_MODEL_LAST_ROW:
×
409
      return TSDB_CACHE_MODEL_LAST_ROW_STR;
×
410
    case TSDB_CACHE_MODEL_LAST_VALUE:
×
411
      return TSDB_CACHE_MODEL_LAST_VALUE_STR;
×
412
    case TSDB_CACHE_MODEL_BOTH:
×
413
      return TSDB_CACHE_MODEL_BOTH_STR;
×
414
    default:
×
415
      break;
×
416
  }
417
  return TSDB_CACHE_MODEL_NONE_STR;
×
418
}
419

420
static const char* encryptAlgorithmStr(int8_t encryptAlgorithm, char* algorithmsId) {
73,387✔
421
  if (algorithmsId[0] != '\0') {
73,387✔
422
    return algorithmsId;
×
423
  } else if (encryptAlgorithm != 0) {
73,387✔
424
    switch (encryptAlgorithm) {
×
425
      case TSDB_ENCRYPT_ALGO_NONE:
×
426
        return TSDB_ENCRYPT_ALGO_NONE_STR;
×
427
      case TSDB_ENCRYPT_ALGO_SM4:
×
428
        return TSDB_ENCRYPT_ALGO_SM4_STR;
×
429
      default:
×
430
        break;
×
431
    }
432
    return TSDB_CACHE_MODEL_NONE_STR;
×
433
  }
434

435
  return TSDB_CACHE_MODEL_NONE_STR;
73,387✔
436
}
437

438
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
4,657,051✔
439
  if (buffer == NULL || bufSize <= 0) {
4,657,051✔
440
    return 0;
×
441
  }
442
  int32_t len = 0;
4,657,051✔
443
  if (timeInMinutes % 1440 == 0) {
4,657,051✔
444
    int32_t days = timeInMinutes / 1440;
4,644,068✔
445
    len = snprintf(buffer, bufSize, "%dd", days);
4,644,068✔
446
  } else if (timeInMinutes % 60 == 0) {
12,983✔
447
    int32_t hours = timeInMinutes / 60;
2,063✔
448
    len = snprintf(buffer, bufSize, "%dh", hours);
2,063✔
449
  } else {
450
    len = snprintf(buffer, bufSize, "%dm", timeInMinutes);
10,920✔
451
  }
452
  return len;
4,657,051✔
453
}
454

455
static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, char* dbFName, SDbCfgInfo* pCfg) {
73,755✔
456
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
73,755✔
457
  pBlock->info.rows = 1;
73,755✔
458

459
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
73,755✔
460
  char             buf1[SHOW_CREATE_DB_RESULT_FIELD1_LEN] = {0};
73,755✔
461
  STR_TO_VARSTR(buf1, dbName);
73,755✔
462
  COL_DATA_SET_VAL_AND_CHECK(pCol1, 0, buf1, false);
73,755✔
463

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

483
  char* pRetentions = NULL;
73,755✔
484
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
73,755✔
485
  int32_t dbFNameLen = strlen(dbFName);
73,755✔
486
  int32_t hashPrefix = 0;
73,755✔
487
  if (pCfg->hashPrefix > 0) {
73,755✔
488
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
1,512✔
489
  } else if (pCfg->hashPrefix < 0) {
72,243✔
490
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
2,268✔
491
  }
492
  char durationStr[128] = {0};
73,755✔
493
  char keep0Str[128] = {0};
73,755✔
494
  char keep1Str[128] = {0};
73,755✔
495
  char keep2Str[128] = {0};
73,755✔
496
  char compactIntervalStr[13] = {0};
73,755✔
497
  char compactStartTimeStr[13] = {0};
73,755✔
498
  char compactEndTimeStr[13] = {0};
73,755✔
499

500
  int32_t lenDuration = formatDurationOrKeep(durationStr, sizeof(durationStr), pCfg->daysPerFile);
73,755✔
501
  int32_t lenKeep0 = formatDurationOrKeep(keep0Str, sizeof(keep0Str), pCfg->daysToKeep0);
73,755✔
502
  int32_t lenKeep1 = formatDurationOrKeep(keep1Str, sizeof(keep1Str), pCfg->daysToKeep1);
73,755✔
503
  int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pCfg->daysToKeep2);
73,755✔
504
  UNUSED(formatDurationOrKeep(compactIntervalStr, sizeof(compactIntervalStr), pCfg->compactInterval));
73,755✔
505
  UNUSED(formatDurationOrKeep(compactStartTimeStr, sizeof(compactStartTimeStr), pCfg->compactStartTime));
73,755✔
506
  UNUSED(formatDurationOrKeep(compactEndTimeStr, sizeof(compactEndTimeStr), pCfg->compactEndTime));
73,755✔
507

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

529

530
    if (pRetentions) {
73,387✔
UNCOV
531
      len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
532
                      " RETENTIONS %s", pRetentions);
533
    }
534
  }
535

536
  taosMemoryFree(pRetentions);
73,755✔
537

538
  (varDataLen(buf2)) = len;
73,755✔
539

540
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
73,755✔
541

542
  return TSDB_CODE_SUCCESS;
73,755✔
543
}
544

545
static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt, SRetrieveTableRsp** pRsp) {
73,755✔
546
  SSDataBlock* pBlock = NULL;
73,755✔
547
  int32_t      code = buildCreateDBResultDataBlock(&pBlock);
73,755✔
548
  if (TSDB_CODE_SUCCESS == code) {
73,755✔
549
    code = setCreateDBResultIntoDataBlock(pBlock, pStmt->dbName, pStmt->dbFName, pStmt->pCfg);
73,755✔
550
  }
551
  if (TSDB_CODE_SUCCESS == code) {
73,755✔
552
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_DB_RESULT_COLS, pRsp);
73,755✔
553
  }
554
  (void)blockDataDestroy(pBlock);
73,755✔
555
  return code;
73,755✔
556
}
557

558
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
89,326✔
559
  QRY_PARAM_CHECK(pOutput);
89,326✔
560

561
  SSDataBlock* pBlock = NULL;
89,326✔
562
  int32_t      code = createDataBlock(&pBlock);
89,326✔
563
  if (code) {
89,326✔
UNCOV
564
    return code;
×
565
  }
566

567
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD1_LEN, 1);
89,326✔
568
  code = blockDataAppendColInfo(pBlock, &infoData);
89,326✔
569
  if (TSDB_CODE_SUCCESS == code) {
89,326✔
570
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD2_LEN, 2);
89,326✔
571
    code = blockDataAppendColInfo(pBlock, &infoData);
89,326✔
572
  }
573

574
  if (TSDB_CODE_SUCCESS == code) {
89,326✔
575
    *pOutput = pBlock;
89,326✔
576
  } else {
UNCOV
577
    (void)blockDataDestroy(pBlock);
×
578
  }
579
  return code;
89,326✔
580
}
581

582
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
6,708✔
583
  QRY_PARAM_CHECK(pOutput);
6,708✔
584

585
  SSDataBlock* pBlock = NULL;
6,708✔
586
  int32_t      code = createDataBlock(&pBlock);
6,708✔
587
  if (code) {
6,708✔
UNCOV
588
    return code;
×
589
  }
590

591
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD1_LEN, 1);
6,708✔
592
  code = blockDataAppendColInfo(pBlock, &infoData);
6,708✔
593
  if (TSDB_CODE_SUCCESS == code) {
6,708✔
594
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD2_LEN, 2);
6,708✔
595
    code = blockDataAppendColInfo(pBlock, &infoData);
6,708✔
596
  }
597

598
  if (TSDB_CODE_SUCCESS == code) {
6,708✔
599
    *pOutput = pBlock;
6,708✔
600
  } else {
UNCOV
601
    (void)blockDataDestroy(pBlock);
×
602
  }
603
  return code;
6,708✔
604
}
605

606
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
43,976✔
607
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
43,976✔
608
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
276,703✔
609
    SSchema* pSchema = pCfg->pSchemas + i;
232,727✔
610
    SColRef* pRef = pCfg->pColRefs + i;
232,727✔
611
#define LTYPE_LEN                                    \
612
  (32 + 60 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN + \
613
   10)  // 60 byte for compress info, TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN for column ref
614
    char type[LTYPE_LEN];
232,727✔
615
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
232,727✔
616
    int typeLen = strlen(type);
232,727✔
617
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
232,727✔
618
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
206,060✔
619
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
28,091✔
620
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
204,636✔
621
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
8,709✔
622
                          (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
8,709✔
623
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
195,927✔
624
      uint8_t precision, scale;
18,192✔
625
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
18,192✔
626
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
18,192✔
627
    }
628

629
    if (withExtSchema(pCfg->tableType) && pCfg->pSchemaExt && tsShowFullCreateTableColumn) {
232,727✔
630
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, " ENCODE \'%s\'",
23,092✔
631
                          columnEncodeStr(COMPRESS_L1_TYPE_U32(pCfg->pSchemaExt[i].compress)));
23,092✔
632
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, " COMPRESS \'%s\'",
23,092✔
633
                          columnCompressStr(COMPRESS_L2_TYPE_U32(pCfg->pSchemaExt[i].compress)));
23,092✔
634
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, " LEVEL \'%s\'",
23,092✔
635
                          columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pCfg->pSchemaExt[i].compress)));
23,092✔
636
    }
637

638
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
232,727✔
639
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, " FROM `%s`", pRef->refDbName);
24,232✔
640
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
24,232✔
641
      typeLen +=
24,232✔
642
          snprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
24,232✔
643
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
24,232✔
644
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
24,232✔
645
    }
646

647
    if (!(pSchema->flags & COL_IS_KEY)) {
232,727✔
648
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
223,298✔
649
                       "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
223,298✔
650
    } else {
651
      char* pk = "COMPOSITE KEY";
9,429✔
652
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
9,429✔
653
                       "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
9,429✔
654
    }
655
  }
656
}
43,976✔
657

658
static void appendColRefFields(char* buf, int32_t* len, STableCfg* pCfg) {
4,295✔
659
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
4,295✔
660
  bool firstRef = true;
4,295✔
661
  for (int32_t i = 1; i < pCfg->numOfColumns; ++i) {
56,945✔
662
    SSchema* pSchema = pCfg->pSchemas + i;
52,650✔
663
    SColRef* pRef = pCfg->pColRefs + i;
52,650✔
664
    char     type[TSDB_COL_NAME_LEN + 10 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN];
52,650✔
665
    int      typeLen = 0;
52,650✔
666

667
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
52,650✔
668
      typeLen += snprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
30,418✔
669
      typeLen += snprintf(type + typeLen, sizeof(type) - typeLen, ".");
30,418✔
670
      typeLen +=
30,418✔
671
          snprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
30,418✔
672
      typeLen += snprintf(type + typeLen, sizeof(type) - typeLen, ".");
30,418✔
673
      typeLen +=
30,418✔
674
          snprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
30,418✔
675
    } else {
676
      continue;
22,232✔
677
    }
678

679
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
30,418✔
680
                      "%s`%s` %s", (!firstRef ? ", " : " ("), expandIdentifier(pSchema->name, expandName), type);
30,418✔
681
    firstRef = false;
30,418✔
682
  }
683
  if (!firstRef) {
4,295✔
684
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
3,671✔
685
  }
686
}
4,295✔
687

688
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
26,382✔
689
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
26,382✔
690
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
79,916✔
691
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
53,534✔
692
    char     type[32];
53,534✔
693
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
53,534✔
694
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
53,534✔
695
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
43,617✔
696
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
9,977✔
697
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
9,977✔
698
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
43,557✔
699
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
1,549✔
700
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
1,549✔
701
    }
702

703
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
53,534✔
704
                     "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
53,534✔
705
  }
706
}
26,382✔
707

708
static void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
37,706✔
709
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
37,706✔
710
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
104,483✔
711
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
66,777✔
712
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
66,777✔
713
                     "%s`%s`", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName));
66,777✔
714
  }
715
}
37,706✔
716

717
static int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg, void* charsetCxt) {
37,706✔
718
  int32_t code = TSDB_CODE_SUCCESS;
37,706✔
719
  SArray* pTagVals = NULL;
37,706✔
720
  STag*   pTag = (STag*)pCfg->pTags;
37,706✔
721

722
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
37,706✔
723
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
UNCOV
724
    return TSDB_CODE_APP_ERROR;
×
725
  }
726

727
  if (tTagIsJson(pTag)) {
37,706✔
728
    char* pJson = NULL;
2,455✔
729
    parseTagDatatoJson(pTag, &pJson, charsetCxt);
2,455✔
730
    if (NULL == pJson) {
2,455✔
731
      qError("failed to parse tag to json, pJson is NULL");
×
UNCOV
732
      return terrno;
×
733
    }
734
    char* escapedJson = taosMemCalloc(sizeof(char), strlen(pJson) * 2 + 1);  // taosMemoryAlloc(strlen(pJson) * 2 + 1);
2,455✔
735
    if (escapedJson) {
2,455✔
736
      char* writer = escapedJson;
2,455✔
737
      for (char* reader = pJson; *reader; ++reader) {
49,100✔
738
        if (*reader == '\'') {
46,645✔
739
          *writer++ = '\'';  // Escape single quote by doubling it
491✔
740
        }
741
        *writer++ = *reader;
46,645✔
742
      }
743
      *writer = '\0';
2,455✔
744

745
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,455✔
746
                       "'%s'", escapedJson);
747
      taosMemoryFree(escapedJson);
2,455✔
748
    } else {
749
      taosMemoryFree(pJson);
×
UNCOV
750
      return terrno;
×
751
    }
752
    taosMemoryFree(pJson);
2,455✔
753

754
    return TSDB_CODE_SUCCESS;
2,455✔
755
  }
756

757
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
35,251✔
758
  int16_t valueNum = taosArrayGetSize(pTagVals);
35,251✔
759
  int32_t num = 0;
35,251✔
760
  int32_t j = 0;
35,251✔
761
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
99,573✔
762
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
64,322✔
763
    if (i > 0) {
64,322✔
764
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
29,071✔
765
                       ", ");
766
    }
767

768
    if (j >= valueNum) {
64,322✔
769
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
15,720✔
770
                       "NULL");
771
      continue;
15,720✔
772
    }
773

774
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
48,602✔
775
    if (pSchema->colId > pTagVal->cid) {
48,602✔
776
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
777
      code = TSDB_CODE_APP_ERROR;
×
UNCOV
778
      TAOS_CHECK_ERRNO(code);
×
779
    } else if (pSchema->colId == pTagVal->cid) {
48,602✔
780
      char    type = pTagVal->type;
47,042✔
781
      int32_t tlen = 0;
47,042✔
782

783
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
47,042✔
784
      if (leftSize <= 0) {
47,042✔
785
        qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
×
786
        code = TSDB_CODE_APP_ERROR;
×
UNCOV
787
        TAOS_CHECK_ERRNO(code);
×
788
      }
789
      if (IS_VAR_DATA_TYPE(type)) {
47,042✔
790
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
12,439✔
791
        TAOS_CHECK_ERRNO(code);
12,439✔
792
      } else {
793
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
34,603✔
794
                               &tlen);
795
        TAOS_CHECK_ERRNO(code);
34,603✔
796
      }
797
      *len += tlen;
47,042✔
798
      j++;
47,042✔
799
    } else {
800
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,560✔
801
                       "NULL");
802
    }
803
  }
804
_exit:
35,251✔
805
  taosArrayDestroy(pTagVals);
35,251✔
806

807
  return code;
35,251✔
808
}
809

810
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
73,759✔
811
  if (pCfg->commentLen > 0) {
73,759✔
UNCOV
812
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
813
                     " COMMENT '%s'", pCfg->pComment);
814
  } else if (0 == pCfg->commentLen) {
73,759✔
815
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,848✔
816
                     " COMMENT ''");
817
  }
818

819
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
73,759✔
UNCOV
820
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
821
                     " WATERMARK %" PRId64 "a", pCfg->watermark1);
822
    if (pCfg->watermark2 > 0) {
×
UNCOV
823
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
824
                       ", %" PRId64 "a", pCfg->watermark2);
825
    }
826
  }
827

828
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
73,759✔
UNCOV
829
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
830
                     " MAX_DELAY %" PRId64 "a", pCfg->delay1);
831
    if (pCfg->delay2 > 0) {
×
UNCOV
832
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
833
                       ", %" PRId64 "a", pCfg->delay2);
834
    }
835
  }
836

837
  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
73,759✔
838
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
73,759✔
UNCOV
839
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
840
                     " ROLLUP(");
841
    for (int32_t i = 0; i < funcNum; ++i) {
×
842
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
×
UNCOV
843
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
844
                       "%s%s", ((i > 0) ? ", " : ""), pFunc);
845
    }
846
    *len +=
×
UNCOV
847
        snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
×
848
  }
849

850
  if (pCfg->ttl > 0) {
73,759✔
UNCOV
851
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
852
                     " TTL %d", pCfg->ttl);
853
  }
854

855
  if (pCfg->keep > 0) {
73,759✔
856
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,730✔
857
                     " KEEP %dm", pCfg->keep);
858
  }
859

860
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
73,759✔
861
    int32_t nSma = 0;
40,348✔
862
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
226,243✔
863
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
185,895✔
864
        ++nSma;
185,895✔
865
      }
866
    }
867

868
    if (nSma < pCfg->numOfColumns && nSma > 0) {
40,348✔
869
      bool smaOn = false;
×
UNCOV
870
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
871
                       " SMA(");
872
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
873
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
874
          if (smaOn) {
×
875
            *len += snprintf(buf + VARSTR_HEADER_SIZE + *len,
×
876
                             SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ",`%s`",
×
UNCOV
877
                             (pCfg->pSchemas + i)->name);
×
878
          } else {
879
            smaOn = true;
×
880
            *len += snprintf(buf + VARSTR_HEADER_SIZE + *len,
×
881
                             SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), "`%s`",
×
UNCOV
882
                             (pCfg->pSchemas + i)->name);
×
883
          }
884
        }
885
      }
UNCOV
886
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, ")");
×
887
    }
888
  }
889

890
  if (pCfg->virtualStb) {
73,759✔
891
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,771✔
892
                     " VIRTUAL %d", pCfg->virtualStb);
1,771✔
893
  }
894

895
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
73,759✔
896
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
26,382✔
897
                      " SECURE_DELETE %d", pCfg->secureDelete);
26,382✔
898
  }
899
}
73,759✔
900

901
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
87,202✔
902
                                              void* charsetCxt) {
903
  int32_t code = TSDB_CODE_SUCCESS;
87,202✔
904
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
87,202✔
905
  pBlock->info.rows = 1;
87,202✔
906

907
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
87,202✔
908
  char             buf1[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1)] = {0};
87,202✔
909
  STR_TO_VARSTR(buf1, tbName);
87,202✔
910
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
87,202✔
911

912
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
87,202✔
913
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
87,202✔
914
  if (NULL == buf2) {
87,202✔
UNCOV
915
    QRY_ERR_RET(terrno);
×
916
  }
917

918
  int32_t len = 0;
87,202✔
919

920
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
87,202✔
921
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
26,382✔
922
                    "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
923
    appendColumnFields(buf2, &len, pCfg);
26,382✔
924
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
26,382✔
925
                    ") TAGS (");
926
    appendTagFields(buf2, &len, pCfg);
26,382✔
927
    len +=
26,382✔
928
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
26,382✔
929
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
26,382✔
930
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
60,820✔
931
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
33,411✔
932
                    "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
933
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
33,411✔
934
                    "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
33,411✔
935
    appendTagNameFields(buf2, &len, pCfg);
33,411✔
936
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
33,411✔
937
                    ") TAGS (");
938
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
33,411✔
939
    TAOS_CHECK_ERRNO(code);
33,411✔
940
    len +=
33,411✔
941
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
33,411✔
942
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
33,411✔
943
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
27,409✔
944
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
13,966✔
945
                    "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
946
    appendColumnFields(buf2, &len, pCfg);
13,966✔
947
    len +=
13,966✔
948
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
13,966✔
949
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
13,966✔
950
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
13,443✔
951
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
3,628✔
952
                    "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
953
    appendColumnFields(buf2, &len, pCfg);
3,628✔
954
    len +=
3,628✔
955
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
3,628✔
956
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
9,815✔
957
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
4,295✔
958
                     "CREATE VTABLE `%s`", expandIdentifier(tbName, buf1));
959
    appendColRefFields(buf2, &len, pCfg);
4,295✔
960
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
4,295✔
961
                    " USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
4,295✔
962
    appendTagNameFields(buf2, &len, pCfg);
4,295✔
963
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
4,295✔
964
                    ") TAGS (");
965
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
4,295✔
966
    TAOS_CHECK_ERRNO(code);
4,295✔
967
    len +=
4,295✔
968
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
4,295✔
969
  }
970

971
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
87,202✔
972

973
  code = colDataSetVal(pCol2, 0, buf2, false);
87,202✔
974
  TAOS_CHECK_ERRNO(code);
87,202✔
975

976
_exit:
87,202✔
977
  taosMemoryFree(buf2);
87,202✔
978

979
  return code;
87,202✔
980
}
981

982
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
6,708✔
983
  int32_t code = 0;
6,708✔
984
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
6,708✔
985
  pBlock->info.rows = 1;
6,708✔
986

987
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
6,708✔
988
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
6,708✔
989
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
6,708✔
990
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
6,708✔
991
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
6,708✔
992

993
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
6,708✔
994
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
6,708✔
995
  if (NULL == buf2) {
6,708✔
UNCOV
996
    return terrno;
×
997
  }
998

999
  SViewMeta* pMeta = pStmt->pViewMeta;
6,708✔
1000
  if (NULL == pMeta) {
6,708✔
1001
    qError("exception: view meta is null");
×
1002
    taosMemoryFree(buf2);
×
UNCOV
1003
    return TSDB_CODE_APP_ERROR;
×
1004
  }
1005
  snprintf(varDataVal(buf2), SHOW_CREATE_VIEW_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE VIEW `%s`.`%s` AS %s",
6,708✔
1006
           pStmt->dbName, pStmt->viewName, pMeta->querySql);
6,708✔
1007
  int32_t len = strlen(varDataVal(buf2));
6,708✔
1008
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
6,708✔
1009
  code = colDataSetVal(pCol2, 0, buf2, false);
6,708✔
1010
  taosMemoryFree(buf2);
6,708✔
1011

1012
  return code;
6,708✔
1013
}
1014

1015
extern const char* fmGetFuncName(int32_t funcId);
1016
static int32_t setCreateRsmaResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateRsmaStmt* pStmt) {
2,124✔
1017
  int32_t       code = 0, lino = 0;
2,124✔
1018
  char*         buf2 = NULL;
2,124✔
1019
  SRsmaInfoRsp* pMeta = pStmt->pRsmaMeta;
2,124✔
1020

1021
  if (pMeta->nFuncs != pMeta->nColNames) {
2,124✔
1022
    qError("exception: rsma meta is invalid, nFuncs:%d != nColNames:%d", pMeta->nFuncs, pMeta->nColNames);
×
UNCOV
1023
    return TSDB_CODE_APP_ERROR;
×
1024
  }
1025

1026
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, 1));
2,124✔
1027
  pBlock->info.rows = 1;
2,124✔
1028

1029
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
2,124✔
1030
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1] = {0};
2,124✔
1031
  snprintf(varDataVal(buf1), TSDB_TABLE_FNAME_LEN + 4, "`%s`", expandIdentifier(pStmt->rsmaName, buf1));
2,124✔
1032
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
2,124✔
1033
  TAOS_CHECK_EXIT(colDataSetVal(pCol1, 0, buf1, false));
2,124✔
1034

1035
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
2,124✔
1036
  if (!(buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN))) {
2,124✔
UNCOV
1037
    return terrno;
×
1038
  }
1039

1040
  SName name = {0};
2,124✔
1041
  TAOS_CHECK_EXIT(tNameFromString(&name, pMeta->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
2,124✔
1042

1043
  int32_t len = 0;
2,124✔
1044
  len += tsnprintf(varDataVal(buf2), SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
4,248✔
1045
                       "CREATE RSMA `%s` ON `%s`.`%s` FUNCTION(", expandIdentifier(pStmt->rsmaName, buf1),
2,124✔
1046
                       expandIdentifier(name.dbname, buf1), expandIdentifier(name.tname, buf1));
1047
  for (int32_t i = 0; i < pMeta->nFuncs; ++i) {
13,620✔
1048
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
22,992✔
1049
                         "%s%s(`%s`)", (i > 0) ? "," : "", fmGetFuncName(pMeta->funcIds[i]),
11,496✔
1050
                         expandIdentifier(*(char**)TARRAY_GET_ELEM(pMeta->colNames, i), buf1));
11,496✔
1051
  }
1052
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
2,124✔
1053
                       ") INTERVAL(%d%c", pMeta->interval[0], pMeta->intervalUnit);
2,124✔
1054
  if (pMeta->interval[1] > 0) {
2,124✔
1055
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ",%d%c",
2,124✔
1056
                         pMeta->interval[1], pMeta->intervalUnit);
2,124✔
1057
  }
1058
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
2,124✔
1059
  varDataLen(buf2) = len;
2,124✔
1060
  code = colDataSetVal(pCol2, 0, buf2, false);
2,124✔
1061
_exit:
2,124✔
1062
  taosMemoryFree(buf2);
2,124✔
1063
  return code;
2,124✔
1064
}
1065

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

1079
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
7,262✔
1080
  SSDataBlock* pBlock = NULL;
7,262✔
1081
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
7,262✔
1082
  if (TSDB_CODE_SUCCESS == code) {
7,262✔
1083
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
7,262✔
1084
  }
1085
  if (TSDB_CODE_SUCCESS == code) {
7,262✔
1086
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
7,262✔
1087
  }
1088
  (void)blockDataDestroy(pBlock);
7,262✔
1089
  return code;
7,262✔
1090
}
1091

1092
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
1,176✔
1093
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
1,176✔
1094
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
1,176✔
1095
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
184✔
1096
    return terrno;
184✔
1097
  }
1098

1099
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
992✔
1100
}
1101

1102
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
493,438✔
1103
  int32_t code = 0;
493,438✔
1104

1105
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
493,438✔
1106
    taosResetLog();
50✔
1107
    cfgDumpCfg(tsCfg, 0, false);
50✔
1108
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
493,388✔
1109
    int32_t tmp = 0;
2,578✔
1110
    code = taosStr2int32(value, &tmp);
2,578✔
1111
    if (code) {
2,578✔
1112
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
UNCOV
1113
      return code;
×
1114
    }
1115
    code = schedulerUpdatePolicy(tmp);
2,578✔
1116
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
490,810✔
1117
    int32_t tmp = 0;
×
1118
    code = taosStr2int32(value, &tmp);
×
1119
    if (code) {
×
1120
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
UNCOV
1121
      return code;
×
1122
    }
UNCOV
1123
    code = schedulerEnableReSchedule(tmp != 0);
×
1124
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
490,810✔
UNCOV
1125
    code = ctgdHandleDbgCommand(value);
×
1126
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
490,810✔
1127
    code = taosMemoryDbgInit();
×
1128
    if (code) {
×
1129
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
UNCOV
1130
      return code;
×
1131
    }
1132
    tsAsyncLog = false;
×
UNCOV
1133
    qInfo("memory dbg enabled");
×
1134
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
490,810✔
1135
    code = taosMemoryDbgInitRestore();
×
1136
    if (code) {
×
1137
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
UNCOV
1138
      return code;
×
1139
    }
UNCOV
1140
    qInfo("memory dbg disabled");
×
1141
  } else {
1142
    goto _return;
490,810✔
1143
  }
1144

1145
  *processed = true;
2,628✔
1146

1147
_return:
493,438✔
1148

1149
  if (code) {
493,438✔
UNCOV
1150
    terrno = code;
×
1151
  }
1152

1153
  return code;
493,438✔
1154
}
1155

1156
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
493,438✔
1157
  bool processed = false;
493,438✔
1158

1159
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
493,438✔
UNCOV
1160
    return terrno;
×
1161
  }
1162

1163
  if (processed) {
493,438✔
1164
    goto _return;
2,628✔
1165
  }
1166

1167
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
490,810✔
1168
    return terrno;
1,711✔
1169
  }
1170

1171
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
489,099✔
UNCOV
1172
    return terrno;
×
1173
  }
1174

1175
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
489,099✔
1176

1177
_return:
491,727✔
1178

1179
  return TSDB_CODE_SUCCESS;
491,727✔
1180
}
1181

1182
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
15,194✔
1183
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
15,194✔
1184
  if (NULL == pBlock) {
15,194✔
UNCOV
1185
    return terrno;
×
1186
  }
1187

1188
  pBlock->info.hasVarCol = true;
15,194✔
1189

1190
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
15,194✔
1191
  if (NULL == pBlock->pDataBlock) {
15,194✔
1192
    taosMemoryFree(pBlock);
×
UNCOV
1193
    return terrno;
×
1194
  }
1195

1196
  SColumnInfoData infoData = {0};
15,194✔
1197

1198
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
15,194✔
1199
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
15,194✔
1200
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
30,388✔
UNCOV
1201
    goto _exit;
×
1202
  }
1203

1204
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
15,194✔
1205
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
15,194✔
1206
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
30,388✔
UNCOV
1207
    goto _exit;
×
1208
  }
1209

1210
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
15,194✔
1211
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
15,194✔
1212
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
30,388✔
UNCOV
1213
    goto _exit;
×
1214
  }
1215

1216
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
15,194✔
1217
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
15,194✔
1218
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
30,388✔
UNCOV
1219
    goto _exit;
×
1220
  }
1221

1222
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
15,194✔
1223
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
15,194✔
1224
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
30,388✔
UNCOV
1225
    goto _exit;
×
1226
  }
1227

1228
  *pOutput = pBlock;
15,194✔
1229

1230
_exit:
15,194✔
1231
  if (terrno != TSDB_CODE_SUCCESS) {
15,194✔
1232
    taosArrayDestroy(pBlock->pDataBlock);
×
UNCOV
1233
    taosMemoryFree(pBlock);
×
1234
  }
1235
  return terrno;
15,194✔
1236
}
1237

1238
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
15,194✔
1239
  SSDataBlock* pBlock = NULL;
15,194✔
1240
  char*        likePattern = NULL;
15,194✔
1241
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
15,194✔
1242
  if (TSDB_CODE_SUCCESS == code) {
15,194✔
1243
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
15,194✔
1244
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
2,891✔
1245
    }
1246
  }
1247
  if (TSDB_CODE_SUCCESS == code) {
15,194✔
1248
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
15,194✔
1249
  }
1250
  if (TSDB_CODE_SUCCESS == code) {
15,194✔
1251
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
15,194✔
1252
  }
1253
  (void)blockDataDestroy(pBlock);
15,194✔
1254
  return code;
15,194✔
1255
}
1256

1257
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
2,601,614✔
1258
  QRY_PARAM_CHECK(pOutput);
2,601,614✔
1259

1260
  SSDataBlock* pBlock = NULL;
2,601,614✔
1261
  int32_t      code = createDataBlock(&pBlock);
2,601,614✔
1262
  if (code) {
2,601,614✔
UNCOV
1263
    return code;
×
1264
  }
1265

1266
  SNode* pProj = NULL;
2,601,614✔
1267
  FOREACH(pProj, pProjects) {
6,627,896✔
1268
    SExprNode*      pExpr = (SExprNode*)pProj;
4,026,282✔
1269
    SColumnInfoData infoData = {0};
4,026,282✔
1270
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
4,026,282✔
1271
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
UNCOV
1272
      infoData.info.bytes = 0;
×
1273
    } else {
1274
      infoData.info.type = pExpr->resType.type;
4,026,282✔
1275
      infoData.info.bytes = pExpr->resType.bytes;
4,026,282✔
1276
      infoData.info.precision = pExpr->resType.precision;
4,026,282✔
1277
      infoData.info.scale = pExpr->resType.scale;
4,026,282✔
1278
    }
1279
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
4,026,282✔
1280
  }
1281

1282
  *pOutput = pBlock;
2,601,614✔
1283
  return code;
2,601,614✔
1284
}
1285

1286
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
2,601,614✔
1287
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
2,601,614✔
1288

1289
  int32_t index = 0;
2,601,614✔
1290
  SNode*  pProj = NULL;
2,601,614✔
1291
  FOREACH(pProj, pProjects) {
6,627,896✔
1292
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
4,026,282✔
UNCOV
1293
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1294
    } else {
1295
      if (((SValueNode*)pProj)->isNull) {
4,026,282✔
1296
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
84,978✔
1297
      } else {
1298
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
3,941,304✔
1299
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1300
      }
1301
    }
1302
  }
1303

1304
  pBlock->info.rows = 1;
2,601,614✔
1305
  return TSDB_CODE_SUCCESS;
2,601,614✔
1306
}
1307

1308
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
2,601,614✔
1309
  SSDataBlock* pBlock = NULL;
2,601,614✔
1310
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
2,601,614✔
1311
  if (TSDB_CODE_SUCCESS == code) {
2,601,614✔
1312
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
2,601,614✔
1313
  }
1314
  if (TSDB_CODE_SUCCESS == code) {
2,601,614✔
1315
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
2,601,614✔
1316
  }
1317
  (void)blockDataDestroy(pBlock);
2,601,614✔
1318
  return code;
2,601,614✔
1319
}
1320

1321
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
6,708✔
1322
  SSDataBlock* pBlock = NULL;
6,708✔
1323
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
6,708✔
1324
  if (TSDB_CODE_SUCCESS == code) {
6,708✔
1325
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
6,708✔
1326
  }
1327
  if (TSDB_CODE_SUCCESS == code) {
6,708✔
1328
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
6,708✔
1329
  }
1330
  (void)blockDataDestroy(pBlock);
6,708✔
1331
  return code;
6,708✔
1332
}
1333

1334
static int32_t execShowCreateRsma(SShowCreateRsmaStmt* pStmt, SRetrieveTableRsp** pRsp) {
2,124✔
1335
  SSDataBlock* pBlock = NULL;
2,124✔
1336
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
2,124✔
1337
  if (TSDB_CODE_SUCCESS == code) {
2,124✔
1338
    code = setCreateRsmaResultIntoDataBlock(pBlock, pStmt);
2,124✔
1339
  }
1340
  if (TSDB_CODE_SUCCESS == code) {
2,124✔
1341
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
2,124✔
1342
  }
1343
  (void)blockDataDestroy(pBlock);
2,124✔
1344
  return code;
2,124✔
1345
}
1346

1347
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
5,841,825✔
1348
                     void* charsetCxt) {
1349
  switch (nodeType(pStmt)) {
5,841,825✔
1350
    case QUERY_NODE_DESCRIBE_STMT:
437,292✔
1351
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
437,292✔
1352
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
2,124,313✔
1353
      return execResetQueryCache();
2,124,313✔
1354
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
73,755✔
1355
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
73,755✔
1356
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
78,948✔
1357
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
78,948✔
1358
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
7,262✔
1359
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
7,262✔
1360
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
1,176✔
1361
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,176✔
1362
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
6,708✔
1363
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
6,708✔
1364
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
2,124✔
1365
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
2,124✔
1366
    case QUERY_NODE_ALTER_LOCAL_STMT:
493,438✔
1367
      return execAlterLocal((SAlterLocalStmt*)pStmt);
493,438✔
1368
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
15,194✔
1369
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
15,194✔
1370
    case QUERY_NODE_SELECT_STMT:
2,601,614✔
1371
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
2,601,614✔
1372
    default:
40✔
1373
      break;
40✔
1374
  }
1375
  return TSDB_CODE_FAILED;
40✔
1376
}
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