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

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

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

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

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

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

37
extern SConfig* tsCfg;
38

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

50
  (*pRsp)->useconds = 0;
3,712,292✔
51
  (*pRsp)->completed = 1;
3,712,292✔
52
  (*pRsp)->precision = 0;
3,712,292✔
53
  (*pRsp)->compressed = 0;
3,712,292✔
54

55
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
3,712,292✔
56
  (*pRsp)->numOfCols = htonl(numOfCols);
3,712,292✔
57

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

69
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
3,712,292✔
70
  (*pRsp)->payloadLen = htonl(payloadLen);
3,712,292✔
71
  (*pRsp)->compLen = htonl(payloadLen);
3,712,292✔
72

73
  return TSDB_CODE_SUCCESS;
3,712,292✔
74
}
75

76
static int32_t getSchemaBytes(const SSchema* pSchema) {
69,022,409✔
77
  switch (pSchema->type) {
69,022,409✔
78
    case TSDB_DATA_TYPE_BINARY:
2,771,856✔
79
    case TSDB_DATA_TYPE_VARBINARY:
80
    case TSDB_DATA_TYPE_GEOMETRY:
81
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
2,771,856✔
82
    case TSDB_DATA_TYPE_NCHAR:
4,840,954✔
83
    case TSDB_DATA_TYPE_JSON:
84
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
4,840,954✔
85
    default:
61,409,599✔
86
      return pSchema->bytes;
61,409,599✔
87
  }
88
}
89

90
static const char* expandIdentifier(const char* name, char* output) {
776,832✔
91
  if (NULL == name) return "";
776,832!
92
  bool containsEscapeChar = false;
776,832✔
93
  for (const char* p = name; *p != '\0'; ++p) {
4,039,847✔
94
    if (*p == TS_ESCAPE_CHAR) {
3,290,375✔
95
      containsEscapeChar = true;
27,360✔
96
      break;
27,360✔
97
    }
98
  }
99
  if (!containsEscapeChar) return name;
776,832✔
100
  if (NULL == output) return "";
27,360!
101
  char* out_ptr = output;
27,360✔
102
  for (const char* src = name; *src != '\0'; ++src) {
168,416✔
103
    if (*src == TS_ESCAPE_CHAR) {
141,056✔
104
      *out_ptr++ = TS_ESCAPE_CHAR;
68,704✔
105
      *out_ptr++ = TS_ESCAPE_CHAR;
68,704✔
106
    } else {
107
      *out_ptr++ = *src;
72,352✔
108
    }
109
  }
110
  *out_ptr = '\0';
27,360✔
111
  return output;
27,360✔
112
}
113

114
static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) {
556,544✔
115
  QRY_PARAM_CHECK(pOutput);
556,544!
116

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

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

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

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

189
  if (hasRefCol(pMeta->tableType)) {
556,544✔
190
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
3,430✔
191
  }
192

193
  int32_t fillTagCol = 0;
556,544✔
194
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
556,544✔
195
  for (int32_t i = 0; i < numOfRows; ++i) {
69,578,953✔
196
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
69,022,409!
197
      continue;
×
198
    }
199
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
69,022,409!
200
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
69,022,409!
201

202
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
69,022,409!
203
      uint8_t prec = 0, scale = 0;
16,211✔
204
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
16,211✔
205
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
16,211✔
206
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
16,211✔
207
      varDataSetLen(buf, len);
16,211✔
208
    } else {
209
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
69,006,198!
210
    }
211
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
69,022,409!
212
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
69,022,409✔
213
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
69,022,409!
214
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
69,022,409✔
215
      if (i >= pMeta->tableInfo.numOfColumns) {
68,984,769✔
216
        STR_TO_VARSTR(buf, "TAG");
3,568,541!
217
        fillTagCol = 1;
3,568,541✔
218
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
65,416,228✔
219
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
12,962!
220
      } else {
221
        STR_TO_VARSTR(buf, "");
65,403,266!
222
      }
223
    } else {
224
      STR_TO_VARSTR(buf, "VIEW COL");
37,640!
225
    }
226
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
69,022,409!
227
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
69,022,409!
228
      if (i < pMeta->tableInfo.numOfColumns) {
68,862,537✔
229
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
65,300,494!
230
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
65,300,494!
231
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
65,300,494!
232
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
65,300,494!
233
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
65,300,494!
234
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
65,300,494!
235
      } else {
236
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,562,043!
237
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
3,562,043!
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,562,043!
239
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
3,562,043!
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
3,562,043!
241
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
3,562,043!
242
      }
243
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
159,872!
244
      if (i < pMeta->numOfColRefs) {
42,866✔
245
        if (pMeta->colRef[i].hasRef) {
36,368!
246
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
15,297✔
247
          strcat(refColName, pMeta->colRef[i].refDbName);
15,297!
248
          strcat(refColName, ".");
15,297!
249
          strcat(refColName, pMeta->colRef[i].refTableName);
15,297!
250
          strcat(refColName, ".");
15,297!
251
          strcat(refColName, pMeta->colRef[i].refColName);
15,297!
252
          STR_TO_VARSTR(buf, refColName);
15,297!
253
        } else {
254
          STR_TO_VARSTR(buf, "");
21,071!
255
        }
256
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
36,368!
257
      } else {
258
        STR_TO_VARSTR(buf, "");
6,498!
259
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
6,498!
260
      }
261
    }
262

263
    fillTagCol = 0;
69,022,409✔
264

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

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

292
  SSDataBlock* pBlock = NULL;
556,544✔
293
  int32_t      code = buildDescResultDataBlock(&pBlock);
556,544✔
294
  if (TSDB_CODE_SUCCESS == code) {
556,544!
295
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
556,544✔
296
  }
297
  if (TSDB_CODE_SUCCESS == code) {
556,544!
298
    if (pDesc->pMeta) {
556,544!
299
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
556,544!
300
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
533,582✔
301
      } else if (hasRefCol(pDesc->pMeta->tableType) && pDesc->pMeta->colRef) {
22,962!
302
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
3,430✔
303
      } else {
304
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
19,532✔
305
      }
306
    } else {
307
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
308
    }
309
  }
310
  (void)blockDataDestroy(pBlock);
556,544✔
311
  return code;
556,544✔
312
}
313

314
static int32_t execResetQueryCache() { return catalogClearCache(); }
2,428,679✔
315

316
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
35,355✔
317
  QRY_PARAM_CHECK(pOutput);
35,355!
318

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

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

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

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

362
  return v;
×
363
}
364

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

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

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

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

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

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

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

426
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
4,287,024✔
427
  if (buffer == NULL || bufSize <= 0) {
4,287,024!
428
    return 0;
×
429
  }
430
  int32_t len = 0;
4,287,024✔
431
  if (timeInMinutes % 1440 == 0) {
4,287,024✔
432
    int32_t days = timeInMinutes / 1440;
4,284,345✔
433
    len = tsnprintf(buffer, bufSize, "%dd", days);
4,284,345✔
434
  } else if (timeInMinutes % 60 == 0) {
2,679✔
435
    int32_t hours = timeInMinutes / 60;
991✔
436
    len = tsnprintf(buffer, bufSize, "%dh", hours);
991✔
437
  } else {
438
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
1,688✔
439
  }
440
  return len;
4,287,024✔
441
}
442

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

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

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

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

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

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

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

523
  taosMemoryFree(pRetentions);
35,355!
524

525
  (varDataLen(buf2)) = len;
35,355✔
526

527
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
35,355!
528

529
  return TSDB_CODE_SUCCESS;
35,355✔
530
}
531

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

545
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
104,415✔
546
  QRY_PARAM_CHECK(pOutput);
104,415!
547

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

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

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

569
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
8,938✔
570
  QRY_PARAM_CHECK(pOutput);
8,938!
571

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

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

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

593
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
65,732✔
594
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
65,732✔
595
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
454,580✔
596
    SSchema* pSchema = pCfg->pSchemas + i;
388,848✔
597
    SColRef* pRef = pCfg->pColRefs + i;
388,848✔
598
#define LTYPE_LEN                                    \
599
  (32 + 60 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN + \
600
   10)  // 60 byte for compress info, TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN for column ref
601
    char type[LTYPE_LEN];
358,214✔
602
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
388,848✔
603
    int typeLen = strlen(type);
388,848✔
604
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
388,848✔
605
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
360,502✔
606
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
31,019✔
607
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
357,829✔
608
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
13,241✔
609
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
13,241✔
610
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
344,588✔
611
      uint8_t precision, scale;
×
612
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
1,072✔
613
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
1,072✔
614
    }
615

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

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

634
    if (!(pSchema->flags & COL_IS_KEY)) {
388,848✔
635
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
377,925✔
636
                        "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
377,925✔
637
    } else {
638
      char* pk = "COMPOSITE KEY";
10,923✔
639
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
10,923!
640
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
10,923✔
641
    }
642
  }
643
}
65,732✔
644

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

653
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
15,789!
654
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
5,051✔
655
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
5,051✔
656
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
5,051✔
657
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
5,051✔
658
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
5,051✔
659
    } else {
660
      continue;
10,738✔
661
    }
662

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

666
  }
667
}
1,375✔
668

669
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
38,910✔
670
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
38,910✔
671
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
201,560✔
672
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
162,650✔
673
    char     type[32];
141,999✔
674
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
162,650✔
675
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
162,650✔
676
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
145,519✔
677
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
18,270✔
678
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
18,270✔
679
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
144,380✔
680
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
5,762✔
681
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
5,762✔
682
    }
683

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

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

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

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

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

719
    return TSDB_CODE_SUCCESS;
236✔
720
  }
721

722
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
33,537!
723
  int16_t valueNum = taosArrayGetSize(pTagVals);
33,537✔
724
  int32_t num = 0;
33,537✔
725
  int32_t j = 0;
33,537✔
726
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
81,827✔
727
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
48,290✔
728
    if (i > 0) {
48,290✔
729
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
14,753✔
730
                        ", ");
731
    }
732

733
    if (j >= valueNum) {
48,290✔
734
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
17,758✔
735
                        "NULL");
736
      continue;
17,758✔
737
    }
738

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

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

772
  return code;
33,537✔
773
}
774

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

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

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

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

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

820
  if (pCfg->keep > 0) {
96,755✔
821
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,772✔
822
                      " KEEP %dm", pCfg->keep);
823
  }
824

825
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
96,755✔
826
    int32_t nSma = 0;
64,357✔
827
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
436,041✔
828
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
371,684!
829
        ++nSma;
371,684✔
830
      }
831
    }
832

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

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

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

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

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

878
  int32_t len = 0;
103,405✔
879

880
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
103,405✔
881
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
38,910✔
882
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
883
    appendColumnFields(buf2, &len, pCfg);
38,910✔
884
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
38,910✔
885
                     ") TAGS (");
886
    appendTagFields(buf2, &len, pCfg);
38,910✔
887
    len +=
38,910✔
888
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
38,910✔
889
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
38,910✔
890
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
64,495✔
891
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
32,398✔
892
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
893
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
32,398✔
894
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
32,398✔
895
    appendTagNameFields(buf2, &len, pCfg);
32,398✔
896
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
32,398✔
897
                     ") TAGS (");
898
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
32,398✔
899
    TAOS_CHECK_ERRNO(code);
32,398!
900
    len +=
32,398✔
901
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
32,398✔
902
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
32,398✔
903
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
32,097✔
904
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
25,447✔
905
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
906
    appendColumnFields(buf2, &len, pCfg);
25,447✔
907
    len +=
25,447✔
908
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
25,447✔
909
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
25,447✔
910
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
6,650✔
911
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1,375✔
912
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
913
    appendColumnFields(buf2, &len, pCfg);
1,375✔
914
    len +=
1,375✔
915
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,375✔
916
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
5,275✔
917
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1,375✔
918
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
919
    appendColRefFields(buf2, &len, pCfg);
1,375✔
920
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,375✔
921
                    ") USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
1,375✔
922
    appendTagNameFields(buf2, &len, pCfg);
1,375✔
923
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,375✔
924
                     ") TAGS (");
925
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
1,375✔
926
    TAOS_CHECK_ERRNO(code);
1,375!
927
    len +=
1,375✔
928
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,375✔
929
  }
930

931
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
103,405✔
932

933
  code = colDataSetVal(pCol2, 0, buf2, false);
103,405✔
934
  TAOS_CHECK_ERRNO(code);
103,405!
935

936
_exit:
103,405✔
937
  taosMemoryFree(buf2);
103,405!
938

939
  return code;
103,405✔
940
}
941

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

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

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

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

972
  return code;
8,938✔
973
}
974

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

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

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

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

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

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

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

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

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

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

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

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

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

1105
  *processed = true;
2,161✔
1106

1107
_return:
174,967✔
1108

1109
  if (code) {
174,967!
1110
    terrno = code;
×
1111
  }
1112

1113
  return code;
174,967✔
1114
}
1115

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

1119
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
174,967!
1120
    return terrno;
×
1121
  }
1122

1123
  if (processed) {
174,967✔
1124
    goto _return;
2,161✔
1125
  }
1126

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

1131
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
170,750!
1132
    return terrno;
×
1133
  }
1134

1135
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
170,750!
1136

1137
_return:
172,745✔
1138

1139
  return TSDB_CODE_SUCCESS;
172,911✔
1140
}
1141

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

1148
  pBlock->info.hasVarCol = true;
48,837✔
1149

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

1156
  SColumnInfoData infoData = {0};
48,837✔
1157

1158
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
48,837✔
1159
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
48,837✔
1160
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
97,674!
1161
    goto _exit;
×
1162
  }
1163

1164
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
48,837✔
1165
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
48,837✔
1166
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
97,674!
1167
    goto _exit;
×
1168
  }
1169

1170
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
48,837✔
1171
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
48,837✔
1172
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
97,674!
1173
    goto _exit;
×
1174
  }
1175

1176
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
48,837✔
1177
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
48,837✔
1178
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
97,674!
1179
    goto _exit;
×
1180
  }
1181

1182
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
48,837✔
1183
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
48,837✔
1184
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
97,674!
1185
    goto _exit;
×
1186
  }
1187

1188
  *pOutput = pBlock;
48,837✔
1189

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

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

1217
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
2,958,203✔
1218
  QRY_PARAM_CHECK(pOutput);
2,958,203!
1219

1220
  SSDataBlock* pBlock = NULL;
2,958,203✔
1221
  int32_t      code = createDataBlock(&pBlock);
2,958,203✔
1222
  if (code) {
2,958,203!
1223
    return code;
×
1224
  }
1225

1226
  SNode* pProj = NULL;
2,958,203✔
1227
  FOREACH(pProj, pProjects) {
6,810,323!
1228
    SExprNode*      pExpr = (SExprNode*)pProj;
3,852,120✔
1229
    SColumnInfoData infoData = {0};
3,852,120✔
1230
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
3,852,120!
1231
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1232
      infoData.info.bytes = 0;
×
1233
    } else {
1234
      infoData.info.type = pExpr->resType.type;
3,852,120✔
1235
      infoData.info.bytes = pExpr->resType.bytes;
3,852,120✔
1236
      infoData.info.precision = pExpr->resType.precision;
3,852,120✔
1237
      infoData.info.scale = pExpr->resType.scale;
3,852,120✔
1238
    }
1239
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
3,852,120!
1240
  }
1241

1242
  *pOutput = pBlock;
2,958,203✔
1243
  return code;
2,958,203✔
1244
}
1245

1246
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
2,958,203✔
1247
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
2,958,203!
1248

1249
  int32_t index = 0;
2,958,203✔
1250
  SNode*  pProj = NULL;
2,958,203✔
1251
  FOREACH(pProj, pProjects) {
6,810,323!
1252
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
3,852,120!
1253
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1254
    } else {
1255
      if (((SValueNode*)pProj)->isNull) {
3,852,120!
1256
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
127,446!
1257
      } else {
1258
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
3,724,674!
1259
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1260
      }
1261
    }
1262
  }
1263

1264
  pBlock->info.rows = 1;
2,958,203✔
1265
  return TSDB_CODE_SUCCESS;
2,958,203✔
1266
}
1267

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

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

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

1307
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
6,316,445✔
1308
                     void* charsetCxt) {
1309
  switch (nodeType(pStmt)) {
6,316,445!
1310
    case QUERY_NODE_DESCRIBE_STMT:
556,544✔
1311
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
556,544✔
1312
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
2,428,679✔
1313
      return execResetQueryCache();
2,428,679✔
1314
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
35,355✔
1315
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
35,355✔
1316
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
100,322✔
1317
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
100,322✔
1318
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
2,446✔
1319
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
2,446✔
1320
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
1,144✔
1321
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,144✔
1322
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
8,938✔
1323
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
8,938✔
1324
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
1,010✔
1325
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
1,010✔
1326
    case QUERY_NODE_ALTER_LOCAL_STMT:
174,967✔
1327
      return execAlterLocal((SAlterLocalStmt*)pStmt);
174,967✔
1328
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
48,837✔
1329
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
48,837✔
1330
    case QUERY_NODE_SELECT_STMT:
2,958,203✔
1331
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
2,958,203✔
1332
    default:
×
1333
      break;
×
1334
  }
1335
  return TSDB_CODE_FAILED;
×
1336
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc