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

taosdata / TDengine / #4473

08 Jul 2025 09:38AM UTC coverage: 62.922% (+0.7%) from 62.22%
#4473

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

158525 of 321496 branches covered (49.31%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

1333 existing lines in 67 files now uncovered.

245526 of 320647 relevant lines covered (76.57%)

17689640.25 hits per line

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

68.41
/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 "catalog.h"
17
#include "command.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) {
119,991✔
40
  if (NULL == pBlock || NULL == pRsp) {
119,991!
41
    return TSDB_CODE_INVALID_PARA;
×
42
  }
43
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
119,991✔
44
  size_t rspSize = sizeof(SRetrieveTableRsp) + dataEncodeBufSize + PAYLOAD_PREFIX_LEN;
119,990✔
45
  *pRsp = taosMemoryCalloc(1, rspSize);
119,990!
46
  if (NULL == *pRsp) {
119,990!
47
    return terrno;
×
48
  }
49

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

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

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

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

73
  return TSDB_CODE_SUCCESS;
119,988✔
74
}
75

76
static int32_t getSchemaBytes(const SSchema* pSchema) {
179,129✔
77
  switch (pSchema->type) {
179,129✔
78
    case TSDB_DATA_TYPE_BINARY:
12,082✔
79
    case TSDB_DATA_TYPE_VARBINARY:
80
    case TSDB_DATA_TYPE_GEOMETRY:
81
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
12,082✔
82
    case TSDB_DATA_TYPE_NCHAR:
12,502✔
83
    case TSDB_DATA_TYPE_JSON:
84
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
12,502✔
85
    default:
154,545✔
86
      return pSchema->bytes;
154,545✔
87
  }
88
}
89

90
static const char* expandIdentifier(const char* name, char* output) {
3,077✔
91
  if (NULL == name) return "";
3,077!
92
  bool containsEscapeChar = false;
3,077✔
93
  for (const char* p = name; *p != '\0'; ++p) {
13,387✔
94
    if (*p == TS_ESCAPE_CHAR) {
10,670✔
95
      containsEscapeChar = true;
360✔
96
      break;
360✔
97
    }
98
  }
99
  if (!containsEscapeChar) return name;
3,077✔
100
  if (NULL == output) return "";
360!
101
  char* out_ptr = output;
360✔
102
  for (const char* src = name; *src != '\0'; ++src) {
2,216✔
103
    if (*src == TS_ESCAPE_CHAR) {
1,856✔
104
      *out_ptr++ = TS_ESCAPE_CHAR;
904✔
105
      *out_ptr++ = TS_ESCAPE_CHAR;
904✔
106
    } else {
107
      *out_ptr++ = *src;
952✔
108
    }
109
  }
110
  *out_ptr = '\0';
360✔
111
  return output;
360✔
112
}
113

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

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

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

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

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

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

193
  int32_t fillTagCol = 0;
3,614✔
194
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
3,614✔
195
  for (int32_t i = 0; i < numOfRows; ++i) {
182,746✔
196
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
179,133!
197
      continue;
×
198
    }
199
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
179,130✔
200
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
179,130!
201

202
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
179,135!
203
      uint8_t prec = 0, scale = 0;
1,727✔
204
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
1,727✔
205
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
1,727✔
206
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
1,727✔
207
      varDataSetLen(buf, len);
1,727✔
208
    } else {
209
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
177,406✔
210
    }
211
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
179,133!
212
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
179,131✔
213
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
179,129!
214
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
179,130✔
215
      if (i >= pMeta->tableInfo.numOfColumns) {
179,070✔
216
        STR_TO_VARSTR(buf, "TAG");
13,271✔
217
        fillTagCol = 1;
13,271✔
218
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
165,799✔
219
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
4✔
220
      } else {
221
        STR_TO_VARSTR(buf, "");
165,795✔
222
      }
223
    } else {
224
      STR_TO_VARSTR(buf, "VIEW COL");
60✔
225
    }
226
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
179,130!
227
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
179,133!
228
      if (i < pMeta->tableInfo.numOfColumns) {
178,788✔
229
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
165,530✔
230
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
165,528!
231
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
165,531✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
165,531!
233
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
165,529✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
165,529!
235
      } else {
236
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
13,258!
237
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
13,258!
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
13,260!
239
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
13,260!
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
13,259!
241
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
13,259✔
242
      }
243
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
345!
244
      if (i < pMeta->numOfColRefs) {
92✔
245
        if (pMeta->colRef[i].hasRef) {
80✔
246
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
30✔
247
          strcat(refColName, pMeta->colRef[i].refDbName);
30✔
248
          strcat(refColName, ".");
30✔
249
          strcat(refColName, pMeta->colRef[i].refTableName);
30✔
250
          strcat(refColName, ".");
30✔
251
          strcat(refColName, pMeta->colRef[i].refColName);
30✔
252
          STR_TO_VARSTR(buf, refColName);
30✔
253
        } else {
254
          STR_TO_VARSTR(buf, "");
50✔
255
        }
256
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
80!
257
      } else {
258
        STR_TO_VARSTR(buf, "");
12✔
259
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
12!
260
      }
261
    }
262

263
    fillTagCol = 0;
179,132✔
264

265
    ++(pBlock->info.rows);
179,132✔
266
  }
267
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
3,613!
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) {
3,613!
279
    qError("no permission to view any columns");
×
280
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
281
  }
282
  return TSDB_CODE_SUCCESS;
3,613✔
283
}
284

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

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

314
static int32_t execResetQueryCache() { return catalogClearCache(); }
8,020✔
315

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

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

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

332
  if (TSDB_CODE_SUCCESS == code) {
24!
333
    *pOutput = pBlock;
24✔
334
  } else {
335
    (void)blockDataDestroy(pBlock);
×
336
  }
337
  return code;
24✔
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) {
24✔
366
  size_t size = taosArrayGetSize(pRetension);
24✔
367
  if (size == 0) {
24!
368
    *ppRetentions = NULL;
24✔
369
    return TSDB_CODE_SUCCESS;
24✔
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) {
22✔
399
  switch (cacheModel) {
22!
400
    case TSDB_CACHE_MODEL_NONE:
22✔
401
      return TSDB_CACHE_MODEL_NONE_STR;
22✔
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) {
22✔
415
  switch (encryptAlgorithm) {
22!
416
    case TSDB_ENCRYPT_ALGO_NONE:
22✔
417
      return TSDB_ENCRYPT_ALGO_NONE_STR;
22✔
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,735,378✔
427
  if (buffer == NULL || bufSize <= 0) {
4,735,378!
428
    return 0;
×
429
  }
430
  int32_t len = 0;
4,737,832✔
431
  if (timeInMinutes % 1440 == 0) {
4,737,832!
432
    int32_t days = timeInMinutes / 1440;
4,740,625✔
433
    len = tsnprintf(buffer, bufSize, "%dd", days);
4,740,625✔
UNCOV
434
  } else if (timeInMinutes % 60 == 0) {
×
435
    int32_t hours = timeInMinutes / 60;
3✔
436
    len = tsnprintf(buffer, bufSize, "%dh", hours);
3✔
437
  } else {
438
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
×
439
  }
440
  return len;
4,768,990✔
441
}
442

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

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

452
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
24✔
453
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
24✔
454
  int32_t          len = 0;
24✔
455
  char*            prec = NULL;
24✔
456
  switch (pCfg->precision) {
24!
457
    case TSDB_TIME_PRECISION_MILLI:
24✔
458
      prec = TSDB_TIME_PRECISION_MILLI_STR;
24✔
459
      break;
24✔
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;
24✔
472
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
24!
473
  int32_t dbFNameLen = strlen(dbFName);
24✔
474
  int32_t hashPrefix = 0;
24✔
475
  if (pCfg->hashPrefix > 0) {
24✔
476
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
2✔
477
  } else if (pCfg->hashPrefix < 0) {
22✔
478
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
3✔
479
  }
480
  char durationStr[128] = {0};
24✔
481
  char keep0Str[128] = {0};
24✔
482
  char keep1Str[128] = {0};
24✔
483
  char keep2Str[128] = {0};
24✔
484
  char compactIntervalStr[13] = {0};
24✔
485
  char compactStartTimeStr[13] = {0};
24✔
486
  char compactEndTimeStr[13] = {0};
24✔
487

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

496
  if (IS_SYS_DBNAME(dbName)) {
24!
497
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
2✔
498
                     "CREATE DATABASE `%s`", dbName);
2✔
499
  } else {
500
    len +=
22✔
501
        tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
22✔
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' S3_CHUNKPAGES %d S3_KEEPLOCAL %dm S3_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,
22✔
510
                  pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str, keep1Str, keep2Str,
22✔
511
                  pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel, pCfg->numOfVgroups,
22✔
512
                  1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize, pCfg->walRetentionPeriod,
22✔
513
                  pCfg->walRetentionSize, pCfg->keepTimeOffset, encryptAlgorithmStr(pCfg->encryptAlgorithm),
22✔
514
                  pCfg->s3ChunkSize, pCfg->s3KeepLocal, pCfg->s3Compact, compactIntervalStr, compactStartTimeStr,
22✔
515
                  compactEndTimeStr, pCfg->compactTimeOffset);
22✔
516

517
    if (pRetentions) {
22!
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);
24!
524

525
  (varDataLen(buf2)) = len;
24✔
526

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

529
  return TSDB_CODE_SUCCESS;
24✔
530
}
531

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

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

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

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

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

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

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

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

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

593
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
215✔
594
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
215✔
595
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
1,887✔
596
    SSchema* pSchema = pCfg->pSchemas + i;
1,672✔
597
    SColRef* pRef = pCfg->pColRefs + i;
1,672✔
598
#define LTYPE_LEN (32 + 60 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN + 10)  // 60 byte for compress info, TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN for column ref
599
    char type[LTYPE_LEN];
600
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
1,672✔
601
    int typeLen = strlen(type);
1,672✔
602
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
1,672✔
603
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
1,529✔
604
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
164✔
605
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
1,508✔
606
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
59✔
607
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
59✔
608
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
1,449✔
609
      uint8_t precision, scale;
610
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
120✔
611
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
120✔
612
    }
613

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

623
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
1,672!
624
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " FROM `%s`", pRef->refDbName);
36✔
625
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
36✔
626
      typeLen +=
36✔
627
          tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
36✔
628
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
36✔
629
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
36✔
630
    }
631

632
    if (!(pSchema->flags & COL_IS_KEY)) {
1,672✔
633
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,670✔
634
                        "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
1,670✔
635
    } else {
636
      char* pk = "COMPOSITE KEY";
2✔
637
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2!
638
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
2✔
639
    }
640
  }
641
}
215✔
642

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

651
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
54!
652
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
26✔
653
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
26✔
654
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
26✔
655
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
26✔
656
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
26✔
657
    } else {
658
      continue;
28✔
659
    }
660

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

664
  }
665
}
10✔
666

667
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
134✔
668
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
134✔
669
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
866✔
670
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
732✔
671
    char     type[32];
672
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
732✔
673
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
732✔
674
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
625✔
675
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
124✔
676
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
124✔
677
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
608✔
678
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
32✔
679
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
32✔
680
    }
681

682
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
732✔
683
                      "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
732✔
684
  }
685
}
134✔
686

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

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

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

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

717
    return TSDB_CODE_SUCCESS;
4✔
718
  }
719

720
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
85!
721
  int16_t valueNum = taosArrayGetSize(pTagVals);
85✔
722
  int32_t num = 0;
85✔
723
  int32_t j = 0;
85✔
724
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
211✔
725
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
126✔
726
    if (i > 0) {
126✔
727
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
41✔
728
                        ", ");
729
    }
730

731
    if (j >= valueNum) {
126✔
732
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
21✔
733
                        "NULL");
734
      continue;
21✔
735
    }
736

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

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

770
  return code;
85✔
771
}
772

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

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

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

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

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

818
  if (pCfg->keep > 0) {
284✔
819
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
4✔
820
                      " KEEP %dm", pCfg->keep);
821
  }
822

823
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
284✔
824
    int32_t nSma = 0;
205✔
825
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
1,813✔
826
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
1,608!
827
        ++nSma;
1,608✔
828
      }
829
    }
830

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

853
  if (pCfg->virtualStb) {
284✔
854
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
8✔
855
                      " VIRTUAL %d", pCfg->virtualStb);
8✔
856
  }
857
}
284✔
858

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

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

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

876
  int32_t len = 0;
334✔
877

878
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
334✔
879
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
134✔
880
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
881
    appendColumnFields(buf2, &len, pCfg);
134✔
882
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
134✔
883
                     ") TAGS (");
884
    appendTagFields(buf2, &len, pCfg);
134✔
885
    len +=
134✔
886
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
134✔
887
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
134✔
888
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
200✔
889
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
79✔
890
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
891
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
79✔
892
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
79✔
893
    appendTagNameFields(buf2, &len, pCfg);
79✔
894
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
79✔
895
                     ") TAGS (");
896
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
79✔
897
    TAOS_CHECK_ERRNO(code);
79!
898
    len +=
79✔
899
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
79✔
900
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
79✔
901
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
121✔
902
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
71✔
903
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
904
    appendColumnFields(buf2, &len, pCfg);
71✔
905
    len +=
71✔
906
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
71✔
907
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
71✔
908
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
50✔
909
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
10✔
910
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
911
    appendColumnFields(buf2, &len, pCfg);
10✔
912
    len +=
10✔
913
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
10✔
914
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
40✔
915
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
10✔
916
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
917
    appendColRefFields(buf2, &len, pCfg);
10✔
918
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
20✔
919
                    ") USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
10✔
920
    appendTagNameFields(buf2, &len, pCfg);
10✔
921
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
10✔
922
                     ") TAGS (");
923
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
10✔
924
    TAOS_CHECK_ERRNO(code);
10!
925
    len +=
10✔
926
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
10✔
927
  }
928

929
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
334✔
930

931
  code = colDataSetVal(pCol2, 0, buf2, false);
334✔
932
  TAOS_CHECK_ERRNO(code);
334!
933

934
_exit:
334✔
935
  taosMemoryFree(buf2);
334!
936

937
  return code;
334✔
938
}
939

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

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

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

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

969
  return code;
15✔
970
}
971

972
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
318✔
973
  SSDataBlock* pBlock = NULL;
318✔
974
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
318✔
975
  if (TSDB_CODE_SUCCESS == code) {
318!
976
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
318✔
977
  }
978
  if (TSDB_CODE_SUCCESS == code) {
318!
979
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
318✔
980
  }
981
  (void)blockDataDestroy(pBlock);
318✔
982
  return code;
318✔
983
}
984

985
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
16✔
986
  SSDataBlock* pBlock = NULL;
16✔
987
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
16✔
988
  if (TSDB_CODE_SUCCESS == code) {
16!
989
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
16✔
990
  }
991
  if (TSDB_CODE_SUCCESS == code) {
16!
992
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
16✔
993
  }
994
  (void)blockDataDestroy(pBlock);
16✔
995
  return code;
16✔
996
}
997

998
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
3✔
999
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
3✔
1000
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
3✔
1001
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
1✔
1002
    return terrno;
1✔
1003
  }
1004

1005
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
2✔
1006
}
1007

1008
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
2,422✔
1009
  int32_t code = 0;
2,422✔
1010

1011
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
2,422!
1012
    taosResetLog();
×
1013
    cfgDumpCfg(tsCfg, 0, false);
×
1014
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
2,422✔
1015
    int32_t tmp = 0;
40✔
1016
    code = taosStr2int32(value, &tmp);
40✔
1017
    if (code) {
40!
1018
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1019
      return code;
×
1020
    }
1021
    code = schedulerUpdatePolicy(tmp);
40✔
1022
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
2,382!
1023
    int32_t tmp = 0;
×
1024
    code = taosStr2int32(value, &tmp);
×
1025
    if (code) {
×
1026
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1027
      return code;
×
1028
    }
1029
    code = schedulerEnableReSchedule(tmp != 0);
×
1030
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
2,382!
1031
    code = ctgdHandleDbgCommand(value);
×
1032
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
2,382!
1033
    code = taosMemoryDbgInit();
×
1034
    if (code) {
×
1035
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
1036
      return code;
×
1037
    }
1038
    tsAsyncLog = false;
×
1039
    qInfo("memory dbg enabled");
×
1040
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
2,382!
1041
    code = taosMemoryDbgInitRestore();
×
1042
    if (code) {
×
1043
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
1044
      return code;
×
1045
    }
1046
    qInfo("memory dbg disabled");
×
1047
  } else {
1048
    goto _return;
2,382✔
1049
  }
1050

1051
  *processed = true;
40✔
1052

1053
_return:
2,422✔
1054

1055
  if (code) {
2,422!
1056
    terrno = code;
×
1057
  }
1058

1059
  return code;
2,422✔
1060
}
1061

1062
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
2,422✔
1063
  bool processed = false;
2,422✔
1064

1065
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
2,422!
1066
    return terrno;
×
1067
  }
1068

1069
  if (processed) {
2,422✔
1070
    goto _return;
40✔
1071
  }
1072

1073
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
2,382✔
1074
    return terrno;
7✔
1075
  }
1076

1077
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
2,375!
1078
    return terrno;
×
1079
  }
1080

1081
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
2,375!
1082

1083
_return:
2,375✔
1084

1085
  return TSDB_CODE_SUCCESS;
2,415✔
1086
}
1087

1088
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
572✔
1089
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
572!
1090
  if (NULL == pBlock) {
572!
1091
    return terrno;
×
1092
  }
1093

1094
  pBlock->info.hasVarCol = true;
572✔
1095

1096
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
572✔
1097
  if (NULL == pBlock->pDataBlock) {
572!
1098
    taosMemoryFree(pBlock);
×
1099
    return terrno;
×
1100
  }
1101

1102
  SColumnInfoData infoData = {0};
572✔
1103

1104
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
572✔
1105
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
572✔
1106
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,144!
1107
    goto _exit;
×
1108
  }
1109

1110
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
572✔
1111
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
572✔
1112
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,144!
1113
    goto _exit;
×
1114
  }
1115

1116
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
572✔
1117
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
572✔
1118
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,144!
1119
    goto _exit;
×
1120
  }
1121

1122
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
572✔
1123
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
572✔
1124
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,144!
1125
    goto _exit;
×
1126
  }
1127

1128
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
572✔
1129
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
572✔
1130
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,144!
1131
    goto _exit;
×
1132
  }
1133

1134
  *pOutput = pBlock;
572✔
1135

1136
_exit:
572✔
1137
  if (terrno != TSDB_CODE_SUCCESS) {
572!
1138
    taosArrayDestroy(pBlock->pDataBlock);
×
1139
    taosMemoryFree(pBlock);
×
1140
  }
1141
  return terrno;
572✔
1142
}
1143

1144
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
572✔
1145
  SSDataBlock* pBlock = NULL;
572✔
1146
  char*        likePattern = NULL;
572✔
1147
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
572✔
1148
  if (TSDB_CODE_SUCCESS == code) {
572!
1149
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
572✔
1150
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
7✔
1151
    }
1152
  }
1153
  if (TSDB_CODE_SUCCESS == code) {
572!
1154
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
572✔
1155
  }
1156
  if (TSDB_CODE_SUCCESS == code) {
572!
1157
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
572✔
1158
  }
1159
  (void)blockDataDestroy(pBlock);
572✔
1160
  return code;
572✔
1161
}
1162

1163
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
115,432✔
1164
  QRY_PARAM_CHECK(pOutput);
115,432!
1165

1166
  SSDataBlock* pBlock = NULL;
115,432✔
1167
  int32_t      code = createDataBlock(&pBlock);
115,432✔
1168
  if (code) {
115,432!
1169
    return code;
×
1170
  }
1171

1172
  SNode* pProj = NULL;
115,432✔
1173
  FOREACH(pProj, pProjects) {
234,900!
1174
    SExprNode*      pExpr = (SExprNode*)pProj;
119,468✔
1175
    SColumnInfoData infoData = {0};
119,468✔
1176
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
119,468!
1177
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1178
      infoData.info.bytes = 0;
×
1179
    } else {
1180
      infoData.info.type = pExpr->resType.type;
119,468✔
1181
      infoData.info.bytes = pExpr->resType.bytes;
119,468✔
1182
      infoData.info.precision = pExpr->resType.precision;
119,468✔
1183
      infoData.info.scale = pExpr->resType.scale;
119,468✔
1184
    }
1185
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
119,468!
1186
  }
1187

1188
  *pOutput = pBlock;
115,432✔
1189
  return code;
115,432✔
1190
}
1191

1192
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
115,432✔
1193
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
115,432!
1194

1195
  int32_t index = 0;
115,432✔
1196
  SNode*  pProj = NULL;
115,432✔
1197
  FOREACH(pProj, pProjects) {
234,900!
1198
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
119,468!
1199
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1200
    } else {
1201
      if (((SValueNode*)pProj)->isNull) {
119,468✔
1202
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
76!
1203
      } else {
1204
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
119,392!
1205
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1206
      }
1207
    }
1208
  }
1209

1210
  pBlock->info.rows = 1;
115,432✔
1211
  return TSDB_CODE_SUCCESS;
115,432✔
1212
}
1213

1214
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
115,432✔
1215
  SSDataBlock* pBlock = NULL;
115,432✔
1216
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
115,432✔
1217
  if (TSDB_CODE_SUCCESS == code) {
115,432!
1218
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
115,432✔
1219
  }
1220
  if (TSDB_CODE_SUCCESS == code) {
115,432!
1221
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
115,432!
1222
  }
1223
  (void)blockDataDestroy(pBlock);
115,432✔
1224
  return code;
115,432✔
1225
}
1226

1227
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
15✔
1228
  SSDataBlock* pBlock = NULL;
15✔
1229
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
15✔
1230
  if (TSDB_CODE_SUCCESS == code) {
15!
1231
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
15✔
1232
  }
1233
  if (TSDB_CODE_SUCCESS == code) {
15!
1234
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
15✔
1235
  }
1236
  (void)blockDataDestroy(pBlock);
15✔
1237
  return code;
15✔
1238
}
1239

1240
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
130,432✔
1241
                     void* charsetCxt) {
1242
  switch (nodeType(pStmt)) {
130,432!
1243
    case QUERY_NODE_DESCRIBE_STMT:
3,612✔
1244
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
3,612✔
1245
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
8,020✔
1246
      return execResetQueryCache();
8,020✔
1247
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
24✔
1248
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
24✔
1249
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
316✔
1250
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
316✔
1251
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
16✔
1252
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
16✔
1253
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
3✔
1254
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
3✔
1255
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
15✔
1256
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
15✔
1257
    case QUERY_NODE_ALTER_LOCAL_STMT:
2,422✔
1258
      return execAlterLocal((SAlterLocalStmt*)pStmt);
2,422✔
1259
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
572✔
1260
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
572✔
1261
    case QUERY_NODE_SELECT_STMT:
115,432✔
1262
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
115,432✔
1263
    default:
×
1264
      break;
×
1265
  }
1266
  return TSDB_CODE_FAILED;
×
1267
}
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

© 2025 Coveralls, Inc