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

taosdata / TDengine / #4881

14 Dec 2025 03:48AM UTC coverage: 60.617% (+0.5%) from 60.092%
#4881

push

travis-ci

web-flow
test: update coverage workflow time (#33918)

156854 of 258761 relevant lines covered (60.62%)

75258957.81 hits per line

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

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

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

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

39
extern SConfig* tsCfg;
40

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

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

57
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
1,612,781✔
58
  (*pRsp)->numOfCols = htonl(numOfCols);
1,612,781✔
59

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

71
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
1,612,781✔
72
  (*pRsp)->payloadLen = htonl(payloadLen);
1,612,781✔
73
  (*pRsp)->compLen = htonl(payloadLen);
1,612,781✔
74

75
  return TSDB_CODE_SUCCESS;
1,612,781✔
76
}
77

78
static int32_t getSchemaBytes(const SSchema* pSchema) {
46,599,918✔
79
  switch (pSchema->type) {
46,599,918✔
80
    case TSDB_DATA_TYPE_BINARY:
2,095,557✔
81
    case TSDB_DATA_TYPE_VARBINARY:
82
    case TSDB_DATA_TYPE_GEOMETRY:
83
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
2,095,557✔
84
    case TSDB_DATA_TYPE_NCHAR:
3,217,304✔
85
    case TSDB_DATA_TYPE_JSON:
86
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
3,217,304✔
87
    default:
41,287,057✔
88
      return pSchema->bytes;
41,287,057✔
89
  }
90
}
91

92
static const char* expandIdentifier(const char* name, char* output) {
563,524✔
93
  if (NULL == name) return "";
563,524✔
94
  bool containsEscapeChar = false;
563,524✔
95
  for (const char* p = name; *p != '\0'; ++p) {
2,775,763✔
96
    if (*p == TS_ESCAPE_CHAR) {
2,223,039✔
97
      containsEscapeChar = true;
10,800✔
98
      break;
10,800✔
99
    }
100
  }
101
  if (!containsEscapeChar) return name;
563,524✔
102
  if (NULL == output) return "";
10,800✔
103
  char* out_ptr = output;
10,800✔
104
  for (const char* src = name; *src != '\0'; ++src) {
66,480✔
105
    if (*src == TS_ESCAPE_CHAR) {
55,680✔
106
      *out_ptr++ = TS_ESCAPE_CHAR;
27,120✔
107
      *out_ptr++ = TS_ESCAPE_CHAR;
27,120✔
108
    } else {
109
      *out_ptr++ = *src;
28,560✔
110
    }
111
  }
112
  *out_ptr = '\0';
10,800✔
113
  return output;
10,800✔
114
}
115

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

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

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

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

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

191
  if (hasRefCol(pMeta->tableType)) {
330,395✔
192
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
3,026✔
193
  }
194

195
  int32_t fillTagCol = 0;
330,395✔
196
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
330,395✔
197
  for (int32_t i = 0; i < numOfRows; ++i) {
46,930,313✔
198
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
46,599,918✔
199
      continue;
×
200
    }
201
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
46,599,918✔
202
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
46,599,918✔
203

204
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
46,599,918✔
205
      uint8_t prec = 0, scale = 0;
187,823✔
206
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
187,823✔
207
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
187,823✔
208
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
187,823✔
209
      varDataSetLen(buf, len);
187,823✔
210
    } else {
211
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
46,412,095✔
212
    }
213
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
46,599,918✔
214
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
46,599,918✔
215
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
46,599,918✔
216
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
46,599,918✔
217
      if (i >= pMeta->tableInfo.numOfColumns) {
46,580,378✔
218
        STR_TO_VARSTR(buf, "TAG");
2,625,243✔
219
        fillTagCol = 1;
2,625,243✔
220
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
43,955,135✔
221
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
8,702✔
222
      } else {
223
        STR_TO_VARSTR(buf, "");
43,946,433✔
224
      }
225
    } else {
226
      STR_TO_VARSTR(buf, "VIEW COL");
19,540✔
227
    }
228
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
46,599,918✔
229
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
46,599,918✔
230
      if (i < pMeta->tableInfo.numOfColumns) {
46,516,190✔
231
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
43,895,777✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
43,895,777✔
233
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
43,895,777✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
43,895,777✔
235
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
43,895,777✔
236
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
43,895,777✔
237
      } else {
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,620,413✔
239
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
2,620,413✔
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,620,413✔
241
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
2,620,413✔
242
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
2,620,413✔
243
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
2,620,413✔
244
      }
245
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
83,728✔
246
      if (i < pMeta->numOfColRefs) {
29,242✔
247
        if (pMeta->colRef[i].hasRef) {
24,412✔
248
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
11,013✔
249
          strcat(refColName, pMeta->colRef[i].refDbName);
11,013✔
250
          strcat(refColName, ".");
11,013✔
251
          strcat(refColName, pMeta->colRef[i].refTableName);
11,013✔
252
          strcat(refColName, ".");
11,013✔
253
          strcat(refColName, pMeta->colRef[i].refColName);
11,013✔
254
          STR_TO_VARSTR(buf, refColName);
11,013✔
255
        } else {
256
          STR_TO_VARSTR(buf, "");
13,399✔
257
        }
258
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
24,412✔
259
      } else {
260
        STR_TO_VARSTR(buf, "");
4,830✔
261
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
4,830✔
262
      }
263
    }
264

265
    fillTagCol = 0;
46,599,918✔
266

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

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

294
  SSDataBlock* pBlock = NULL;
330,395✔
295
  int32_t      code = buildDescResultDataBlock(&pBlock);
330,395✔
296
  if (TSDB_CODE_SUCCESS == code) {
330,395✔
297
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
330,395✔
298
  }
299
  if (TSDB_CODE_SUCCESS == code) {
330,395✔
300
    if (pDesc->pMeta) {
330,395✔
301
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
330,395✔
302
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
318,615✔
303
      } else if (hasRefCol(pDesc->pMeta->tableType) && pDesc->pMeta->colRef) {
11,780✔
304
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
3,026✔
305
      } else {
306
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
8,754✔
307
      }
308
    } else {
309
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
310
    }
311
  }
312
  (void)blockDataDestroy(pBlock);
330,395✔
313
  return code;
330,395✔
314
}
315

316
static int32_t execResetQueryCache() { return catalogClearCache(); }
1,306,448✔
317

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

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

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

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

434
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
3,524,815✔
435
  if (buffer == NULL || bufSize <= 0) {
3,524,815✔
436
    return 0;
×
437
  }
438
  int32_t len = 0;
3,524,815✔
439
  if (timeInMinutes % 1440 == 0) {
3,524,815✔
440
    int32_t days = timeInMinutes / 1440;
3,515,131✔
441
    len = tsnprintf(buffer, bufSize, "%dd", days);
3,515,131✔
442
  } else if (timeInMinutes % 60 == 0) {
9,684✔
443
    int32_t hours = timeInMinutes / 60;
1,746✔
444
    len = tsnprintf(buffer, bufSize, "%dh", hours);
1,746✔
445
  } else {
446
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
7,938✔
447
  }
448
  return len;
3,524,815✔
449
}
450

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

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

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

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

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

525
    if (pRetentions) {
52,848✔
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);
53,290✔
532

533
  (varDataLen(buf2)) = len;
53,290✔
534

535
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
53,290✔
536

537
  return TSDB_CODE_SUCCESS;
53,290✔
538
}
539

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

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

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

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

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

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

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

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

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

601
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
42,849✔
602
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
42,849✔
603
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
327,819✔
604
    SSchema* pSchema = pCfg->pSchemas + i;
284,970✔
605
    SColRef* pRef = pCfg->pColRefs + i;
284,970✔
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];
284,970✔
610
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
284,970✔
611
    int typeLen = strlen(type);
284,970✔
612
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
284,970✔
613
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
256,009✔
614
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
33,131✔
615
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
251,839✔
616
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
9,342✔
617
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
9,342✔
618
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
242,497✔
619
      uint8_t precision, scale;
13,072✔
620
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
13,072✔
621
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
13,072✔
622
    }
623

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

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

642
    if (!(pSchema->flags & COL_IS_KEY)) {
284,970✔
643
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
278,001✔
644
                        "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
278,001✔
645
    } else {
646
      char* pk = "COMPOSITE KEY";
6,969✔
647
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
6,969✔
648
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
6,969✔
649
    }
650
  }
651
}
42,849✔
652

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

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

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

674
  }
675
}
691✔
676

677
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
25,809✔
678
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
25,809✔
679
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
146,390✔
680
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
120,581✔
681
    char     type[32];
120,581✔
682
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
120,581✔
683
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
120,581✔
684
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
103,106✔
685
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
20,445✔
686
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
20,445✔
687
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
100,136✔
688
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
5,073✔
689
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
5,073✔
690
    }
691

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

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

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

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

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

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

743
    return TSDB_CODE_SUCCESS;
1,610✔
744
  }
745

746
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
22,714✔
747
  int16_t valueNum = taosArrayGetSize(pTagVals);
22,714✔
748
  int32_t num = 0;
22,714✔
749
  int32_t j = 0;
22,714✔
750
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
57,488✔
751
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
34,774✔
752
    if (i > 0) {
34,774✔
753
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
12,060✔
754
                        ", ");
755
    }
756

757
    if (j >= valueNum) {
34,774✔
758
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
11,303✔
759
                        "NULL");
760
      continue;
11,303✔
761
    }
762

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

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

796
  return code;
22,714✔
797
}
798

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

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

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

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

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

844
  if (pCfg->keep > 0) {
65,791✔
845
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,928✔
846
                      " KEEP %dm", pCfg->keep);
847
  }
848

849
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
65,791✔
850
    int32_t nSma = 0;
42,158✔
851
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
317,388✔
852
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
275,230✔
853
        ++nSma;
275,230✔
854
      }
855
    }
856

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

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

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

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

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

902
  int32_t len = 0;
73,803✔
903

904
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
73,803✔
905
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
25,809✔
906
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
907
    appendColumnFields(buf2, &len, pCfg);
25,809✔
908
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
25,809✔
909
                     ") TAGS (");
910
    appendTagFields(buf2, &len, pCfg);
25,809✔
911
    len +=
25,809✔
912
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
25,809✔
913
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
25,809✔
914
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
47,994✔
915
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
23,633✔
916
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
917
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
23,633✔
918
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
23,633✔
919
    appendTagNameFields(buf2, &len, pCfg);
23,633✔
920
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
23,633✔
921
                     ") TAGS (");
922
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
23,633✔
923
    TAOS_CHECK_ERRNO(code);
23,633✔
924
    len +=
23,633✔
925
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
23,633✔
926
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
23,633✔
927
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
24,361✔
928
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
16,349✔
929
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
930
    appendColumnFields(buf2, &len, pCfg);
16,349✔
931
    len +=
16,349✔
932
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
16,349✔
933
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
16,349✔
934
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
8,012✔
935
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
691✔
936
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
937
    appendColumnFields(buf2, &len, pCfg);
691✔
938
    len +=
691✔
939
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
691✔
940
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
7,321✔
941
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
691✔
942
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
943
    appendColRefFields(buf2, &len, pCfg);
691✔
944
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
691✔
945
                    ") USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
691✔
946
    appendTagNameFields(buf2, &len, pCfg);
691✔
947
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
691✔
948
                     ") TAGS (");
949
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
691✔
950
    TAOS_CHECK_ERRNO(code);
691✔
951
    len +=
691✔
952
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
691✔
953
  }
954

955
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
73,803✔
956

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

960
_exit:
73,803✔
961
  taosMemoryFree(buf2);
73,803✔
962

963
  return code;
73,803✔
964
}
965

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

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

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

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

996
  return code;
3,941✔
997
}
998

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

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

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

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

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

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

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

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

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

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

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

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

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

1129
  *processed = true;
653✔
1130

1131
_return:
162,613✔
1132

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

1137
  return code;
162,613✔
1138
}
1139

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

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

1147
  if (processed) {
162,613✔
1148
    goto _return;
653✔
1149
  }
1150

1151
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
161,960✔
1152
    return terrno;
1,842✔
1153
  }
1154

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

1159
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
160,118✔
1160

1161
_return:
160,735✔
1162

1163
  return TSDB_CODE_SUCCESS;
160,771✔
1164
}
1165

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

1172
  pBlock->info.hasVarCol = true;
19,665✔
1173

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

1180
  SColumnInfoData infoData = {0};
19,665✔
1181

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

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

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

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

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

1212
  *pOutput = pBlock;
19,665✔
1213

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

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

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

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

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

1266
  *pOutput = pBlock;
1,130,553✔
1267
  return code;
1,130,553✔
1268
}
1269

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

1273
  int32_t index = 0;
1,130,553✔
1274
  SNode*  pProj = NULL;
1,130,553✔
1275
  FOREACH(pProj, pProjects) {
2,641,796✔
1276
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
1,511,243✔
1277
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1278
    } else {
1279
      if (((SValueNode*)pProj)->isNull) {
1,511,243✔
1280
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
48,257✔
1281
      } else {
1282
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
1,462,986✔
1283
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1284
      }
1285
    }
1286
  }
1287

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

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

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

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

1331
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
3,082,063✔
1332
                     void* charsetCxt) {
1333
  switch (nodeType(pStmt)) {
3,082,063✔
1334
    case QUERY_NODE_DESCRIBE_STMT:
330,395✔
1335
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
330,395✔
1336
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
1,306,448✔
1337
      return execResetQueryCache();
1,306,448✔
1338
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
53,290✔
1339
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
53,290✔
1340
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
72,099✔
1341
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
72,099✔
1342
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
1,262✔
1343
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,262✔
1344
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
663✔
1345
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
663✔
1346
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
3,941✔
1347
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
3,941✔
1348
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
1,134✔
1349
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
1,134✔
1350
    case QUERY_NODE_ALTER_LOCAL_STMT:
162,613✔
1351
      return execAlterLocal((SAlterLocalStmt*)pStmt);
162,613✔
1352
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
19,665✔
1353
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
19,665✔
1354
    case QUERY_NODE_SELECT_STMT:
1,130,553✔
1355
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
1,130,553✔
1356
    default:
×
1357
      break;
×
1358
  }
1359
  return TSDB_CODE_FAILED;
×
1360
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc