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

taosdata / TDengine / #5059

17 May 2026 01:15AM UTC coverage: 73.443% (+0.06%) from 73.387%
#5059

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281870 of 383795 relevant lines covered (73.44%)

135516561.93 hits per line

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

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

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

57
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
3,812,227✔
58
  (*pRsp)->numOfCols = htonl(numOfCols);
3,812,227✔
59

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

71
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
3,812,227✔
72
  (*pRsp)->payloadLen = htonl(payloadLen);
3,812,227✔
73
  (*pRsp)->compLen = htonl(payloadLen);
3,812,227✔
74

75
  return TSDB_CODE_SUCCESS;
3,812,227✔
76
}
77

78
static int32_t getSchemaBytes(const SSchema* pSchema) {
66,916,117✔
79
  switch (pSchema->type) {
66,916,117✔
80
    case TSDB_DATA_TYPE_BINARY:
2,701,890✔
81
    case TSDB_DATA_TYPE_VARBINARY:
82
    case TSDB_DATA_TYPE_GEOMETRY:
83
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
2,701,890✔
84
    case TSDB_DATA_TYPE_NCHAR:
4,523,099✔
85
    case TSDB_DATA_TYPE_JSON:
86
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
4,523,099✔
87
    default:
59,691,128✔
88
      return pSchema->bytes;
59,691,128✔
89
  }
90
}
91

92
static const char* expandIdentifier(const char* name, char* output) {
867,455✔
93
  if (NULL == name) return "";
867,455✔
94
  bool containsEscapeChar = false;
867,455✔
95
  for (const char* p = name; *p != '\0'; ++p) {
6,058,801✔
96
    if (*p == TS_ESCAPE_CHAR) {
5,244,986✔
97
      containsEscapeChar = true;
53,640✔
98
      break;
53,640✔
99
    }
100
  }
101
  if (!containsEscapeChar) return name;
867,455✔
102
  if (NULL == output) return "";
53,640✔
103
  char* out_ptr = output;
53,640✔
104
  for (const char* src = name; *src != '\0'; ++src) {
330,184✔
105
    if (*src == TS_ESCAPE_CHAR) {
276,544✔
106
      *out_ptr++ = TS_ESCAPE_CHAR;
134,696✔
107
      *out_ptr++ = TS_ESCAPE_CHAR;
134,696✔
108
    } else {
109
      *out_ptr++ = *src;
141,848✔
110
    }
111
  }
112
  *out_ptr = '\0';
53,640✔
113
  return output;
53,640✔
114
}
115

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

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

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

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

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

191
  if (hasRefCol(pMeta->tableType)) {
511,468✔
192
    pCol8 = taosArrayGet(pBlock->pDataBlock, 4);
8,565✔
193
  }
194

195
  int32_t fillTagCol = 0;
511,468✔
196
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
511,468✔
197
  for (int32_t i = 0; i < numOfRows; ++i) {
67,427,558✔
198
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
66,916,089✔
199
      continue;
×
200
    }
201
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
66,916,089✔
202
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
66,916,093✔
203

204
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
66,916,073✔
205
      uint8_t prec = 0, scale = 0;
305,520✔
206
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
305,520✔
207
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
305,519✔
208
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
305,519✔
209
      varDataSetLen(buf, len);
305,520✔
210
    } else {
211
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
66,610,553✔
212
    }
213
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
66,916,097✔
214
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
66,916,117✔
215
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
66,916,117✔
216
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
66,916,116✔
217
      if (i >= pMeta->tableInfo.numOfColumns) {
66,881,289✔
218
        STR_TO_VARSTR(buf, "TAG");
3,541,990✔
219
        fillTagCol = 1;
3,541,990✔
220
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
63,339,326✔
221
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
12,441✔
222
      } else {
223
        STR_TO_VARSTR(buf, "");
63,326,885✔
224
      }
225
    } else {
226
      STR_TO_VARSTR(buf, "VIEW COL");
34,800✔
227
    }
228
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
66,916,089✔
229
    if (withColCompress(pMeta->tableType) && pMeta->schemaExt) {
66,916,115✔
230
      if (i < pMeta->tableInfo.numOfColumns) {
66,788,257✔
231
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
63,255,674✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
63,255,647✔
233
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
63,255,675✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
63,255,648✔
235
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
63,255,675✔
236
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
63,255,648✔
237
      } else {
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,532,584✔
239
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
3,532,584✔
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,532,584✔
241
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
3,532,584✔
242
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,532,584✔
243
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
3,532,584✔
244
      }
245
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
127,858✔
246
      if (i < pMeta->numOfColRefs) {
67,690✔
247
        if (pMeta->colRef[i].hasRef) {
57,587✔
248
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
28,678✔
249

250
          TSlice strBuf = {0};
28,678✔
251
          sliceInit(&strBuf, refColName, sizeof(refColName));
28,678✔
252

253
          QRY_ERR_RET(sliceAppend(&strBuf, pMeta->colRef[i].refDbName, strlen(pMeta->colRef[i].refDbName)));
28,678✔
254
          QRY_ERR_RET(sliceAppend(&strBuf, ".", 1));
28,678✔
255
          QRY_ERR_RET(sliceAppend(&strBuf, pMeta->colRef[i].refTableName, strlen(pMeta->colRef[i].refTableName)));
28,678✔
256
          QRY_ERR_RET(sliceAppend(&strBuf, ".", 1));
28,678✔
257
          QRY_ERR_RET(sliceAppend(&strBuf, pMeta->colRef[i].refColName, strlen(pMeta->colRef[i].refColName)));
28,678✔
258
          STR_TO_VARSTR(buf, refColName);
28,678✔
259
        } else {
260
          STR_TO_VARSTR(buf, "");
28,909✔
261
        }
262
        COL_DATA_SET_VAL_AND_CHECK(pCol8, pBlock->info.rows, buf, false);
57,587✔
263
      } else {
264
        STR_TO_VARSTR(buf, "");
10,103✔
265
        COL_DATA_SET_VAL_AND_CHECK(pCol8, pBlock->info.rows, buf, false);
10,103✔
266
      }
267
    }
268

269
    fillTagCol = 0;
66,916,117✔
270

271
    ++(pBlock->info.rows);
66,916,117✔
272
  }
273
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
511,469✔
274
    STR_TO_VARSTR(buf, "tbname");
×
275
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
×
276
    STR_TO_VARSTR(buf, "VARCHAR");
×
277
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
×
278
    int32_t bytes = TSDB_TABLE_NAME_LEN - 1;
×
279
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
×
280
    STR_TO_VARSTR(buf, "TAG");
×
281
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
×
282
    ++(pBlock->info.rows);
×
283
  }
284
  if (pBlock->info.rows <= 0) {
511,468✔
285
    qError("no permission to view any columns");
×
286
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
287
  }
288
  return TSDB_CODE_SUCCESS;
511,468✔
289
}
290

291
static int32_t execDescribe(bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode) {
511,468✔
292
  SDescribeStmt* pDesc = (SDescribeStmt*)pStmt;
511,468✔
293
  if (NULL == pDesc || NULL == pDesc->pMeta) {
511,468✔
294
    return TSDB_CODE_INVALID_PARA;
×
295
  }
296
  int32_t numOfRows = TABLE_TOTAL_COL_NUM(pDesc->pMeta);
511,468✔
297

298
  SSDataBlock* pBlock = NULL;
511,441✔
299
  int32_t      code = buildDescResultDataBlock(&pBlock);
511,468✔
300
  if (TSDB_CODE_SUCCESS == code) {
511,468✔
301
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
511,468✔
302
  }
303
  if (TSDB_CODE_SUCCESS == code) {
511,468✔
304
    if (pDesc->pMeta) {
511,468✔
305
      if (hasRefCol(pDesc->pMeta->tableType)) {
511,468✔
306
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
8,565✔
307
      } else if (withColCompress(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
502,903✔
308
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
490,625✔
309
      } else {
310
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
12,278✔
311
      }
312
    } else {
313
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
314
    }
315
  }
316
  (void)blockDataDestroy(pBlock);
511,468✔
317
  return code;
511,468✔
318
}
319

320
static int32_t execResetQueryCache() { return catalogClearCache(); }
2,387,629✔
321

322
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
79,196✔
323
  QRY_PARAM_CHECK(pOutput);
79,196✔
324

325
  SSDataBlock* pBlock = NULL;
79,196✔
326
  int32_t      code = createDataBlock(&pBlock);
79,196✔
327
  if (code) {
79,196✔
328
    return code;
×
329
  }
330

331
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_COLS, 1);
79,196✔
332
  code = blockDataAppendColInfo(pBlock, &infoData);
79,196✔
333
  if (TSDB_CODE_SUCCESS == code) {
79,196✔
334
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_FIELD2_LEN, 2);
79,196✔
335
    code = blockDataAppendColInfo(pBlock, &infoData);
79,196✔
336
  }
337

338
  if (TSDB_CODE_SUCCESS == code) {
79,196✔
339
    *pOutput = pBlock;
79,196✔
340
  } else {
341
    (void)blockDataDestroy(pBlock);
×
342
  }
343
  return code;
79,196✔
344
}
345

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

368
  return v;
×
369
}
370

371
static int32_t buildRetension(SArray* pRetension, char** ppRetentions) {
79,196✔
372
  size_t size = taosArrayGetSize(pRetension);
79,196✔
373
  if (size == 0) {
79,196✔
374
    *ppRetentions = NULL;
79,196✔
375
    return TSDB_CODE_SUCCESS;
79,196✔
376
  }
377

378
  const int lMaxLen = 128;
×
379
  char*     p1 = taosMemoryCalloc(1, lMaxLen);
×
380
  if (NULL == p1) {
×
381
    return terrno;
×
382
  }
383
  int32_t len = 0;
×
384

385
  for (int32_t i = 0; i < size; ++i) {
×
386
    SRetention* p = TARRAY_GET_ELEM(pRetension, i);
×
387
    int64_t     v1 = getValOfDiffPrecision(p->freqUnit, p->freq);
×
388
    int64_t     v2 = getValOfDiffPrecision(p->keepUnit, p->keep);
×
389
    if (i == 0) {
×
390
      len += snprintf(p1 + len, lMaxLen - len, "-:%" PRId64 "%c", v2, p->keepUnit);
×
391
    } else {
392
      len += snprintf(p1 + len, lMaxLen - len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit);
×
393
    }
394

395
    if (i < size - 1) {
×
396
      len += snprintf(p1 + len, lMaxLen - len, ",");
×
397
    }
398
  }
399

400
  *ppRetentions = p1;
×
401
  return TSDB_CODE_SUCCESS;
×
402
}
403

404
static const char* cacheModelStr(int8_t cacheModel) {
78,752✔
405
  switch (cacheModel) {
78,752✔
406
    case TSDB_CACHE_MODEL_NONE:
78,752✔
407
      return TSDB_CACHE_MODEL_NONE_STR;
78,752✔
408
    case TSDB_CACHE_MODEL_LAST_ROW:
×
409
      return TSDB_CACHE_MODEL_LAST_ROW_STR;
×
410
    case TSDB_CACHE_MODEL_LAST_VALUE:
×
411
      return TSDB_CACHE_MODEL_LAST_VALUE_STR;
×
412
    case TSDB_CACHE_MODEL_BOTH:
×
413
      return TSDB_CACHE_MODEL_BOTH_STR;
×
414
    default:
×
415
      break;
×
416
  }
417
  return TSDB_CACHE_MODEL_NONE_STR;
×
418
}
419

420
static const char* encryptAlgorithmStr(int8_t encryptAlgorithm, char* algorithmsId) {
78,752✔
421
  if (algorithmsId[0] != '\0') {
78,752✔
422
    return algorithmsId;
×
423
  } else if (encryptAlgorithm != 0) {
78,752✔
424
    switch (encryptAlgorithm) {
×
425
      case TSDB_ENCRYPT_ALGO_NONE:
×
426
        return TSDB_ENCRYPT_ALGO_NONE_STR;
×
427
      case TSDB_ENCRYPT_ALGO_SM4:
×
428
        return TSDB_ENCRYPT_ALGO_SM4_STR;
×
429
      default:
×
430
        break;
×
431
    }
432
    return TSDB_CACHE_MODEL_NONE_STR;
×
433
  }
434

435
  return TSDB_CACHE_MODEL_NONE_STR;
78,752✔
436
}
437

438
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
5,505,395✔
439
  if (buffer == NULL || bufSize <= 0) {
5,505,395✔
440
    return 0;
×
441
  }
442
  int32_t len = 0;
5,505,395✔
443
  if (timeInMinutes % 1440 == 0) {
5,505,395✔
444
    int32_t days = timeInMinutes / 1440;
5,491,126✔
445
    len = snprintf(buffer, bufSize, "%dd", days);
5,491,126✔
446
  } else if (timeInMinutes % 60 == 0) {
14,269✔
447
    int32_t hours = timeInMinutes / 60;
2,327✔
448
    len = snprintf(buffer, bufSize, "%dh", hours);
2,327✔
449
  } else {
450
    len = snprintf(buffer, bufSize, "%dm", timeInMinutes);
11,942✔
451
  }
452
  return len;
5,505,395✔
453
}
454

455
static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, char* dbFName, SDbCfgInfo* pCfg) {
79,196✔
456
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
79,196✔
457
  pBlock->info.rows = 1;
79,196✔
458

459
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
79,196✔
460
  char             buf1[SHOW_CREATE_DB_RESULT_FIELD1_LEN] = {0};
79,196✔
461
  STR_TO_VARSTR(buf1, dbName);
79,196✔
462
  COL_DATA_SET_VAL_AND_CHECK(pCol1, 0, buf1, false);
79,196✔
463

464
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
79,196✔
465
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
79,196✔
466
  int32_t          len = 0;
79,196✔
467
  char*            prec = NULL;
79,196✔
468
  switch (pCfg->precision) {
79,196✔
469
    case TSDB_TIME_PRECISION_MILLI:
78,746✔
470
      prec = TSDB_TIME_PRECISION_MILLI_STR;
78,746✔
471
      break;
78,746✔
472
    case TSDB_TIME_PRECISION_MICRO:
220✔
473
      prec = TSDB_TIME_PRECISION_MICRO_STR;
220✔
474
      break;
220✔
475
    case TSDB_TIME_PRECISION_NANO:
230✔
476
      prec = TSDB_TIME_PRECISION_NANO_STR;
230✔
477
      break;
230✔
478
    default:
×
479
      prec = "none";
×
480
      break;
×
481
  }
482

483
  char* pRetentions = NULL;
79,196✔
484
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
79,196✔
485
  int32_t dbFNameLen = strlen(dbFName);
79,196✔
486
  int32_t hashPrefix = 0;
79,196✔
487
  if (pCfg->hashPrefix > 0) {
79,196✔
488
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
1,658✔
489
  } else if (pCfg->hashPrefix < 0) {
77,538✔
490
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
2,487✔
491
  }
492
  char durationStr[128] = {0};
79,196✔
493
  char keep0Str[128] = {0};
79,196✔
494
  char keep1Str[128] = {0};
79,196✔
495
  char keep2Str[128] = {0};
79,196✔
496
  char compactIntervalStr[13] = {0};
79,196✔
497
  char compactStartTimeStr[13] = {0};
79,196✔
498
  char compactEndTimeStr[13] = {0};
79,196✔
499

500
  int32_t lenDuration = formatDurationOrKeep(durationStr, sizeof(durationStr), pCfg->daysPerFile);
79,196✔
501
  int32_t lenKeep0 = formatDurationOrKeep(keep0Str, sizeof(keep0Str), pCfg->daysToKeep0);
79,196✔
502
  int32_t lenKeep1 = formatDurationOrKeep(keep1Str, sizeof(keep1Str), pCfg->daysToKeep1);
79,196✔
503
  int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pCfg->daysToKeep2);
79,196✔
504
  UNUSED(formatDurationOrKeep(compactIntervalStr, sizeof(compactIntervalStr), pCfg->compactInterval));
79,196✔
505
  UNUSED(formatDurationOrKeep(compactStartTimeStr, sizeof(compactStartTimeStr), pCfg->compactStartTime));
79,196✔
506
  UNUSED(formatDurationOrKeep(compactEndTimeStr, sizeof(compactEndTimeStr), pCfg->compactEndTime));
79,196✔
507

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

529

530
    if (pRetentions) {
78,752✔
531
      len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
532
                      " RETENTIONS %s", pRetentions);
533
    }
534
  }
535

536
  taosMemoryFree(pRetentions);
79,196✔
537

538
  (varDataLen(buf2)) = len;
79,196✔
539

540
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
79,196✔
541

542
  return TSDB_CODE_SUCCESS;
79,196✔
543
}
544

545
static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt, SRetrieveTableRsp** pRsp) {
79,196✔
546
  SSDataBlock* pBlock = NULL;
79,196✔
547
  int32_t      code = buildCreateDBResultDataBlock(&pBlock);
79,196✔
548
  if (TSDB_CODE_SUCCESS == code) {
79,196✔
549
    code = setCreateDBResultIntoDataBlock(pBlock, pStmt->dbName, pStmt->dbFName, pStmt->pCfg);
79,196✔
550
  }
551
  if (TSDB_CODE_SUCCESS == code) {
79,196✔
552
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_DB_RESULT_COLS, pRsp);
79,196✔
553
  }
554
  (void)blockDataDestroy(pBlock);
79,196✔
555
  return code;
79,196✔
556
}
557

558
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
116,295✔
559
  QRY_PARAM_CHECK(pOutput);
116,295✔
560

561
  SSDataBlock* pBlock = NULL;
116,295✔
562
  int32_t      code = createDataBlock(&pBlock);
116,295✔
563
  if (code) {
116,295✔
564
    return code;
×
565
  }
566

567
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD1_LEN, 1);
116,295✔
568
  code = blockDataAppendColInfo(pBlock, &infoData);
116,295✔
569
  if (TSDB_CODE_SUCCESS == code) {
116,295✔
570
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD2_LEN, 2);
116,295✔
571
    code = blockDataAppendColInfo(pBlock, &infoData);
116,295✔
572
  }
573

574
  if (TSDB_CODE_SUCCESS == code) {
116,295✔
575
    *pOutput = pBlock;
116,295✔
576
  } else {
577
    (void)blockDataDestroy(pBlock);
×
578
  }
579
  return code;
116,295✔
580
}
581

582
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
7,362✔
583
  QRY_PARAM_CHECK(pOutput);
7,362✔
584

585
  SSDataBlock* pBlock = NULL;
7,362✔
586
  int32_t      code = createDataBlock(&pBlock);
7,362✔
587
  if (code) {
7,362✔
588
    return code;
×
589
  }
590

591
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD1_LEN, 1);
7,362✔
592
  code = blockDataAppendColInfo(pBlock, &infoData);
7,362✔
593
  if (TSDB_CODE_SUCCESS == code) {
7,362✔
594
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD2_LEN, 2);
7,362✔
595
    code = blockDataAppendColInfo(pBlock, &infoData);
7,362✔
596
  }
597

598
  if (TSDB_CODE_SUCCESS == code) {
7,362✔
599
    *pOutput = pBlock;
7,362✔
600
  } else {
601
    (void)blockDataDestroy(pBlock);
×
602
  }
603
  return code;
7,362✔
604
}
605

606
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
63,671✔
607
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
63,671✔
608
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
404,854✔
609
    SSchema* pSchema = pCfg->pSchemas + i;
341,183✔
610
    SColRef* pRef = pCfg->pColRefs ? pCfg->pColRefs + i : NULL;
341,183✔
611
#define LTYPE_LEN                                    \
612
  (32 + 60 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN + \
613
   10)  // 60 byte for compress info, TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN for column ref
614
    char type[LTYPE_LEN];
341,183✔
615
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
341,183✔
616
    int typeLen = strlen(type);
341,183✔
617
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
341,183✔
618
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
304,046✔
619
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
40,552✔
620
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
300,631✔
621
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
12,726✔
622
                          (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
12,726✔
623
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
287,905✔
624
      uint8_t precision = 0, scale = 0;
24,440✔
625
      if (pCfg->pSchemaExt) {
24,440✔
626
        decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
24,440✔
627
      } else if ((((uint32_t)pSchema->bytes) >> 24) != 0) {
×
628
        int32_t bytes = pSchema->bytes;
×
629
        extractDecimalTypeInfoFromBytes(&bytes, &precision, &scale);
×
630
      }
631
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
24,440✔
632
    }
633

634
    if (tsShowFullCreateTableColumn && pCfg->pSchemaExt && pCfg->pSchemaExt[i].compress != 0) {
341,183✔
635
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, " ENCODE \'%s\'",
25,712✔
636
                          columnEncodeStr(COMPRESS_L1_TYPE_U32(pCfg->pSchemaExt[i].compress)));
25,712✔
637
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, " COMPRESS \'%s\'",
25,712✔
638
                          columnCompressStr(COMPRESS_L2_TYPE_U32(pCfg->pSchemaExt[i].compress)));
25,712✔
639
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, " LEVEL \'%s\'",
25,712✔
640
                          columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pCfg->pSchemaExt[i].compress)));
25,712✔
641
    }
642

643
    if (hasRefCol(pCfg->tableType) && pRef && pRef->hasRef) {
341,183✔
644
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, " FROM `%s`", pRef->refDbName);
28,635✔
645
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
28,635✔
646
      typeLen +=
28,635✔
647
          snprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
28,635✔
648
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
28,635✔
649
      typeLen += snprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
28,635✔
650
    }
651

652
    if (!(pSchema->flags & COL_IS_KEY)) {
341,183✔
653
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
330,628✔
654
                       "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
330,628✔
655
    } else {
656
      char* pk = "COMPOSITE KEY";
10,555✔
657
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
10,555✔
658
                       "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
10,555✔
659
    }
660
  }
661
}
63,671✔
662

663
static void appendColRefFields(char* buf, int32_t* len, STableCfg* pCfg) {
6,169✔
664
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
6,169✔
665
  bool firstRef = true;
6,169✔
666
  for (int32_t i = 1; i < pCfg->numOfColumns; ++i) {
68,710✔
667
    SSchema* pSchema = pCfg->pSchemas + i;
62,541✔
668
    SColRef* pRef = pCfg->pColRefs + i;
62,541✔
669
    char     type[TSDB_COL_NAME_LEN + 10 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN];
62,541✔
670
    int      typeLen = 0;
62,541✔
671

672
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
62,541✔
673
      typeLen += snprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
37,600✔
674
      typeLen += snprintf(type + typeLen, sizeof(type) - typeLen, ".");
37,600✔
675
      typeLen +=
37,600✔
676
          snprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
37,600✔
677
      typeLen += snprintf(type + typeLen, sizeof(type) - typeLen, ".");
37,600✔
678
      typeLen +=
37,600✔
679
          snprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
37,600✔
680
    } else {
681
      continue;
24,941✔
682
    }
683

684
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
37,600✔
685
                      "%s`%s` %s", (!firstRef ? ", " : " ("), expandIdentifier(pSchema->name, expandName), type);
37,600✔
686
    firstRef = false;
37,600✔
687
  }
688
  if (!firstRef) {
6,169✔
689
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
5,470✔
690
  }
691
}
6,169✔
692

693
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
42,026✔
694
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
42,026✔
695
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
149,598✔
696
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
107,572✔
697
    char     type[32];
107,572✔
698
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
107,572✔
699
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
107,572✔
700
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
88,637✔
701
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
20,811✔
702
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
20,811✔
703
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
86,761✔
704
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
4,599✔
705
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
4,599✔
706
    }
707

708
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
107,572✔
709
                     "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
107,572✔
710
  }
711
}
42,026✔
712

713
static void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
43,578✔
714
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
43,578✔
715
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
119,073✔
716
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
75,495✔
717
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
75,495✔
718
                     "%s`%s`", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName));
75,495✔
719
  }
720
}
43,578✔
721

722
static int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg, void* charsetCxt) {
43,578✔
723
  int32_t code = TSDB_CODE_SUCCESS;
43,578✔
724
  SArray* pTagVals = NULL;
43,578✔
725
  STag*   pTag = (STag*)pCfg->pTags;
43,578✔
726

727
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
43,578✔
728
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
729
    return TSDB_CODE_APP_ERROR;
×
730
  }
731

732
  if (tTagIsJson(pTag)) {
43,578✔
733
    char* pJson = NULL;
2,780✔
734
    parseTagDatatoJson(pTag, &pJson, charsetCxt);
2,780✔
735
    if (NULL == pJson) {
2,780✔
736
      qError("failed to parse tag to json, pJson is NULL");
×
737
      return terrno;
×
738
    }
739
    char* escapedJson = taosMemCalloc(sizeof(char), strlen(pJson) * 2 + 1);  // taosMemoryAlloc(strlen(pJson) * 2 + 1);
2,780✔
740
    if (escapedJson) {
2,780✔
741
      char* writer = escapedJson;
2,780✔
742
      for (char* reader = pJson; *reader; ++reader) {
55,600✔
743
        if (*reader == '\'') {
52,820✔
744
          *writer++ = '\'';  // Escape single quote by doubling it
556✔
745
        }
746
        *writer++ = *reader;
52,820✔
747
      }
748
      *writer = '\0';
2,780✔
749

750
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,780✔
751
                       "'%s'", escapedJson);
752
      taosMemoryFree(escapedJson);
2,780✔
753
    } else {
754
      taosMemoryFree(pJson);
×
755
      return terrno;
×
756
    }
757
    taosMemoryFree(pJson);
2,780✔
758

759
    return TSDB_CODE_SUCCESS;
2,780✔
760
  }
761

762
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
40,798✔
763
  int16_t valueNum = taosArrayGetSize(pTagVals);
40,798✔
764
  int32_t num = 0;
40,798✔
765
  int32_t j = 0;
40,798✔
766
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
113,513✔
767
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
72,715✔
768
    if (i > 0) {
72,715✔
769
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
31,917✔
770
                       ", ");
771
    }
772

773
    if (pCfg->pTagRefs && i < pCfg->numOfTagRefs) {
72,715✔
774
      SColRef* pTagRef = pCfg->pTagRefs + i;
×
775
      if (pTagRef->hasRef) {
×
776
        char expandRefTable[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
×
777
        char expandRefCol[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
×
778
        *len += snprintf(buf + VARSTR_HEADER_SIZE + *len,
×
779
                         SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
780
                         "FROM `%s`.`%s`.`%s`", pTagRef->refDbName,
×
781
                         expandIdentifier(pTagRef->refTableName, expandRefTable),
×
782
                         expandIdentifier(pTagRef->refColName, expandRefCol));
×
783
        continue;
×
784
      }
785
    }
786

787
    if (j >= valueNum) {
72,715✔
788
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
17,237✔
789
                       "NULL");
790
      continue;
17,237✔
791
    }
792

793
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
55,478✔
794
    if (pSchema->colId > pTagVal->cid) {
55,478✔
795
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
796
      code = TSDB_CODE_APP_ERROR;
×
797
      TAOS_CHECK_ERRNO(code);
×
798
    } else if (pSchema->colId == pTagVal->cid) {
55,478✔
799
      char    type = pTagVal->type;
53,772✔
800
      int32_t tlen = 0;
53,772✔
801

802
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
53,772✔
803
      if (leftSize <= 0) {
53,772✔
804
        qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
×
805
        code = TSDB_CODE_APP_ERROR;
×
806
        TAOS_CHECK_ERRNO(code);
×
807
      }
808
      if (IS_VAR_DATA_TYPE(type)) {
53,772✔
809
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
13,770✔
810
        TAOS_CHECK_ERRNO(code);
13,770✔
811
      } else {
812
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
40,002✔
813
                               &tlen);
814
        TAOS_CHECK_ERRNO(code);
40,002✔
815
      }
816
      *len += tlen;
53,772✔
817
      j++;
53,772✔
818
    } else {
819
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,706✔
820
                       "NULL");
821
    }
822
  }
823
_exit:
40,798✔
824
  taosArrayDestroy(pTagVals);
40,798✔
825

826
  return code;
40,798✔
827
}
828

829
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
96,334✔
830
  if (pCfg->commentLen > 0) {
96,334✔
831
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
832
                     " COMMENT '%s'", pCfg->pComment);
833
  } else if (0 == pCfg->commentLen) {
96,334✔
834
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,382✔
835
                     " COMMENT ''");
836
  }
837

838
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
96,334✔
839
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
840
                     " WATERMARK %" PRId64 "a", pCfg->watermark1);
841
    if (pCfg->watermark2 > 0) {
×
842
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
843
                       ", %" PRId64 "a", pCfg->watermark2);
844
    }
845
  }
846

847
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
96,334✔
848
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
849
                     " MAX_DELAY %" PRId64 "a", pCfg->delay1);
850
    if (pCfg->delay2 > 0) {
×
851
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
852
                       ", %" PRId64 "a", pCfg->delay2);
853
    }
854
  }
855

856
  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
96,334✔
857
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
96,334✔
858
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
859
                     " ROLLUP(");
860
    for (int32_t i = 0; i < funcNum; ++i) {
×
861
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
×
862
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
863
                       "%s%s", ((i > 0) ? ", " : ""), pFunc);
864
    }
865
    *len +=
×
866
        snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
×
867
  }
868

869
  if (pCfg->ttl > 0) {
96,334✔
870
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
871
                     " TTL %d", pCfg->ttl);
872
  }
873

874
  if (pCfg->keep > 0) {
96,334✔
875
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
3,022✔
876
                     " KEEP %dm", pCfg->keep);
877
  }
878

879
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
96,334✔
880
    int32_t nSma = 0;
58,925✔
881
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
345,451✔
882
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
286,526✔
883
        ++nSma;
286,040✔
884
      }
885
    }
886

887
    if (nSma < pCfg->numOfColumns && nSma > 0) {
58,925✔
888
      bool smaOn = false;
×
889
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
890
                       " SMA(");
891
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
892
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
893
          if (smaOn) {
×
894
            *len += snprintf(buf + VARSTR_HEADER_SIZE + *len,
×
895
                             SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ",`%s`",
×
896
                             (pCfg->pSchemas + i)->name);
×
897
          } else {
898
            smaOn = true;
×
899
            *len += snprintf(buf + VARSTR_HEADER_SIZE + *len,
×
900
                             SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), "`%s`",
×
901
                             (pCfg->pSchemas + i)->name);
×
902
          }
903
        }
904
      }
905
      *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, ")");
×
906
    }
907
  }
908

909
  if (pCfg->virtualStb) {
96,334✔
910
    *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
4,063✔
911
                     " VIRTUAL %d", pCfg->virtualStb);
4,063✔
912
  }
913

914
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
96,334✔
915
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
42,026✔
916
                      " SECURE_DELETE %d", pCfg->secureDelete);
42,026✔
917
  }
918
}
96,334✔
919

920
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
113,909✔
921
                                              void* charsetCxt) {
922
  int32_t code = TSDB_CODE_SUCCESS;
113,909✔
923
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
113,909✔
924
  pBlock->info.rows = 1;
113,909✔
925

926
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
113,909✔
927
  char             buf1[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1)] = {0};
113,909✔
928
  STR_TO_VARSTR(buf1, tbName);
113,909✔
929
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
113,909✔
930

931
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
113,909✔
932
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
113,909✔
933
  if (NULL == buf2) {
113,909✔
934
    QRY_ERR_RET(terrno);
×
935
  }
936

937
  int32_t len = 0;
113,909✔
938

939
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
113,909✔
940
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
42,026✔
941
                    "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
942
    appendColumnFields(buf2, &len, pCfg);
42,026✔
943
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
42,026✔
944
                    ") TAGS (");
945
    appendTagFields(buf2, &len, pCfg);
42,026✔
946
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
84,052✔
947
                    ") SECURITY_LEVEL %d", pCfg->securityLevel);
42,026✔
948
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
42,026✔
949
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
71,883✔
950
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
37,409✔
951
                    "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
952
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
37,409✔
953
                    "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
37,409✔
954
    appendTagNameFields(buf2, &len, pCfg);
37,409✔
955
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
37,409✔
956
                    ") TAGS (");
957
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
37,409✔
958
    TAOS_CHECK_ERRNO(code);
37,409✔
959
    len +=
37,409✔
960
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
37,409✔
961
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
37,409✔
962
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
34,474✔
963
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
16,899✔
964
                    "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
965
    appendColumnFields(buf2, &len, pCfg);
16,899✔
966
    len +=
16,899✔
967
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
16,899✔
968
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
16,899✔
969
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
17,575✔
970
    len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
4,746✔
971
                    "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
972
    appendColumnFields(buf2, &len, pCfg);
4,746✔
973
    len +=
4,746✔
974
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
4,746✔
975
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
12,829✔
976
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
6,169✔
977
                     "CREATE VTABLE `%s`", expandIdentifier(tbName, buf1));
978
    appendColRefFields(buf2, &len, pCfg);
6,169✔
979
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
6,169✔
980
                    " USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
6,169✔
981
    appendTagNameFields(buf2, &len, pCfg);
6,169✔
982
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
6,169✔
983
                    ") TAGS (");
984
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
6,169✔
985
    TAOS_CHECK_ERRNO(code);
6,169✔
986
    len +=
6,169✔
987
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
6,169✔
988
  }
989

990
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
113,909✔
991

992
  code = colDataSetVal(pCol2, 0, buf2, false);
113,909✔
993
  TAOS_CHECK_ERRNO(code);
113,909✔
994

995
_exit:
113,909✔
996
  taosMemoryFree(buf2);
113,909✔
997

998
  return code;
113,909✔
999
}
1000

1001
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
7,362✔
1002
  int32_t code = 0;
7,362✔
1003
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
7,362✔
1004
  pBlock->info.rows = 1;
7,362✔
1005

1006
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
7,362✔
1007
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
7,362✔
1008
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
7,362✔
1009
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
7,362✔
1010
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
7,362✔
1011

1012
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
7,362✔
1013
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
7,362✔
1014
  if (NULL == buf2) {
7,362✔
1015
    return terrno;
×
1016
  }
1017

1018
  SViewMeta* pMeta = pStmt->pViewMeta;
7,362✔
1019
  if (NULL == pMeta) {
7,362✔
1020
    qError("exception: view meta is null");
×
1021
    taosMemoryFree(buf2);
×
1022
    return TSDB_CODE_APP_ERROR;
×
1023
  }
1024
  snprintf(varDataVal(buf2), SHOW_CREATE_VIEW_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE VIEW `%s`.`%s` AS %s",
7,362✔
1025
           pStmt->dbName, pStmt->viewName, pMeta->querySql);
7,362✔
1026
  int32_t len = strlen(varDataVal(buf2));
7,362✔
1027
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
7,362✔
1028
  code = colDataSetVal(pCol2, 0, buf2, false);
7,362✔
1029
  taosMemoryFree(buf2);
7,362✔
1030

1031
  return code;
7,362✔
1032
}
1033

1034
extern const char* fmGetFuncName(int32_t funcId);
1035
static int32_t setCreateRsmaResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateRsmaStmt* pStmt) {
2,386✔
1036
  int32_t       code = 0, lino = 0;
2,386✔
1037
  char*         buf2 = NULL;
2,386✔
1038
  SRsmaInfoRsp* pMeta = pStmt->pRsmaMeta;
2,386✔
1039

1040
  if (pMeta->nFuncs != pMeta->nColNames) {
2,386✔
1041
    qError("exception: rsma meta is invalid, nFuncs:%d != nColNames:%d", pMeta->nFuncs, pMeta->nColNames);
×
1042
    return TSDB_CODE_APP_ERROR;
×
1043
  }
1044

1045
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, 1));
2,386✔
1046
  pBlock->info.rows = 1;
2,386✔
1047

1048
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
2,386✔
1049
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1] = {0};
2,386✔
1050
  snprintf(varDataVal(buf1), TSDB_TABLE_FNAME_LEN + 4, "`%s`", expandIdentifier(pStmt->rsmaName, buf1));
2,386✔
1051
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
2,386✔
1052
  TAOS_CHECK_EXIT(colDataSetVal(pCol1, 0, buf1, false));
2,386✔
1053

1054
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
2,386✔
1055
  if (!(buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN))) {
2,386✔
1056
    return terrno;
×
1057
  }
1058

1059
  SName name = {0};
2,386✔
1060
  TAOS_CHECK_EXIT(tNameFromString(&name, pMeta->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
2,386✔
1061

1062
  int32_t len = 0;
2,386✔
1063
  len += tsnprintf(varDataVal(buf2), SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
4,772✔
1064
                       "CREATE RSMA `%s` ON `%s`.`%s` FUNCTION(", expandIdentifier(pStmt->rsmaName, buf1),
2,386✔
1065
                       expandIdentifier(name.dbname, buf1), expandIdentifier(name.tname, buf1));
1066
  for (int32_t i = 0; i < pMeta->nFuncs; ++i) {
15,150✔
1067
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
25,528✔
1068
                         "%s%s(`%s`)", (i > 0) ? "," : "", fmGetFuncName(pMeta->funcIds[i]),
12,764✔
1069
                         expandIdentifier(*(char**)TARRAY_GET_ELEM(pMeta->colNames, i), buf1));
12,764✔
1070
  }
1071
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
2,386✔
1072
                       ") INTERVAL(%d%c", pMeta->interval[0], pMeta->intervalUnit);
2,386✔
1073
  if (pMeta->interval[1] > 0) {
2,386✔
1074
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ",%d%c",
2,386✔
1075
                         pMeta->interval[1], pMeta->intervalUnit);
2,386✔
1076
  }
1077
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
2,386✔
1078
  varDataLen(buf2) = len;
2,386✔
1079
  code = colDataSetVal(pCol2, 0, buf2, false);
2,386✔
1080
_exit:
2,386✔
1081
  taosMemoryFree(buf2);
2,386✔
1082
  return code;
2,386✔
1083
}
1084

1085
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
103,590✔
1086
  SSDataBlock* pBlock = NULL;
103,590✔
1087
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
103,590✔
1088
  if (TSDB_CODE_SUCCESS == code) {
103,590✔
1089
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
103,590✔
1090
  }
1091
  if (TSDB_CODE_SUCCESS == code) {
103,590✔
1092
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
103,590✔
1093
  }
1094
  (void)blockDataDestroy(pBlock);
103,590✔
1095
  return code;
103,590✔
1096
}
1097

1098
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
10,319✔
1099
  SSDataBlock* pBlock = NULL;
10,319✔
1100
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
10,319✔
1101
  if (TSDB_CODE_SUCCESS == code) {
10,319✔
1102
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
10,319✔
1103
  }
1104
  if (TSDB_CODE_SUCCESS == code) {
10,319✔
1105
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
10,319✔
1106
  }
1107
  (void)blockDataDestroy(pBlock);
10,319✔
1108
  return code;
10,319✔
1109
}
1110

1111
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
3,456✔
1112
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
3,456✔
1113
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
3,456✔
1114
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
222✔
1115
    return terrno;
222✔
1116
  }
1117

1118
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
3,234✔
1119
}
1120

1121
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
676,351✔
1122
  int32_t code = 0;
676,351✔
1123

1124
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
676,351✔
1125
    taosResetLog();
68✔
1126
    cfgDumpCfg(tsCfg, 0, false);
68✔
1127
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
676,283✔
1128
    int32_t tmp = 0;
2,856✔
1129
    code = taosStr2int32(value, &tmp);
2,856✔
1130
    if (code) {
2,856✔
1131
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1132
      return code;
×
1133
    }
1134
    code = schedulerUpdatePolicy(tmp);
2,856✔
1135
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
673,427✔
1136
    int32_t tmp = 0;
×
1137
    code = taosStr2int32(value, &tmp);
×
1138
    if (code) {
×
1139
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1140
      return code;
×
1141
    }
1142
    code = schedulerEnableReSchedule(tmp != 0);
×
1143
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
673,427✔
1144
    code = ctgdHandleDbgCommand(value);
×
1145
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
673,427✔
1146
    code = taosMemoryDbgInit();
×
1147
    if (code) {
×
1148
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
1149
      return code;
×
1150
    }
1151
    tsAsyncLog = false;
×
1152
    qInfo("memory dbg enabled");
×
1153
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
673,427✔
1154
    code = taosMemoryDbgInitRestore();
×
1155
    if (code) {
×
1156
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
1157
      return code;
×
1158
    }
1159
    qInfo("memory dbg disabled");
×
1160
  } else {
1161
    goto _return;
673,427✔
1162
  }
1163

1164
  *processed = true;
2,924✔
1165

1166
_return:
676,351✔
1167

1168
  if (code) {
676,351✔
1169
    terrno = code;
×
1170
  }
1171

1172
  return code;
676,351✔
1173
}
1174

1175
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
676,351✔
1176
  bool processed = false;
676,351✔
1177

1178
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
676,351✔
1179
    return terrno;
×
1180
  }
1181

1182
  if (processed) {
676,351✔
1183
    goto _return;
2,924✔
1184
  }
1185

1186
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
673,427✔
1187
    return terrno;
2,141✔
1188
  }
1189

1190
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
671,286✔
1191
    return terrno;
×
1192
  }
1193

1194
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
671,286✔
1195

1196
_return:
674,210✔
1197

1198
  return TSDB_CODE_SUCCESS;
674,210✔
1199
}
1200

1201
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
18,909✔
1202
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
18,909✔
1203
  if (NULL == pBlock) {
18,909✔
1204
    return terrno;
×
1205
  }
1206

1207
  pBlock->info.hasVarCol = true;
18,909✔
1208

1209
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
18,909✔
1210
  if (NULL == pBlock->pDataBlock) {
18,909✔
1211
    taosMemoryFree(pBlock);
×
1212
    return terrno;
×
1213
  }
1214

1215
  SColumnInfoData infoData = {0};
18,909✔
1216

1217
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
18,909✔
1218
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
18,909✔
1219
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
37,818✔
1220
    goto _exit;
×
1221
  }
1222

1223
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
18,909✔
1224
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
18,909✔
1225
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
37,818✔
1226
    goto _exit;
×
1227
  }
1228

1229
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
18,909✔
1230
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
18,909✔
1231
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
37,818✔
1232
    goto _exit;
×
1233
  }
1234

1235
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
18,909✔
1236
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
18,909✔
1237
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
37,818✔
1238
    goto _exit;
×
1239
  }
1240

1241
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
18,909✔
1242
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
18,909✔
1243
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
37,818✔
1244
    goto _exit;
×
1245
  }
1246

1247
  *pOutput = pBlock;
18,909✔
1248

1249
_exit:
18,909✔
1250
  if (terrno != TSDB_CODE_SUCCESS) {
18,909✔
1251
    taosArrayDestroy(pBlock->pDataBlock);
×
1252
    taosMemoryFree(pBlock);
×
1253
  }
1254
  return terrno;
18,909✔
1255
}
1256

1257
static int32_t execShowLocalVariables(SShowStmt* pStmt, uint8_t showVarPrivMask, SRetrieveTableRsp** pRsp) {
18,909✔
1258
  SSDataBlock* pBlock = NULL;
18,909✔
1259
  char*        likePattern = NULL;
18,909✔
1260
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
18,909✔
1261
  if (TSDB_CODE_SUCCESS == code) {
18,909✔
1262
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
18,909✔
1263
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
4,115✔
1264
    }
1265
  }
1266
  if (TSDB_CODE_SUCCESS == code) {
18,909✔
1267
    code = dumpConfToDataBlock(pBlock, 0, likePattern, showVarPrivMask);
18,909✔
1268
  }
1269
  if (TSDB_CODE_SUCCESS == code) {
18,909✔
1270
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
18,909✔
1271
  }
1272
  (void)blockDataDestroy(pBlock);
18,909✔
1273
  return code;
18,909✔
1274
}
1275

1276
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
3,078,823✔
1277
  QRY_PARAM_CHECK(pOutput);
3,078,823✔
1278

1279
  SSDataBlock* pBlock = NULL;
3,078,823✔
1280
  int32_t      code = createDataBlock(&pBlock);
3,078,823✔
1281
  if (code) {
3,078,997✔
1282
    return code;
×
1283
  }
1284

1285
  SNode* pProj = NULL;
3,078,997✔
1286
  FOREACH(pProj, pProjects) {
7,770,170✔
1287
    SExprNode*      pExpr = (SExprNode*)pProj;
4,691,173✔
1288
    SColumnInfoData infoData = {0};
4,691,173✔
1289
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
4,691,173✔
1290
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1291
      infoData.info.bytes = 0;
×
1292
    } else {
1293
      infoData.info.type = pExpr->resType.type;
4,691,173✔
1294
      infoData.info.bytes = pExpr->resType.bytes;
4,691,173✔
1295
      infoData.info.precision = pExpr->resType.precision;
4,691,173✔
1296
      infoData.info.scale = pExpr->resType.scale;
4,691,173✔
1297
    }
1298
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
4,691,173✔
1299
  }
1300

1301
  *pOutput = pBlock;
3,078,997✔
1302
  return code;
3,078,997✔
1303
}
1304

1305
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
3,078,997✔
1306
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
3,078,997✔
1307

1308
  int32_t index = 0;
3,078,997✔
1309
  SNode*  pProj = NULL;
3,078,997✔
1310
  FOREACH(pProj, pProjects) {
7,770,170✔
1311
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
4,691,173✔
1312
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1313
    } else {
1314
      if (((SValueNode*)pProj)->isNull) {
4,691,173✔
1315
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
116,531✔
1316
      } else {
1317
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
4,574,642✔
1318
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1319
      }
1320
    }
1321
  }
1322

1323
  pBlock->info.rows = 1;
3,078,997✔
1324
  return TSDB_CODE_SUCCESS;
3,078,997✔
1325
}
1326

1327
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
3,078,823✔
1328
  SSDataBlock* pBlock = NULL;
3,078,823✔
1329
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
3,078,823✔
1330
  if (TSDB_CODE_SUCCESS == code) {
3,078,997✔
1331
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
3,078,997✔
1332
  }
1333
  if (TSDB_CODE_SUCCESS == code) {
3,078,997✔
1334
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
3,078,997✔
1335
  }
1336
  (void)blockDataDestroy(pBlock);
3,078,823✔
1337
  return code;
3,078,823✔
1338
}
1339

1340
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
7,362✔
1341
  SSDataBlock* pBlock = NULL;
7,362✔
1342
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
7,362✔
1343
  if (TSDB_CODE_SUCCESS == code) {
7,362✔
1344
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
7,362✔
1345
  }
1346
  if (TSDB_CODE_SUCCESS == code) {
7,362✔
1347
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
7,362✔
1348
  }
1349
  (void)blockDataDestroy(pBlock);
7,362✔
1350
  return code;
7,362✔
1351
}
1352

1353
static int32_t execShowCreateRsma(SShowCreateRsmaStmt* pStmt, SRetrieveTableRsp** pRsp) {
2,386✔
1354
  SSDataBlock* pBlock = NULL;
2,386✔
1355
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
2,386✔
1356
  if (TSDB_CODE_SUCCESS == code) {
2,386✔
1357
    code = setCreateRsmaResultIntoDataBlock(pBlock, pStmt);
2,386✔
1358
  }
1359
  if (TSDB_CODE_SUCCESS == code) {
2,386✔
1360
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
2,386✔
1361
  }
1362
  (void)blockDataDestroy(pBlock);
2,386✔
1363
  return code;
2,386✔
1364
}
1365

1366
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, uint8_t showVarPrivMask, SNode* pStmt,
6,876,254✔
1367
                     SRetrieveTableRsp** pRsp, int8_t biMode, void* charsetCxt) {
1368
  switch (nodeType(pStmt)) {
6,876,254✔
1369
    case QUERY_NODE_DESCRIBE_STMT:
511,441✔
1370
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
511,441✔
1371
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
2,387,629✔
1372
      return execResetQueryCache();
2,387,629✔
1373
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
79,196✔
1374
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
79,196✔
1375
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
100,356✔
1376
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
100,356✔
1377
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
10,319✔
1378
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
10,319✔
1379
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
3,456✔
1380
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
3,456✔
1381
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
7,362✔
1382
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
7,362✔
1383
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
2,386✔
1384
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
2,386✔
1385
    case QUERY_NODE_ALTER_LOCAL_STMT:
676,351✔
1386
      return execAlterLocal((SAlterLocalStmt*)pStmt);
676,351✔
1387
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
18,909✔
1388
      return execShowLocalVariables((SShowStmt*)pStmt, showVarPrivMask, pRsp);
18,909✔
1389
    case QUERY_NODE_SELECT_STMT:
3,079,024✔
1390
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
3,079,024✔
1391
    default:
×
1392
      break;
×
1393
  }
1394
  return TSDB_CODE_FAILED;
×
1395
}
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