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

taosdata / TDengine / #4998

21 Mar 2026 01:22PM UTC coverage: 72.335% (+0.6%) from 71.739%
#4998

push

travis-ci

web-flow
enh:register add secondEp (#34867)

61 of 69 new or added lines in 1 file covered. (88.41%)

8670 existing lines in 142 files now uncovered.

253516 of 350475 relevant lines covered (72.33%)

133451446.75 hits per line

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

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

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

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

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

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

75
  return TSDB_CODE_SUCCESS;
3,265,989✔
76
}
77

78
static int32_t getSchemaBytes(const SSchema* pSchema) {
63,531,973✔
79
  switch (pSchema->type) {
63,531,973✔
80
    case TSDB_DATA_TYPE_BINARY:
2,684,034✔
81
    case TSDB_DATA_TYPE_VARBINARY:
82
    case TSDB_DATA_TYPE_GEOMETRY:
83
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
2,684,034✔
84
    case TSDB_DATA_TYPE_NCHAR:
4,424,895✔
85
    case TSDB_DATA_TYPE_JSON:
86
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
4,424,895✔
87
    default:
56,423,044✔
88
      return pSchema->bytes;
56,423,044✔
89
  }
90
}
91

92
static const char* expandIdentifier(const char* name, char* output) {
622,309✔
93
  if (NULL == name) return "";
622,309✔
94
  bool containsEscapeChar = false;
622,309✔
95
  for (const char* p = name; *p != '\0'; ++p) {
4,539,365✔
96
    if (*p == TS_ESCAPE_CHAR) {
3,964,576✔
97
      containsEscapeChar = true;
47,520✔
98
      break;
47,520✔
99
    }
100
  }
101
  if (!containsEscapeChar) return name;
622,309✔
102
  if (NULL == output) return "";
47,520✔
103
  char* out_ptr = output;
47,520✔
104
  for (const char* src = name; *src != '\0'; ++src) {
292,512✔
105
    if (*src == TS_ESCAPE_CHAR) {
244,992✔
106
      *out_ptr++ = TS_ESCAPE_CHAR;
119,328✔
107
      *out_ptr++ = TS_ESCAPE_CHAR;
119,328✔
108
    } else {
109
      *out_ptr++ = *src;
125,664✔
110
    }
111
  }
112
  *out_ptr = '\0';
47,520✔
113
  return output;
47,520✔
114
}
115

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

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

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

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

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

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

195
  int32_t fillTagCol = 0;
439,727✔
196
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
439,727✔
197
  for (int32_t i = 0; i < numOfRows; ++i) {
63,971,667✔
198
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
63,531,940✔
199
      continue;
×
200
    }
201
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
63,531,902✔
202
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
63,531,902✔
203

204
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
63,531,935✔
205
      uint8_t prec = 0, scale = 0;
267,224✔
206
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
267,224✔
207
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
267,224✔
208
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
267,224✔
209
      varDataSetLen(buf, len);
267,224✔
210
    } else {
211
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
63,264,711✔
212
    }
213
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
63,531,935✔
214
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
63,531,973✔
215
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
63,531,973✔
216
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
63,531,973✔
217
      if (i >= pMeta->tableInfo.numOfColumns) {
63,501,133✔
218
        STR_TO_VARSTR(buf, "TAG");
3,087,306✔
219
        fillTagCol = 1;
3,087,306✔
220
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
60,413,827✔
221
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
10,821✔
222
      } else {
223
        STR_TO_VARSTR(buf, "");
60,403,006✔
224
      }
225
    } else {
226
      STR_TO_VARSTR(buf, "VIEW COL");
30,840✔
227
    }
228
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
63,531,973✔
229
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
63,531,940✔
230
      if (i < pMeta->tableInfo.numOfColumns) {
63,444,948✔
231
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
60,363,282✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
60,363,282✔
233
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
60,363,282✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
60,363,315✔
235
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
60,363,277✔
236
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
60,363,244✔
237
      } else {
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,081,666✔
239
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
3,081,666✔
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,081,666✔
241
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
3,081,666✔
242
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,081,666✔
243
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
3,081,666✔
244
      }
245
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
86,992✔
246
      if (i < pMeta->numOfColRefs) {
37,489✔
247
        if (pMeta->colRef[i].hasRef) {
31,849✔
248
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
13,782✔
249

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

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

269
    fillTagCol = 0;
63,531,973✔
270

271
    ++(pBlock->info.rows);
63,531,973✔
272
  }
273
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
439,727✔
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) {
439,727✔
285
    qError("no permission to view any columns");
×
286
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
287
  }
288
  return TSDB_CODE_SUCCESS;
439,727✔
289
}
290

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

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

320
static int32_t execResetQueryCache() { return catalogClearCache(); }
2,146,846✔
321

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

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

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

338
  if (TSDB_CODE_SUCCESS == code) {
67,770✔
339
    *pOutput = pBlock;
67,770✔
340
  } else {
341
    (void)blockDataDestroy(pBlock);
×
342
  }
343
  return code;
67,770✔
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) {
67,770✔
372
  size_t size = taosArrayGetSize(pRetension);
67,770✔
373
  if (size == 0) {
67,770✔
374
    *ppRetentions = NULL;
67,770✔
375
    return TSDB_CODE_SUCCESS;
67,770✔
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) {
67,396✔
405
  switch (cacheModel) {
67,396✔
406
    case TSDB_CACHE_MODEL_NONE:
67,396✔
407
      return TSDB_CACHE_MODEL_NONE_STR;
67,396✔
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) {
67,396✔
421
  if (algorithmsId[0] != '\0') {
67,396✔
422
    return algorithmsId;
×
423
  } else if (encryptAlgorithm != 0) {
67,396✔
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;
67,396✔
436
}
437

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

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

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

464
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
67,770✔
465
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
67,770✔
466
  int32_t          len = 0;
67,770✔
467
  char*            prec = NULL;
67,770✔
468
  switch (pCfg->precision) {
67,770✔
469
    case TSDB_TIME_PRECISION_MILLI:
67,770✔
470
      prec = TSDB_TIME_PRECISION_MILLI_STR;
67,770✔
471
      break;
67,770✔
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;
67,770✔
484
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
67,770✔
485
  int32_t dbFNameLen = strlen(dbFName);
67,770✔
486
  int32_t hashPrefix = 0;
67,770✔
487
  if (pCfg->hashPrefix > 0) {
67,770✔
488
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
1,534✔
489
  } else if (pCfg->hashPrefix < 0) {
66,236✔
490
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
2,301✔
491
  }
492
  char durationStr[128] = {0};
67,770✔
493
  char keep0Str[128] = {0};
67,770✔
494
  char keep1Str[128] = {0};
67,770✔
495
  char keep2Str[128] = {0};
67,770✔
496
  char compactIntervalStr[13] = {0};
67,770✔
497
  char compactStartTimeStr[13] = {0};
67,770✔
498
  char compactEndTimeStr[13] = {0};
67,770✔
499

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

508
  if (IS_SYS_DBNAME(dbName)) {
67,770✔
509
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
374✔
510
                    "CREATE DATABASE `%s`", dbName);
374✔
511
  } else {
512
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
471,772✔
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,
134,792✔
521
                     durationStr, pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str,
67,396✔
522
                     keep1Str, keep2Str, pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel,
67,396✔
523
                     pCfg->numOfVgroups, 1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize,
67,396✔
524
                     pCfg->walRetentionPeriod, pCfg->walRetentionSize, pCfg->keepTimeOffset,
525
                     encryptAlgorithmStr(pCfg->encryptAlgr, pCfg->algorithmsId), pCfg->ssChunkSize, pCfg->ssKeepLocal,
67,396✔
526
                     pCfg->ssCompact, compactIntervalStr, compactStartTimeStr, compactEndTimeStr,
67,396✔
527
                     pCfg->compactTimeOffset, pCfg->isAudit, pCfg->secureDelete);
67,396✔
528

529

530
    if (pRetentions) {
67,396✔
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);
67,770✔
537

538
  (varDataLen(buf2)) = len;
67,770✔
539

540
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
67,770✔
541

542
  return TSDB_CODE_SUCCESS;
67,770✔
543
}
544

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

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

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

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

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

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

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

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

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

606
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
43,128✔
607
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
43,128✔
608
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
272,136✔
609
    SSchema* pSchema = pCfg->pSchemas + i;
229,008✔
610
    SColRef* pRef = pCfg->pColRefs + i;
229,008✔
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];
229,008✔
615
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
229,008✔
616
    int typeLen = strlen(type);
229,008✔
617
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
229,008✔
618
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
202,479✔
619
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
27,961✔
620
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
201,047✔
621
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
8,518✔
622
                          (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
8,518✔
623
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
192,529✔
624
      uint8_t precision, scale;
18,672✔
625
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
18,672✔
626
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
18,672✔
627
    }
628

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

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

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

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

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

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

688
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
25,807✔
689
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
25,807✔
690
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
77,441✔
691
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
51,634✔
692
    char     type[32];
51,634✔
693
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
51,634✔
694
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
51,634✔
695
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
42,017✔
696
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
9,681✔
697
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
9,681✔
698
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
41,953✔
699
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
1,478✔
700
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
1,478✔
701
    }
702

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

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

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

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

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

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

754
    return TSDB_CODE_SUCCESS;
2,405✔
755
  }
756

757
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
34,898✔
758
  int16_t valueNum = taosArrayGetSize(pTagVals);
34,898✔
759
  int32_t num = 0;
34,898✔
760
  int32_t j = 0;
34,898✔
761
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
98,609✔
762
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
63,711✔
763
    if (i > 0) {
63,711✔
764
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
28,813✔
765
                       ", ");
766
    }
767

768
    if (pCfg->pTagRefs && i < pCfg->numOfTagRefs) {
63,711✔
UNCOV
769
      SColRef* pTagRef = pCfg->pTagRefs + i;
×
UNCOV
770
      if (pTagRef->hasRef) {
×
UNCOV
771
        char expandRefTable[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
×
UNCOV
772
        char expandRefCol[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
×
UNCOV
773
        *len += snprintf(buf + VARSTR_HEADER_SIZE + *len,
×
UNCOV
774
                         SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
UNCOV
775
                         "FROM `%s`.`%s`.`%s`", pTagRef->refDbName,
×
776
                         expandIdentifier(pTagRef->refTableName, expandRefTable),
×
777
                         expandIdentifier(pTagRef->refColName, expandRefCol));
×
778
        continue;
×
779
      }
780
    }
781

782
    if (j >= valueNum) {
63,711✔
783
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
15,779✔
784
                       "NULL");
785
      continue;
15,779✔
786
    }
787

788
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
47,932✔
789
    if (pSchema->colId > pTagVal->cid) {
47,932✔
UNCOV
790
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
UNCOV
791
      code = TSDB_CODE_APP_ERROR;
×
UNCOV
792
      TAOS_CHECK_ERRNO(code);
×
793
    } else if (pSchema->colId == pTagVal->cid) {
47,932✔
794
      char    type = pTagVal->type;
46,398✔
795
      int32_t tlen = 0;
46,398✔
796

797
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
46,398✔
798
      if (leftSize <= 0) {
46,398✔
UNCOV
799
        qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
×
UNCOV
800
        code = TSDB_CODE_APP_ERROR;
×
UNCOV
801
        TAOS_CHECK_ERRNO(code);
×
802
      }
803
      if (IS_VAR_DATA_TYPE(type)) {
46,398✔
804
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
12,329✔
805
        TAOS_CHECK_ERRNO(code);
12,329✔
806
      } else {
807
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
34,069✔
808
                               &tlen);
809
        TAOS_CHECK_ERRNO(code);
34,069✔
810
      }
811
      *len += tlen;
46,398✔
812
      j++;
46,398✔
813
    } else {
814
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,534✔
815
                       "NULL");
816
    }
817
  }
818
_exit:
34,898✔
819
  taosArrayDestroy(pTagVals);
34,898✔
820

821
  return code;
34,898✔
822
}
823

824
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
72,558✔
825
  if (pCfg->commentLen > 0) {
72,558✔
UNCOV
826
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
827
                     " COMMENT '%s'", pCfg->pComment);
828
  } else if (0 == pCfg->commentLen) {
72,558✔
829
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,728✔
830
                     " COMMENT ''");
831
  }
832

