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

taosdata / TDengine / #4875

09 Dec 2025 01:22AM UTC coverage: 64.472% (-0.2%) from 64.623%
#4875

push

travis-ci

guanshengliang
fix: temporarily disable memory leak detection for UDF tests (#33856)

162014 of 251293 relevant lines covered (64.47%)

104318075.66 hits per line

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

80.26
/source/libs/command/src/command.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "command.h"
17
#include "catalog.h"
18
#include "commandInt.h"
19
#include "decimal.h"
20
#include "osMemory.h"
21
#include "osString.h"
22
#include "scheduler.h"
23
#include "systable.h"
24
#include "taosdef.h"
25
#include "tdatablock.h"
26
#include "tdataformat.h"
27
#include "tglobal.h"
28
#include "tgrant.h"
29

30
#define COL_DATA_SET_VAL_AND_CHECK(pCol, rows, buf, isNull) \
31
  do {                                                      \
32
    int _code = colDataSetVal(pCol, rows, buf, isNull);     \
33
    if (TSDB_CODE_SUCCESS != _code) {                       \
34
      terrno = _code;                                       \
35
      return _code;                                         \
36
    }                                                       \
37
  } while (0)
38

39
extern SConfig* tsCfg;
40

41
static int32_t buildRetrieveTableRsp(SSDataBlock* pBlock, int32_t numOfCols, SRetrieveTableRsp** pRsp) {
2,774,574✔
42
  if (NULL == pBlock || NULL == pRsp) {
2,774,574✔
43
    return TSDB_CODE_INVALID_PARA;
×
44
  }
45
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
2,774,574✔
46
  size_t rspSize = sizeof(SRetrieveTableRsp) + dataEncodeBufSize + PAYLOAD_PREFIX_LEN;
2,774,574✔
47
  *pRsp = taosMemoryCalloc(1, rspSize);
2,774,574✔
48
  if (NULL == *pRsp) {
2,774,574✔
49
    return terrno;
×
50
  }
51

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

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

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

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

75
  return TSDB_CODE_SUCCESS;
2,774,574✔
76
}
77

78
static int32_t getSchemaBytes(const SSchema* pSchema) {
72,061,375✔
79
  switch (pSchema->type) {
72,061,375✔
80
    case TSDB_DATA_TYPE_BINARY:
3,764,734✔
81
    case TSDB_DATA_TYPE_VARBINARY:
82
    case TSDB_DATA_TYPE_GEOMETRY:
83
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
3,764,734✔
84
    case TSDB_DATA_TYPE_NCHAR:
4,735,452✔
85
    case TSDB_DATA_TYPE_JSON:
86
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
4,735,452✔
87
    default:
63,561,189✔
88
      return pSchema->bytes;
63,561,189✔
89
  }
90
}
91

92
static const char* expandIdentifier(const char* name, char* output) {
896,618✔
93
  if (NULL == name) return "";
896,618✔
94
  bool containsEscapeChar = false;
896,618✔
95
  for (const char* p = name; *p != '\0'; ++p) {
4,340,494✔
96
    if (*p == TS_ESCAPE_CHAR) {
3,463,586✔
97
      containsEscapeChar = true;
19,710✔
98
      break;
19,710✔
99
    }
100
  }
101
  if (!containsEscapeChar) return name;
896,618✔
102
  if (NULL == output) return "";
19,710✔
103
  char* out_ptr = output;
19,710✔
104
  for (const char* src = name; *src != '\0'; ++src) {
121,326✔
105
    if (*src == TS_ESCAPE_CHAR) {
101,616✔
106
      *out_ptr++ = TS_ESCAPE_CHAR;
49,494✔
107
      *out_ptr++ = TS_ESCAPE_CHAR;
49,494✔
108
    } else {
109
      *out_ptr++ = *src;
52,122✔
110
    }
111
  }
112
  *out_ptr = '\0';
19,710✔
113
  return output;
19,710✔
114
}
115

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

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

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

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

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

191
  if (hasRefCol(pMeta->tableType)) {
603,238✔
192
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
5,114✔
193
  }
194

195
  int32_t fillTagCol = 0;
603,238✔
196
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
603,238✔
197
  for (int32_t i = 0; i < numOfRows; ++i) {
72,664,685✔
198
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
72,061,447✔
199
      continue;
×
200
    }
201
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
72,061,447✔
202
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
72,061,447✔
203

204
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
72,061,559✔
205
      uint8_t prec = 0, scale = 0;
266,562✔
206
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
266,562✔
207
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
266,562✔
208
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
266,562✔
209
      varDataSetLen(buf, len);
266,562✔
210
    } else {
211
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
71,794,997✔
212
    }
213
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
72,061,559✔
214
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
72,061,375✔
215
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
72,061,375✔
216
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
72,061,559✔
217
      if (i >= pMeta->tableInfo.numOfColumns) {
72,025,419✔
218
        STR_TO_VARSTR(buf, "TAG");
4,312,352✔
219
        fillTagCol = 1;
4,312,352✔
220
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
67,713,067✔
221
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
13,962✔
222
      } else {
223
        STR_TO_VARSTR(buf, "");
67,699,105✔
224
      }
225
    } else {
226
      STR_TO_VARSTR(buf, "VIEW COL");
36,140✔
227
    }
228
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
72,061,559✔
229
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
72,061,375✔
230
      if (i < pMeta->tableInfo.numOfColumns) {
71,781,019✔
231
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
67,476,425✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
67,476,425✔
233
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
67,476,425✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
67,476,425✔
235
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
67,476,497✔
236
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
67,476,609✔
237
      } else {
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
4,304,594✔
239
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
4,304,594✔
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
4,304,594✔
241
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
4,304,594✔
242
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
4,304,594✔
243
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
4,304,594✔
244
      }
245
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
280,356✔
246
      if (i < pMeta->numOfColRefs) {
45,574✔
247
        if (pMeta->colRef[i].hasRef) {
37,816✔
248
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
17,499✔
249
          strcat(refColName, pMeta->colRef[i].refDbName);
17,499✔
250
          strcat(refColName, ".");
17,499✔
251
          strcat(refColName, pMeta->colRef[i].refTableName);
17,499✔
252
          strcat(refColName, ".");
17,499✔
253
          strcat(refColName, pMeta->colRef[i].refColName);
17,499✔
254
          STR_TO_VARSTR(buf, refColName);
17,499✔
255
        } else {
256
          STR_TO_VARSTR(buf, "");
20,317✔
257
        }
258
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
37,816✔
259
      } else {
260
        STR_TO_VARSTR(buf, "");
7,758✔
261
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
7,758✔
262
      }
263
    }
264

265
    fillTagCol = 0;
72,061,447✔
266

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

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

294
  SSDataBlock* pBlock = NULL;
602,744✔
295
  int32_t      code = buildDescResultDataBlock(&pBlock);
602,991✔
296
  if (TSDB_CODE_SUCCESS == code) {
603,238✔
297
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
603,238✔
298
  }
299
  if (TSDB_CODE_SUCCESS == code) {
603,238✔
300
    if (pDesc->pMeta) {
603,238✔
301
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
603,238✔
302
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
579,029✔
303
      } else if (hasRefCol(pDesc->pMeta->tableType) && pDesc->pMeta->colRef) {
24,209✔
304
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
5,114✔
305
      } else {
306
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
19,095✔
307
      }
308
    } else {
309
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
310
    }
311
  }
312
  (void)blockDataDestroy(pBlock);
603,238✔
313
  return code;
603,238✔
314
}
315

316
static int32_t execResetQueryCache() { return catalogClearCache(); }
2,106,025✔
317

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

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

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

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

434
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
5,777,695✔
435
  if (buffer == NULL || bufSize <= 0) {
5,777,695✔
436
    return 0;
×
437
  }
438
  int32_t len = 0;
