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

taosdata / TDengine / #4936

23 Jan 2026 09:40AM UTC coverage: 66.746% (+0.04%) from 66.708%
#4936

push

travis-ci

web-flow
fix: case failuer caused by the modification of the error description (#34391)

204023 of 305671 relevant lines covered (66.75%)

124768167.97 hits per line

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

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

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

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

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

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

75
  return TSDB_CODE_SUCCESS;
2,959,376✔
76
}
77

78
static int32_t getSchemaBytes(const SSchema* pSchema) {
58,186,914✔
79
  switch (pSchema->type) {
58,186,914✔
80
    case TSDB_DATA_TYPE_BINARY:
2,423,921✔
81
    case TSDB_DATA_TYPE_VARBINARY:
82
    case TSDB_DATA_TYPE_GEOMETRY:
83
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
2,423,921✔
84
    case TSDB_DATA_TYPE_NCHAR:
3,983,068✔
85
    case TSDB_DATA_TYPE_JSON:
86
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
3,983,068✔
87
    default:
51,779,925✔
88
      return pSchema->bytes;
51,779,925✔
89
  }
90
}
91

92
static const char* expandIdentifier(const char* name, char* output) {
551,721✔
93
  if (NULL == name) return "";
551,721✔
94
  bool containsEscapeChar = false;
551,721✔
95
  for (const char* p = name; *p != '\0'; ++p) {
4,137,311✔
96
    if (*p == TS_ESCAPE_CHAR) {
3,630,500✔
97
      containsEscapeChar = true;
44,910✔
98
      break;
44,910✔
99
    }
100
  }
101
  if (!containsEscapeChar) return name;
551,721✔
102
  if (NULL == output) return "";
44,910✔
103
  char* out_ptr = output;
44,910✔
104
  for (const char* src = name; *src != '\0'; ++src) {
276,446✔
105
    if (*src == TS_ESCAPE_CHAR) {
231,536✔
106
      *out_ptr++ = TS_ESCAPE_CHAR;
112,774✔
107
      *out_ptr++ = TS_ESCAPE_CHAR;
112,774✔
108
    } else {
109
      *out_ptr++ = *src;
118,762✔
110
    }
111
  }
112
  *out_ptr = '\0';
44,910✔
113
  return output;
44,910✔
114
}
115

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

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

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

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

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

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

195
  int32_t fillTagCol = 0;
392,663✔
196
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
392,663✔
197
  for (int32_t i = 0; i < numOfRows; ++i) {
58,579,577✔
198
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
58,186,914✔
199
      continue;
×
200
    }
201
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
58,186,914✔
202
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
58,186,914✔
203

204
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
58,186,914✔
205
      uint8_t prec = 0, scale = 0;
243,110✔
206
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
243,110✔
207
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
243,110✔
208
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
243,110✔
209
      varDataSetLen(buf, len);
243,110✔
210
    } else {
211
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
57,943,804✔
212
    }
213
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
58,186,914✔
214
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
58,186,914✔
215
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
58,186,914✔
216
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
58,186,914✔
217
      if (i >= pMeta->tableInfo.numOfColumns) {
58,186,914✔
218
        STR_TO_VARSTR(buf, "TAG");
2,786,348✔
219
        fillTagCol = 1;
2,786,348✔
220
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
55,400,566✔
221
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
10,001✔
222
      } else {
223
        STR_TO_VARSTR(buf, "");
55,390,565✔
224
      }
225
    } else {
226
      STR_TO_VARSTR(buf, "VIEW COL");
×
227
    }
228
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
58,186,914✔
229
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
58,186,914✔
230
      if (i < pMeta->tableInfo.numOfColumns) {
58,136,297✔
231
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
55,355,091✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
55,355,091✔
233
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
55,355,091✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
55,355,091✔
235
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
55,355,091✔
236
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
55,355,091✔
237
      } else {
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,781,206✔
239
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
2,781,206✔
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,781,206✔
241
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
2,781,206✔
242
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,781,206✔
243
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
2,781,206✔
244
      }
245
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
50,617✔
246
      if (i < pMeta->numOfColRefs) {
34,010✔
247
        if (pMeta->colRef[i].hasRef) {
28,868✔
248
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
12,117✔
249
          strcat(refColName, pMeta->colRef[i].refDbName);
12,117✔
250
          strcat(refColName, ".");
12,117✔
251
          strcat(refColName, pMeta->colRef[i].refTableName);
12,117✔
252
          strcat(refColName, ".");
12,117✔
253
          strcat(refColName, pMeta->colRef[i].refColName);
12,117✔
254
          STR_TO_VARSTR(buf, refColName);
12,117✔
255
        } else {
256
          STR_TO_VARSTR(buf, "");
16,751✔
257
        }
258
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
28,868✔
259
      } else {
260
        STR_TO_VARSTR(buf, "");
5,142✔
261
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
5,142✔
262
      }
263
    }
264

265
    fillTagCol = 0;
58,186,914✔
266

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

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

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

316
static int32_t execResetQueryCache() { return catalogClearCache(); }
1,955,696✔
317

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

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

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

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

434
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
4,003,160✔
435
  if (buffer == NULL || bufSize <= 0) {
4,003,160✔
436
    return 0;
×
437
  }
438
  int32_t len = 0;
4,003,160✔
439
  if (timeInMinutes % 1440 == 0) {
4,003,160✔
440
    int32_t days = timeInMinutes / 1440;
3,991,981✔
441
    len = tsnprintf(buffer, bufSize, "%dd", days);
3,991,981✔
442
  } else if (timeInMinutes % 60 == 0) {
11,179✔
443
    int32_t hours = timeInMinutes / 60;
1,910✔
444
    len = tsnprintf(buffer, bufSize, "%dh", hours);
1,910✔
445
  } else {
446
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
9,269✔
447
  }
448
  return len;
4,003,160✔
449
}
450

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

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

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

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

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

525
    if (pRetentions) {
61,239✔
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);
61,569✔
532

533
  (varDataLen(buf2)) = len;
61,569✔
534

535
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
61,569✔
536

537
  return TSDB_CODE_SUCCESS;
61,569✔
538
}
539

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

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

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

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

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

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

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

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

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

601
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
36,019✔
602
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
36,019✔
603
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
235,012✔
604
    SSchema* pSchema = pCfg->pSchemas + i;
198,993✔
605
    SColRef* pRef = pCfg->pColRefs + i;
198,993✔
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];
198,993✔
610
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
198,993✔
611
    int typeLen = strlen(type);
198,993✔
612
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
198,993✔
613
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
174,305✔
614
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
26,026✔
615
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
172,967✔
616
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
7,847✔
617
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
7,847✔
618
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
165,120✔
619
      uint8_t precision, scale;
16,872✔
620
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
16,872✔
621
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
16,872✔
622
    }
623

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

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

642
    if (!(pSchema->flags & COL_IS_KEY)) {
198,993✔
643
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
190,292✔
644
                        "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
190,292✔
645
    } else {
646
      char* pk = "COMPOSITE KEY";
8,701✔
647
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
8,701✔
648
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
8,701✔
649
    }
650
  }
651
}
36,019✔
652

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

662
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
48,645✔
663
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
27,827✔
664
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
27,827✔
665
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
27,827✔
666
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
27,827✔
667
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
27,827✔
668
    } else {
669
      continue;
20,818✔
670
    }
671

672
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
27,827✔
673
                      "%s`%s` %s", (!firstRef ? ", " : " ("), expandIdentifier(pSchema->name, expandName), type);
27,827✔
674
    firstRef = false;
27,827✔
675
  }
676
  if (!firstRef) {
3,945✔
677
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
3,361✔
678
  }
679
}
3,945✔
680

681
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
20,159✔
682
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
20,159✔
683
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
62,976✔
684
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
42,817✔
685
    char     type[32];
42,817✔
686
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
42,817✔
687
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
42,817✔
688
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
34,336✔
689
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
8,539✔
690
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
8,539✔
691
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
34,278✔
692
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
1,263✔
693
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
1,263✔
694
    }
695

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

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

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

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

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

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

747
    return TSDB_CODE_SUCCESS;
2,240✔
748
  }
749

750
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
32,474✔
751
  int16_t valueNum = taosArrayGetSize(pTagVals);
32,474✔
752
  int32_t num = 0;
32,474✔
753
  int32_t j = 0;
32,474✔
754
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
91,538✔
755
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
59,064✔
756
    if (i > 0) {
59,064✔
757
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
26,590✔
758
                        ", ");
759
    }
760

761
    if (j >= valueNum) {
59,064✔
762
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
14,807✔
763
                        "NULL");
764
      continue;
14,807✔
765
    }
766

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

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

800
  return code;
32,474✔
801
}
802

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

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

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

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

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

848
  if (pCfg->keep > 0) {
63,427✔
849
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,556✔
850
                      " KEEP %dm", pCfg->keep);
851
  }
852

853
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
63,427✔
854
    int32_t nSma = 0;
32,658✔
855
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
188,405✔
856
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
155,747✔
857
        ++nSma;
155,747✔
858
      }
859
    }
860

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

883
  if (pCfg->virtualStb) {
63,427✔
884
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,582✔
885
                      " VIRTUAL %d", pCfg->virtualStb);
1,582✔
886
  }
887
}
63,427✔
888

889
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
75,683✔
890
                                              void* charsetCxt) {
891
  int32_t code = TSDB_CODE_SUCCESS;
75,683✔
892
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
75,683✔
893
  pBlock->info.rows = 1;
75,683✔
894

895
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
75,683✔
896
  char             buf1[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1)] = {0};
75,683✔
897
  STR_TO_VARSTR(buf1, tbName);
75,683✔
898
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
75,683✔
899

900
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
75,683✔
901
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
75,683✔
902
  if (NULL == buf2) {
75,683✔
903
    QRY_ERR_RET(terrno);
×
904
  }
905

906
  int32_t len = 0;
75,683✔
907

908
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
75,683✔
909
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
20,159✔
910
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
911
    appendColumnFields(buf2, &len, pCfg);
20,159✔
912
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
20,159✔
913
                     ") TAGS (");
914
    appendTagFields(buf2, &len, pCfg);
20,159✔
915
    len +=
20,159✔
916
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
20,159✔
917
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
20,159✔
918
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
55,524✔
919
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
30,769✔
920
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
921
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
30,769✔
922
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
30,769✔
923
    appendTagNameFields(buf2, &len, pCfg);
30,769✔
924
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
30,769✔
925
                     ") TAGS (");
926
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
30,769✔
927
    TAOS_CHECK_ERRNO(code);
30,769✔
928
    len +=
30,769✔
929
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
30,769✔
930
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
30,769✔
931
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
24,755✔
932
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
12,499✔
933
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
934
    appendColumnFields(buf2, &len, pCfg);
12,499✔
935
    len +=
12,499✔
936
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
12,499✔
937
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
12,499✔
938
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
12,256✔
939
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
3,361✔
940
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
941
    appendColumnFields(buf2, &len, pCfg);
3,361✔
942
    len +=
3,361✔
943
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
3,361✔
944
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
8,895✔
945
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
3,945✔
946
                     "CREATE VTABLE `%s`", expandIdentifier(tbName, buf1));
947
    appendColRefFields(buf2, &len, pCfg);
3,945✔
948
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
3,945✔
949
                    " USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
3,945✔
950
    appendTagNameFields(buf2, &len, pCfg);
3,945✔
951
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
3,945✔
952
                     ") TAGS (");
953
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
3,945✔
954
    TAOS_CHECK_ERRNO(code);
3,945✔
955
    len +=
3,945✔
956
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
3,945✔
957
  }
958

959
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
75,683✔
960

961
  code = colDataSetVal(pCol2, 0, buf2, false);
75,683✔
962
  TAOS_CHECK_ERRNO(code);
75,683✔
963

964
_exit:
75,683✔
965
  taosMemoryFree(buf2);
75,683✔
966

967
  return code;
75,683✔
968
}
969

970
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
×
971
  int32_t code = 0;
×
972
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
×
973
  pBlock->info.rows = 1;
×
974

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

981
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
×
982
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
×
983
  if (NULL == buf2) {
×
984
    return terrno;
×
985
  }
986

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

1000
  return code;
×
1001
}
1002

1003
extern const char* fmGetFuncName(int32_t funcId);
1004
static int32_t setCreateRsmaResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateRsmaStmt* pStmt) {
1,470✔
1005
  int32_t       code = 0, lino = 0;
1,470✔
1006
  char*         buf2 = NULL;
1,470✔
1007
  SRsmaInfoRsp* pMeta = pStmt->pRsmaMeta;
1,470✔
1008

1009
  if (pMeta->nFuncs != pMeta->nColNames) {
1,470✔
1010
    qError("exception: rsma meta is invalid, nFuncs:%d != nColNames:%d", pMeta->nFuncs, pMeta->nColNames);
×
1011
    return TSDB_CODE_APP_ERROR;
×
1012
  }
1013

1014
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, 1));
1,470✔
1015
  pBlock->info.rows = 1;
1,470✔
1016

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

1023
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
1,470✔
1024
  if (!(buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN))) {
1,470✔
1025
    return terrno;
×
1026
  }
1027

1028
  SName name = {0};
1,470✔
1029
  TAOS_CHECK_EXIT(tNameFromString(&name, pMeta->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
1,470✔
1030

1031
  int32_t len = 0;
1,470✔
1032
  len += tsnprintf(varDataVal(buf2), SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
2,940✔
1033
                       "CREATE RSMA `%s` ON `%s`.`%s` FUNCTION(", expandIdentifier(pStmt->rsmaName, buf1),
1,470✔
1034
                       expandIdentifier(name.dbname, buf1), expandIdentifier(name.tname, buf1));
1035
  for (int32_t i = 0; i < pMeta->nFuncs; ++i) {
11,025✔
1036
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
19,110✔
1037
                         "%s%s(`%s`)", (i > 0) ? "," : "", fmGetFuncName(pMeta->funcIds[i]),
9,555✔
1038
                         expandIdentifier(*(char**)TARRAY_GET_ELEM(pMeta->colNames, i), buf1));
9,555✔
1039
  }
1040
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,470✔
1041
                       ") INTERVAL(%d%c", pMeta->interval[0], pMeta->intervalUnit);
1,470✔
1042
  if (pMeta->interval[1] > 0) {
1,470✔
1043
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,470✔
1044
                         ",%d%c", pMeta->interval[1], pMeta->intervalUnit);
1,470✔
1045
  }
1046
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,470✔
1047
  varDataLen(buf2) = len;
1,470✔
1048
  code = colDataSetVal(pCol2, 0, buf2, false);
1,470✔
1049
_exit:
1,470✔
1050
  taosMemoryFree(buf2);
1,470✔
1051
  return code;
1,470✔
1052
}
1053

1054
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
68,876✔
1055
  SSDataBlock* pBlock = NULL;
68,876✔
1056
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
68,876✔
1057
  if (TSDB_CODE_SUCCESS == code) {
68,876✔
1058
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
68,876✔
1059
  }
1060
  if (TSDB_CODE_SUCCESS == code) {
68,876✔
1061
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
68,876✔
1062
  }
1063
  (void)blockDataDestroy(pBlock);
68,876✔
1064
  return code;
68,876✔
1065
}
1066

1067
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
6,807✔
1068
  SSDataBlock* pBlock = NULL;
6,807✔
1069
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
6,807✔
1070
  if (TSDB_CODE_SUCCESS == code) {
6,807✔
1071
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
6,807✔
1072
  }
1073
  if (TSDB_CODE_SUCCESS == code) {
6,807✔
1074
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
6,807✔
1075
  }
1076
  (void)blockDataDestroy(pBlock);
6,807✔
1077
  return code;
6,807✔
1078
}
1079

1080
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
1,079✔
1081
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
1,079✔
1082
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
1,079✔
1083
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
165✔
1084
    return terrno;
165✔
1085
  }
1086

1087
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
914✔
1088
}
1089

1090
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
220,706✔
1091
  int32_t code = 0;
220,706✔
1092

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

1133
  *processed = true;
2,433✔
1134

1135
_return:
220,706✔
1136

1137
  if (code) {
220,706✔
1138
    terrno = code;
×
1139
  }
1140

1141
  return code;
220,706✔
1142
}
1143

1144
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
220,706✔
1145
  bool processed = false;
220,706✔
1146

1147
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
220,706✔
1148
    return terrno;
×
1149
  }
1150

1151
  if (processed) {
220,706✔
1152
    goto _return;
2,433✔
1153
  }
1154

1155
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
218,273✔
1156
    return terrno;
1,503✔
1157
  }
1158

1159
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
216,770✔
1160
    return terrno;
×
1161
  }
1162

1163
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
216,770✔
1164

1165
_return:
219,203✔
1166

1167
  return TSDB_CODE_SUCCESS;
219,203✔
1168
}
1169

1170
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
12,116✔
1171
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
12,116✔
1172
  if (NULL == pBlock) {
12,116✔
1173
    return terrno;
×
1174
  }
1175

1176
  pBlock->info.hasVarCol = true;
12,116✔
1177

1178
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
12,116✔
1179
  if (NULL == pBlock->pDataBlock) {
12,116✔
1180
    taosMemoryFree(pBlock);
×
1181
    return terrno;
×
1182
  }
1183

1184
  SColumnInfoData infoData = {0};
12,116✔
1185

1186
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
12,116✔
1187
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
12,116✔
1188
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
24,232✔
1189
    goto _exit;
×
1190
  }
1191

1192
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
12,116✔
1193
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
12,116✔
1194
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
24,232✔
1195
    goto _exit;
×
1196
  }
1197

1198
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
12,116✔
1199
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
12,116✔
1200
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
24,232✔
1201
    goto _exit;
×
1202
  }
1203

1204
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
12,116✔
1205
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
12,116✔
1206
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
24,232✔
1207
    goto _exit;
×
1208
  }
1209

1210
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
12,116✔
1211
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
12,116✔
1212
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
24,232✔
1213
    goto _exit;
×
1214
  }
1215

1216
  *pOutput = pBlock;
12,116✔
1217

1218
_exit:
12,116✔
1219
  if (terrno != TSDB_CODE_SUCCESS) {
12,116✔
1220
    taosArrayDestroy(pBlock->pDataBlock);
×
1221
    taosMemoryFree(pBlock);
×
1222
  }
1223
  return terrno;
12,116✔
1224
}
1225

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

1245
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
2,415,875✔
1246
  QRY_PARAM_CHECK(pOutput);
2,415,875✔
1247

1248
  SSDataBlock* pBlock = NULL;
2,415,875✔
1249
  int32_t      code = createDataBlock(&pBlock);
2,415,875✔
1250
  if (code) {
2,415,875✔
1251
    return code;
×
1252
  }
1253

1254
  SNode* pProj = NULL;
2,415,875✔
1255
  FOREACH(pProj, pProjects) {
6,193,401✔
1256
    SExprNode*      pExpr = (SExprNode*)pProj;
3,777,526✔
1257
    SColumnInfoData infoData = {0};
3,777,526✔
1258
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
3,777,526✔
1259
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1260
      infoData.info.bytes = 0;
×
1261
    } else {
1262
      infoData.info.type = pExpr->resType.type;
3,777,526✔
1263
      infoData.info.bytes = pExpr->resType.bytes;
3,777,526✔
1264
      infoData.info.precision = pExpr->resType.precision;
3,777,526✔
1265
      infoData.info.scale = pExpr->resType.scale;
3,777,526✔
1266
    }
1267
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
3,777,526✔
1268
  }
1269

1270
  *pOutput = pBlock;
2,415,875✔
1271
  return code;
2,415,875✔
1272
}
1273

1274
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
2,415,875✔
1275
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
2,415,875✔
1276

1277
  int32_t index = 0;
2,415,875✔
1278
  SNode*  pProj = NULL;
2,415,875✔
1279
  FOREACH(pProj, pProjects) {
6,193,401✔
1280
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
3,777,526✔
1281
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1282
    } else {
1283
      if (((SValueNode*)pProj)->isNull) {
3,777,526✔
1284
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
78,120✔
1285
      } else {
1286
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
3,699,406✔
1287
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1288
      }
1289
    }
1290
  }
1291

1292
  pBlock->info.rows = 1;
2,415,875✔
1293
  return TSDB_CODE_SUCCESS;
2,415,875✔
1294
}
1295

1296
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
2,415,875✔
1297
  SSDataBlock* pBlock = NULL;
2,415,875✔
1298
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
2,415,875✔
1299
  if (TSDB_CODE_SUCCESS == code) {
2,415,875✔
1300
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
2,415,875✔
1301
  }
1302
  if (TSDB_CODE_SUCCESS == code) {
2,415,875✔
1303
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
2,415,875✔
1304
  }
1305
  (void)blockDataDestroy(pBlock);
2,415,875✔
1306
  return code;
2,415,875✔
1307
}
1308

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

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

1335
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
5,135,943✔
1336
                     void* charsetCxt) {
1337
  switch (nodeType(pStmt)) {
5,135,943✔
1338
    case QUERY_NODE_DESCRIBE_STMT:
392,663✔
1339
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
392,663✔
1340
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
1,955,696✔
1341
      return execResetQueryCache();
1,955,696✔
1342
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
61,569✔
1343
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
61,569✔
1344
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
67,962✔
1345
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
67,962✔
1346
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
6,807✔
1347
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
6,807✔
1348
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
1,079✔
1349
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,079✔
1350
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
×
1351
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
×
1352
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
1,470✔
1353
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
1,470✔
1354
    case QUERY_NODE_ALTER_LOCAL_STMT:
220,706✔
1355
      return execAlterLocal((SAlterLocalStmt*)pStmt);
220,706✔
1356
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
12,116✔
1357
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
12,116✔
1358
    case QUERY_NODE_SELECT_STMT:
2,415,875✔
1359
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
2,415,875✔
1360
    default:
×
1361
      break;
×
1362
  }
1363
  return TSDB_CODE_FAILED;
×
1364
}
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