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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

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

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

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

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

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

73
  return TSDB_CODE_SUCCESS;
3,157✔
74
}
75

76
static int32_t getSchemaBytes(const SSchema* pSchema) {
3,225✔
77
  switch (pSchema->type) {
3,225✔
78
    case TSDB_DATA_TYPE_BINARY:
183✔
79
    case TSDB_DATA_TYPE_VARBINARY:
80
    case TSDB_DATA_TYPE_GEOMETRY:
81
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
183✔
82
    case TSDB_DATA_TYPE_NCHAR:
144✔
83
    case TSDB_DATA_TYPE_JSON:
84
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
144✔
85
    default:
2,898✔
86
      return pSchema->bytes;
2,898✔
87
  }
88
}
89

90
static const char* expandIdentifier(const char* name, char* output) {
1,180✔
91
  if (NULL == name) return "";
1,180!
92
  bool containsEscapeChar = false;
1,180✔
93
  for (const char* p = name; *p != '\0'; ++p) {
6,231✔
94
    if (*p == TS_ESCAPE_CHAR) {
5,051!
95
      containsEscapeChar = true;
×
96
      break;
×
97
    }
98
  }
99
  if (!containsEscapeChar) return name;
1,180!
100
  if (NULL == output) return "";
×
101
  char* out_ptr = output;
×
102
  for (const char* src = name; *src != '\0'; ++src) {
×
103
    if (*src == TS_ESCAPE_CHAR) {
×
104
      *out_ptr++ = TS_ESCAPE_CHAR;
×
105
      *out_ptr++ = TS_ESCAPE_CHAR;
×
106
    } else {
107
      *out_ptr++ = *src;
×
108
    }
109
  }
110
  *out_ptr = '\0';
×
111
  return output;
×
112
}
113

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

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

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

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

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

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

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

202
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
3,226!
203
      uint8_t prec = 0, scale = 0;
×
204
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
×
205
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
×
206
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
×
207
      varDataSetLen(buf, len);
×
208
    } else {
209
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
3,226✔
210
    }
211
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
3,226!
212
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
3,225✔
213
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
3,225!
214
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
3,224!
215
      if (i >= pMeta->tableInfo.numOfColumns) {
3,224✔
216
        STR_TO_VARSTR(buf, "TAG");
1,061✔
217
        fillTagCol = 1;
1,061✔
218
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
2,163!
219
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
×
220
      } else {
221
        STR_TO_VARSTR(buf, "");
2,163✔
222
      }
223
    } else {
224
      STR_TO_VARSTR(buf, "VIEW COL");
×
225
    }
226
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
3,224!
227
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
3,225!
228
      if (i < pMeta->tableInfo.numOfColumns) {
3,179✔
229
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
2,125✔
230
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
2,125!
231
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
2,125✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
2,125!
233
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
2,125✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
2,125!
235
      } else {
236
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
1,054!
237
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
1,054!
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
1,055!
239
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
1,055!
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
1,055!
241
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
1,055!
242
      }
243
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
46!
244
      if (i < pMeta->numOfColRefs) {
46✔
245
        if (pMeta->colRef[i].hasRef) {
40✔
246
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
15✔
247
          strcat(refColName, pMeta->colRef[i].refDbName);
15✔
248
          strcat(refColName, ".");
15✔
249
          strcat(refColName, pMeta->colRef[i].refTableName);
15✔
250
          strcat(refColName, ".");
15✔
251
          strcat(refColName, pMeta->colRef[i].refColName);
15✔
252
          STR_TO_VARSTR(buf, refColName);
15✔
253
        } else {
254
          STR_TO_VARSTR(buf, "");
25✔
255
        }
256
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
40!
257
      } else {
258
        STR_TO_VARSTR(buf, "");
6✔
259
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
6!
260
      }
261
    }
262

263
    fillTagCol = 0;
3,225✔
264

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

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

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

314
static int32_t execResetQueryCache() { return catalogClearCache(); }
1,204✔
315

316
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
×
317
  QRY_PARAM_CHECK(pOutput);
×
318

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

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

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

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

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

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

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

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

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

525
  (varDataLen(buf2)) = len;
×
526

527
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
×
528

529
  return TSDB_CODE_SUCCESS;
×
530
}
531

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

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

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

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

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

569
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
×
570
  QRY_PARAM_CHECK(pOutput);
×
571

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

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

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

593
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
77✔
594
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
77✔
595
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
808✔
596
    SSchema* pSchema = pCfg->pSchemas + i;
731✔
597
    SColRef* pRef = pCfg->pColRefs + i;
731✔
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];
602
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
731✔
603
    int typeLen = strlen(type);
731✔
604
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
731✔
605
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
706✔
606
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
27✔
607
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
704✔
608
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
30✔
609
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
30✔
610
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
674!
611
      uint8_t precision, scale;
612
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
×
613
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
×
614
    }
615

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

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

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

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

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

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

666
  }
667
}
1✔
668

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

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

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

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

703
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
1!
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)) {
1!
709
    char* pJson = NULL;
×
710
    parseTagDatatoJson(pTag, &pJson, charsetCxt);
×
711
    if (NULL == pJson) {
×
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),
×
716
                      "%s", pJson);
717
    taosMemoryFree(pJson);
×
718

719
    return TSDB_CODE_SUCCESS;
×
720
  }
721

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

733
    if (j >= valueNum) {
6!
734
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
735
                        "NULL");
736
      continue;
×
737
    }
738

739
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
6✔
740
    if (pSchema->colId > pTagVal->cid) {
6!
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) {
6!
745
      char    type = pTagVal->type;
6✔
746
      int32_t tlen = 0;
6✔
747

748
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
6✔
749
      if (leftSize <= 0) {
6!
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)) {
6!
755
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
2✔
756
        TAOS_CHECK_ERRNO(code);
2!
757
      } else {
758
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
4✔
759
                               &tlen);
760
        TAOS_CHECK_ERRNO(code);
4!
761
      }
762
      *len += tlen;
6✔
763
      j++;
6✔
764
    } else {
765
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
766
                        "NULL");
767
    }
768
  }
769
_exit:
1✔
770
  taosArrayDestroy(pTagVals);
1✔
771

772
  return code;
1✔
773
}
774

775
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
76✔
776
  if (pCfg->commentLen > 0) {
76!
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) {
76!
780
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
781
                      " COMMENT ''");
782
  }
783

784
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
76!
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) {
76!
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);
76✔
803
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
76!
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) {
76!
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) {
76!
821
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
822
                      " KEEP %dm", pCfg->keep);
823
  }
824

825
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
76!
826
    int32_t nSma = 0;
76✔
827
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
787✔
828
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
711!
829
        ++nSma;
711✔
830
      }
831
    }
832

833
    if (nSma < pCfg->numOfColumns && nSma > 0) {
76!
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) {
76!
856
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
857
                      " VIRTUAL %d", pCfg->virtualStb);
×
858
  }
859
}
76✔
860

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

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

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

878
  int32_t len = 0;
78✔
879

880
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
78✔
881
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
42✔
882
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
883
    appendColumnFields(buf2, &len, pCfg);
42✔
884
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
42✔
885
                     ") TAGS (");
886
    appendTagFields(buf2, &len, pCfg);
42✔
887
    len +=
42✔
888
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
42✔
889
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
42✔
890
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
36!
891
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
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),
×
894
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
×
895
    appendTagNameFields(buf2, &len, pCfg);
×
896
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
×
897
                     ") TAGS (");
898
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
×
899
    TAOS_CHECK_ERRNO(code);
×
900
    len +=
×
901
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
×
902
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
×
903
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
36✔
904
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
34✔
905
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
906
    appendColumnFields(buf2, &len, pCfg);
34✔
907
    len +=
34✔
908
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
34✔
909
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
34✔
910
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
2✔
911
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1✔
912
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
913
    appendColumnFields(buf2, &len, pCfg);
1✔
914
    len +=
1✔
915
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1✔
916
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
1!
917
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1✔
918
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
919
    appendColRefFields(buf2, &len, pCfg);
1✔
920
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
2✔
921
                    ") USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
1✔
922
    appendTagNameFields(buf2, &len, pCfg);
1✔
923
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1✔
924
                     ") TAGS (");
925
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
1✔
926
    TAOS_CHECK_ERRNO(code);
1!
927
    len +=
1✔
928
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1✔
929
  }
930

931
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
78✔
932

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

936
_exit:
78✔
937
  taosMemoryFree(buf2);
78!
938

939
  return code;
78✔
940
}
941

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

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

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

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

971
  return code;
×
972
}
973

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

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

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

1007
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
×
1008
}
1009

1010
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
13✔
1011
  int32_t code = 0;
13✔
1012

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

1053
  *processed = true;
1✔
1054

1055
_return:
13✔
1056

1057
  if (code) {
13!
1058
    terrno = code;
×
1059
  }
1060

1061
  return code;
13✔
1062
}
1063

1064
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
13✔
1065
  bool processed = false;
13✔
1066

1067
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
13!
1068
    return terrno;
×
1069
  }
1070

1071
  if (processed) {
13✔
1072
    goto _return;
1✔
1073
  }
1074

1075
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
12!
1076
    return terrno;
×
1077
  }
1078

1079
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
12!
1080
    return terrno;
×
1081
  }
1082

1083
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
12!
1084

1085
_return:
12✔
1086

1087
  return TSDB_CODE_SUCCESS;
13✔
1088
}
1089

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

1096
  pBlock->info.hasVarCol = true;
18✔
1097

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

1104
  SColumnInfoData infoData = {0};
18✔
1105

1106
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
18✔
1107
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
18✔
1108
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
36!
1109
    goto _exit;
×
1110
  }
1111

1112
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
18✔
1113
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
18✔
1114
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
36!
1115
    goto _exit;
×
1116
  }
1117

1118
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
18✔
1119
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
18✔
1120
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
36!
1121
    goto _exit;
×
1122
  }
1123

1124
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
18✔
1125
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
18✔
1126
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
36!
1127
    goto _exit;
×
1128
  }
1129

1130
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
18✔
1131
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
18✔
1132
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
36!
1133
    goto _exit;
×
1134
  }
1135

1136
  *pOutput = pBlock;
18✔
1137

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

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

1165
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
2,836✔
1166
  QRY_PARAM_CHECK(pOutput);
2,836!
1167

1168
  SSDataBlock* pBlock = NULL;
2,836✔
1169
  int32_t      code = createDataBlock(&pBlock);
2,836✔
1170
  if (code) {
2,836!
1171
    return code;
×
1172
  }
1173

1174
  SNode* pProj = NULL;
2,836✔
1175
  FOREACH(pProj, pProjects) {
5,674!
1176
    SExprNode*      pExpr = (SExprNode*)pProj;
2,838✔
1177
    SColumnInfoData infoData = {0};
2,838✔
1178
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
2,838!
1179
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1180
      infoData.info.bytes = 0;
×
1181
    } else {
1182
      infoData.info.type = pExpr->resType.type;
2,838✔
1183
      infoData.info.bytes = pExpr->resType.bytes;
2,838✔
1184
      infoData.info.precision = pExpr->resType.precision;
2,838✔
1185
      infoData.info.scale = pExpr->resType.scale;
2,838✔
1186
    }
1187
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
2,838!
1188
  }
1189

1190
  *pOutput = pBlock;
2,836✔
1191
  return code;
2,836✔
1192
}
1193

1194
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
2,836✔
1195
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
2,836!
1196

1197
  int32_t index = 0;
2,836✔
1198
  SNode*  pProj = NULL;
2,836✔
1199
  FOREACH(pProj, pProjects) {
5,674!
1200
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
2,838!
1201
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1202
    } else {
1203
      if (((SValueNode*)pProj)->isNull) {
2,838✔
1204
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
194!
1205
      } else {
1206
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
2,644!
1207
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1208
      }
1209
    }
1210
  }
1211

1212
  pBlock->info.rows = 1;
2,836✔
1213
  return TSDB_CODE_SUCCESS;
2,836✔
1214
}
1215

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

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

1242
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
4,373✔
1243
                     void* charsetCxt) {
1244
  switch (nodeType(pStmt)) {
4,373!
1245
    case QUERY_NODE_DESCRIBE_STMT:
225✔
1246
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
225✔
1247
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
1,204✔
1248
      return execResetQueryCache();
1,204✔
1249
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
×
1250
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
×
1251
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
76✔
1252
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
76✔
1253
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
2✔
1254
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
2✔
1255
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
×
1256
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
×
1257
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
×
1258
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
×
1259
    case QUERY_NODE_ALTER_LOCAL_STMT:
13✔
1260
      return execAlterLocal((SAlterLocalStmt*)pStmt);
13✔
1261
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
18✔
1262
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
18✔
1263
    case QUERY_NODE_SELECT_STMT:
2,836✔
1264
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
2,836✔
1265
    default:
×
1266
      break;
×
1267
  }
1268
  return TSDB_CODE_FAILED;
×
1269
}
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