5,777,695✔
439
  if (timeInMinutes % 1440 == 0) {
5,777,695✔
440
    int32_t days = timeInMinutes / 1440;
5,765,278✔
441
    len = tsnprintf(buffer, bufSize, "%dd", days);
5,765,278✔
442
  } else if (timeInMinutes % 60 == 0) {
12,417✔
443
    int32_t hours = timeInMinutes / 60;
2,589✔
444
    len = tsnprintf(buffer, bufSize, "%dh", hours);
2,589✔
445
  } else {
446
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
9,828✔
447
  }
448
  return len;
5,777,695✔
449
}
450

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

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

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

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

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

525
    if (pRetentions) {
73,897✔
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);
74,611✔
532

533
  (varDataLen(buf2)) = len;
74,611✔
534

535
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
74,611✔
536

537
  return TSDB_CODE_SUCCESS;
74,611✔
538
}
539

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

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

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

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

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

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

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

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

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

601
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
66,837✔
602
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
66,837✔
603
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
514,644✔
604
    SSchema* pSchema = pCfg->pSchemas + i;
447,807✔
605
    SColRef* pRef = pCfg->pColRefs + i;
447,807✔
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];
447,807✔
610
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
447,807✔
611
    int typeLen = strlen(type);
447,807✔
612
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
447,807✔
613
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
403,803✔
614
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
50,603✔
615
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
397,204✔
616
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
14,914✔
617
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
14,914✔
618
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
382,290✔
619
      uint8_t precision, scale;
18,408✔
620
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
18,408✔
621
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
18,408✔
622
    }
623

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

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

642
    if (!(pSchema->flags & COL_IS_KEY)) {
447,807✔
643
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
437,546✔
644
                        "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
437,546✔
645
    } else {
646
      char* pk = "COMPOSITE KEY";
10,261✔
647
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
10,261✔
648
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
10,261✔
649
    }
650
  }
651
}
66,837✔
652

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

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

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

674
  }
675
}
1,099✔
676

677
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
40,672✔
678
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
40,672✔
679
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
233,380✔
680
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
192,708✔
681
    char     type[32];
192,708✔
682
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
192,708✔
683
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
192,708✔
684
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
165,396✔
685
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
32,101✔
686
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
32,101✔
687
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
160,607✔
688
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
8,173✔
689
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
8,173✔
690
    }
691

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

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

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

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

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

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

743
    return TSDB_CODE_SUCCESS;
2,690✔
744
  }
745

746
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
34,557✔
747
  int16_t valueNum = taosArrayGetSize(pTagVals);
34,557✔
748
  int32_t num = 0;
34,557✔
749
  int32_t j = 0;
34,557✔
750
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
86,658✔
751
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
52,101✔
752
    if (i > 0) {
52,101✔
753
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
17,544✔
754
                        ", ");
755
    }
756

757
    if (j >= valueNum) {
52,101✔
758
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
15,908✔
759
                        "NULL");
760
      continue;
15,908✔
761
    }
762

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

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

796
  return code;
34,557✔
797
}
798

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

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

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

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

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

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

849
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
101,886✔
850
    int32_t nSma = 0;
65,738✔
851
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
499,011✔
852
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
433,273✔
853
        ++nSma;
433,273✔
854
      }
855
    }
856

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

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

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

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

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

902
  int32_t len = 0;
114,794✔
903

904
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
114,794✔
905
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
40,672✔
906
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
907
    appendColumnFields(buf2, &len, pCfg);
40,672✔
908
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
40,672✔
909
                     ") TAGS (");
910
    appendTagFields(buf2, &len, pCfg);
40,672✔
911
    len +=
40,672✔
912
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
40,672✔
913
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
40,672✔
914
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
74,122✔
915
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
36,148✔
916
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
917
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
36,148✔
918
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
36,148✔
919
    appendTagNameFields(buf2, &len, pCfg);
36,148✔
920
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
36,148✔
921
                     ") TAGS (");
922
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
36,148✔
923
    TAOS_CHECK_ERRNO(code);
36,148✔
924
    len +=
36,148✔
925
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
36,148✔
926
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
36,148✔
927
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
37,974✔
928
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
25,066✔
929
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
930
    appendColumnFields(buf2, &len, pCfg);
25,066✔
931
    len +=
25,066✔
932
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
25,066✔
933
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
25,066✔
934
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
12,908✔
935
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1,099✔
936
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
937
    appendColumnFields(buf2, &len, pCfg);
1,099✔
938
    len +=
1,099✔
939
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,099✔
940
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
11,809✔
941
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1,099✔
942
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
943
    appendColRefFields(buf2, &len, pCfg);
1,099✔
944
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,099✔
945
                    ") USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
1,099✔
946
    appendTagNameFields(buf2, &len, pCfg);
1,099✔
947
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,099✔
948
                     ") TAGS (");
949
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
1,099✔
950
    TAOS_CHECK_ERRNO(code);
1,099✔
951
    len +=
1,099✔
952
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,099✔
953
  }
954

955
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
114,794✔
956

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

960
_exit:
114,794✔
961
  taosMemoryFree(buf2);
114,794✔
962

963
  return code;
114,794✔
964
}
965

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

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

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

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

996
  return code;
7,255✔
997
}
998

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

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

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

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

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

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

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

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

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

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

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

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

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

1129
  *processed = true;
2,371✔
1130

1131
_return:
468,211✔
1132

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

1137
  return code;
468,211✔
1138
}
1139

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

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

1147
  if (processed) {
468,211✔
1148
    goto _return;
2,371✔
1149
  }
1150

1151
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
465,840✔
1152
    return terrno;
3,717✔
1153
  }
1154

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

1159
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
462,123✔
1160

1161
_return:
463,390✔
1162

1163
  return TSDB_CODE_SUCCESS;
464,494✔
1164
}
1165

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

1172
  pBlock->info.hasVarCol = true;
34,372✔
1173

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

1180
  SColumnInfoData infoData = {0};
34,372✔
1181

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

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

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

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

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

1212
  *pOutput = pBlock;
34,372✔
1213

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

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

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

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

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

1266
  *pOutput = pBlock;
1,937,212✔
1267
  return code;
1,937,212✔
1268
}
1269

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

1273
  int32_t index = 0;
1,937,212✔
1274
  SNode*  pProj = NULL;
1,937,212✔
1275
  FOREACH(pProj, pProjects) {
4,580,999✔
1276
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
2,643,787✔
1277
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1278
    } else {
1279
      if (((SValueNode*)pProj)->isNull) {
2,643,787✔
1280
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
78,312✔
1281
      } else {
1282
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
2,565,475✔
1283
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1284
      }
1285
    }
1286
  }
1287

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

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

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

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

1331
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
5,348,379✔
1332
                     void* charsetCxt) {
1333
  switch (nodeType(pStmt)) {
5,348,379✔
1334
    case QUERY_NODE_DESCRIBE_STMT:
602,991✔
1335
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
602,991✔
1336
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
2,106,025✔
1337
      return execResetQueryCache();
2,106,025✔
1338
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
74,611✔
1339
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
74,611✔
1340
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
112,101✔
1341
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
112,101✔
1342
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
1,979✔
1343
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,979✔
1344
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
1,071✔
1345
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,071✔
1346
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
7,255✔
1347
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
7,255✔
1348
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
3,092✔
1349
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
3,092✔
1350
    case QUERY_NODE_ALTER_LOCAL_STMT:
468,211✔
1351
      return execAlterLocal((SAlterLocalStmt*)pStmt);
468,211✔
1352
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
34,372✔
1353
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
34,372✔
1354
    case QUERY_NODE_SELECT_STMT:
1,937,459✔
1355
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
1,937,459✔
1356
    default:
×
1357
      break;
×
1358
  }
1359
  return TSDB_CODE_FAILED;
×
1360
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc