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

taosdata / TDengine / #4870

26 Nov 2025 05:46AM UTC coverage: 64.545% (+0.006%) from 64.539%
#4870

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

768 of 945 new or added lines in 33 files covered. (81.27%)

2982 existing lines in 119 files now uncovered.

158219 of 245129 relevant lines covered (64.55%)

112474797.36 hits per line

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

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

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

37
extern SConfig* tsCfg;
38

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

50
  (*pRsp)->useconds = 0;
2,797,463✔
51
  (*pRsp)->completed = 1;
2,797,463✔
52
  (*pRsp)->precision = 0;
2,797,463✔
53
  (*pRsp)->compressed = 0;
2,797,463✔
54

55
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
2,797,463✔
56
  (*pRsp)->numOfCols = htonl(numOfCols);
2,797,463✔
57

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

69
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
2,797,463✔
70
  (*pRsp)->payloadLen = htonl(payloadLen);
2,797,463✔
71
  (*pRsp)->compLen = htonl(payloadLen);
2,797,463✔
72

73
  return TSDB_CODE_SUCCESS;
2,797,463✔
74
}
75

76
static int32_t getSchemaBytes(const SSchema* pSchema) {
71,258,537✔
77
  switch (pSchema->type) {
71,258,537✔
78
    case TSDB_DATA_TYPE_BINARY:
3,438,719✔
79
    case TSDB_DATA_TYPE_VARBINARY:
80
    case TSDB_DATA_TYPE_GEOMETRY:
81
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
3,438,719✔
82
    case TSDB_DATA_TYPE_NCHAR:
4,514,173✔
83
    case TSDB_DATA_TYPE_JSON:
84
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
4,514,173✔
85
    default:
63,305,645✔
86
      return pSchema->bytes;
63,305,645✔
87
  }
88
}
89

90
static const char* expandIdentifier(const char* name, char* output) {
908,229✔
91
  if (NULL == name) return "";
908,229✔
92
  bool containsEscapeChar = false;
908,229✔
93
  for (const char* p = name; *p != '\0'; ++p) {
4,346,099✔
94
    if (*p == TS_ESCAPE_CHAR) {
3,457,400✔
95
      containsEscapeChar = true;
19,530✔
96
      break;
19,530✔
97
    }
98
  }
99
  if (!containsEscapeChar) return name;
908,229✔
100
  if (NULL == output) return "";
19,530✔
101
  char* out_ptr = output;
19,530✔
102
  for (const char* src = name; *src != '\0'; ++src) {
120,218✔
103
    if (*src == TS_ESCAPE_CHAR) {
100,688✔
104
      *out_ptr++ = TS_ESCAPE_CHAR;
49,042✔
105
      *out_ptr++ = TS_ESCAPE_CHAR;
49,042✔
106
    } else {
107
      *out_ptr++ = *src;
51,646✔
108
    }
109
  }
110
  *out_ptr = '\0';
19,530✔
111
  return output;
19,530✔
112
}
113

114
static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) {
622,010✔
115
  QRY_PARAM_CHECK(pOutput);
622,010✔
116

117
  SSDataBlock* pBlock = NULL;
622,789✔
118
  int32_t      code = createDataBlock(&pBlock);
622,789✔
119
  if (code) {
622,789✔
120
    return code;
×
121
  }
122

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

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

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

189
  if (hasRefCol(pMeta->tableType)) {
622,605✔
190
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
5,072✔
191
  }
192

193
  int32_t fillTagCol = 0;
622,605✔
194
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
622,605✔
195
  for (int32_t i = 0; i < numOfRows; ++i) {
71,881,142✔
196
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
71,258,353✔
197
      continue;
×
198
    }
199
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
71,258,537✔
200
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
71,258,537✔
201

202
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
71,258,537✔
203
      uint8_t prec = 0, scale = 0;
350,625✔
204
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
350,625✔
205
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
350,625✔
206
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
350,625✔
207
      varDataSetLen(buf, len);
350,625✔
208
    } else {
209
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
70,907,912✔
210
    }
211
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
71,258,537✔
212
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
71,258,537✔
213
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
71,258,537✔
214
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
71,258,537✔
215
      if (i >= pMeta->tableInfo.numOfColumns) {
71,222,677✔
216
        STR_TO_VARSTR(buf, "TAG");
4,283,789✔
217
        fillTagCol = 1;
4,283,789✔
218
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
66,938,888✔
219
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
13,822✔
220
      } else {
221
        STR_TO_VARSTR(buf, "");
66,925,066✔
222
      }
223
    } else {
224
      STR_TO_VARSTR(buf, "VIEW COL");
35,860✔
225
    }
226
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
71,258,537✔
227
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
71,258,537✔
228
      if (i < pMeta->tableInfo.numOfColumns) {
70,940,626✔
229
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
66,664,517✔
230
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
66,664,517✔
231
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
66,664,517✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
66,664,517✔
233
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
66,664,517✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
66,664,517✔
235
      } else {
236
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
4,276,109✔
237
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
4,276,109✔
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
4,276,109✔
239
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
4,276,109✔
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
4,276,109✔
241
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
4,276,109✔
242
      }
243
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
317,911✔
244
      if (i < pMeta->numOfColRefs) {
45,064✔
245
        if (pMeta->colRef[i].hasRef) {
37,384✔
246
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
17,316✔
247
          strcat(refColName, pMeta->colRef[i].refDbName);
17,316✔
248
          strcat(refColName, ".");
17,316✔
249
          strcat(refColName, pMeta->colRef[i].refTableName);
17,316✔
250
          strcat(refColName, ".");
17,316✔
251
          strcat(refColName, pMeta->colRef[i].refColName);
17,316✔
252
          STR_TO_VARSTR(buf, refColName);
17,316✔
253
        } else {
254
          STR_TO_VARSTR(buf, "");
20,068✔
255
        }
256
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
37,384✔
257
      } else {
258
        STR_TO_VARSTR(buf, "");
7,680✔
259
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
7,680✔
260
      }
261
    }
262

263
    fillTagCol = 0;
71,258,537✔
264

265
    ++(pBlock->info.rows);
71,258,537✔
266
  }
267
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
622,789✔
268
    STR_TO_VARSTR(buf, "tbname");
×
269
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
×
270
    STR_TO_VARSTR(buf, "VARCHAR");
×
271
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
×
272
    int32_t bytes = TSDB_TABLE_NAME_LEN - 1;
×
273
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
×
274
    STR_TO_VARSTR(buf, "TAG");
×
275
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
×
276
    ++(pBlock->info.rows);
×
277
  }
278
  if (pBlock->info.rows <= 0) {
622,789✔
279
    qError("no permission to view any columns");
×
280
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
281
  }
282
  return TSDB_CODE_SUCCESS;
622,789✔
283
}
284

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

292
  SSDataBlock* pBlock = NULL;
622,789✔
293
  int32_t      code = buildDescResultDataBlock(&pBlock);
622,789✔
294
  if (TSDB_CODE_SUCCESS == code) {
622,789✔
295
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
622,789✔
296
  }
297
  if (TSDB_CODE_SUCCESS == code) {
622,789✔
298
    if (pDesc->pMeta) {
622,789✔
299
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
622,789✔
300
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
597,652✔
301
      } else if (hasRefCol(pDesc->pMeta->tableType) && pDesc->pMeta->colRef) {
25,137✔
302
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
5,072✔
303
      } else {
304
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
20,065✔
305
      }
306
    } else {
307
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
308
    }
309
  }
310
  (void)blockDataDestroy(pBlock);
622,789✔
311
  return code;
622,789✔
312
}
313

314
static int32_t execResetQueryCache() { return catalogClearCache(); }
2,108,915✔
315

316
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
77,905✔
317
  QRY_PARAM_CHECK(pOutput);
77,905✔
318

319
  SSDataBlock* pBlock = NULL;
77,905✔
320
  int32_t      code = createDataBlock(&pBlock);
77,905✔
321
  if (code) {
77,905✔
322
    return code;
×
323
  }
324

325
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_COLS, 1);
77,905✔
326
  code = blockDataAppendColInfo(pBlock, &infoData);
77,905✔
327
  if (TSDB_CODE_SUCCESS == code) {
77,905✔
328
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_FIELD2_LEN, 2);
77,905✔
329
    code = blockDataAppendColInfo(pBlock, &infoData);
77,905✔
330
  }
331

332
  if (TSDB_CODE_SUCCESS == code) {
77,905✔
333
    *pOutput = pBlock;
77,905✔
334
  } else {
335
    (void)blockDataDestroy(pBlock);
×
336
  }
337
  return code;
77,905✔
338
}
339

340
int64_t getValOfDiffPrecision(int8_t unit, int64_t val) {
×
341
  int64_t v = 0;
×
342
  switch (unit) {
×
343
    case 's':
×
344
      v = val / 1000;
×
345
      break;
×
346
    case 'm':
×
347
      v = val / tsTickPerMin[TSDB_TIME_PRECISION_MILLI];
×
348
      break;
×
349
    case 'h':
×
350
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 60);
×
351
      break;
×
352
    case 'd':
×
353
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60);
×
354
      break;
×
355
    case 'w':
×
356
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60 * 7);
×
357
      break;
×
358
    default:
×
359
      break;
×
360
  }
361

362
  return v;
×
363
}
364

365
static int32_t buildRetension(SArray* pRetension, char** ppRetentions) {
77,905✔
366
  size_t size = taosArrayGetSize(pRetension);
77,905✔
367
  if (size == 0) {
77,905✔
368
    *ppRetentions = NULL;
77,905✔
369
    return TSDB_CODE_SUCCESS;
77,905✔
370
  }
371

372
  const int lMaxLen = 128;
×
373
  char*     p1 = taosMemoryCalloc(1, lMaxLen);
×
374
  if (NULL == p1) {
×
375
    return terrno;
×
376
  }
377
  int32_t len = 0;
×
378

379
  for (int32_t i = 0; i < size; ++i) {
×
380
    SRetention* p = TARRAY_GET_ELEM(pRetension, i);
×
381
    int64_t     v1 = getValOfDiffPrecision(p->freqUnit, p->freq);
×
382
    int64_t     v2 = getValOfDiffPrecision(p->keepUnit, p->keep);
×
383
    if (i == 0) {
×
384
      len += tsnprintf(p1 + len, lMaxLen - len, "-:%" PRId64 "%c", v2, p->keepUnit);
×
385
    } else {
386
      len += tsnprintf(p1 + len, lMaxLen - len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit);
×
387
    }
388

389
    if (i < size - 1) {
×
390
      len += tsnprintf(p1 + len, lMaxLen - len, ",");
×
391
    }
392
  }
393

394
  *ppRetentions = p1;
×
395
  return TSDB_CODE_SUCCESS;
×
396
}
397

398
static const char* cacheModelStr(int8_t cacheModel) {
77,191✔
399
  switch (cacheModel) {
77,191✔
400
    case TSDB_CACHE_MODEL_NONE:
77,191✔
401
      return TSDB_CACHE_MODEL_NONE_STR;
77,191✔
402
    case TSDB_CACHE_MODEL_LAST_ROW:
×
403
      return TSDB_CACHE_MODEL_LAST_ROW_STR;
×
404
    case TSDB_CACHE_MODEL_LAST_VALUE:
×
405
      return TSDB_CACHE_MODEL_LAST_VALUE_STR;
×
406
    case TSDB_CACHE_MODEL_BOTH:
×
407
      return TSDB_CACHE_MODEL_BOTH_STR;
×
408
    default:
×
409
      break;
×
410
  }
411
  return TSDB_CACHE_MODEL_NONE_STR;
×
412
}
413

414
static const char* encryptAlgorithmStr(int8_t encryptAlgorithm) {
77,191✔
415
  switch (encryptAlgorithm) {
77,191✔
416
    case TSDB_ENCRYPT_ALGO_NONE:
77,191✔
417
      return TSDB_ENCRYPT_ALGO_NONE_STR;
77,191✔
418
    case TSDB_ENCRYPT_ALGO_SM4:
×
419
      return TSDB_ENCRYPT_ALGO_SM4_STR;
×
420
    default:
×
421
      break;
×
422
  }
423
  return TSDB_CACHE_MODEL_NONE_STR;
×
424
}
425

426
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
5,742,709✔
427
  if (buffer == NULL || bufSize <= 0) {
5,742,709✔
428
    return 0;
×
429
  }
430
  int32_t len = 0;
5,742,709✔
431
  if (timeInMinutes % 1440 == 0) {
5,742,709✔
432
    int32_t days = timeInMinutes / 1440;
5,730,539✔
433
    len = tsnprintf(buffer, bufSize, "%dd", days);
5,730,539✔
434
  } else if (timeInMinutes % 60 == 0) {
12,170✔
435
    int32_t hours = timeInMinutes / 60;
2,550✔
436
    len = tsnprintf(buffer, bufSize, "%dh", hours);
2,550✔
437
  } else {
438
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
9,620✔
439
  }
440
  return len;
5,742,709✔
441
}
442

443
static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, char* dbFName, SDbCfgInfo* pCfg) {
77,905✔
444
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
77,905✔
445
  pBlock->info.rows = 1;
77,905✔
446

447
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
77,905✔
448
  char             buf1[SHOW_CREATE_DB_RESULT_FIELD1_LEN] = {0};
77,905✔
449
  STR_TO_VARSTR(buf1, dbName);
77,905✔
450
  COL_DATA_SET_VAL_AND_CHECK(pCol1, 0, buf1, false);
77,905✔
451

452
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
77,905✔
453
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
77,905✔
454
  int32_t          len = 0;
77,905✔
455
  char*            prec = NULL;
77,905✔
456
  switch (pCfg->precision) {
77,905✔
457
    case TSDB_TIME_PRECISION_MILLI:
77,905✔
458
      prec = TSDB_TIME_PRECISION_MILLI_STR;
77,905✔
459
      break;
77,905✔
460
    case TSDB_TIME_PRECISION_MICRO:
×
461
      prec = TSDB_TIME_PRECISION_MICRO_STR;
×
462
      break;
×
463
    case TSDB_TIME_PRECISION_NANO:
×
464
      prec = TSDB_TIME_PRECISION_NANO_STR;
×
465
      break;
×
466
    default:
×
467
      prec = "none";
×
468
      break;
×
469
  }
470

471
  char* pRetentions = NULL;
77,905✔
472
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
77,905✔
473
  int32_t dbFNameLen = strlen(dbFName);
77,905✔
474
  int32_t hashPrefix = 0;
77,905✔
475
  if (pCfg->hashPrefix > 0) {
77,905✔
476
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
1,512✔
477
  } else if (pCfg->hashPrefix < 0) {
76,393✔
478
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
2,268✔
479
  }
480
  char durationStr[128] = {0};
77,905✔
481
  char keep0Str[128] = {0};
77,905✔
482
  char keep1Str[128] = {0};
77,905✔
483
  char keep2Str[128] = {0};
77,905✔
484
  char compactIntervalStr[13] = {0};
77,905✔
485
  char compactStartTimeStr[13] = {0};
77,905✔
486
  char compactEndTimeStr[13] = {0};
77,905✔
487

488
  int32_t lenDuration = formatDurationOrKeep(durationStr, sizeof(durationStr), pCfg->daysPerFile);
77,905✔
489
  int32_t lenKeep0 = formatDurationOrKeep(keep0Str, sizeof(keep0Str), pCfg->daysToKeep0);
77,905✔
490
  int32_t lenKeep1 = formatDurationOrKeep(keep1Str, sizeof(keep1Str), pCfg->daysToKeep1);
77,905✔
491
  int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pCfg->daysToKeep2);
77,905✔
492
  UNUSED(formatDurationOrKeep(compactIntervalStr, sizeof(compactIntervalStr), pCfg->compactInterval));
77,905✔
493
  UNUSED(formatDurationOrKeep(compactStartTimeStr, sizeof(compactStartTimeStr), pCfg->compactStartTime));
77,905✔
494
  UNUSED(formatDurationOrKeep(compactEndTimeStr, sizeof(compactEndTimeStr), pCfg->compactEndTime));
77,905✔
495

496
  if (IS_SYS_DBNAME(dbName)) {
77,905✔
497
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
714✔
498
                     "CREATE DATABASE `%s`", dbName);
714✔
499
  } else {
500
    len +=
77,191✔
501
        tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
463,146✔
502
                  "CREATE DATABASE `%s` BUFFER %d CACHESIZE %d CACHEMODEL '%s' COMP %d DURATION %s "
503
                  "WAL_FSYNC_PERIOD %d MAXROWS %d MINROWS %d STT_TRIGGER %d KEEP %s,%s,%s PAGES %d PAGESIZE %d "
504
                  "PRECISION '%s' REPLICA %d "
505
                  "WAL_LEVEL %d VGROUPS %d SINGLE_STABLE %d TABLE_PREFIX %d TABLE_SUFFIX %d TSDB_PAGESIZE %d "
506
                  "WAL_RETENTION_PERIOD %d WAL_RETENTION_SIZE %" PRId64
507
                  " KEEP_TIME_OFFSET %d ENCRYPT_ALGORITHM '%s' SS_CHUNKPAGES %d SS_KEEPLOCAL %dm SS_COMPACT %d "
508
                  "COMPACT_INTERVAL %s COMPACT_TIME_RANGE %s,%s COMPACT_TIME_OFFSET %"PRIi8 "h",
509
                  dbName, pCfg->buffer, pCfg->cacheSize, cacheModelStr(pCfg->cacheLast), pCfg->compression, durationStr,
77,191✔
510
                  pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str, keep1Str, keep2Str,
77,191✔
511
                  pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel, pCfg->numOfVgroups,
77,191✔
512
                  1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize, pCfg->walRetentionPeriod,
77,191✔
513
                  pCfg->walRetentionSize, pCfg->keepTimeOffset, encryptAlgorithmStr(pCfg->encryptAlgorithm),
77,191✔
514
                  pCfg->ssChunkSize, pCfg->ssKeepLocal, pCfg->ssCompact, compactIntervalStr, compactStartTimeStr,
77,191✔
515
                  compactEndTimeStr, pCfg->compactTimeOffset);
77,191✔
516

517
    if (pRetentions) {
77,191✔
518
      len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
519
                       " RETENTIONS %s", pRetentions);
520
    }
521
  }
522

523
  taosMemoryFree(pRetentions);
77,905✔
524

525
  (varDataLen(buf2)) = len;
77,905✔
526

527
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
77,905✔
528

529
  return TSDB_CODE_SUCCESS;
77,905✔
530
}
531

532
static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt, SRetrieveTableRsp** pRsp) {
77,905✔
533
  SSDataBlock* pBlock = NULL;
77,905✔
534
  int32_t      code = buildCreateDBResultDataBlock(&pBlock);
77,905✔
535
  if (TSDB_CODE_SUCCESS == code) {
77,905✔
536
    code = setCreateDBResultIntoDataBlock(pBlock, pStmt->dbName, pStmt->dbFName, pStmt->pCfg);
77,905✔
537
  }
538
  if (TSDB_CODE_SUCCESS == code) {
77,905✔
539
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_DB_RESULT_COLS, pRsp);
77,905✔
540
  }
541
  (void)blockDataDestroy(pBlock);
77,905✔
542
  return code;
77,905✔
543
}
544

545
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
116,722✔
546
  QRY_PARAM_CHECK(pOutput);
116,722✔
547

548
  SSDataBlock* pBlock = NULL;
116,722✔
549
  int32_t      code = createDataBlock(&pBlock);
116,722✔
550
  if (code) {
116,722✔
551
    return code;
×
552
  }
553

554
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD1_LEN, 1);
116,722✔
555
  code = blockDataAppendColInfo(pBlock, &infoData);
116,722✔
556
  if (TSDB_CODE_SUCCESS == code) {
116,722✔
557
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD2_LEN, 2);
116,722✔
558
    code = blockDataAppendColInfo(pBlock, &infoData);
116,722✔
559
  }
560

561
  if (TSDB_CODE_SUCCESS == code) {
116,722✔
562
    *pOutput = pBlock;
116,722✔
563
  } else {
564
    (void)blockDataDestroy(pBlock);
×
565
  }
566
  return code;
116,722✔
567
}
568

569
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
7,193✔
570
  QRY_PARAM_CHECK(pOutput);
7,193✔
571

572
  SSDataBlock* pBlock = NULL;
7,193✔
573
  int32_t      code = createDataBlock(&pBlock);
7,193✔
574
  if (code) {
7,193✔
575
    return code;
×
576
  }
577

578
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD1_LEN, 1);
7,193✔
579
  code = blockDataAppendColInfo(pBlock, &infoData);
7,193✔
580
  if (TSDB_CODE_SUCCESS == code) {
7,193✔
581
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD2_LEN, 2);
7,193✔
582
    code = blockDataAppendColInfo(pBlock, &infoData);
7,193✔
583
  }
584

585
  if (TSDB_CODE_SUCCESS == code) {
7,193✔
586
    *pOutput = pBlock;
7,193✔
587
  } else {
588
    (void)blockDataDestroy(pBlock);
×
589
  }
590
  return code;
7,193✔
591
}
592

593
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
68,240✔
594
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
68,240✔
595
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
532,895✔
596
    SSchema* pSchema = pCfg->pSchemas + i;
464,655✔
597
    SColRef* pRef = pCfg->pColRefs + i;
464,655✔
598
#define LTYPE_LEN                                    \
599
  (32 + 60 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN + \
600
   10)  // 60 byte for compress info, TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN for column ref
601
    char type[LTYPE_LEN];
464,655✔
602
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
464,655✔
603
    int typeLen = strlen(type);
464,655✔
604
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
464,655✔
605
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
419,506✔
606
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
51,743✔
607
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
412,912✔
608
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
14,955✔
609
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
14,955✔
610
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
397,957✔
611
      uint8_t precision, scale;
23,944✔
612
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
23,944✔
613
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
23,944✔
614
    }
615

616
    if (withExtSchema(pCfg->tableType) && pCfg->pSchemaExt && tsShowFullCreateTableColumn) {
464,655✔
617
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " ENCODE \'%s\'",
18,738✔
618
                           columnEncodeStr(COMPRESS_L1_TYPE_U32(pCfg->pSchemaExt[i].compress)));
18,738✔
619
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " COMPRESS \'%s\'",
18,738✔
620
                           columnCompressStr(COMPRESS_L2_TYPE_U32(pCfg->pSchemaExt[i].compress)));
18,738✔
621
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " LEVEL \'%s\'",
18,738✔
622
                           columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pCfg->pSchemaExt[i].compress)));
18,738✔
623
    }
624

625
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
464,655✔
626
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " FROM `%s`", pRef->refDbName);
7,388✔
627
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
7,388✔
628
      typeLen +=
7,388✔
629
          tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
7,388✔
630
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
7,388✔
631
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
7,388✔
632
    }
633

634
    if (!(pSchema->flags & COL_IS_KEY)) {
464,655✔
635
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
454,511✔
636
                        "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
454,511✔
637
    } else {
638
      char* pk = "COMPOSITE KEY";
10,144✔
639
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
10,144✔
640
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
10,144✔
641
    }
642
  }
643
}
68,240✔
644

645
static void appendColRefFields(char* buf, int32_t* len, STableCfg* pCfg) {
1,086✔
646
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
1,086✔
647
  for (int32_t i = 1; i < pCfg->numOfColumns; ++i) {
14,342✔
648
    SSchema* pSchema = pCfg->pSchemas + i;
13,256✔
649
    SColRef* pRef = pCfg->pColRefs + i;
13,256✔
650
    char     type[TSDB_COL_NAME_LEN + 10 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN];
13,256✔
651
    int      typeLen = 0;
13,256✔
652

653
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
13,256✔
654
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
4,128✔
655
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
4,128✔
656
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
4,128✔
657
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
4,128✔
658
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
4,128✔
659
    } else {
660
      continue;
9,128✔
661
    }
662

663
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
4,128✔
664
                      "%s`%s` %s", ((i > 1) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
4,128✔
665

666
  }
667
}
1,086✔
668

669
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
41,159✔
670
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
41,159✔
671
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
235,766✔
672
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
194,607✔
673
    char     type[32];
194,607✔
674
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
194,607✔
675
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
194,607✔
676
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
166,657✔
677
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
32,752✔
678
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
32,752✔
679
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
161,855✔
680
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
8,208✔
681
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
8,208✔
682
    }
683

684
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
194,607✔
685
                      "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
194,607✔
686
  }
687
}
41,159✔
688

689
static void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
34,716✔
690
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
34,716✔
691
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
86,763✔
692
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
52,047✔
693
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
52,047✔
694
                      "%s`%s`", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName));
52,047✔
695
  }
696
}
34,716✔
697

698
static int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg, void* charsetCxt) {
34,716✔
699
  int32_t code = TSDB_CODE_SUCCESS;
34,716✔
700
  SArray* pTagVals = NULL;
34,716✔
701
  STag*   pTag = (STag*)pCfg->pTags;
34,716✔
702

703
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
34,716✔
704
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
705
    return TSDB_CODE_APP_ERROR;
×
706
  }
707

708
  if (tTagIsJson(pTag)) {
34,716✔
709
    char* pJson = NULL;
535✔
710
    parseTagDatatoJson(pTag, &pJson, charsetCxt);
535✔
711
    if (NULL == pJson) {
535✔
712
      qError("failed to parse tag to json, pJson is NULL");
×
713
      return terrno;
×
714
    }
715
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
535✔
716
                      "%s", pJson);
717
    taosMemoryFree(pJson);
535✔
718

719
    return TSDB_CODE_SUCCESS;
535✔
720
  }
721

722
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
34,181✔
723
  int16_t valueNum = taosArrayGetSize(pTagVals);
34,181✔
724
  int32_t num = 0;
34,181✔
725
  int32_t j = 0;
34,181✔
726
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
85,693✔
727
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
51,512✔
728
    if (i > 0) {
51,512✔
729
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
17,331✔
730
                        ", ");
731
    }
732

733
    if (j >= valueNum) {
51,512✔
734
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
15,729✔
735
                        "NULL");
736
      continue;
15,729✔
737
    }
738

739
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
35,783✔
740
    if (pSchema->colId > pTagVal->cid) {
35,783✔
741
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
742
      code = TSDB_CODE_APP_ERROR;
×
743
      TAOS_CHECK_ERRNO(code);
×
744
    } else if (pSchema->colId == pTagVal->cid) {
35,783✔
745
      char    type = pTagVal->type;
34,269✔
746
      int32_t tlen = 0;
34,269✔
747

748
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
34,269✔
749
      if (leftSize <= 0) {
34,269✔
750
        qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
×
751
        code = TSDB_CODE_APP_ERROR;
×
752
        TAOS_CHECK_ERRNO(code);
×
753
      }
754
      if (IS_VAR_DATA_TYPE(type)) {
34,269✔
755
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
6,906✔
756
        TAOS_CHECK_ERRNO(code);
6,906✔
757
      } else {
758
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
27,363✔
759
                               &tlen);
760
        TAOS_CHECK_ERRNO(code);
27,363✔
761
      }
762
      *len += tlen;
34,269✔
763
      j++;
34,269✔
764
    } else {
765
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,514✔
766
                        "NULL");
767
    }
768
  }
769
_exit:
34,181✔
770
  taosArrayDestroy(pTagVals);
34,181✔
771

772
  return code;
34,181✔
773
}
774

775
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
100,784✔
776
  if (pCfg->commentLen > 0) {
100,784✔
777
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
778
                      " COMMENT '%s'", pCfg->pComment);
779
  } else if (0 == pCfg->commentLen) {
100,784✔
780
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
3,768✔
781
                      " COMMENT ''");
782
  }
783

784
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
100,784✔
785
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
786
                      " WATERMARK %" PRId64 "a", pCfg->watermark1);
787
    if (pCfg->watermark2 > 0) {
×
788
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
789
                        ", %" PRId64 "a", pCfg->watermark2);
790
    }
791
  }
792

793
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
100,784✔
794
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
795
                      " MAX_DELAY %" PRId64 "a", pCfg->delay1);
796
    if (pCfg->delay2 > 0) {
×
797
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
798
                        ", %" PRId64 "a", pCfg->delay2);
799
    }
800
  }
801

802
  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
100,784✔
803
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
100,784✔
804
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
805
                      " ROLLUP(");
806
    for (int32_t i = 0; i < funcNum; ++i) {
×
807
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
×
808
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
809
                        "%s%s", ((i > 0) ? ", " : ""), pFunc);
810
    }
811
    *len +=
×
812
        snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
×
813
  }
814

815
  if (pCfg->ttl > 0) {
100,784✔
816
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
817
                      " TTL %d", pCfg->ttl);
818
  }
819

820
  if (pCfg->keep > 0) {
100,784✔
821
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,716✔
822
                      " KEEP %dm", pCfg->keep);
823
  }
824

825
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
100,784✔
826
    int32_t nSma = 0;
67,154✔
827
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
517,467✔
828
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
450,313✔
829
        ++nSma;
450,313✔
830
      }
831
    }
832

833
    if (nSma < pCfg->numOfColumns && nSma > 0) {
67,154✔
834
      bool smaOn = false;
×
835
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
836
                        " SMA(");
837
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
838
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
839
          if (smaOn) {
×
840
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
841
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ",`%s`",
×
842
                              (pCfg->pSchemas + i)->name);
×
843
          } else {
844
            smaOn = true;
×
845
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
846
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), "`%s`",
×
847
                              (pCfg->pSchemas + i)->name);
×
848
          }
849
        }
850
      }
851
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, ")");
×
852
    }
853
  }
854

855
  if (pCfg->virtualStb) {
100,784✔
856
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
434✔
857
                      " VIRTUAL %d", pCfg->virtualStb);
434✔
858
  }
859
}
100,784✔
860

861
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
113,666✔
862
                                              void* charsetCxt) {
863
  int32_t code = TSDB_CODE_SUCCESS;
113,666✔
864
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
113,666✔
865
  pBlock->info.rows = 1;
113,666✔
866

867
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
113,666✔
868
  char             buf1[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1)] = {0};
113,666✔
869
  STR_TO_VARSTR(buf1, tbName);
113,666✔
870
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
113,666✔
871

872
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
113,666✔
873
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
113,666✔
874
  if (NULL == buf2) {
113,666✔
875
    QRY_ERR_RET(terrno);
×
876
  }
877

878
  int32_t len = 0;
113,666✔
879

880
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
113,666✔
881
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
41,159✔
882
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
883
    appendColumnFields(buf2, &len, pCfg);
41,159✔
884
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
41,159✔
885
                     ") TAGS (");
886
    appendTagFields(buf2, &len, pCfg);
41,159✔
887
    len +=
41,159✔
888
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
41,159✔
889
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
41,159✔
890
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
72,507✔
891
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
33,630✔
892
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
893
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
33,630✔
894
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
33,630✔
895
    appendTagNameFields(buf2, &len, pCfg);
33,630✔
896
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
33,630✔
897
                     ") TAGS (");
898
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
33,630✔
899
    TAOS_CHECK_ERRNO(code);
33,630✔
900
    len +=
33,630✔
901
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
33,630✔
902
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
33,630✔
903
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
38,877✔
904
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
25,995✔
905
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
906
    appendColumnFields(buf2, &len, pCfg);
25,995✔
907
    len +=
25,995✔
908
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
25,995✔
909
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
25,995✔
910
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
12,882✔
911
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1,086✔
912
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
913
    appendColumnFields(buf2, &len, pCfg);
1,086✔
914
    len +=
1,086✔
915
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,086✔
916
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
11,796✔
917
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1,086✔
918
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
919
    appendColRefFields(buf2, &len, pCfg);
1,086✔
920
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,086✔
921
                    ") USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
1,086✔
922
    appendTagNameFields(buf2, &len, pCfg);
1,086✔
923
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,086✔
924
                     ") TAGS (");
925
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
1,086✔
926
    TAOS_CHECK_ERRNO(code);
1,086✔
927
    len +=
1,086✔
928
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,086✔
929
  }
930

931
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
113,666✔
932

933
  code = colDataSetVal(pCol2, 0, buf2, false);
113,666✔
934
  TAOS_CHECK_ERRNO(code);
113,666✔
935

936
_exit:
113,666✔
937
  taosMemoryFree(buf2);
113,666✔
938

939
  return code;
113,666✔
940
}
941

942
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
7,193✔
943
  int32_t code = 0;
7,193✔
944
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
7,193✔
945
  pBlock->info.rows = 1;
7,193✔
946

947
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
7,193✔
948
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
7,193✔
949
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
7,193✔
950
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
7,193✔
951
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
7,193✔
952

953
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
7,193✔
954
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
7,193✔
955
  if (NULL == buf2) {
7,193✔
956
    return terrno;
×
957
  }
958

959
  SViewMeta* pMeta = pStmt->pViewMeta;
7,193✔
960
  if (NULL == pMeta) {
7,193✔
961
    qError("exception: view meta is null");
×
962
    taosMemoryFree(buf2);
×
963
    return TSDB_CODE_APP_ERROR;
×
964
  }
965
  snprintf(varDataVal(buf2), SHOW_CREATE_VIEW_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE VIEW `%s`.`%s` AS %s",
7,193✔
966
           pStmt->dbName, pStmt->viewName, pMeta->querySql);
7,193✔
967
  int32_t len = strlen(varDataVal(buf2));
7,193✔
968
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
7,193✔
969
  code = colDataSetVal(pCol2, 0, buf2, false);
7,193✔
970
  taosMemoryFree(buf2);
7,193✔
971

972
  return code;
7,193✔
973
}
974

975
extern const char* fmGetFuncName(int32_t funcId);
976
static int32_t setCreateRsmaResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateRsmaStmt* pStmt) {
3,056✔
977
  int32_t       code = 0, lino = 0;
3,056✔
978
  char*         buf2 = NULL;
3,056✔
979
  SRsmaInfoRsp* pMeta = pStmt->pRsmaMeta;
3,056✔
980

981
  if (pMeta->nFuncs != pMeta->nColNames) {
3,056✔
982
    qError("exception: rsma meta is invalid, nFuncs:%d != nColNames:%d", pMeta->nFuncs, pMeta->nColNames);
×
983
    return TSDB_CODE_APP_ERROR;
×
984
  }
985

986
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, 1));
3,056✔
987
  pBlock->info.rows = 1;
3,056✔
988

989
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
3,056✔
990
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1] = {0};
3,056✔
991
  snprintf(varDataVal(buf1), TSDB_TABLE_FNAME_LEN + 4, "`%s`", expandIdentifier(pStmt->rsmaName, buf1));
3,056✔
992
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
3,056✔
993
  TAOS_CHECK_EXIT(colDataSetVal(pCol1, 0, buf1, false));
3,056✔
994

995
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
3,056✔
996
  if (!(buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN))) {
3,056✔
997
    return terrno;
×
998
  }
999

1000
  SName name = {0};
3,056✔
1001
  TAOS_CHECK_EXIT(tNameFromString(&name, pMeta->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
3,056✔
1002

1003
  int32_t len = 0;
3,056✔
1004
  len += tsnprintf(varDataVal(buf2), SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
6,112✔
1005
                       "CREATE RSMA `%s` ON `%s`.`%s` FUNCTION(", expandIdentifier(pStmt->rsmaName, buf1),
3,056✔
1006
                       expandIdentifier(name.dbname, buf1), expandIdentifier(name.tname, buf1));
1007
  for (int32_t i = 0; i < pMeta->nFuncs; ++i) {
22,920✔
1008
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
39,728✔
1009
                         "%s%s(`%s`)", (i > 0) ? "," : "", fmGetFuncName(pMeta->funcIds[i]),
19,864✔
1010
                         expandIdentifier(*(char**)TARRAY_GET_ELEM(pMeta->colNames, i), buf1));
19,864✔
1011
  }
1012
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
3,056✔
1013
                       ") INTERVAL(%d%c", pMeta->interval[0], pMeta->intervalUnit);
3,056✔
1014
  if (pMeta->interval[1] > 0) {
3,056✔
1015
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
3,056✔
1016
                         ",%d%c", pMeta->interval[1], pMeta->intervalUnit);
3,056✔
1017
  }
1018
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
3,056✔
1019
  varDataLen(buf2) = len;
3,056✔
1020
  code = colDataSetVal(pCol2, 0, buf2, false);
3,056✔
1021
_exit:
3,056✔
1022
  taosMemoryFree(buf2);
3,056✔
1023
  return code;
3,056✔
1024
}
1025

1026
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
111,711✔
1027
  SSDataBlock* pBlock = NULL;
111,711✔
1028
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
111,711✔
1029
  if (TSDB_CODE_SUCCESS == code) {
111,711✔
1030
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
111,711✔
1031
  }
1032
  if (TSDB_CODE_SUCCESS == code) {
111,711✔
1033
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
111,711✔
1034
  }
1035
  (void)blockDataDestroy(pBlock);
111,711✔
1036
  return code;
111,711✔
1037
}
1038

1039
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
1,955✔
1040
  SSDataBlock* pBlock = NULL;
1,955✔
1041
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
1,955✔
1042
  if (TSDB_CODE_SUCCESS == code) {
1,955✔
1043
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
1,955✔
1044
  }
1045
  if (TSDB_CODE_SUCCESS == code) {
1,955✔
1046
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
1,955✔
1047
  }
1048
  (void)blockDataDestroy(pBlock);
1,955✔
1049
  return code;
1,955✔
1050
}
1051

1052
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
1,071✔
1053
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
1,071✔
1054
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
1,071✔
1055
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
357✔
1056
    return terrno;
357✔
1057
  }
1058

1059
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
714✔
1060
}
1061

1062
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
464,497✔
1063
  int32_t code = 0;
464,497✔
1064

1065
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
464,497✔
1066
    taosResetLog();
270✔
1067
    cfgDumpCfg(tsCfg, 0, false);
270✔
1068
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
464,227✔
1069
    int32_t tmp = 0;
2,098✔
1070
    code = taosStr2int32(value, &tmp);
2,098✔
1071
    if (code) {
2,098✔
1072
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1073
      return code;
×
1074
    }
1075
    code = schedulerUpdatePolicy(tmp);
2,098✔
1076
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
462,129✔
1077
    int32_t tmp = 0;
×
1078
    code = taosStr2int32(value, &tmp);
×
1079
    if (code) {
×
1080
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1081
      return code;
×
1082
    }
1083
    code = schedulerEnableReSchedule(tmp != 0);
×
1084
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
462,129✔
1085
    code = ctgdHandleDbgCommand(value);
×
1086
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
462,129✔
1087
    code = taosMemoryDbgInit();
×
1088
    if (code) {
×
1089
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
1090
      return code;
×
1091
    }
1092
    tsAsyncLog = false;
×
1093
    qInfo("memory dbg enabled");
×
1094
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
462,129✔
1095
    code = taosMemoryDbgInitRestore();
×
1096
    if (code) {
×
1097
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
1098
      return code;
×
1099
    }
1100
    qInfo("memory dbg disabled");
×
1101
  } else {
1102
    goto _return;
462,129✔
1103
  }
1104

1105
  *processed = true;
2,368✔
1106

1107
_return:
464,497✔
1108

1109
  if (code) {
464,497✔
1110
    terrno = code;
×
1111
  }
1112

1113
  return code;
464,497✔
1114
}
1115

1116
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
464,497✔
1117
  bool processed = false;
464,497✔
1118

1119
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
464,497✔
1120
    return terrno;
×
1121
  }
1122

1123
  if (processed) {
464,497✔
1124
    goto _return;
2,368✔
1125
  }
1126

1127
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
462,129✔
1128
    return terrno;
3,056✔
1129
  }
1130

1131
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
459,073✔
1132
    return terrno;
×
1133
  }
1134

1135
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
459,073✔
1136

1137
_return:
460,327✔
1138

1139
  return TSDB_CODE_SUCCESS;
461,441✔
1140
}
1141

1142
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
34,150✔
1143
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
34,150✔
1144
  if (NULL == pBlock) {
34,150✔
1145
    return terrno;
×
1146
  }
1147

1148
  pBlock->info.hasVarCol = true;
34,150✔
1149

1150
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
34,150✔
1151
  if (NULL == pBlock->pDataBlock) {
34,150✔
1152
    taosMemoryFree(pBlock);
×
1153
    return terrno;
×
1154
  }
1155

1156
  SColumnInfoData infoData = {0};
34,150✔
1157

1158
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
34,150✔
1159
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
34,150✔
1160
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
68,300✔
1161
    goto _exit;
×
1162
  }
1163

1164
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
34,150✔
1165
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
34,150✔
1166
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
68,300✔
1167
    goto _exit;
×
1168
  }
1169

1170
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
34,150✔
1171
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
34,150✔
1172
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
68,300✔
1173
    goto _exit;
×
1174
  }
1175

1176
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
34,150✔
1177
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
34,150✔
1178
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
68,300✔
1179
    goto _exit;
×
1180
  }
1181

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

1188
  *pOutput = pBlock;
34,150✔
1189

1190
_exit:
34,150✔
1191
  if (terrno != TSDB_CODE_SUCCESS) {
34,150✔
1192
    taosArrayDestroy(pBlock->pDataBlock);
×
1193
    taosMemoryFree(pBlock);
×
1194
  }
1195
  return terrno;
34,150✔
1196
}
1197

1198
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
34,150✔
1199
  SSDataBlock* pBlock = NULL;
34,150✔
1200
  char*        likePattern = NULL;
34,150✔
1201
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
34,150✔
1202
  if (TSDB_CODE_SUCCESS == code) {
34,150✔
1203
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
34,150✔
1204
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
2,468✔
1205
    }
1206
  }
1207
  if (TSDB_CODE_SUCCESS == code) {
34,150✔
1208
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
34,150✔
1209
  }
1210
  if (TSDB_CODE_SUCCESS == code) {
34,150✔
1211
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
34,150✔
1212
  }
1213
  (void)blockDataDestroy(pBlock);
34,150✔
1214
  return code;
34,150✔
1215
}
1216

1217
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
1,938,704✔
1218
  QRY_PARAM_CHECK(pOutput);
1,938,704✔
1219

1220
  SSDataBlock* pBlock = NULL;
1,938,704✔
1221
  int32_t      code = createDataBlock(&pBlock);
1,938,704✔
1222
  if (code) {
1,938,704✔
1223
    return code;
×
1224
  }
1225

1226
  SNode* pProj = NULL;
1,938,704✔
1227
  FOREACH(pProj, pProjects) {
4,581,533✔
1228
    SExprNode*      pExpr = (SExprNode*)pProj;
2,642,829✔
1229
    SColumnInfoData infoData = {0};
2,642,829✔
1230
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
2,642,829✔
1231
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1232
      infoData.info.bytes = 0;
×
1233
    } else {
1234
      infoData.info.type = pExpr->resType.type;
2,642,829✔
1235
      infoData.info.bytes = pExpr->resType.bytes;
2,642,829✔
1236
      infoData.info.precision = pExpr->resType.precision;
2,642,829✔
1237
      infoData.info.scale = pExpr->resType.scale;
2,642,829✔
1238
    }
1239
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
2,642,829✔
1240
  }
1241

1242
  *pOutput = pBlock;
1,938,704✔
1243
  return code;
1,938,704✔
1244
}
1245

1246
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
1,938,704✔
1247
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
1,938,704✔
1248

1249
  int32_t index = 0;
1,938,704✔
1250
  SNode*  pProj = NULL;
1,938,704✔
1251
  FOREACH(pProj, pProjects) {
4,581,533✔
1252
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
2,642,829✔
1253
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1254
    } else {
1255
      if (((SValueNode*)pProj)->isNull) {
2,642,829✔
1256
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
78,207✔
1257
      } else {
1258
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
2,564,622✔
1259
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1260
      }
1261
    }
1262
  }
1263

1264
  pBlock->info.rows = 1;
1,938,704✔
1265
  return TSDB_CODE_SUCCESS;
1,938,704✔
1266
}
1267

1268
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
1,938,704✔
1269
  SSDataBlock* pBlock = NULL;
1,938,704✔
1270
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
1,938,704✔
1271
  if (TSDB_CODE_SUCCESS == code) {
1,938,704✔
1272
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
1,938,704✔
1273
  }
1274
  if (TSDB_CODE_SUCCESS == code) {
1,938,704✔
1275
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
1,938,704✔
1276
  }
1277
  (void)blockDataDestroy(pBlock);
1,938,704✔
1278
  return code;
1,938,704✔
1279
}
1280

1281
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
7,193✔
1282
  SSDataBlock* pBlock = NULL;
7,193✔
1283
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
7,193✔
1284
  if (TSDB_CODE_SUCCESS == code) {
7,193✔
1285
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
7,193✔
1286
  }
1287
  if (TSDB_CODE_SUCCESS == code) {
7,193✔
1288
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
7,193✔
1289
  }
1290
  (void)blockDataDestroy(pBlock);
7,193✔
1291
  return code;
7,193✔
1292
}
1293

1294
static int32_t execShowCreateRsma(SShowCreateRsmaStmt* pStmt, SRetrieveTableRsp** pRsp) {
3,056✔
1295
  SSDataBlock* pBlock = NULL;
3,056✔
1296
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
3,056✔
1297
  if (TSDB_CODE_SUCCESS == code) {
3,056✔
1298
    code = setCreateRsmaResultIntoDataBlock(pBlock, pStmt);
3,056✔
1299
  }
1300
  if (TSDB_CODE_SUCCESS == code) {
3,056✔
1301
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
3,056✔
1302
  }
1303
  (void)blockDataDestroy(pBlock);
3,056✔
1304
  return code;
3,056✔
1305
}
1306

1307
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
5,370,453✔
1308
                     void* charsetCxt) {
1309
  switch (nodeType(pStmt)) {
5,370,453✔
1310
    case QUERY_NODE_DESCRIBE_STMT:
622,789✔
1311
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
622,789✔
1312
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
2,108,915✔
1313
      return execResetQueryCache();
2,108,915✔
1314
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
77,905✔
1315
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
77,905✔
1316
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
110,997✔
1317
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
110,997✔
1318
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
1,955✔
1319
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,955✔
1320
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
1,071✔
1321
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,071✔
1322
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
7,193✔
1323
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
7,193✔
1324
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
3,056✔
1325
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
3,056✔
1326
    case QUERY_NODE_ALTER_LOCAL_STMT:
464,497✔
1327
      return execAlterLocal((SAlterLocalStmt*)pStmt);
464,497✔
1328
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
34,150✔
1329
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
34,150✔
1330
    case QUERY_NODE_SELECT_STMT:
1,938,704✔
1331
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
1,938,704✔
UNCOV
1332
    default:
×
UNCOV
1333
      break;
×
1334
  }
UNCOV
1335
  return TSDB_CODE_FAILED;
×
1336
}
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