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

taosdata / TDengine / #3548

04 Dec 2024 01:03PM UTC coverage: 59.846% (-0.8%) from 60.691%
#3548

push

travis-ci

web-flow
Merge pull request #29033 from taosdata/fix/calculate-vnode-memory-used

fix/calculate-vnode-memory-used

118484 of 254183 branches covered (46.61%)

Branch coverage included in aggregate %.

199691 of 277471 relevant lines covered (71.97%)

18794141.86 hits per line

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

62.79
/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 "scheduler.h"
20
#include "systable.h"
21
#include "taosdef.h"
22
#include "tdatablock.h"
23
#include "tglobal.h"
24
#include "tgrant.h"
25

26
#define COL_DATA_SET_VAL_AND_CHECK(pCol, rows, buf, isNull) \
27
  do {                                                      \
28
    int _code = colDataSetVal(pCol, rows, buf, isNull);     \
29
    if (TSDB_CODE_SUCCESS != _code) {                       \
30
      terrno = _code;                                       \
31
      return _code;                                         \
32
    }                                                       \
33
  } while (0)
34

35
extern SConfig* tsCfg;
36

37
static int32_t buildRetrieveTableRsp(SSDataBlock* pBlock, int32_t numOfCols, SRetrieveTableRsp** pRsp) {
75,190✔
38
  if (NULL == pBlock || NULL == pRsp) {
75,190!
39
    return TSDB_CODE_INVALID_PARA;
×
40
  }
41
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
75,190✔
42
  size_t rspSize = sizeof(SRetrieveTableRsp) + dataEncodeBufSize + PAYLOAD_PREFIX_LEN;
75,190✔
43
  *pRsp = taosMemoryCalloc(1, rspSize);
75,190✔
44
  if (NULL == *pRsp) {
75,190!
45
    return terrno;
×
46
  }
47

48
  (*pRsp)->useconds = 0;
75,190✔
49
  (*pRsp)->completed = 1;
75,190✔
50
  (*pRsp)->precision = 0;
75,190✔
51
  (*pRsp)->compressed = 0;
75,190✔
52

53
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
75,190✔
54
  (*pRsp)->numOfCols = htonl(numOfCols);
75,190✔
55

56
  int32_t len = blockEncode(pBlock, (*pRsp)->data + PAYLOAD_PREFIX_LEN, dataEncodeBufSize, numOfCols);
75,190✔
57
  if(len < 0) {
75,190!
58
    taosMemoryFree(*pRsp);
×
59
    *pRsp = NULL;
×
60
    return terrno;
×
61
  }
62
  SET_PAYLOAD_LEN((*pRsp)->data, len, len);
75,190✔
63

64
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
75,190✔
65
  (*pRsp)->payloadLen = htonl(payloadLen);
75,190✔
66
  (*pRsp)->compLen = htonl(payloadLen);
75,190✔
67

68
  return TSDB_CODE_SUCCESS;
75,190✔
69
}
70

71
static int32_t getSchemaBytes(const SSchema* pSchema) {
8,189✔
72
  switch (pSchema->type) {
8,189✔
73
    case TSDB_DATA_TYPE_BINARY:
800✔
74
    case TSDB_DATA_TYPE_VARBINARY:
75
    case TSDB_DATA_TYPE_GEOMETRY:
76
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
800✔
77
    case TSDB_DATA_TYPE_NCHAR:
504✔
78
    case TSDB_DATA_TYPE_JSON:
79
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
504✔
80
    default:
6,885✔
81
      return pSchema->bytes;
6,885✔
82
  }
83
}
84

85
static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) {
506✔
86
  QRY_PARAM_CHECK(pOutput);
506!
87

88
  SSDataBlock* pBlock = NULL;
506✔
89
  int32_t      code = createDataBlock(&pBlock);
506✔
90
  if (code) {
506!
91
    return code;
×
92
  }
93

94
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_FIELD_LEN, 1);
506✔
95
  code = blockDataAppendColInfo(pBlock, &infoData);
506✔
96
  if (TSDB_CODE_SUCCESS == code) {
506!
97
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_TYPE_LEN, 2);
506✔
98
    code = blockDataAppendColInfo(pBlock, &infoData);
506✔
99
  }
100
  if (TSDB_CODE_SUCCESS == code) {
506!
101
    infoData = createColumnInfoData(TSDB_DATA_TYPE_INT, tDataTypes[TSDB_DATA_TYPE_INT].bytes, 3);
506✔
102
    code = blockDataAppendColInfo(pBlock, &infoData);
506✔
103
  }
104
  if (TSDB_CODE_SUCCESS == code) {
506!
105
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_NOTE_LEN, 4);
506✔
106
    code = blockDataAppendColInfo(pBlock, &infoData);
506✔
107
  }
108
  if (TSDB_CODE_SUCCESS == code) {
506!
109
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 5);
506✔
110
    code = blockDataAppendColInfo(pBlock, &infoData);
506✔
111
  }
112
  if (TSDB_CODE_SUCCESS == code) {
506!
113
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 6);
506✔
114
    code = blockDataAppendColInfo(pBlock, &infoData);
506✔
115
  }
116
  if (TSDB_CODE_SUCCESS == code) {
506!
117
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 7);
506✔
118
    code = blockDataAppendColInfo(pBlock, &infoData);
506✔
119
  }
120

121
  if (TSDB_CODE_SUCCESS == code) {
506!
122
    *pOutput = pBlock;
506✔
123
  } else {
124
    (void)blockDataDestroy(pBlock);
×
125
  }
126
  return code;
506✔
127
}
128

129
static int32_t setDescResultIntoDataBlock(bool sysInfoUser, SSDataBlock* pBlock, int32_t numOfRows, STableMeta* pMeta,
506✔
130
                                          int8_t biMode) {
131
  int32_t blockCap = (biMode != 0) ? numOfRows + 1 : numOfRows;
506!
132
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, blockCap));
506!
133
  pBlock->info.rows = 0;
506✔
134

135
  // field
136
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
506✔
137
  // Type
138
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
506✔
139
  // Length
140
  SColumnInfoData* pCol3 = taosArrayGet(pBlock->pDataBlock, 2);
506✔
141
  // Note
142
  SColumnInfoData* pCol4 = taosArrayGet(pBlock->pDataBlock, 3);
506✔
143
  // encode
144
  SColumnInfoData* pCol5 = NULL;
506✔
145
  // compress
146
  SColumnInfoData* pCol6 = NULL;
506✔
147
  // level
148
  SColumnInfoData* pCol7 = NULL;
506✔
149
  if (useCompress(pMeta->tableType)) {
506✔
150
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
491✔
151
    pCol6 = taosArrayGet(pBlock->pDataBlock, 5);
491✔
152
    pCol7 = taosArrayGet(pBlock->pDataBlock, 6);
491✔
153
  }
154

155
  int32_t fillTagCol = 0;
506✔
156
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
506✔
157
  for (int32_t i = 0; i < numOfRows; ++i) {
8,695✔
158
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
8,189!
159
      continue;
×
160
    }
161
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
8,189✔
162
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
8,189!
163

164
    STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
8,189✔
165
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
8,189!
166
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
8,189✔
167
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
8,189!
168
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
8,189✔
169
      if (i >= pMeta->tableInfo.numOfColumns) {
8,149✔
170
        STR_TO_VARSTR(buf, "TAG");
1,987✔
171
        fillTagCol = 1;
1,987✔
172
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
6,162!
173
        STR_TO_VARSTR(buf, "PRIMARY KEY")
×
174
      } else {
175
        STR_TO_VARSTR(buf, "");
6,162✔
176
      }
177
    } else {
178
      STR_TO_VARSTR(buf, "VIEW COL");
40✔
179
    }
180
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
8,189!
181
    if (useCompress(pMeta->tableType) && pMeta->schemaExt) {
8,189!
182
      if (i < pMeta->tableInfo.numOfColumns) {
8,140✔
183
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
6,153✔
184
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
6,153!
185
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
6,153✔
186
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
6,153!
187
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
6,153✔
188
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
6,153!
189
      } else {
190
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
1,987!
191
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
1,987!
192
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
1,987!
193
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
1,987!
194
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
1,987!
195
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
1,987!
196
      }
197
    }
198

199
    fillTagCol = 0;
8,189✔
200

201
    ++(pBlock->info.rows);
8,189✔
202
  }
203
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
506!
204
    STR_TO_VARSTR(buf, "tbname");
×
205
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
×
206
    STR_TO_VARSTR(buf, "VARCHAR");
×
207
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
×
208
    int32_t bytes = TSDB_TABLE_NAME_LEN - 1;
×
209
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
×
210
    STR_TO_VARSTR(buf, "TAG");
×
211
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
×
212
    ++(pBlock->info.rows);
×
213
  }
214
  if (pBlock->info.rows <= 0) {
506!
215
    qError("no permission to view any columns");
×
216
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
217
  }
218
  return TSDB_CODE_SUCCESS;
506✔
219
}
220

221
static int32_t execDescribe(bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode) {
506✔
222
  SDescribeStmt* pDesc = (SDescribeStmt*)pStmt;
506✔
223
  if (NULL == pDesc || NULL == pDesc->pMeta) {
506!
224
    return TSDB_CODE_INVALID_PARA;
×
225
  }
226
  int32_t        numOfRows = TABLE_TOTAL_COL_NUM(pDesc->pMeta);
506✔
227

228
  SSDataBlock* pBlock = NULL;
506✔
229
  int32_t      code = buildDescResultDataBlock(&pBlock);
506✔
230
  if (TSDB_CODE_SUCCESS == code) {
506!
231
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
506✔
232
  }
233
  if (TSDB_CODE_SUCCESS == code) {
506!
234
    if (pDesc->pMeta && useCompress(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
506!
235
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
491✔
236
    } else {
237
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
15✔
238
    }
239
  }
240
  (void)blockDataDestroy(pBlock);
506✔
241
  return code;
506✔
242
}
243

244
static int32_t execResetQueryCache() { return catalogClearCache(); }
6,085✔
245

246
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
18✔
247
  QRY_PARAM_CHECK(pOutput);
18!
248

249
  SSDataBlock* pBlock = NULL;
18✔
250
  int32_t      code = createDataBlock(&pBlock);
18✔
251
  if (code) {
18!
252
    return code;
×
253
  }
254

255
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_COLS, 1);
18✔
256
  code = blockDataAppendColInfo(pBlock, &infoData);
18✔
257
  if (TSDB_CODE_SUCCESS == code) {
18!
258
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_FIELD2_LEN, 2);
18✔
259
    code = blockDataAppendColInfo(pBlock, &infoData);
18✔
260
  }
261

262
  if (TSDB_CODE_SUCCESS == code) {
18!
263
    *pOutput = pBlock;
18✔
264
  } else {
265
    (void)blockDataDestroy(pBlock);
×
266
  }
267
  return code;
18✔
268
}
269

270
int64_t getValOfDiffPrecision(int8_t unit, int64_t val) {
×
271
  int64_t v = 0;
×
272
  switch (unit) {
×
273
    case 's':
×
274
      v = val / 1000;
×
275
      break;
×
276
    case 'm':
×
277
      v = val / tsTickPerMin[TSDB_TIME_PRECISION_MILLI];
×
278
      break;
×
279
    case 'h':
×
280
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 60);
×
281
      break;
×
282
    case 'd':
×
283
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60);
×
284
      break;
×
285
    case 'w':
×
286
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60 * 7);
×
287
      break;
×
288
    default:
×
289
      break;
×
290
  }
291

292
  return v;
×
293
}
294

295
static int32_t buildRetension(SArray* pRetension, char** ppRetentions) {
18✔
296
  size_t size = taosArrayGetSize(pRetension);
18✔
297
  if (size == 0) {
18!
298
    *ppRetentions = NULL;
18✔
299
    return TSDB_CODE_SUCCESS;
18✔
300
  }
301

302
  const int lMaxLen = 128;
×
303
  char*     p1 = taosMemoryCalloc(1, lMaxLen);
×
304
  if (NULL == p1) {
×
305
    return terrno;
×
306
  }
307
  int32_t len = 0;
×
308

309
  for (int32_t i = 0; i < size; ++i) {
×
310
    SRetention* p = TARRAY_GET_ELEM(pRetension, i);
×
311
    int64_t     v1 = getValOfDiffPrecision(p->freqUnit, p->freq);
×
312
    int64_t     v2 = getValOfDiffPrecision(p->keepUnit, p->keep);
×
313
    if (i == 0) {
×
314
      len += tsnprintf(p1 + len, lMaxLen - len, "-:%" PRId64 "%c", v2, p->keepUnit);
×
315
    } else {
316
      len += tsnprintf(p1 + len, lMaxLen - len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit);
×
317
    }
318

319
    if (i < size - 1) {
×
320
      len += tsnprintf(p1 + len, lMaxLen - len, ",");
×
321
    }
322
  }
323

324
  *ppRetentions = p1;
×
325
  return TSDB_CODE_SUCCESS;
×
326
}
327

328
static const char* cacheModelStr(int8_t cacheModel) {
18✔
329
  switch (cacheModel) {
18!
330
    case TSDB_CACHE_MODEL_NONE:
18✔
331
      return TSDB_CACHE_MODEL_NONE_STR;
18✔
332
    case TSDB_CACHE_MODEL_LAST_ROW:
×
333
      return TSDB_CACHE_MODEL_LAST_ROW_STR;
×
334
    case TSDB_CACHE_MODEL_LAST_VALUE:
×
335
      return TSDB_CACHE_MODEL_LAST_VALUE_STR;
×
336
    case TSDB_CACHE_MODEL_BOTH:
×
337
      return TSDB_CACHE_MODEL_BOTH_STR;
×
338
    default:
×
339
      break;
×
340
  }
341
  return TSDB_CACHE_MODEL_NONE_STR;
×
342
}
343

344
static const char* encryptAlgorithmStr(int8_t encryptAlgorithm) {
18✔
345
  switch (encryptAlgorithm) {
18!
346
    case TSDB_ENCRYPT_ALGO_NONE:
18✔
347
      return TSDB_ENCRYPT_ALGO_NONE_STR;
18✔
348
    case TSDB_ENCRYPT_ALGO_SM4:
×
349
      return TSDB_ENCRYPT_ALGO_SM4_STR;
×
350
    default:
×
351
      break;
×
352
  }
353
  return TSDB_CACHE_MODEL_NONE_STR;
×
354
}
355

356
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
2,342,742✔
357
  if (buffer == NULL || bufSize <= 0) {
2,342,742!
358
    return 0;
×
359
  }
360
  int32_t len = 0;
2,343,024✔
361
  if (timeInMinutes % 1440 == 0) {
2,343,024!
362
    int32_t days = timeInMinutes / 1440;
2,343,367✔
363
    len = tsnprintf(buffer, bufSize, "%dd", days);
2,343,367✔
364
  } else if (timeInMinutes % 60 == 0) {
×
365
    int32_t hours = timeInMinutes / 60;
×
366
    len = tsnprintf(buffer, bufSize, "%dh", hours);
×
367
  } else {
368
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
×
369
  }
370
  return len;
2,346,227✔
371
}
372

373
static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, char* dbFName, SDbCfgInfo* pCfg) {
18✔
374
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
18!
375
  pBlock->info.rows = 1;
18✔
376

377
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
18✔
378
  char             buf1[SHOW_CREATE_DB_RESULT_FIELD1_LEN] = {0};
18✔
379
  STR_TO_VARSTR(buf1, dbName);
18✔
380
  COL_DATA_SET_VAL_AND_CHECK(pCol1, 0, buf1, false);
18!
381

382
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
18✔
383
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
18✔
384
  int32_t          len = 0;
18✔
385
  char*            prec = NULL;
18✔
386
  switch (pCfg->precision) {
18!
387
    case TSDB_TIME_PRECISION_MILLI:
18✔
388
      prec = TSDB_TIME_PRECISION_MILLI_STR;
18✔
389
      break;
18✔
390
    case TSDB_TIME_PRECISION_MICRO:
×
391
      prec = TSDB_TIME_PRECISION_MICRO_STR;
×
392
      break;
×
393
    case TSDB_TIME_PRECISION_NANO:
×
394
      prec = TSDB_TIME_PRECISION_NANO_STR;
×
395
      break;
×
396
    default:
×
397
      prec = "none";
×
398
      break;
×
399
  }
400

401
  char* pRetentions = NULL;
18✔
402
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
18!
403
  int32_t dbFNameLen = strlen(dbFName);
18✔
404
  int32_t hashPrefix = 0;
18✔
405
  if (pCfg->hashPrefix > 0) {
18✔
406
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
2✔
407
  } else if (pCfg->hashPrefix < 0) {
16✔
408
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
3✔
409
  }
410
  char durationStr[128] = {0};
18✔
411
  char keep0Str[128] = {0};
18✔
412
  char keep1Str[128] = {0};
18✔
413
  char keep2Str[128] = {0};
18✔
414

415
  int32_t lenDuration = formatDurationOrKeep(durationStr, sizeof(durationStr), pCfg->daysPerFile);
18✔
416
  int32_t lenKeep0 = formatDurationOrKeep(keep0Str, sizeof(keep0Str), pCfg->daysToKeep0);
18✔
417
  int32_t lenKeep1 = formatDurationOrKeep(keep1Str, sizeof(keep1Str), pCfg->daysToKeep1);
18✔
418
  int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pCfg->daysToKeep2);
18✔
419

420
  if (IS_SYS_DBNAME(dbName)) {
18!
421
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
422
                     "CREATE DATABASE `%s`", dbName);
×
423
  } else {
424
    len +=
18✔
425
        tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
18✔
426
                  "CREATE DATABASE `%s` BUFFER %d CACHESIZE %d CACHEMODEL '%s' COMP %d DURATION %s "
427
                  "WAL_FSYNC_PERIOD %d MAXROWS %d MINROWS %d STT_TRIGGER %d KEEP %s,%s,%s PAGES %d PAGESIZE %d "
428
                  "PRECISION '%s' REPLICA %d "
429
                  "WAL_LEVEL %d VGROUPS %d SINGLE_STABLE %d TABLE_PREFIX %d TABLE_SUFFIX %d TSDB_PAGESIZE %d "
430
                  "WAL_RETENTION_PERIOD %d WAL_RETENTION_SIZE %" PRId64
431
                  " KEEP_TIME_OFFSET %d ENCRYPT_ALGORITHM '%s' S3_CHUNKPAGES %d S3_KEEPLOCAL %dm S3_COMPACT %d",
432
                  dbName, pCfg->buffer, pCfg->cacheSize, cacheModelStr(pCfg->cacheLast), pCfg->compression, durationStr,
18✔
433
                  pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str, keep1Str, keep2Str,
18✔
434
                  pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel, pCfg->numOfVgroups,
18✔
435
                  1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize, pCfg->walRetentionPeriod,
18✔
436
                  pCfg->walRetentionSize, pCfg->keepTimeOffset, encryptAlgorithmStr(pCfg->encryptAlgorithm),
18✔
437
                  pCfg->s3ChunkSize, pCfg->s3KeepLocal, pCfg->s3Compact);
18✔
438

439
    if (pRetentions) {
18!
440
      len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
441
                       " RETENTIONS %s", pRetentions);
442
    }
443
  }
444

445
  taosMemoryFree(pRetentions);
18✔
446

447
  (varDataLen(buf2)) = len;
18✔
448

449
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
18!
450

451
  return TSDB_CODE_SUCCESS;
18✔
452
}
453

454
static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt, SRetrieveTableRsp** pRsp) {
18✔
455
  SSDataBlock* pBlock = NULL;
18✔
456
  int32_t      code = buildCreateDBResultDataBlock(&pBlock);
18✔
457
  if (TSDB_CODE_SUCCESS == code) {
18!
458
    code = setCreateDBResultIntoDataBlock(pBlock, pStmt->dbName, pStmt->dbFName, pStmt->pCfg);
18✔
459
  }
460
  if (TSDB_CODE_SUCCESS == code) {
18!
461
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_DB_RESULT_COLS, pRsp);
18✔
462
  }
463
  (void)blockDataDestroy(pBlock);
18✔
464
  return code;
18✔
465
}
466

467
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
70✔
468
  QRY_PARAM_CHECK(pOutput);
70!
469

470
  SSDataBlock* pBlock = NULL;
70✔
471
  int32_t      code = createDataBlock(&pBlock);
70✔
472
  if (code) {
70!
473
    return code;
×
474
  }
475

476
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD1_LEN, 1);
70✔
477
  code = blockDataAppendColInfo(pBlock, &infoData);
70✔
478
  if (TSDB_CODE_SUCCESS == code) {
70!
479
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD2_LEN, 2);
70✔
480
    code = blockDataAppendColInfo(pBlock, &infoData);
70✔
481
  }
482

483
  if (TSDB_CODE_SUCCESS == code) {
70!
484
    *pOutput = pBlock;
70✔
485
  } else {
486
    (void)blockDataDestroy(pBlock);
×
487
  }
488
  return code;
70✔
489
}
490

491
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
10✔
492
  QRY_PARAM_CHECK(pOutput);
10!
493

494
  SSDataBlock* pBlock = NULL;
10✔
495
  int32_t      code = createDataBlock(&pBlock);
10✔
496
  if (code) {
10!
497
    return code;
×
498
  }
499

500
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD1_LEN, 1);
10✔
501
  code = blockDataAppendColInfo(pBlock, &infoData);
10✔
502
  if (TSDB_CODE_SUCCESS == code) {
10!
503
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD2_LEN, 2);
10✔
504
    code = blockDataAppendColInfo(pBlock, &infoData);
10✔
505
  }
506

507
  if (TSDB_CODE_SUCCESS == code) {
10!
508
    *pOutput = pBlock;
10✔
509
  } else {
510
    (void)blockDataDestroy(pBlock);
×
511
  }
512
  return code;
10✔
513
}
514

515
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
33✔
516
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
148✔
517
    SSchema* pSchema = pCfg->pSchemas + i;
115✔
518
#define LTYPE_LEN (32 + 60)  // 60 byte for compress info
519
    char type[LTYPE_LEN];
520
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
115✔
521
    int typeLen = strlen(type);
115✔
522
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
115!
523
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
101!
524
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
14✔
525
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
101✔
526
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
9✔
527
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
9✔
528
    }
529

530
    if (useCompress(pCfg->tableType) && pCfg->pSchemaExt) {
115!
531
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " ENCODE \'%s\'",
115✔
532
                           columnEncodeStr(COMPRESS_L1_TYPE_U32(pCfg->pSchemaExt[i].compress)));
115✔
533
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " COMPRESS \'%s\'",
115✔
534
                           columnCompressStr(COMPRESS_L2_TYPE_U32(pCfg->pSchemaExt[i].compress)));
115✔
535
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " LEVEL \'%s\'",
115✔
536
                           columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pCfg->pSchemaExt[i].compress)));
115✔
537
    }
538
    if (!(pSchema->flags & COL_IS_KEY)) {
115!
539
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
115✔
540
                        "%s`%s` %s", ((i > 0) ? ", " : ""), pSchema->name, type);
115✔
541
    } else {
542
      char* pk = "PRIMARY KEY";
×
543
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
544
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), pSchema->name, type, pk);
×
545
    }
546
  }
547
}
33✔
548

549
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
23✔
550
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
69✔
551
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
46✔
552
    char     type[32];
553
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
46✔
554
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
46!
555
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
39!
556
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
7✔
557
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
7✔
558
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
39✔
559
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
4✔
560
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
4✔
561
    }
562

563
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
46✔
564
                      "%s`%s` %s", ((i > 0) ? ", " : ""), pSchema->name, type);
46✔
565
  }
566
}
23✔
567

568
static void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
37✔
569
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
84✔
570
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
47✔
571
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
47✔
572
                      "%s`%s`", ((i > 0) ? ", " : ""), pSchema->name);
47✔
573
  }
574
}
37✔
575

576
static int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) {
37✔
577
  int32_t code = TSDB_CODE_SUCCESS;
37✔
578
  SArray* pTagVals = NULL;
37✔
579
  STag*   pTag = (STag*)pCfg->pTags;
37✔
580

581
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
37!
582
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
583
    return TSDB_CODE_APP_ERROR;
×
584
  }
585

586
  if (tTagIsJson(pTag)) {
37✔
587
    char* pJson = NULL;
4✔
588
    parseTagDatatoJson(pTag, &pJson);
4✔
589
    if (NULL == pJson) {
4!
590
      qError("failed to parse tag to json, pJson is NULL");
×
591
      return terrno;
×
592
    }
593
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
4✔
594
                      "%s", pJson);
595
    taosMemoryFree(pJson);
4✔
596

597
    return TSDB_CODE_SUCCESS;
4✔
598
  }
599

600
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
33!
601
  int16_t valueNum = taosArrayGetSize(pTagVals);
33✔
602
  int32_t num = 0;
33✔
603
  int32_t j = 0;
33✔
604
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
76✔
605
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
43✔
606
    if (i > 0) {
43✔
607
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
10✔
608
                        ", ");
609
    }
610

611
    if (j >= valueNum) {
43✔
612
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
21✔
613
                        "NULL");
614
      continue;
21✔
615
    }
616

617
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
22✔
618
    if (pSchema->colId > pTagVal->cid) {
22!
619
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
620
      code = TSDB_CODE_APP_ERROR;
×
621
      TAOS_CHECK_ERRNO(code);
×
622
    } else if (pSchema->colId == pTagVal->cid) {
22✔
623
      char    type = pTagVal->type;
20✔
624
      int32_t tlen = 0;
20✔
625

626
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
20✔
627
      if (leftSize <= 0) {
20!
628
        qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
×
629
        code = TSDB_CODE_APP_ERROR;
×
630
        TAOS_CHECK_ERRNO(code);
×
631
      }
632
      if (IS_VAR_DATA_TYPE(type)) {
20!
633
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
4✔
634
        TAOS_CHECK_ERRNO(code);
4!
635
      } else {
636
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
16✔
637
                               &tlen);
638
        TAOS_CHECK_ERRNO(code);
16!
639
      }
640
      *len += tlen;
20✔
641
      j++;
20✔
642
    } else {
643
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2✔
644
                        "NULL");
645
    }
646
  }
647
_exit:
33✔
648
  taosArrayDestroy(pTagVals);
33✔
649

650
  return code;
33✔
651
}
652

653
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
70✔
654
  if (pCfg->commentLen > 0) {
70!
655
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
656
                      " COMMENT '%s'", pCfg->pComment);
657
  } else if (0 == pCfg->commentLen) {
70!
658
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
659
                      " COMMENT ''");
660
  }
661

662
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
70!
663
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
664
                      " WATERMARK %" PRId64 "a", pCfg->watermark1);
665
    if (pCfg->watermark2 > 0) {
×
666
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
667
                        ", %" PRId64 "a", pCfg->watermark2);
668
    }
669
  }
670

671
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
70!
672
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
673
                      " MAX_DELAY %" PRId64 "a", pCfg->delay1);
674
    if (pCfg->delay2 > 0) {
×
675
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
676
                        ", %" PRId64 "a", pCfg->delay2);
677
    }
678
  }
679

680
  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
70✔
681
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
70!
682
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
683
                      " ROLLUP(");
684
    for (int32_t i = 0; i < funcNum; ++i) {
×
685
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
×
686
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
687
                        "%s%s", ((i > 0) ? ", " : ""), pFunc);
688
    }
689
    *len +=
×
690
        snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
×
691
  }
692

693
  if (pCfg->ttl > 0) {
70!
694
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
695
                      " TTL %d", pCfg->ttl);
696
  }
697

698
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
70✔
699
    int32_t nSma = 0;
33✔
700
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
148✔
701
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
115!
702
        ++nSma;
115✔
703
      }
704
    }
705

706
    if (nSma < pCfg->numOfColumns && nSma > 0) {
33!
707
      bool smaOn = false;
×
708
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
709
                        " SMA(");
710
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
711
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
712
          if (smaOn) {
×
713
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
714
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ",`%s`",
×
715
                              (pCfg->pSchemas + i)->name);
×
716
          } else {
717
            smaOn = true;
×
718
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
719
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), "`%s`",
×
720
                              (pCfg->pSchemas + i)->name);
×
721
          }
722
        }
723
      }
724
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, ")");
×
725
    }
726
  }
727
}
70✔
728

729
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg) {
70✔
730
  int32_t code = TSDB_CODE_SUCCESS;
70✔
731
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
70!
732
  pBlock->info.rows = 1;
70✔
733

734
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
70✔
735
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN] = {0};
70✔
736
  STR_TO_VARSTR(buf1, tbName);
70✔
737
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
70!
738

739
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
70✔
740
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
70✔
741
  if (NULL == buf2) {
70!
742
    QRY_ERR_RET(terrno);
×
743
  }
744

745
  int32_t len = 0;
70✔
746

747
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
70✔
748
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
23✔
749
                     "CREATE STABLE `%s` (", tbName);
750
    appendColumnFields(buf2, &len, pCfg);
23✔
751
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
23✔
752
                     ") TAGS (");
753
    appendTagFields(buf2, &len, pCfg);
23✔
754
    len +=
23✔
755
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
23✔
756
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
23✔
757
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
47✔
758
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
74✔
759
                     "CREATE TABLE `%s` USING `%s` (", tbName, pCfg->stbName);
37✔
760
    appendTagNameFields(buf2, &len, pCfg);
37✔
761
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
37✔
762
                     ") TAGS (");
763
    code = appendTagValues(buf2, &len, pCfg);
37✔
764
    TAOS_CHECK_ERRNO(code);
37!
765
    len +=
37✔
766
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
37✔
767
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
37✔
768
  } else {
769
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
10✔
770
                     "CREATE TABLE `%s` (", tbName);
771
    appendColumnFields(buf2, &len, pCfg);
10✔
772
    len +=
10✔
773
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
10✔
774
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
10✔
775
  }
776

777
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
70✔
778

779
  code = colDataSetVal(pCol2, 0, buf2, false);
70✔
780
  TAOS_CHECK_ERRNO(code);
70!
781

782
_exit:
70✔
783
  taosMemoryFree(buf2);
70✔
784

785
  return code;
70✔
786
}
787

788
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
10✔
789
  int32_t code = 0;
10✔
790
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
10!
791
  pBlock->info.rows = 1;
10✔
792

793
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
10✔
794
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
10✔
795
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
10✔
796
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
10✔
797
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
10!
798

799
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
10✔
800
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
10✔
801
  if (NULL == buf2) {
10!
802
    return terrno;
×
803
  }
804

805
  SViewMeta* pMeta = pStmt->pViewMeta;
10✔
806
  if (NULL == pMeta) {
10!
807
    qError("exception: view meta is null");
×
808
    return TSDB_CODE_APP_ERROR;
×
809
  }
810
  snprintf(varDataVal(buf2), SHOW_CREATE_VIEW_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE VIEW `%s`.`%s` AS %s",
10✔
811
           pStmt->dbName, pStmt->viewName, pMeta->querySql);
10✔
812
  int32_t len = strlen(varDataVal(buf2));
10✔
813
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
10✔
814
  code = colDataSetVal(pCol2, 0, buf2, false);
10✔
815
  taosMemoryFree(buf2);
10✔
816

817
  return code;
10✔
818
}
819

820
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp) {
70✔
821
  SSDataBlock* pBlock = NULL;
70✔
822
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
70✔
823
  if (TSDB_CODE_SUCCESS == code) {
70!
824
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg);
70✔
825
  }
826
  if (TSDB_CODE_SUCCESS == code) {
70!
827
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
70✔
828
  }
829
  (void)blockDataDestroy(pBlock);
70✔
830
  return code;
70✔
831
}
832

833
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp) {
2✔
834
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
2✔
835
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
2✔
836
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
1✔
837
    return terrno;
1✔
838
  }
839

840
  return execShowCreateTable(pStmt, pRsp);
1✔
841
}
842

843
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
15,865✔
844
  int32_t code = 0;
15,865✔
845

846
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
15,865!
847
    taosResetLog();
×
848
    cfgDumpCfg(tsCfg, 0, false);
×
849
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
15,865!
850
    int32_t tmp = 0;
×
851
    code = taosStr2int32(value, &tmp);
×
852
    if (code) {
×
853
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
854
      return code;
×
855
    }
856
    code = schedulerUpdatePolicy(tmp);
×
857
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
15,865!
858
    int32_t tmp = 0;
×
859
    code = taosStr2int32(value, &tmp);
×
860
    if (code) {
×
861
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
862
      return code;
×
863
    }
864
    code = schedulerEnableReSchedule(tmp != 0);
×
865
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
15,865!
866
    code = ctgdHandleDbgCommand(value);
×
867
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
15,865!
868
    code = taosMemoryDbgInit();
×
869
    if (code) {
×
870
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
871
      return code;
×
872
    }
873
    tsAsyncLog = false;
×
874
    qInfo("memory dbg enabled");
×
875
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
15,865!
876
    code = taosMemoryDbgInitRestore();
×
877
    if (code) {
×
878
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
879
      return code;
×
880
    }
881
    qInfo("memory dbg disabled");
×
882
  } else {
883
    goto _return;
15,865✔
884
  }
885

886
  *processed = true;
×
887

888
_return:
15,865✔
889

890
  if (code) {
15,865!
891
    terrno = code;
×
892
  }
893

894
  return code;
15,865✔
895
}
896

897
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
15,865✔
898
  bool processed = false;
15,865✔
899

900
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
15,865!
901
    return terrno;
×
902
  }
903

904
  if (processed) {
15,865!
905
    goto _return;
×
906
  }
907

908
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false)) {
15,865!
909
    return terrno;
×
910
  }
911

912
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CMD, true)) {
15,865!
913
    return terrno;
×
914
  }
915

916
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
15,865✔
917

918
_return:
15,864✔
919

920
  return TSDB_CODE_SUCCESS;
15,864✔
921
}
922

923
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
405✔
924
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
405✔
925
  if (NULL == pBlock) {
405!
926
    return terrno;
×
927
  }
928

929
  pBlock->info.hasVarCol = true;
405✔
930

931
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
405✔
932
  if (NULL == pBlock->pDataBlock) {
405!
933
    taosMemoryFree(pBlock);
×
934
    return terrno;
×
935
  }
936

937
  SColumnInfoData infoData = {0};
405✔
938

939
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
405✔
940
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
405✔
941
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
810!
942
    goto _exit;
×
943
  }
944

945
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
405✔
946
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
405✔
947
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
810!
948
    goto _exit;
×
949
  }
950

951
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
405✔
952
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
405✔
953
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
810!
954
    goto _exit;
×
955
  }
956

957
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
405✔
958
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
405✔
959
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
810!
960
    goto _exit;
×
961
  }
962

963
  *pOutput = pBlock;
405✔
964

965
_exit:
405✔
966
  if (terrno != TSDB_CODE_SUCCESS) {
405!
967
    taosArrayDestroy(pBlock->pDataBlock);
×
968
    taosMemoryFree(pBlock);
×
969
  }
970
  return terrno;
405✔
971
}
972

973
static int32_t execShowLocalVariables(SRetrieveTableRsp** pRsp) {
405✔
974
  SSDataBlock* pBlock = NULL;
405✔
975
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
405✔
976
  if (TSDB_CODE_SUCCESS == code) {
405!
977
    code = dumpConfToDataBlock(pBlock, 0);
405✔
978
  }
979
  if (TSDB_CODE_SUCCESS == code) {
405!
980
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
405✔
981
  }
982
  (void)blockDataDestroy(pBlock);
405✔
983
  return code;
405✔
984
}
985

986
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
74,181✔
987
  QRY_PARAM_CHECK(pOutput);
74,181!
988

989
  SSDataBlock* pBlock = NULL;
74,181✔
990
  int32_t      code = createDataBlock(&pBlock);
74,181✔
991
  if (code) {
74,181!
992
    return code;
×
993
  }
994

995
  SNode* pProj = NULL;
74,181✔
996
  FOREACH(pProj, pProjects) {
152,399!
997
    SExprNode*      pExpr = (SExprNode*)pProj;
78,218✔
998
    SColumnInfoData infoData = {0};
78,218✔
999
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
78,218!
1000
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1001
      infoData.info.bytes = 0;
×
1002
    } else {
1003
      infoData.info.type = pExpr->resType.type;
78,218✔
1004
      infoData.info.bytes = pExpr->resType.bytes;
78,218✔
1005
    }
1006
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
78,218!
1007
  }
1008

1009
  *pOutput = pBlock;
74,181✔
1010
  return code;
74,181✔
1011
}
1012

1013
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
74,181✔
1014
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
74,181!
1015

1016
  int32_t index = 0;
74,181✔
1017
  SNode*  pProj = NULL;
74,181✔
1018
  FOREACH(pProj, pProjects) {
152,399!
1019
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
78,218!
1020
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1021
    } else {
1022
      if (((SValueNode*)pProj)->isNull) {
78,218✔
1023
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
123!
1024
      } else {
1025
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
78,095!
1026
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1027
      }
1028
    }
1029
  }
1030

1031
  pBlock->info.rows = 1;
74,181✔
1032
  return TSDB_CODE_SUCCESS;
74,181✔
1033
}
1034

1035
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
74,181✔
1036
  SSDataBlock* pBlock = NULL;
74,181✔
1037
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
74,181✔
1038
  if (TSDB_CODE_SUCCESS == code) {
74,181!
1039
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
74,181✔
1040
  }
1041
  if (TSDB_CODE_SUCCESS == code) {
74,181!
1042
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
74,181!
1043
  }
1044
  (void)blockDataDestroy(pBlock);
74,181✔
1045
  return code;
74,181✔
1046
}
1047

1048
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
10✔
1049
  SSDataBlock* pBlock = NULL;
10✔
1050
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
10✔
1051
  if (TSDB_CODE_SUCCESS == code) {
10!
1052
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
10✔
1053
  }
1054
  if (TSDB_CODE_SUCCESS == code) {
10!
1055
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
10✔
1056
  }
1057
  (void)blockDataDestroy(pBlock);
10✔
1058
  return code;
10✔
1059
}
1060

1061
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode) {
97,141✔
1062
  switch (nodeType(pStmt)) {
97,141!
1063
    case QUERY_NODE_DESCRIBE_STMT:
506✔
1064
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
506✔
1065
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
6,085✔
1066
      return execResetQueryCache();
6,085✔
1067
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
18✔
1068
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
18✔
1069
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
69✔
1070
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp);
69✔
1071
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
2✔
1072
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp);
2✔
1073
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
10✔
1074
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
10✔
1075
    case QUERY_NODE_ALTER_LOCAL_STMT:
15,865✔
1076
      return execAlterLocal((SAlterLocalStmt*)pStmt);
15,865✔
1077
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
405✔
1078
      return execShowLocalVariables(pRsp);
405✔
1079
    case QUERY_NODE_SELECT_STMT:
74,181✔
1080
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
74,181✔
1081
    default:
×
1082
      break;
×
1083
  }
1084
  return TSDB_CODE_FAILED;
×
1085
}
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

© 2025 Coveralls, Inc