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

taosdata / TDengine / #4988

16 Mar 2026 12:26PM UTC coverage: 75.821% (+1.9%) from 73.883%
#4988

push

travis-ci

web-flow
feat: support secure delete option. (#34591)

274 of 464 new or added lines in 29 files covered. (59.05%)

4404 existing lines in 23 files now uncovered.

337108 of 444611 relevant lines covered (75.82%)

146708292.94 hits per line

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

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

52
  (*pRsp)->useconds = 0;
6,343,504✔
53
  (*pRsp)->completed = 1;
6,343,504✔
54
  (*pRsp)->precision = 0;
6,343,504✔
55
  (*pRsp)->compressed = 0;
6,343,504✔
56

57
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
6,343,504✔
58
  (*pRsp)->numOfCols = htonl(numOfCols);
6,343,504✔
59

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

71
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
6,343,504✔
72
  (*pRsp)->payloadLen = htonl(payloadLen);
6,343,504✔
73
  (*pRsp)->compLen = htonl(payloadLen);
6,343,504✔
74

75
  return TSDB_CODE_SUCCESS;
6,343,504✔
76
}
77

78
static int32_t getSchemaBytes(const SSchema* pSchema) {
124,837,186✔
79
  switch (pSchema->type) {
124,837,186✔
80
    case TSDB_DATA_TYPE_BINARY:
5,472,254✔
81
    case TSDB_DATA_TYPE_VARBINARY:
82
    case TSDB_DATA_TYPE_GEOMETRY:
83
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
5,472,254✔
84
    case TSDB_DATA_TYPE_NCHAR:
8,397,004✔
85
    case TSDB_DATA_TYPE_JSON:
86
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
8,397,004✔
87
    default:
110,967,928✔
88
      return pSchema->bytes;
110,967,928✔
89
  }
90
}
91

92
static const char* expandIdentifier(const char* name, char* output) {
1,231,508✔
93
  if (NULL == name) return "";
1,231,508✔
94
  bool containsEscapeChar = false;
1,231,508✔
95
  for (const char* p = name; *p != '\0'; ++p) {
9,003,304✔
96
    if (*p == TS_ESCAPE_CHAR) {
7,867,196✔
97
      containsEscapeChar = true;
95,400✔
98
      break;
95,400✔
99
    }
100
  }
101
  if (!containsEscapeChar) return name;
1,231,508✔
102
  if (NULL == output) return "";
95,400✔
103
  char* out_ptr = output;
95,400✔
104
  for (const char* src = name; *src != '\0'; ++src) {
587,240✔
105
    if (*src == TS_ESCAPE_CHAR) {
491,840✔
106
      *out_ptr++ = TS_ESCAPE_CHAR;
239,560✔
107
      *out_ptr++ = TS_ESCAPE_CHAR;
239,560✔
108
    } else {
109
      *out_ptr++ = *src;
252,280✔
110
    }
111
  }
112
  *out_ptr = '\0';
95,400✔
113
  return output;
95,400✔
114
}
115

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

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

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

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

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

191
  if (hasRefCol(pMeta->tableType)) {
858,802✔
192
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
6,056✔
193
  }
194

195
  int32_t fillTagCol = 0;
858,802✔
196
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
858,802✔
197
  for (int32_t i = 0; i < numOfRows; ++i) {
125,695,988✔
198
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
124,837,186✔
199
      continue;
×
200
    }
201
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
124,837,186✔
202
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
124,837,186✔
203

204
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
124,837,186✔
205
      uint8_t prec = 0, scale = 0;
517,010✔
206
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
517,010✔
207
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
517,010✔
208
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
517,010✔
209
      varDataSetLen(buf, len);
517,010✔
210
    } else {
211
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
124,320,176✔
212
    }
213
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
124,837,186✔
214
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
124,837,186✔
215
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
124,837,186✔
216
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
124,837,186✔
217
      if (i >= pMeta->tableInfo.numOfColumns) {
124,776,666✔
218
        STR_TO_VARSTR(buf, "TAG");
5,877,944✔
219
        fillTagCol = 1;
5,877,944✔
220
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
118,898,722✔
221
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
21,506✔
222
      } else {
223
        STR_TO_VARSTR(buf, "");
118,877,216✔
224
      }
225
    } else {
226
      STR_TO_VARSTR(buf, "VIEW COL");
60,520✔
227
    }
228
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
124,837,186✔
229
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
124,837,186✔
230
      if (i < pMeta->tableInfo.numOfColumns) {
124,667,012✔
231
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
118,800,168✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
118,800,168✔
233
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
118,800,168✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
118,800,168✔
235
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
118,800,168✔
236
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
118,800,168✔
237
      } else {
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
5,866,844✔
239
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
5,866,844✔
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
5,866,844✔
241
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
5,866,844✔
242
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
5,866,844✔
243
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
5,866,844✔
244
      }
245
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
170,174✔
246
      if (i < pMeta->numOfColRefs) {
73,864✔
247
        if (pMeta->colRef[i].hasRef) {
62,764✔
248
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
27,222✔
249
          strcat(refColName, pMeta->colRef[i].refDbName);
27,222✔
250
          strcat(refColName, ".");
27,222✔
251
          strcat(refColName, pMeta->colRef[i].refTableName);
27,222✔
252
          strcat(refColName, ".");
27,222✔
253
          strcat(refColName, pMeta->colRef[i].refColName);
27,222✔
254
          STR_TO_VARSTR(buf, refColName);
27,222✔
255
        } else {
256
          STR_TO_VARSTR(buf, "");
35,542✔
257
        }
258
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
62,764✔
259
      } else {
260
        STR_TO_VARSTR(buf, "");
11,100✔
261
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
11,100✔
262
      }
263
    }
264

265
    fillTagCol = 0;
124,837,186✔
266

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

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

294
  SSDataBlock* pBlock = NULL;
858,802✔
295
  int32_t      code = buildDescResultDataBlock(&pBlock);
858,802✔
296
  if (TSDB_CODE_SUCCESS == code) {
858,802✔
297
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
858,802✔
298
  }
299
  if (TSDB_CODE_SUCCESS == code) {
858,802✔
300
    if (pDesc->pMeta) {
858,802✔
301
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
858,802✔
302
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
832,046✔
303
      } else if (hasRefCol(pDesc->pMeta->tableType) && pDesc->pMeta->colRef) {
26,756✔
304
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
6,056✔
305
      } else {
306
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
20,700✔
307
      }
308
    } else {
309
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
310
    }
311
  }
312
  (void)blockDataDestroy(pBlock);
858,802✔
313
  return code;
858,802✔
314
}
315

316
static int32_t execResetQueryCache() { return catalogClearCache(); }
4,208,510✔
317

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

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

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

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

434
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
8,744,106✔
435
  if (buffer == NULL || bufSize <= 0) {
8,744,106✔
436
    return 0;
×
437
  }
438
  int32_t len = 0;
8,744,106✔
439
  if (timeInMinutes % 1440 == 0) {
8,744,106✔
440
    int32_t days = timeInMinutes / 1440;
8,720,368✔
441
    len = tsnprintf(buffer, bufSize, "%dd", days);
8,720,368✔
442
  } else if (timeInMinutes % 60 == 0) {
23,738✔
443
    int32_t hours = timeInMinutes / 60;
4,082✔
444
    len = tsnprintf(buffer, bufSize, "%dh", hours);
4,082✔
445
  } else {
446
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
19,656✔
447
  }
448
  return len;
8,744,106✔
449
}
450

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

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

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

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

504
  if (IS_SYS_DBNAME(dbName)) {
129,636✔
505
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
728✔
506
                     "CREATE DATABASE `%s`", dbName);
728✔
507
  } else {
508
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
773,448✔
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 SECURE_DELETE %d",
516
                     dbName, pCfg->buffer, pCfg->cacheSize, cacheModelStr(pCfg->cacheLast), pCfg->compression,
128,908✔
517
                     durationStr, pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str,
128,908✔
518
                     keep1Str, keep2Str, pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel,
128,908✔
519
                     pCfg->numOfVgroups, 1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize,
128,908✔
520
                     pCfg->walRetentionPeriod, pCfg->walRetentionSize, pCfg->keepTimeOffset,
521
                     encryptAlgorithmStr(pCfg->encryptAlgr, pCfg->algorithmsId), pCfg->ssChunkSize, pCfg->ssKeepLocal,
128,908✔
522
                     pCfg->ssCompact, compactIntervalStr, compactStartTimeStr, compactEndTimeStr,
128,908✔
523
                     pCfg->compactTimeOffset, pCfg->isAudit, pCfg->secureDelete);
128,908✔
524

525
    if (pRetentions) {
128,908✔
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);
129,636✔
532

533
  (varDataLen(buf2)) = len;
129,636✔
534

535
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
129,636✔
536

537
  return TSDB_CODE_SUCCESS;
129,636✔
538
}
539

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

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

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

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

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

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

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

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

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

601
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
85,288✔
602
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
85,288✔
603
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
534,408✔
604
    SSchema* pSchema = pCfg->pSchemas + i;
449,120✔
605
    SColRef* pRef = pCfg->pColRefs + i;
449,120✔
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];
449,120✔
610
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
449,120✔
611
    int typeLen = strlen(type);
449,120✔
612
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
449,120✔
613
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
396,546✔
614
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
55,322✔
615
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
393,798✔
616
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
16,918✔
617
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
16,918✔
618
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
376,880✔
619
      uint8_t precision, scale;
36,124✔
620
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
36,124✔
621
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
36,124✔
622
    }
623

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

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

642
    if (!(pSchema->flags & COL_IS_KEY)) {
449,120✔
643
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
430,496✔
644
                        "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
430,496✔
645
    } else {
646
      char* pk = "COMPOSITE KEY";
18,624✔
647
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
18,624✔
648
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
18,624✔
649
    }
650
  }
651
}
85,288✔
652

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

662
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
104,218✔
663
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
60,272✔
664
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
60,272✔
665
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
60,272✔
666
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
60,272✔
667
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
60,272✔
668
    } else {
669
      continue;
43,946✔
670
    }
671

672
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
60,272✔
673
                      "%s`%s` %s", (!firstRef ? ", " : " ("), expandIdentifier(pSchema->name, expandName), type);
60,272✔
674
    firstRef = false;
60,272✔
675
  }
676
  if (!firstRef) {
8,520✔
677
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
7,282✔
678
  }
679
}
8,520✔
680

681
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
50,952✔
682
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
50,952✔
683
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
151,806✔
684
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
100,854✔
685
    char     type[32];
100,854✔
686
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
100,854✔
687
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
100,854✔
688
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
81,868✔
689
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
19,078✔
690
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
19,078✔
691
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
81,776✔
692
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
2,890✔
693
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
2,890✔
694
    }
695

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

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

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

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

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

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

747
    return TSDB_CODE_SUCCESS;
4,810✔
748
  }
749

750
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
69,518✔
751
  int16_t valueNum = taosArrayGetSize(pTagVals);
69,518✔
752
  int32_t num = 0;
69,518✔
753
  int32_t j = 0;
69,518✔
754
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
196,688✔
755
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
127,170✔
756
    if (i > 0) {
127,170✔
757
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
57,652✔
758
                        ", ");
759
    }
760

761
    if (j >= valueNum) {
127,170✔
762
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
31,340✔
763
                        "NULL");
764
      continue;
31,340✔
765
    }
766

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

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

800
  return code;
69,518✔
801
}
802

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

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

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

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

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

848
  if (pCfg->keep > 0) {
143,896✔
849
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
5,412✔
850
                      " KEEP %dm", pCfg->keep);
851
  }
852

853
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
143,896✔
854
    int32_t nSma = 0;
78,088✔
855
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
434,524✔
856
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
356,436✔
857
        ++nSma;
356,436✔
858
      }
859
    }
860

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

883
  if (pCfg->virtualStb) {
143,896✔
884
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
3,524✔
885
                      " VIRTUAL %d", pCfg->virtualStb);
3,524✔
886
  }
887

888
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
143,896✔
889
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
50,952✔
890
                      " SECURE_DELETE %d", pCfg->secureDelete);
50,952✔
891
  }
892
}
143,896✔
893

894
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
170,536✔
895
                                              void* charsetCxt) {
896
  int32_t code = TSDB_CODE_SUCCESS;
170,536✔
897
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
170,536✔
898
  pBlock->info.rows = 1;
170,536✔
899

900
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
170,536✔
901
  char             buf1[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1)] = {0};
170,536✔
902
  STR_TO_VARSTR(buf1, tbName);
170,536✔
903
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
170,536✔
904

905
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
170,536✔
906
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
170,536✔
907
  if (NULL == buf2) {
170,536✔
UNCOV
908
    QRY_ERR_RET(terrno);
×
909
  }
910

911
  int32_t len = 0;
170,536✔
912

913
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
170,536✔
914
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
50,952✔
915
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
916
    appendColumnFields(buf2, &len, pCfg);
50,952✔
917
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
50,952✔
918
                     ") TAGS (");
919
    appendTagFields(buf2, &len, pCfg);
50,952✔
920
    len +=
50,952✔
921
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
50,952✔
922
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
50,952✔
923
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
119,584✔
924
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
65,808✔
925
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
926
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
65,808✔
927
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
65,808✔
928
    appendTagNameFields(buf2, &len, pCfg);
65,808✔
929
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
65,808✔
930
                     ") TAGS (");
931
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
65,808✔
932
    TAOS_CHECK_ERRNO(code);
65,808✔
933
    len +=
65,808✔
934
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
65,808✔
935
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
65,808✔
936
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
53,776✔
937
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
27,136✔
938
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
939
    appendColumnFields(buf2, &len, pCfg);
27,136✔
940
    len +=
27,136✔
941
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
27,136✔
942
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
27,136✔
943
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
26,640✔
944
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
7,200✔
945
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
946
    appendColumnFields(buf2, &len, pCfg);
7,200✔
947
    len +=
7,200✔
948
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
7,200✔
949
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
19,440✔
950
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
8,520✔
951
                     "CREATE VTABLE `%s`", expandIdentifier(tbName, buf1));
952
    appendColRefFields(buf2, &len, pCfg);
8,520✔
953
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
8,520✔
954
                    " USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
8,520✔
955
    appendTagNameFields(buf2, &len, pCfg);
8,520✔
956
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
8,520✔
957
                     ") TAGS (");
958
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
8,520✔
959
    TAOS_CHECK_ERRNO(code);
8,520✔
960
    len +=
8,520✔
961
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
8,520✔
962
  }
963

964
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
170,536✔
965

966
  code = colDataSetVal(pCol2, 0, buf2, false);
170,536✔
967
  TAOS_CHECK_ERRNO(code);
170,536✔
968

969
_exit:
170,536✔
970
  taosMemoryFree(buf2);
170,536✔
971

972
  return code;
170,536✔
973
}
974

975
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
12,718✔
976
  int32_t code = 0;
12,718✔
977
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
12,718✔
978
  pBlock->info.rows = 1;
12,718✔
979

980
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
12,718✔
981
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
12,718✔
982
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
12,718✔
983
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
12,718✔
984
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
12,718✔
985

986
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
12,718✔
987
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
12,718✔
988
  if (NULL == buf2) {
12,718✔
UNCOV
989
    return terrno;
×
990
  }
991

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

1005
  return code;
12,718✔
1006
}
1007

1008
extern const char* fmGetFuncName(int32_t funcId);
1009
static int32_t setCreateRsmaResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateRsmaStmt* pStmt) {
4,096✔
1010
  int32_t       code = 0, lino = 0;
4,096✔
1011
  char*         buf2 = NULL;
4,096✔
1012
  SRsmaInfoRsp* pMeta = pStmt->pRsmaMeta;
4,096✔
1013

1014
  if (pMeta->nFuncs != pMeta->nColNames) {
4,096✔
UNCOV
1015
    qError("exception: rsma meta is invalid, nFuncs:%d != nColNames:%d", pMeta->nFuncs, pMeta->nColNames);
×
UNCOV
1016
    return TSDB_CODE_APP_ERROR;
×
1017
  }
1018

1019
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, 1));
4,096✔
1020
  pBlock->info.rows = 1;
4,096✔
1021

1022
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
4,096✔
1023
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1] = {0};
4,096✔
1024
  snprintf(varDataVal(buf1), TSDB_TABLE_FNAME_LEN + 4, "`%s`", expandIdentifier(pStmt->rsmaName, buf1));
4,096✔
1025
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
4,096✔
1026
  TAOS_CHECK_EXIT(colDataSetVal(pCol1, 0, buf1, false));
4,096✔
1027

1028
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
4,096✔
1029
  if (!(buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN))) {
4,096✔
UNCOV
1030
    return terrno;
×
1031
  }
1032

1033
  SName name = {0};
4,096✔
1034
  TAOS_CHECK_EXIT(tNameFromString(&name, pMeta->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
4,096✔
1035

1036
  int32_t len = 0;
4,096✔
1037
  len += tsnprintf(varDataVal(buf2), SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
8,192✔
1038
                       "CREATE RSMA `%s` ON `%s`.`%s` FUNCTION(", expandIdentifier(pStmt->rsmaName, buf1),
4,096✔
1039
                       expandIdentifier(name.dbname, buf1), expandIdentifier(name.tname, buf1));
1040
  for (int32_t i = 0; i < pMeta->nFuncs; ++i) {
26,610✔
1041
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
45,028✔
1042
                         "%s%s(`%s`)", (i > 0) ? "," : "", fmGetFuncName(pMeta->funcIds[i]),
22,514✔
1043
                         expandIdentifier(*(char**)TARRAY_GET_ELEM(pMeta->colNames, i), buf1));
22,514✔
1044
  }
1045
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
4,096✔
1046
                       ") INTERVAL(%d%c", pMeta->interval[0], pMeta->intervalUnit);
4,096✔
1047
  if (pMeta->interval[1] > 0) {
4,096✔
1048
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
4,096✔
1049
                         ",%d%c", pMeta->interval[1], pMeta->intervalUnit);
4,096✔
1050
  }
1051
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
4,096✔
1052
  varDataLen(buf2) = len;
4,096✔
1053
  code = colDataSetVal(pCol2, 0, buf2, false);
4,096✔
1054
_exit:
4,096✔
1055
  taosMemoryFree(buf2);
4,096✔
1056
  return code;
4,096✔
1057
}
1058

1059
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
156,126✔
1060
  SSDataBlock* pBlock = NULL;
156,126✔
1061
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
156,126✔
1062
  if (TSDB_CODE_SUCCESS == code) {
156,126✔
1063
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
156,126✔
1064
  }
1065
  if (TSDB_CODE_SUCCESS == code) {
156,126✔
1066
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
156,126✔
1067
  }
1068
  (void)blockDataDestroy(pBlock);
156,126✔
1069
  return code;
156,126✔
1070
}
1071

1072
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
14,410✔
1073
  SSDataBlock* pBlock = NULL;
14,410✔
1074
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
14,410✔
1075
  if (TSDB_CODE_SUCCESS == code) {
14,410✔
1076
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
14,410✔
1077
  }
1078
  if (TSDB_CODE_SUCCESS == code) {
14,410✔
1079
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
14,410✔
1080
  }
1081
  (void)blockDataDestroy(pBlock);
14,410✔
1082
  return code;
14,410✔
1083
}
1084

1085
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
2,330✔
1086
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
2,330✔
1087
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
2,330✔
1088
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
364✔
1089
    return terrno;
364✔
1090
  }
1091

1092
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
1,966✔
1093
}
1094

1095
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
877,532✔
1096
  int32_t code = 0;
877,532✔
1097

1098
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
877,532✔
1099
    taosResetLog();
92✔
1100
    cfgDumpCfg(tsCfg, 0, false);
92✔
1101
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
877,440✔
1102
    int32_t tmp = 0;
5,088✔
1103
    code = taosStr2int32(value, &tmp);
5,088✔
1104
    if (code) {
5,088✔
UNCOV
1105
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
UNCOV
1106
      return code;
×
1107
    }
1108
    code = schedulerUpdatePolicy(tmp);
5,088✔
1109
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
872,352✔
1110
    int32_t tmp = 0;
×
1111
    code = taosStr2int32(value, &tmp);
×
UNCOV
1112
    if (code) {
×
UNCOV
1113
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
UNCOV
1114
      return code;
×
1115
    }
1116
    code = schedulerEnableReSchedule(tmp != 0);
×
1117
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
872,352✔
1118
    code = ctgdHandleDbgCommand(value);
×
1119
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
872,352✔
UNCOV
1120
    code = taosMemoryDbgInit();
×
1121
    if (code) {
×
UNCOV
1122
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
1123
      return code;
×
1124
    }
1125
    tsAsyncLog = false;
×
1126
    qInfo("memory dbg enabled");
×
1127
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
872,352✔
1128
    code = taosMemoryDbgInitRestore();
×
UNCOV
1129
    if (code) {
×
1130
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
1131
      return code;
×
1132
    }
1133
    qInfo("memory dbg disabled");
×
1134
  } else {
1135
    goto _return;
872,352✔
1136
  }
1137

1138
  *processed = true;
5,180✔
1139

1140
_return:
877,532✔
1141

1142
  if (code) {
877,532✔
UNCOV
1143
    terrno = code;
×
1144
  }
1145

1146
  return code;
877,532✔
1147
}
1148

1149
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
877,532✔
1150
  bool processed = false;
877,532✔
1151

1152
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
877,532✔
UNCOV
1153
    return terrno;
×
1154
  }
1155

1156
  if (processed) {
877,532✔
1157
    goto _return;
5,180✔
1158
  }
1159

1160
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
872,352✔
1161
    return terrno;
3,192✔
1162
  }
1163

1164
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
869,160✔
UNCOV
1165
    return terrno;
×
1166
  }
1167

1168
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
869,160✔
1169

1170
_return:
874,340✔
1171

1172
  return TSDB_CODE_SUCCESS;
874,340✔
1173
}
1174

1175
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
28,500✔
1176
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
28,500✔
1177
  if (NULL == pBlock) {
28,500✔
UNCOV
1178
    return terrno;
×
1179
  }
1180

1181
  pBlock->info.hasVarCol = true;
28,500✔
1182

1183
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
28,500✔
1184
  if (NULL == pBlock->pDataBlock) {
28,500✔
UNCOV
1185
    taosMemoryFree(pBlock);
×
UNCOV
1186
    return terrno;
×
1187
  }
1188

1189
  SColumnInfoData infoData = {0};
28,500✔
1190

1191
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
28,500✔
1192
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
28,500✔
1193
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
57,000✔
UNCOV
1194
    goto _exit;
×
1195
  }
1196

1197
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
28,500✔
1198
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
28,500✔
1199
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
57,000✔
UNCOV
1200
    goto _exit;
×
1201
  }
1202

1203
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
28,500✔
1204
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
28,500✔
1205
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
57,000✔
UNCOV
1206
    goto _exit;
×
1207
  }
1208

1209
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
28,500✔
1210
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
28,500✔
1211
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
57,000✔
UNCOV
1212
    goto _exit;
×
1213
  }
1214

1215
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
28,500✔
1216
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
28,500✔
1217
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
57,000✔
UNCOV
1218
    goto _exit;
×
1219
  }
1220

1221
  *pOutput = pBlock;
28,500✔
1222

1223
_exit:
28,500✔
1224
  if (terrno != TSDB_CODE_SUCCESS) {
28,500✔
UNCOV
1225
    taosArrayDestroy(pBlock->pDataBlock);
×
UNCOV
1226
    taosMemoryFree(pBlock);
×
1227
  }
1228
  return terrno;
28,500✔
1229
}
1230

1231
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
28,500✔
1232
  SSDataBlock* pBlock = NULL;
28,500✔
1233
  char*        likePattern = NULL;
28,500✔
1234
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
28,500✔
1235
  if (TSDB_CODE_SUCCESS == code) {
28,500✔
1236
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
28,500✔
1237
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
5,630✔
1238
    }
1239
  }
1240
  if (TSDB_CODE_SUCCESS == code) {
28,500✔
1241
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
28,500✔
1242
  }
1243
  if (TSDB_CODE_SUCCESS == code) {
28,500✔
1244
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
28,500✔
1245
  }
1246
  (void)blockDataDestroy(pBlock);
28,500✔
1247
  return code;
28,500✔
1248
}
1249

1250
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
5,139,216✔
1251
  QRY_PARAM_CHECK(pOutput);
5,139,216✔
1252

1253
  SSDataBlock* pBlock = NULL;
5,139,216✔
1254
  int32_t      code = createDataBlock(&pBlock);
5,139,216✔
1255
  if (code) {
5,139,216✔
UNCOV
1256
    return code;
×
1257
  }
1258

1259
  SNode* pProj = NULL;
5,139,216✔
1260
  FOREACH(pProj, pProjects) {
13,100,480✔
1261
    SExprNode*      pExpr = (SExprNode*)pProj;
7,961,264✔
1262
    SColumnInfoData infoData = {0};
7,961,264✔
1263
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
7,961,264✔
UNCOV
1264
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
UNCOV
1265
      infoData.info.bytes = 0;
×
1266
    } else {
1267
      infoData.info.type = pExpr->resType.type;
7,961,264✔
1268
      infoData.info.bytes = pExpr->resType.bytes;
7,961,264✔
1269
      infoData.info.precision = pExpr->resType.precision;
7,961,264✔
1270
      infoData.info.scale = pExpr->resType.scale;
7,961,264✔
1271
    }
1272
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
7,961,264✔
1273
  }
1274

1275
  *pOutput = pBlock;
5,139,216✔
1276
  return code;
5,139,216✔
1277
}
1278

1279
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
5,139,216✔
1280
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
5,139,216✔
1281

1282
  int32_t index = 0;
5,139,216✔
1283
  SNode*  pProj = NULL;
5,139,216✔
1284
  FOREACH(pProj, pProjects) {
13,100,480✔
1285
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
7,961,264✔
UNCOV
1286
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1287
    } else {
1288
      if (((SValueNode*)pProj)->isNull) {
7,961,264✔
1289
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
167,742✔
1290
      } else {
1291
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
7,793,522✔
1292
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1293
      }
1294
    }
1295
  }
1296

1297
  pBlock->info.rows = 1;
5,139,216✔
1298
  return TSDB_CODE_SUCCESS;
5,139,216✔
1299
}
1300

1301
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
5,139,216✔
1302
  SSDataBlock* pBlock = NULL;
5,139,216✔
1303
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
5,139,216✔
1304
  if (TSDB_CODE_SUCCESS == code) {
5,139,216✔
1305
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
5,139,216✔
1306
  }
1307
  if (TSDB_CODE_SUCCESS == code) {
5,139,216✔
1308
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
5,139,216✔
1309
  }
1310
  (void)blockDataDestroy(pBlock);
5,139,216✔
1311
  return code;
5,139,216✔
1312
}
1313

1314
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
12,718✔
1315
  SSDataBlock* pBlock = NULL;
12,718✔
1316
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
12,718✔
1317
  if (TSDB_CODE_SUCCESS == code) {
12,718✔
1318
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
12,718✔
1319
  }
1320
  if (TSDB_CODE_SUCCESS == code) {
12,718✔
1321
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
12,718✔
1322
  }
1323
  (void)blockDataDestroy(pBlock);
12,718✔
1324
  return code;
12,718✔
1325
}
1326

1327
static int32_t execShowCreateRsma(SShowCreateRsmaStmt* pStmt, SRetrieveTableRsp** pRsp) {
4,096✔
1328
  SSDataBlock* pBlock = NULL;
4,096✔
1329
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
4,096✔
1330
  if (TSDB_CODE_SUCCESS == code) {
4,096✔
1331
    code = setCreateRsmaResultIntoDataBlock(pBlock, pStmt);
4,096✔
1332
  }
1333
  if (TSDB_CODE_SUCCESS == code) {
4,096✔
1334
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
4,096✔
1335
  }
1336
  (void)blockDataDestroy(pBlock);
4,096✔
1337
  return code;
4,096✔
1338
}
1339

1340
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
11,429,910✔
1341
                     void* charsetCxt) {
1342
  switch (nodeType(pStmt)) {
11,429,910✔
1343
    case QUERY_NODE_DESCRIBE_STMT:
858,802✔
1344
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
858,802✔
1345
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
4,208,510✔
1346
      return execResetQueryCache();
4,208,510✔
1347
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
129,636✔
1348
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
129,636✔
1349
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
154,160✔
1350
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
154,160✔
1351
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
14,410✔
1352
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
14,410✔
1353
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
2,330✔
1354
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
2,330✔
1355
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
12,718✔
1356
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
12,718✔
1357
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
4,096✔
1358
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
4,096✔
1359
    case QUERY_NODE_ALTER_LOCAL_STMT:
877,532✔
1360
      return execAlterLocal((SAlterLocalStmt*)pStmt);
877,532✔
1361
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
28,500✔
1362
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
28,500✔
1363
    case QUERY_NODE_SELECT_STMT:
5,139,216✔
1364
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
5,139,216✔
UNCOV
1365
    default:
×
UNCOV
1366
      break;
×
1367
  }
UNCOV
1368
  return TSDB_CODE_FAILED;
×
1369
}
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