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

taosdata / TDengine / #4905

29 Dec 2025 02:08PM UTC coverage: 65.423% (-0.3%) from 65.734%
#4905

push

travis-ci

web-flow
enh: sign connect request (#34067)

23 of 29 new or added lines in 4 files covered. (79.31%)

11614 existing lines in 186 files now uncovered.

193476 of 295730 relevant lines covered (65.42%)

115752566.53 hits per line

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

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

52
  (*pRsp)->useconds = 0;
2,417,145✔
53
  (*pRsp)->completed = 1;
2,417,145✔
54
  (*pRsp)->precision = 0;
2,417,145✔
55
  (*pRsp)->compressed = 0;
2,417,145✔
56

57
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
2,417,145✔
58
  (*pRsp)->numOfCols = htonl(numOfCols);
2,417,145✔
59

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

71
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
2,417,145✔
72
  (*pRsp)->payloadLen = htonl(payloadLen);
2,417,145✔
73
  (*pRsp)->compLen = htonl(payloadLen);
2,417,145✔
74

75
  return TSDB_CODE_SUCCESS;
2,417,145✔
76
}
77

78
static int32_t getSchemaBytes(const SSchema* pSchema) {
55,426,642✔
79
  switch (pSchema->type) {
55,426,642✔
80
    case TSDB_DATA_TYPE_BINARY:
2,364,122✔
81
    case TSDB_DATA_TYPE_VARBINARY:
82
    case TSDB_DATA_TYPE_GEOMETRY:
83
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
2,364,122✔
84
    case TSDB_DATA_TYPE_NCHAR:
3,879,039✔
85
    case TSDB_DATA_TYPE_JSON:
86
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
3,879,039✔
87
    default:
49,183,481✔
88
      return pSchema->bytes;
49,183,481✔
89
  }
90
}
91

92
static const char* expandIdentifier(const char* name, char* output) {
385,892✔
93
  if (NULL == name) return "";
385,892✔
94
  bool containsEscapeChar = false;
385,892✔
95
  for (const char* p = name; *p != '\0'; ++p) {
2,012,371✔
96
    if (*p == TS_ESCAPE_CHAR) {
1,670,669✔
97
      containsEscapeChar = true;
44,190✔
98
      break;
44,190✔
99
    }
100
  }
101
  if (!containsEscapeChar) return name;
385,892✔
102
  if (NULL == output) return "";
44,190✔
103
  char* out_ptr = output;
44,190✔
104
  for (const char* src = name; *src != '\0'; ++src) {
272,014✔
105
    if (*src == TS_ESCAPE_CHAR) {
227,824✔
106
      *out_ptr++ = TS_ESCAPE_CHAR;
110,966✔
107
      *out_ptr++ = TS_ESCAPE_CHAR;
110,966✔
108
    } else {
109
      *out_ptr++ = *src;
116,858✔
110
    }
111
  }
112
  *out_ptr = '\0';
44,190✔
113
  return output;
44,190✔
114
}
115

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

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

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

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

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

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

195
  int32_t fillTagCol = 0;
377,084✔
196
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
377,084✔
197
  for (int32_t i = 0; i < numOfRows; ++i) {
55,803,688✔
198
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
55,426,604✔
199
      continue;
×
200
    }
201
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
55,426,604✔
202
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
55,426,604✔
203

204
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
55,426,642✔
205
      uint8_t prec = 0, scale = 0;
239,094✔
206
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
239,094✔
207
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
239,094✔
208
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
239,094✔
209
      varDataSetLen(buf, len);
239,094✔
210
    } else {
211
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
55,187,548✔
212
    }
213
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
55,426,642✔
214
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
55,426,642✔
215
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
55,426,642✔
216
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
55,426,642✔
217
      if (i >= pMeta->tableInfo.numOfColumns) {
55,426,642✔
218
        STR_TO_VARSTR(buf, "TAG");
2,653,405✔
219
        fillTagCol = 1;
2,653,405✔
220
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
52,773,237✔
221
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
9,762✔
222
      } else {
223
        STR_TO_VARSTR(buf, "");
52,763,475✔
224
      }
225
    } else {
UNCOV
226
      STR_TO_VARSTR(buf, "VIEW COL");
×
227
    }
228
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
55,426,642✔
229
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
55,426,642✔
230
      if (i < pMeta->tableInfo.numOfColumns) {
55,380,255✔
231
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
52,731,818✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
52,731,818✔
233
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
52,731,818✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
52,731,818✔
235
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
52,731,818✔
236
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
52,731,818✔
237
      } else {
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,648,437✔
239
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
2,648,437✔
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,648,437✔
241
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
2,648,437✔
242
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,648,437✔
243
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
2,648,437✔
244
      }
245
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
46,387✔
246
      if (i < pMeta->numOfColRefs) {
33,072✔
247
        if (pMeta->colRef[i].hasRef) {
28,104✔
248
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
11,736✔
249
          strcat(refColName, pMeta->colRef[i].refDbName);
11,736✔
250
          strcat(refColName, ".");
11,736✔
251
          strcat(refColName, pMeta->colRef[i].refTableName);
11,736✔
252
          strcat(refColName, ".");
11,736✔
253
          strcat(refColName, pMeta->colRef[i].refColName);
11,736✔
254
          STR_TO_VARSTR(buf, refColName);
11,736✔
255
        } else {
256
          STR_TO_VARSTR(buf, "");
16,368✔
257
        }
258
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
28,104✔
259
      } else {
260
        STR_TO_VARSTR(buf, "");
4,968✔
261
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
4,968✔
262
      }
263
    }
264

265
    fillTagCol = 0;
55,426,604✔
266

267
    ++(pBlock->info.rows);
55,426,604✔
268
  }
269
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
377,084✔
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) {
377,084✔
281
    qError("no permission to view any columns");
×
282
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
283
  }
284
  return TSDB_CODE_SUCCESS;
377,084✔
285
}
286

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

294
  SSDataBlock* pBlock = NULL;
377,084✔
295
  int32_t      code = buildDescResultDataBlock(&pBlock);
377,084✔
296
  if (TSDB_CODE_SUCCESS == code) {
377,084✔
297
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
377,084✔
298
  }
299
  if (TSDB_CODE_SUCCESS == code) {
377,084✔
300
    if (pDesc->pMeta) {
377,084✔
301
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
377,084✔
302
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
373,386✔
303
      } else if (hasRefCol(pDesc->pMeta->tableType) && pDesc->pMeta->colRef) {
3,698✔
304
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
2,568✔
305
      } else {
306
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
1,130✔
307
      }
308
    } else {
309
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
310
    }
311
  }
312
  (void)blockDataDestroy(pBlock);
377,084✔
313
  return code;
377,084✔
314
}
315

316
static int32_t execResetQueryCache() { return catalogClearCache(); }
1,916,465✔
317

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

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

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

334
  if (TSDB_CODE_SUCCESS == code) {
54,183✔
335
    *pOutput = pBlock;
54,183✔
336
  } else {
337
    (void)blockDataDestroy(pBlock);
×
338
  }
339
  return code;
54,183✔
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) {
54,183✔
368
  size_t size = taosArrayGetSize(pRetension);
54,183✔
369
  if (size == 0) {
54,183✔
370
    *ppRetentions = NULL;
54,183✔
371
    return TSDB_CODE_SUCCESS;
54,183✔
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) {
53,869✔
401
  switch (cacheModel) {
53,869✔
402
    case TSDB_CACHE_MODEL_NONE:
53,869✔
403
      return TSDB_CACHE_MODEL_NONE_STR;
53,869✔
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) {
53,869✔
417
  if (algorithmsId[0] != '\0') {
53,869✔
418
    return algorithmsId;
×
419
  } else if (encryptAlgorithm != 0) {
53,869✔
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;
53,869✔
432
}
433

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

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

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

460
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
54,183✔
461
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
54,183✔
462
  int32_t          len = 0;
54,183✔
463
  char*            prec = NULL;
54,183✔
464
  switch (pCfg->precision) {
54,183✔
465
    case TSDB_TIME_PRECISION_MILLI:
54,183✔
466
      prec = TSDB_TIME_PRECISION_MILLI_STR;
54,183✔
467
      break;
54,183✔
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;
54,183✔
480
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
54,183✔
481
  int32_t dbFNameLen = strlen(dbFName);
54,183✔
482
  int32_t hashPrefix = 0;
54,183✔
483
  if (pCfg->hashPrefix > 0) {
54,183✔
484
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
1,394✔
485
  } else if (pCfg->hashPrefix < 0) {
52,789✔
486
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
2,091✔
487
  }
488
  char durationStr[128] = {0};
54,183✔
489
  char keep0Str[128] = {0};
54,183✔
490
  char keep1Str[128] = {0};
54,183✔
491
  char keep2Str[128] = {0};
54,183✔
492
  char compactIntervalStr[13] = {0};
54,183✔
493
  char compactStartTimeStr[13] = {0};
54,183✔
494
  char compactEndTimeStr[13] = {0};
54,183✔
495

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

504
  if (IS_SYS_DBNAME(dbName)) {
54,183✔
505
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
314✔
506
                     "CREATE DATABASE `%s`", dbName);
314✔
507
  } else {
508
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
323,214✔
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,
53,869✔
517
                     durationStr, pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str,
53,869✔
518
                     keep1Str, keep2Str, pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel,
53,869✔
519
                     pCfg->numOfVgroups, 1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize,
53,869✔
520
                     pCfg->walRetentionPeriod, pCfg->walRetentionSize, pCfg->keepTimeOffset,
521
                     encryptAlgorithmStr(pCfg->encryptAlgr, pCfg->algorithmsId), pCfg->ssChunkSize, pCfg->ssKeepLocal,
53,869✔
522
                     pCfg->ssCompact, compactIntervalStr, compactStartTimeStr, compactEndTimeStr,
53,869✔
523
                     pCfg->compactTimeOffset, pCfg->isAudit);
53,869✔
524

525
    if (pRetentions) {
53,869✔
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);
54,183✔
532

533
  (varDataLen(buf2)) = len;
54,183✔
534

535
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
54,183✔
536

537
  return TSDB_CODE_SUCCESS;
54,183✔
538
}
539

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

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

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

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

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

UNCOV
577
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
×
UNCOV
578
  QRY_PARAM_CHECK(pOutput);
×
579

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

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

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

601
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
33,066✔
602
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
33,066✔
603
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
192,024✔
604
    SSchema* pSchema = pCfg->pSchemas + i;
158,958✔
605
    SColRef* pRef = pCfg->pColRefs + i;
158,958✔
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];
158,958✔
610
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
158,958✔
611
    int typeLen = strlen(type);
158,958✔
612
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
158,958✔
613
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
139,245✔
614
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
21,025✔
615
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
137,933✔
616
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
3,151✔
617
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
3,151✔
618
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
134,782✔
619
      uint8_t precision, scale;
16,632✔
620
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
16,632✔
621
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
16,632✔
622
    }
623

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

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

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

653
static void appendColRefFields(char* buf, int32_t* len, STableCfg* pCfg) {
1,582✔
654
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
1,582✔
655
  for (int32_t i = 1; i < pCfg->numOfColumns; ++i) {
14,946✔
656
    SSchema* pSchema = pCfg->pSchemas + i;
13,364✔
657
    SColRef* pRef = pCfg->pColRefs + i;
13,364✔
658
    char     type[TSDB_COL_NAME_LEN + 10 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN];
13,364✔
659
    int      typeLen = 0;
13,364✔
660

661
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
13,364✔
662
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
4,964✔
663
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
4,964✔
664
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
4,964✔
665
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
4,964✔
666
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
4,964✔
667
    } else {
668
      continue;
8,400✔
669
    }
670

671
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
4,964✔
672
                      "%s`%s` %s", ((i > 1) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
4,964✔
673

674
  }
675
}
1,582✔
676

677
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
19,185✔
678
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
19,185✔
679
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
57,858✔
680
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
38,673✔
681
    char     type[32];
38,673✔
682
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
38,673✔
683
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
38,673✔
684
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
30,862✔
685
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
7,867✔
686
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
7,867✔
687
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
30,806✔
688
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
667✔
689
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
667✔
690
    }
691

692
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
38,673✔
693
                      "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
38,673✔
694
  }
695
}
19,185✔
696

697
static void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
31,653✔
698
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
31,653✔
699
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
77,917✔
700
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
46,264✔
701
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
46,264✔
702
                      "%s`%s`", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName));
46,264✔
703
  }
704
}
31,653✔
705

706
static int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg, void* charsetCxt) {
31,653✔
707
  int32_t code = TSDB_CODE_SUCCESS;
31,653✔
708
  SArray* pTagVals = NULL;
31,653✔
709
  STag*   pTag = (STag*)pCfg->pTags;
31,653✔
710

711
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
31,653✔
712
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
713
    return TSDB_CODE_APP_ERROR;
×
714
  }
715

716
  if (tTagIsJson(pTag)) {
31,653✔
717
    char* pJson = NULL;
2,185✔
718
    parseTagDatatoJson(pTag, &pJson, charsetCxt);
2,185✔
719
    if (NULL == pJson) {
2,185✔
720
      qError("failed to parse tag to json, pJson is NULL");
×
721
      return terrno;
×
722
    }
723
    char* escapedJson = taosMemCalloc(sizeof(char), strlen(pJson) * 2 + 1);  // taosMemoryAlloc(strlen(pJson) * 2 + 1);
2,185✔
724
    if (escapedJson) {
2,185✔
725
      char* writer = escapedJson;
2,185✔
726
      for (char* reader = pJson; *reader; ++reader) {
43,700✔
727
        if (*reader == '\'') {
41,515✔
728
          *writer++ = '\'';  // Escape single quote by doubling it
437✔
729
        }
730
        *writer++ = *reader;
41,515✔
731
      }
732
      *writer = '\0';
2,185✔
733

734
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,185✔
735
                        "'%s'", escapedJson);
736
      taosMemoryFree(escapedJson);
2,185✔
737
    } else {
738
      taosMemoryFree(pJson);
×
739
      return terrno;
×
740
    }
741
    taosMemoryFree(pJson);
2,185✔
742

743
    return TSDB_CODE_SUCCESS;
2,185✔
744
  }
745

746
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
29,468✔
747
  int16_t valueNum = taosArrayGetSize(pTagVals);
29,468✔
748
  int32_t num = 0;
29,468✔
749
  int32_t j = 0;
29,468✔
750
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
73,547✔
751
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
44,079✔
752
    if (i > 0) {
44,079✔
753
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
14,611✔
754
                        ", ");
755
    }
756

757
    if (j >= valueNum) {
44,079✔
758
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
14,589✔
759
                        "NULL");
760
      continue;
14,589✔
761
    }
762

763
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
29,490✔
764
    if (pSchema->colId > pTagVal->cid) {
29,490✔
765
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
766
      code = TSDB_CODE_APP_ERROR;
×
767
      TAOS_CHECK_ERRNO(code);
×
768
    } else if (pSchema->colId == pTagVal->cid) {
29,490✔
769
      char    type = pTagVal->type;
28,040✔
770
      int32_t tlen = 0;
28,040✔
771

772
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
28,040✔
773
      if (leftSize <= 0) {
28,040✔
774
        qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
×
775
        code = TSDB_CODE_APP_ERROR;
×
776
        TAOS_CHECK_ERRNO(code);
×
777
      }
778
      if (IS_VAR_DATA_TYPE(type)) {
28,040✔
779
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
6,734✔
780
        TAOS_CHECK_ERRNO(code);
6,734✔
781
      } else {
782
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
21,306✔
783
                               &tlen);
784
        TAOS_CHECK_ERRNO(code);
21,306✔
785
      }
786
      *len += tlen;
28,040✔
787
      j++;
28,040✔
788
    } else {
789
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,450✔
790
                        "NULL");
791
    }
792
  }
793
_exit:
29,468✔
794
  taosArrayDestroy(pTagVals);
29,468✔
795

796
  return code;
29,468✔
797
}
798

799
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
61,555✔
800
  if (pCfg->commentLen > 0) {
61,555✔
801
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
802
                      " COMMENT '%s'", pCfg->pComment);
803
  } else if (0 == pCfg->commentLen) {
61,555✔
804
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,368✔
805
                      " COMMENT ''");
806
  }
807

808
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
61,555✔
809
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
810
                      " WATERMARK %" PRId64 "a", pCfg->watermark1);
811
    if (pCfg->watermark2 > 0) {
×
812
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
813
                        ", %" PRId64 "a", pCfg->watermark2);
814
    }
815
  }
816

817
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
61,555✔
818
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
819
                      " MAX_DELAY %" PRId64 "a", pCfg->delay1);
820
    if (pCfg->delay2 > 0) {
×
821
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
822
                        ", %" PRId64 "a", pCfg->delay2);
823
    }
824
  }
825

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

839
  if (pCfg->ttl > 0) {
61,555✔
840
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
841
                      " TTL %d", pCfg->ttl);
842
  }
843

844
  if (pCfg->keep > 0) {
61,555✔
845
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,510✔
846
                      " KEEP %dm", pCfg->keep);
847
  }
848

849
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
61,555✔
850
    int32_t nSma = 0;
31,484✔
851
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
175,496✔
852
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
144,012✔
853
        ++nSma;
144,012✔
854
      }
855
    }
856

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

879
  if (pCfg->virtualStb) {
61,555✔
880
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
982✔
881
                      " VIRTUAL %d", pCfg->virtualStb);
982✔
882
  }
883
}
61,555✔
884

885
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
69,429✔
886
                                              void* charsetCxt) {
887
  int32_t code = TSDB_CODE_SUCCESS;
69,429✔
888
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
69,429✔
889
  pBlock->info.rows = 1;
69,429✔
890

891
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
69,429✔
892
  char             buf1[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1)] = {0};
69,429✔
893
  STR_TO_VARSTR(buf1, tbName);
69,429✔
894
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
69,429✔
895

896
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
69,429✔
897
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
69,429✔
898
  if (NULL == buf2) {
69,429✔
899
    QRY_ERR_RET(terrno);
×
900
  }
901

902
  int32_t len = 0;
69,429✔
903

904
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
69,429✔
905
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
19,185✔
906
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
907
    appendColumnFields(buf2, &len, pCfg);
19,185✔
908
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
19,185✔
909
                     ") TAGS (");
910
    appendTagFields(buf2, &len, pCfg);
19,185✔
911
    len +=
19,185✔
912
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
19,185✔
913
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
19,185✔
914
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
50,244✔
915
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
30,071✔
916
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
917
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
30,071✔
918
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
30,071✔
919
    appendTagNameFields(buf2, &len, pCfg);
30,071✔
920
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
30,071✔
921
                     ") TAGS (");
922
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
30,071✔
923
    TAOS_CHECK_ERRNO(code);
30,071✔
924
    len +=
30,071✔
925
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
30,071✔
926
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
30,071✔
927
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
20,173✔
928
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
12,299✔
929
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
930
    appendColumnFields(buf2, &len, pCfg);
12,299✔
931
    len +=
12,299✔
932
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
12,299✔
933
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
12,299✔
934
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
7,874✔
935
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1,582✔
936
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
937
    appendColumnFields(buf2, &len, pCfg);
1,582✔
938
    len +=
1,582✔
939
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,582✔
940
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
6,292✔
941
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1,582✔
942
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
943
    appendColRefFields(buf2, &len, pCfg);
1,582✔
944
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,582✔
945
                    ") USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
1,582✔
946
    appendTagNameFields(buf2, &len, pCfg);
1,582✔
947
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,582✔
948
                     ") TAGS (");
949
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
1,582✔
950
    TAOS_CHECK_ERRNO(code);
1,582✔
951
    len +=
1,582✔
952
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,582✔
953
  }
954

955
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
69,429✔
956

957
  code = colDataSetVal(pCol2, 0, buf2, false);
69,429✔
958
  TAOS_CHECK_ERRNO(code);
69,429✔
959

960
_exit:
69,429✔
961
  taosMemoryFree(buf2);
69,429✔
962

963
  return code;
69,429✔
964
}
965

UNCOV
966
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
×
UNCOV
967
  int32_t code = 0;
×
UNCOV
968
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
×
UNCOV
969
  pBlock->info.rows = 1;
×
970

UNCOV
971
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
×
UNCOV
972
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
×
UNCOV
973
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
×
UNCOV
974
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
×
UNCOV
975
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
×
976

UNCOV
977
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
×
UNCOV
978
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
×
UNCOV
979
  if (NULL == buf2) {
×
980
    return terrno;
×
981
  }
982

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

UNCOV
996
  return code;
×
997
}
998

999
extern const char* fmGetFuncName(int32_t funcId);
1000
static int32_t setCreateRsmaResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateRsmaStmt* pStmt) {
1,410✔
1001
  int32_t       code = 0, lino = 0;
1,410✔
1002
  char*         buf2 = NULL;
1,410✔
1003
  SRsmaInfoRsp* pMeta = pStmt->pRsmaMeta;
1,410✔
1004

1005
  if (pMeta->nFuncs != pMeta->nColNames) {
1,410✔
1006
    qError("exception: rsma meta is invalid, nFuncs:%d != nColNames:%d", pMeta->nFuncs, pMeta->nColNames);
×
1007
    return TSDB_CODE_APP_ERROR;
×
1008
  }
1009

1010
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, 1));
1,410✔
1011
  pBlock->info.rows = 1;
1,410✔
1012

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

1019
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
1,410✔
1020
  if (!(buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN))) {
1,410✔
1021
    return terrno;
×
1022
  }
1023

1024
  SName name = {0};
1,410✔
1025
  TAOS_CHECK_EXIT(tNameFromString(&name, pMeta->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
1,410✔
1026

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

1050
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
66,756✔
1051
  SSDataBlock* pBlock = NULL;
66,756✔
1052
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
66,756✔
1053
  if (TSDB_CODE_SUCCESS == code) {
66,756✔
1054
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
66,756✔
1055
  }
1056
  if (TSDB_CODE_SUCCESS == code) {
66,756✔
1057
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
66,756✔
1058
  }
1059
  (void)blockDataDestroy(pBlock);
66,756✔
1060
  return code;
66,756✔
1061
}
1062

1063
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
2,673✔
1064
  SSDataBlock* pBlock = NULL;
2,673✔
1065
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
2,673✔
1066
  if (TSDB_CODE_SUCCESS == code) {
2,673✔
1067
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
2,673✔
1068
  }
1069
  if (TSDB_CODE_SUCCESS == code) {
2,673✔
1070
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
2,673✔
1071
  }
1072
  (void)blockDataDestroy(pBlock);
2,673✔
1073
  return code;
2,673✔
1074
}
1075

1076
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
471✔
1077
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
471✔
1078
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
471✔
1079
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
157✔
1080
    return terrno;
157✔
1081
  }
1082

1083
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
314✔
1084
}
1085

1086
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
213,554✔
1087
  int32_t code = 0;
213,554✔
1088

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

1129
  *processed = true;
2,426✔
1130

1131
_return:
213,554✔
1132

1133
  if (code) {
213,554✔
1134
    terrno = code;
×
1135
  }
1136

1137
  return code;
213,554✔
1138
}
1139

1140
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
213,554✔
1141
  bool processed = false;
213,554✔
1142

1143
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
213,554✔
1144
    return terrno;
×
1145
  }
1146

1147
  if (processed) {
213,554✔
1148
    goto _return;
2,426✔
1149
  }
1150

1151
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
211,128✔
1152
    return terrno;
1,492✔
1153
  }
1154

1155
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
209,636✔
1156
    return terrno;
×
1157
  }
1158

1159
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
209,636✔
1160

1161
_return:
212,048✔
1162

1163
  return TSDB_CODE_SUCCESS;
212,062✔
1164
}
1165

1166
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
10,786✔
1167
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
10,786✔
1168
  if (NULL == pBlock) {
10,786✔
1169
    return terrno;
×
1170
  }
1171

1172
  pBlock->info.hasVarCol = true;
10,786✔
1173

1174
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
10,786✔
1175
  if (NULL == pBlock->pDataBlock) {
10,786✔
1176
    taosMemoryFree(pBlock);
×
1177
    return terrno;
×
1178
  }
1179

1180
  SColumnInfoData infoData = {0};
10,786✔
1181

1182
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
10,786✔
1183
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
10,786✔
1184
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
21,572✔
1185
    goto _exit;
×
1186
  }
1187

1188
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
10,786✔
1189
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
10,786✔
1190
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
21,572✔
1191
    goto _exit;
×
1192
  }
1193

1194
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
10,786✔
1195
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
10,786✔
1196
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
21,572✔
1197
    goto _exit;
×
1198
  }
1199

1200
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
10,786✔
1201
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
10,786✔
1202
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
21,572✔
1203
    goto _exit;
×
1204
  }
1205

1206
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
10,786✔
1207
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
10,786✔
1208
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
21,572✔
1209
    goto _exit;
×
1210
  }
1211

1212
  *pOutput = pBlock;
10,786✔
1213

1214
_exit:
10,786✔
1215
  if (terrno != TSDB_CODE_SUCCESS) {
10,786✔
1216
    taosArrayDestroy(pBlock->pDataBlock);
×
1217
    taosMemoryFree(pBlock);
×
1218
  }
1219
  return terrno;
10,786✔
1220
}
1221

1222
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
10,786✔
1223
  SSDataBlock* pBlock = NULL;
10,786✔
1224
  char*        likePattern = NULL;
10,786✔
1225
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
10,786✔
1226
  if (TSDB_CODE_SUCCESS == code) {
10,786✔
1227
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
10,786✔
1228
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
1,609✔
1229
    }
1230
  }
1231
  if (TSDB_CODE_SUCCESS == code) {
10,786✔
1232
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
10,786✔
1233
  }
1234
  if (TSDB_CODE_SUCCESS == code) {
10,786✔
1235
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
10,786✔
1236
  }
1237
  (void)blockDataDestroy(pBlock);
10,786✔
1238
  return code;
10,786✔
1239
}
1240

1241
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
1,904,253✔
1242
  QRY_PARAM_CHECK(pOutput);
1,904,253✔
1243

1244
  SSDataBlock* pBlock = NULL;
1,904,253✔
1245
  int32_t      code = createDataBlock(&pBlock);
1,904,253✔
1246
  if (code) {
1,904,253✔
1247
    return code;
×
1248
  }
1249

1250
  SNode* pProj = NULL;
1,904,253✔
1251
  FOREACH(pProj, pProjects) {
4,678,417✔
1252
    SExprNode*      pExpr = (SExprNode*)pProj;
2,774,164✔
1253
    SColumnInfoData infoData = {0};
2,774,164✔
1254
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
2,774,164✔
1255
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1256
      infoData.info.bytes = 0;
×
1257
    } else {
1258
      infoData.info.type = pExpr->resType.type;
2,774,164✔
1259
      infoData.info.bytes = pExpr->resType.bytes;
2,774,164✔
1260
      infoData.info.precision = pExpr->resType.precision;
2,774,164✔
1261
      infoData.info.scale = pExpr->resType.scale;
2,774,164✔
1262
    }
1263
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
2,774,164✔
1264
  }
1265

1266
  *pOutput = pBlock;
1,904,253✔
1267
  return code;
1,904,253✔
1268
}
1269

1270
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
1,904,253✔
1271
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
1,904,253✔
1272

1273
  int32_t index = 0;
1,904,253✔
1274
  SNode*  pProj = NULL;
1,904,253✔
1275
  FOREACH(pProj, pProjects) {
4,678,417✔
1276
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
2,774,164✔
1277
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1278
    } else {
1279
      if (((SValueNode*)pProj)->isNull) {
2,774,164✔
1280
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
76,069✔
1281
      } else {
1282
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
2,698,095✔
1283
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1284
      }
1285
    }
1286
  }
1287

1288
  pBlock->info.rows = 1;
1,904,253✔
1289
  return TSDB_CODE_SUCCESS;
1,904,253✔
1290
}
1291

1292
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
1,904,253✔
1293
  SSDataBlock* pBlock = NULL;
1,904,253✔
1294
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
1,904,253✔
1295
  if (TSDB_CODE_SUCCESS == code) {
1,904,253✔
1296
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
1,904,253✔
1297
  }
1298
  if (TSDB_CODE_SUCCESS == code) {
1,904,253✔
1299
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
1,904,253✔
1300
  }
1301
  (void)blockDataDestroy(pBlock);
1,904,253✔
1302
  return code;
1,904,253✔
1303
}
1304

UNCOV
1305
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
×
UNCOV
1306
  SSDataBlock* pBlock = NULL;
×
UNCOV
1307
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
×
UNCOV
1308
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
1309
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
×
1310
  }
UNCOV
1311
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
1312
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
×
1313
  }
UNCOV
1314
  (void)blockDataDestroy(pBlock);
×
UNCOV
1315
  return code;
×
1316
}
1317

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

1331
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
4,547,321✔
1332
                     void* charsetCxt) {
1333
  switch (nodeType(pStmt)) {
4,547,321✔
1334
    case QUERY_NODE_DESCRIBE_STMT:
377,084✔
1335
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
377,084✔
1336
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
1,916,465✔
1337
      return execResetQueryCache();
1,916,465✔
1338
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
54,183✔
1339
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
54,183✔
1340
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
66,442✔
1341
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
66,442✔
1342
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
2,673✔
1343
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
2,673✔
1344
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
471✔
1345
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
471✔
UNCOV
1346
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
×
UNCOV
1347
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
×
1348
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
1,410✔
1349
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
1,410✔
1350
    case QUERY_NODE_ALTER_LOCAL_STMT:
213,554✔
1351
      return execAlterLocal((SAlterLocalStmt*)pStmt);
213,554✔
1352
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
10,786✔
1353
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
10,786✔
1354
    case QUERY_NODE_SELECT_STMT:
1,904,253✔
1355
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
1,904,253✔
1356
    default:
×
1357
      break;
×
1358
  }
1359
  return TSDB_CODE_FAILED;
×
1360
}
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