833
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
72,558✔
UNCOV
834
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
835
                     " WATERMARK %" PRId64 "a", pCfg->watermark1);
UNCOV
836
    if (pCfg->watermark2 > 0) {
×
UNCOV
837
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
838
                       ", %" PRId64 "a", pCfg->watermark2);
839
    }
840
  }
841

842
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
72,558✔
843
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
844
                     " MAX_DELAY %" PRId64 "a", pCfg->delay1);
UNCOV
845
    if (pCfg->delay2 > 0) {
×
846
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
847
                       ", %" PRId64 "a", pCfg->delay2);
848
    }
849
  }
850

851
  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
72,558✔
852
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
72,558✔
UNCOV
853
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
854
                     " ROLLUP(");
UNCOV
855
    for (int32_t i = 0; i < funcNum; ++i) {
×
UNCOV
856
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
×
UNCOV
857
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
858
                       "%s%s", ((i > 0) ? ", " : ""), pFunc);
859
    }
UNCOV
860
    *len +=
×
UNCOV
861
        snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
×
862
  }
863

864
  if (pCfg->ttl > 0) {
72,558✔
UNCOV
865
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
866
                     " TTL %d", pCfg->ttl);
867
  }
868

869
  if (pCfg->keep > 0) {
72,558✔
870
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,734✔
871
                     " KEEP %dm", pCfg->keep);
872
  }
873

874
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
72,558✔
875
    int32_t nSma = 0;
39,521✔
876
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
221,976✔
877
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
182,455✔
878
        ++nSma;
182,455✔
879
      }
880
    }
881

882
    if (nSma < pCfg->numOfColumns && nSma > 0) {
39,521✔
UNCOV
883
      bool smaOn = false;
×
UNCOV
884
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
885
                       " SMA(");
886
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
UNCOV
887
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
UNCOV
888
          if (smaOn) {
×
UNCOV
889
            *len += snprintf(buf + VARSTR_HEADER_SIZE + *len,
×
UNCOV
890
                             SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ",`%s`",
×
UNCOV
891
                             (pCfg->pSchemas + i)->name);
×
892
          } else {
UNCOV
893
            smaOn = true;
×
UNCOV
894
            *len += snprintf(buf + VARSTR_HEADER_SIZE + *len,
×
UNCOV
895
                             SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), "`%s`",
×
UNCOV
896
                             (pCfg->pSchemas + i)->name);
×
897
          }
898
        }
899
      }
UNCOV
900
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, ")");
×
901
    }
902
  }
903

904
  if (pCfg->virtualStb) {
72,558✔
905
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,754✔
906
                     " VIRTUAL %d", pCfg->virtualStb);
1,754✔
907
  }
908

909
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
72,558✔
910
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
25,807✔
911
                      " SECURE_DELETE %d", pCfg->secureDelete);
25,807✔
912
  }
913
}
72,558✔
914

915
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
86,041✔
916
                                              void* charsetCxt) {
917
  int32_t code = TSDB_CODE_SUCCESS;
86,041✔
918
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
86,041✔
919
  pBlock->info.rows = 1;
86,041✔
920

921
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
86,041✔
922
  char             buf1[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1)] = {0};
86,041✔
923
  STR_TO_VARSTR(buf1, tbName);
86,041✔
924
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
86,041✔
925

926
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
86,041✔
927
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
86,041✔
928
  if (NULL == buf2) {
86,041✔
UNCOV
929
    QRY_ERR_RET(terrno);
×
930
  }
931

932
  int32_t len = 0;
86,041✔
933

934
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
86,041✔
935
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
25,807✔
936
                    "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
937
    appendColumnFields(buf2, &len, pCfg);
25,807✔
938
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
25,807✔
939
                    ") TAGS (");
940
    appendTagFields(buf2, &len, pCfg);
25,807✔
941
    len +=
25,807✔
942
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
25,807✔
943
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
25,807✔
944
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
60,234✔
945
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
33,037✔
946
                    "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
947
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
33,037✔
948
                    "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
33,037✔
949
    appendTagNameFields(buf2, &len, pCfg);
33,037✔
950
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
33,037✔
951
                    ") TAGS (");
952
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
33,037✔
953
    TAOS_CHECK_ERRNO(code);
33,037✔
954
    len +=
33,037✔
955
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
33,037✔
956
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
33,037✔
957
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
27,197✔
958
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
13,714✔
959
                    "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
960
    appendColumnFields(buf2, &len, pCfg);
13,714✔
961
    len +=
13,714✔
962
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
13,714✔
963
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
13,714✔
964
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
13,483✔
965
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
3,607✔
966
                    "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
967
    appendColumnFields(buf2, &len, pCfg);
3,607✔
968
    len +=
3,607✔
969
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
3,607✔
970
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
9,876✔
971
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
4,266✔
972
                     "CREATE VTABLE `%s`", expandIdentifier(tbName, buf1));
973
    appendColRefFields(buf2, &len, pCfg);
4,266✔
974
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
4,266✔
975
                    " USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
4,266✔
976
    appendTagNameFields(buf2, &len, pCfg);
4,266✔
977
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
4,266✔
978
                    ") TAGS (");
979
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
4,266✔
980
    TAOS_CHECK_ERRNO(code);
4,266✔
981
    len +=
4,266✔
982
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
4,266✔
983
  }
984

985
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
86,041✔
986

987
  code = colDataSetVal(pCol2, 0, buf2, false);
86,041✔
988
  TAOS_CHECK_ERRNO(code);
86,041✔
989

990
_exit:
86,041✔
991
  taosMemoryFree(buf2);
86,041✔
992

993
  return code;
86,041✔
994
}
995

996
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
6,504✔
997
  int32_t code = 0;
6,504✔
998
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
6,504✔
999
  pBlock->info.rows = 1;
6,504✔
1000

1001
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
6,504✔
1002
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
6,504✔
1003
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
6,504✔
1004
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
6,504✔
1005
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
6,504✔
1006

1007
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
6,504✔
1008
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
6,504✔
1009
  if (NULL == buf2) {
6,504✔
UNCOV
1010
    return terrno;
×
1011
  }
1012

1013
  SViewMeta* pMeta = pStmt->pViewMeta;
6,504✔
1014
  if (NULL == pMeta) {
6,504✔
UNCOV
1015
    qError("exception: view meta is null");
×
UNCOV
1016
    taosMemoryFree(buf2);
×
UNCOV
1017
    return TSDB_CODE_APP_ERROR;
×
1018
  }
1019
  snprintf(varDataVal(buf2), SHOW_CREATE_VIEW_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE VIEW `%s`.`%s` AS %s",
6,504✔
1020
           pStmt->dbName, pStmt->viewName, pMeta->querySql);
6,504✔
1021
  int32_t len = strlen(varDataVal(buf2));
6,504✔
1022
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
6,504✔
1023
  code = colDataSetVal(pCol2, 0, buf2, false);
6,504✔
1024
  taosMemoryFree(buf2);
6,504✔
1025

1026
  return code;
6,504✔
1027
}
1028

1029
extern const char* fmGetFuncName(int32_t funcId);
1030
static int32_t setCreateRsmaResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateRsmaStmt* pStmt) {
1,973✔
1031
  int32_t       code = 0, lino = 0;
1,973✔
1032
  char*         buf2 = NULL;
1,973✔
1033
  SRsmaInfoRsp* pMeta = pStmt->pRsmaMeta;
1,973✔
1034

1035
  if (pMeta->nFuncs != pMeta->nColNames) {
1,973✔
UNCOV
1036
    qError("exception: rsma meta is invalid, nFuncs:%d != nColNames:%d", pMeta->nFuncs, pMeta->nColNames);
×
1037
    return TSDB_CODE_APP_ERROR;
×
1038
  }
1039

1040
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, 1));
1,973✔
1041
  pBlock->info.rows = 1;
1,973✔
1042

1043
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
1,973✔
1044
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1] = {0};
1,973✔
1045
  snprintf(varDataVal(buf1), TSDB_TABLE_FNAME_LEN + 4, "`%s`", expandIdentifier(pStmt->rsmaName, buf1));
1,973✔
1046
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
1,973✔
1047
  TAOS_CHECK_EXIT(colDataSetVal(pCol1, 0, buf1, false));
1,973✔
1048

1049
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
1,973✔
1050
  if (!(buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN))) {
1,973✔
UNCOV
1051
    return terrno;
×
1052
  }
1053

1054
  SName name = {0};
1,973✔
1055
  TAOS_CHECK_EXIT(tNameFromString(&name, pMeta->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
1,973✔
1056

1057
  int32_t len = 0;
1,973✔
1058
  len += tsnprintf(varDataVal(buf2), SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
3,946✔
1059
                       "CREATE RSMA `%s` ON `%s`.`%s` FUNCTION(", expandIdentifier(pStmt->rsmaName, buf1),
1,973✔
1060
                       expandIdentifier(name.dbname, buf1), expandIdentifier(name.tname, buf1));
1061
  for (int32_t i = 0; i < pMeta->nFuncs; ++i) {
13,254✔
1062
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
22,562✔
1063
                         "%s%s(`%s`)", (i > 0) ? "," : "", fmGetFuncName(pMeta->funcIds[i]),
11,281✔
1064
                         expandIdentifier(*(char**)TARRAY_GET_ELEM(pMeta->colNames, i), buf1));
11,281✔
1065
  }
1066
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,973✔
1067
                       ") INTERVAL(%d%c", pMeta->interval[0], pMeta->intervalUnit);
1,973✔
1068
  if (pMeta->interval[1] > 0) {
1,973✔
1069
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ",%d%c",
1,973✔
1070
                         pMeta->interval[1], pMeta->intervalUnit);
1,973✔
1071
  }
1072
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,973✔
1073
  varDataLen(buf2) = len;
1,973✔
1074
  code = colDataSetVal(pCol2, 0, buf2, false);
1,973✔
1075
_exit:
1,973✔
1076
  taosMemoryFree(buf2);
1,973✔
1077
  return code;
1,973✔
1078
}
1079

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

1093
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
7,228✔
1094
  SSDataBlock* pBlock = NULL;
7,228✔
1095
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
7,228✔
1096
  if (TSDB_CODE_SUCCESS == code) {
7,228✔
1097
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
7,228✔
1098
  }
1099
  if (TSDB_CODE_SUCCESS == code) {
7,228✔
1100
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
7,228✔
1101
  }
1102
  (void)blockDataDestroy(pBlock);
7,228✔
1103
  return code;
7,228✔
1104
}
1105

1106
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
1,181✔
1107
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
1,181✔
1108
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
1,181✔
1109
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
187✔
1110
    return terrno;
187✔
1111
  }
1112

1113
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
994✔
1114
}
1115

1116
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
540,763✔
1117
  int32_t code = 0;
540,763✔
1118

1119
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
540,763✔
1120
    taosResetLog();
57✔
1121
    cfgDumpCfg(tsCfg, 0, false);
57✔
1122
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
540,706✔
1123
    int32_t tmp = 0;
2,612✔
1124
    code = taosStr2int32(value, &tmp);
2,612✔
1125
    if (code) {
2,612✔
UNCOV
1126
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1127
      return code;
×
1128
    }
1129
    code = schedulerUpdatePolicy(tmp);
2,612✔
1130
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
538,094✔
UNCOV
1131
    int32_t tmp = 0;
×
1132
    code = taosStr2int32(value, &tmp);
×
1133
    if (code) {
×
UNCOV
1134
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1135
      return code;
×
1136
    }
1137
    code = schedulerEnableReSchedule(tmp != 0);
×
1138
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
538,094✔
UNCOV
1139
    code = ctgdHandleDbgCommand(value);
×
1140
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
538,094✔
UNCOV
1141
    code = taosMemoryDbgInit();
×
UNCOV
1142
    if (code) {
×
UNCOV
1143
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
UNCOV
1144
      return code;
×
1145
    }
UNCOV
1146
    tsAsyncLog = false;
×
UNCOV
1147
    qInfo("memory dbg enabled");
×
1148
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
538,094✔
UNCOV
1149
    code = taosMemoryDbgInitRestore();
×
1150
    if (code) {
×
UNCOV
1151
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
UNCOV
1152
      return code;
×
1153
    }
UNCOV
1154
    qInfo("memory dbg disabled");
×
1155
  } else {
1156
    goto _return;
538,094✔
1157
  }
1158

1159
  *processed = true;
2,669✔
1160

1161
_return:
540,763✔
1162

1163
  if (code) {
540,763✔
UNCOV
1164
    terrno = code;
×
1165
  }
1166

1167
  return code;
540,763✔
1168
}
1169

1170
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
540,763✔
1171
  bool processed = false;
540,763✔
1172

1173
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
540,763✔
UNCOV
1174
    return terrno;
×
1175
  }
1176

1177
  if (processed) {
540,763✔
1178
    goto _return;
2,669✔
1179
  }
1180

1181
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
538,094✔
1182
    return terrno;
1,661✔
1183
  }
1184

1185
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
536,433✔
UNCOV
1186
    return terrno;
×
1187
  }
1188

1189
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
536,433✔
1190

1191
_return:
539,102✔
1192

1193
  return TSDB_CODE_SUCCESS;
539,102✔
1194
}
1195

1196
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
14,549✔
1197
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
14,549✔
1198
  if (NULL == pBlock) {
14,549✔
UNCOV
1199
    return terrno;
×
1200
  }
1201

1202
  pBlock->info.hasVarCol = true;
14,549✔
1203

1204
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
14,549✔
1205
  if (NULL == pBlock->pDataBlock) {
14,549✔
UNCOV
1206
    taosMemoryFree(pBlock);
×
1207
    return terrno;
×
1208
  }
1209

1210
  SColumnInfoData infoData = {0};
14,549✔
1211

1212
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
14,549✔
1213
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
14,549✔
1214
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
29,098✔
UNCOV
1215
    goto _exit;
×
1216
  }
1217

1218
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
14,549✔
1219
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
14,549✔
1220
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
29,098✔
UNCOV
1221
    goto _exit;
×
1222
  }
1223

1224
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
14,549✔
1225
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
14,549✔
1226
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
29,098✔
UNCOV
1227
    goto _exit;
×
1228
  }
1229

1230
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
14,549✔
1231
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
14,549✔
1232
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
29,098✔
1233
    goto _exit;
×
1234
  }
1235

1236
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
14,549✔
1237
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
14,549✔
1238
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
29,098✔
UNCOV
1239
    goto _exit;
×
1240
  }
1241

1242
  *pOutput = pBlock;
14,549✔
1243

1244
_exit:
14,549✔
1245
  if (terrno != TSDB_CODE_SUCCESS) {
14,549✔
UNCOV
1246
    taosArrayDestroy(pBlock->pDataBlock);
×
UNCOV
1247
    taosMemoryFree(pBlock);
×
1248
  }
1249
  return terrno;
14,549✔
1250
}
1251

1252
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
14,549✔
1253
  SSDataBlock* pBlock = NULL;
14,549✔
1254
  char*        likePattern = NULL;
14,549✔
1255
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
14,549✔
1256
  if (TSDB_CODE_SUCCESS == code) {
14,549✔
1257
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
14,549✔
1258
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
2,855✔
1259
    }
1260
  }
1261
  if (TSDB_CODE_SUCCESS == code) {
14,549✔
1262
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
14,549✔
1263
  }
1264
  if (TSDB_CODE_SUCCESS == code) {
14,549✔
1265
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
14,549✔
1266
  }
1267
  (void)blockDataDestroy(pBlock);
14,549✔
1268
  return code;
14,549✔
1269
}
1270

1271
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
2,649,425✔
1272
  QRY_PARAM_CHECK(pOutput);
2,649,425✔
1273

1274
  SSDataBlock* pBlock = NULL;
2,649,425✔
1275
  int32_t      code = createDataBlock(&pBlock);
2,649,425✔
1276
  if (code) {
2,649,425✔
UNCOV
1277
    return code;
×
1278
  }
1279

1280
  SNode* pProj = NULL;
2,649,425✔
1281
  FOREACH(pProj, pProjects) {
6,759,942✔
1282
    SExprNode*      pExpr = (SExprNode*)pProj;
4,110,517✔
1283
    SColumnInfoData infoData = {0};
4,110,517✔
1284
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
4,110,517✔
UNCOV
1285
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
UNCOV
1286
      infoData.info.bytes = 0;
×
1287
    } else {
1288
      infoData.info.type = pExpr->resType.type;
4,110,517✔
1289
      infoData.info.bytes = pExpr->resType.bytes;
4,110,517✔
1290
      infoData.info.precision = pExpr->resType.precision;
4,110,517✔
1291
      infoData.info.scale = pExpr->resType.scale;
4,110,517✔
1292
    }
1293
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
4,110,517✔
1294
  }
1295

1296
  *pOutput = pBlock;
2,649,425✔
1297
  return code;
2,649,425✔
1298
}
1299

1300
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
2,649,425✔
1301
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
2,649,425✔
1302

1303
  int32_t index = 0;
2,649,425✔
1304
  SNode*  pProj = NULL;
2,649,425✔
1305
  FOREACH(pProj, pProjects) {
6,759,942✔
1306
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
4,110,517✔
UNCOV
1307
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1308
    } else {
1309
      if (((SValueNode*)pProj)->isNull) {
4,110,517✔
1310
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
85,076✔
1311
      } else {
1312
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
4,025,441✔
1313
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1314
      }
1315
    }
1316
  }
1317

1318
  pBlock->info.rows = 1;
2,649,425✔
1319
  return TSDB_CODE_SUCCESS;
2,649,425✔
1320
}
1321

1322
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
2,649,425✔
1323
  SSDataBlock* pBlock = NULL;
2,649,425✔
1324
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
2,649,425✔
1325
  if (TSDB_CODE_SUCCESS == code) {
2,649,425✔
1326
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
2,649,425✔
1327
  }
1328
  if (TSDB_CODE_SUCCESS == code) {
2,649,425✔
1329
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
2,649,425✔
1330
  }
1331
  (void)blockDataDestroy(pBlock);
2,649,425✔
1332
  return code;
2,649,425✔
1333
}
1334

1335
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
6,504✔
1336
  SSDataBlock* pBlock = NULL;
6,504✔
1337
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
6,504✔
1338
  if (TSDB_CODE_SUCCESS == code) {
6,504✔
1339
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
6,504✔
1340
  }
1341
  if (TSDB_CODE_SUCCESS == code) {
6,504✔
1342
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
6,504✔
1343
  }
1344
  (void)blockDataDestroy(pBlock);
6,504✔
1345
  return code;
6,504✔
1346
}
1347

1348
static int32_t execShowCreateRsma(SShowCreateRsmaStmt* pStmt, SRetrieveTableRsp** pRsp) {
1,973✔
1349
  SSDataBlock* pBlock = NULL;
1,973✔
1350
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
1,973✔
1351
  if (TSDB_CODE_SUCCESS == code) {
1,973✔
1352
    code = setCreateRsmaResultIntoDataBlock(pBlock, pStmt);
1,973✔
1353
  }
1354
  if (TSDB_CODE_SUCCESS == code) {
1,973✔
1355
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
1,973✔
1356
  }
1357
  (void)blockDataDestroy(pBlock);
1,973✔
1358
  return code;
1,973✔
1359
}
1360

1361
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
5,952,986✔
1362
                     void* charsetCxt) {
1363
  switch (nodeType(pStmt)) {
5,952,986✔
1364
    case QUERY_NODE_DESCRIBE_STMT:
439,727✔
1365
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
439,727✔
1366
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
2,146,846✔
1367
      return execResetQueryCache();
2,146,846✔
1368
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
67,770✔
1369
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
67,770✔
1370
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
77,819✔
1371
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
77,819✔
1372
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
7,228✔
1373
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
7,228✔
1374
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
1,181✔
1375
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,181✔
1376
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
6,504✔
1377
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
6,504✔
1378
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
1,973✔
1379
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
1,973✔
1380
    case QUERY_NODE_ALTER_LOCAL_STMT:
540,763✔
1381
      return execAlterLocal((SAlterLocalStmt*)pStmt);
540,763✔
1382
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
14,549✔
1383
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
14,549✔
1384
    case QUERY_NODE_SELECT_STMT:
2,649,425✔
1385
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
2,649,425✔
UNCOV
1386
    default:
×
UNCOV
1387
      break;
×
1388
  }
UNCOV
1389
  return TSDB_CODE_FAILED;
×
1390
}
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