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

taosdata / TDengine / #3661

17 Mar 2025 05:39AM UTC coverage: 62.007% (-0.03%) from 62.039%
#3661

push

travis-ci

web-flow
tests: add tdb ut (#30093)

* fix: compile warnings

* tests: add tdb ut

* test(tdb): fix return code

* test: recover ut

* fix: minor changes

* fix: enable test

* fix: ut errors

---------

Co-authored-by: Minglei Jin <mljin@taosdata.com>

153829 of 317582 branches covered (48.44%)

Branch coverage included in aggregate %.

240310 of 318051 relevant lines covered (75.56%)

19602636.8 hits per line

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

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

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

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

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

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

73
  return TSDB_CODE_SUCCESS;
96,046✔
74
}
75

76
static int32_t getSchemaBytes(const SSchema* pSchema) {
74,910✔
77
  switch (pSchema->type) {
74,910✔
78
    case TSDB_DATA_TYPE_BINARY:
5,941✔
79
    case TSDB_DATA_TYPE_VARBINARY:
80
    case TSDB_DATA_TYPE_GEOMETRY:
81
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
5,941✔
82
    case TSDB_DATA_TYPE_NCHAR:
5,543✔
83
    case TSDB_DATA_TYPE_JSON:
84
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
5,543✔
85
    default:
63,426✔
86
      return pSchema->bytes;
63,426✔
87
  }
88
}
89

90
static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) {
1,459✔
91
  QRY_PARAM_CHECK(pOutput);
1,459!
92

93
  SSDataBlock* pBlock = NULL;
1,459✔
94
  int32_t      code = createDataBlock(&pBlock);
1,459✔
95
  if (code) {
1,460!
96
    return code;
×
97
  }
98

99
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_FIELD_LEN, 1);
1,460✔
100
  code = blockDataAppendColInfo(pBlock, &infoData);
1,460✔
101
  if (TSDB_CODE_SUCCESS == code) {
1,460!
102
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_TYPE_LEN, 2);
1,460✔
103
    code = blockDataAppendColInfo(pBlock, &infoData);
1,460✔
104
  }
105
  if (TSDB_CODE_SUCCESS == code) {
1,460!
106
    infoData = createColumnInfoData(TSDB_DATA_TYPE_INT, tDataTypes[TSDB_DATA_TYPE_INT].bytes, 3);
1,460✔
107
    code = blockDataAppendColInfo(pBlock, &infoData);
1,460✔
108
  }
109
  if (TSDB_CODE_SUCCESS == code) {
1,460!
110
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_NOTE_LEN, 4);
1,460✔
111
    code = blockDataAppendColInfo(pBlock, &infoData);
1,459✔
112
  }
113
  if (TSDB_CODE_SUCCESS == code) {
1,458!
114
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 5);
1,458✔
115
    code = blockDataAppendColInfo(pBlock, &infoData);
1,460✔
116
  }
117
  if (TSDB_CODE_SUCCESS == code) {
1,460!
118
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 6);
1,460✔
119
    code = blockDataAppendColInfo(pBlock, &infoData);
1,460✔
120
  }
121
  if (TSDB_CODE_SUCCESS == code) {
1,460!
122
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 7);
1,460✔
123
    code = blockDataAppendColInfo(pBlock, &infoData);
1,460✔
124
  }
125
  if (TSDB_CODE_SUCCESS == code) {
1,460!
126
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COL_REF_LEN, 8);
1,460✔
127
    code = blockDataAppendColInfo(pBlock, &infoData);
1,460✔
128
  }
129
  if (TSDB_CODE_SUCCESS == code) {
1,460!
130
    *pOutput = pBlock;
1,460✔
131
  } else {
132
    (void)blockDataDestroy(pBlock);
×
133
  }
134
  return code;
1,460✔
135
}
136

137
static int32_t setDescResultIntoDataBlock(bool sysInfoUser, SSDataBlock* pBlock, int32_t numOfRows, STableMeta* pMeta,
1,460✔
138
                                          int8_t biMode) {
139
  int32_t blockCap = (biMode != 0) ? numOfRows + 1 : numOfRows;
1,460!
140
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, blockCap));
1,460!
141
  pBlock->info.rows = 0;
1,460✔
142

143
  // field
144
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
1,460✔
145
  // Type
146
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
1,460✔
147
  // Length
148
  SColumnInfoData* pCol3 = taosArrayGet(pBlock->pDataBlock, 2);
1,460✔
149
  // Note
150
  SColumnInfoData* pCol4 = taosArrayGet(pBlock->pDataBlock, 3);
1,460✔
151
  // encode
152
  SColumnInfoData* pCol5 = NULL;
1,460✔
153
  // compress
154
  SColumnInfoData* pCol6 = NULL;
1,460✔
155
  // level
156
  SColumnInfoData* pCol7 = NULL;
1,460✔
157
  // colref
158
  SColumnInfoData* pCol8 = NULL;
1,460✔
159
  if (withExtSchema(pMeta->tableType)) {
1,460✔
160
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
1,432✔
161
    pCol6 = taosArrayGet(pBlock->pDataBlock, 5);
1,432✔
162
    pCol7 = taosArrayGet(pBlock->pDataBlock, 6);
1,431✔
163
  }
164

165
  if (hasRefCol(pMeta->tableType)) {
1,459✔
166
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
2✔
167
  }
168

169
  int32_t fillTagCol = 0;
1,460✔
170
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
1,460✔
171
  for (int32_t i = 0; i < numOfRows; ++i) {
76,372✔
172
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
74,912!
173
      continue;
×
174
    }
175
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
74,912✔
176
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
74,912!
177

178
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
74,911!
179
      uint8_t prec = 0, scale = 0;
1,165✔
180
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
1,165✔
181
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
1,165✔
182
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
1,165✔
183
      varDataSetLen(buf, len);
1,165✔
184
    } else {
185
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
73,745✔
186
    }
187
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
74,910!
188
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
74,910✔
189
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
74,910!
190
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
74,906✔
191
      if (i >= pMeta->tableInfo.numOfColumns) {
74,846✔
192
        STR_TO_VARSTR(buf, "TAG");
9,437✔
193
        fillTagCol = 1;
9,437✔
194
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
65,409✔
195
        STR_TO_VARSTR(buf, "PRIMARY KEY")
13✔
196
      } else {
197
        STR_TO_VARSTR(buf, "");
65,396✔
198
      }
199
    } else {
200
      STR_TO_VARSTR(buf, "VIEW COL");
60✔
201
    }
202
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
74,906!
203
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
74,907!
204
      if (i < pMeta->tableInfo.numOfColumns) {
74,751✔
205
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
65,321✔
206
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
65,318!
207
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
65,322✔
208
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
65,320!
209
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
65,325✔
210
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
65,324!
211
      } else {
212
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
9,430!
213
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
9,430!
214
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
9,430!
215
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
9,430!
216
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
9,430!
217
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
9,430!
218
      }
219
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
156!
220
      if (i < pMeta->numOfColRefs) {
46✔
221
        if (pMeta->colRef[i].hasRef) {
40✔
222
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
15✔
223
          strcat(refColName, pMeta->colRef[i].refDbName);
15✔
224
          strcat(refColName, ".");
15✔
225
          strcat(refColName, pMeta->colRef[i].refTableName);
15✔
226
          strcat(refColName, ".");
15✔
227
          strcat(refColName, pMeta->colRef[i].refColName);
15✔
228
          STR_TO_VARSTR(buf, refColName);
15✔
229
        } else {
230
          STR_TO_VARSTR(buf, "");
25✔
231
        }
232
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
40!
233
      } else {
234
        STR_TO_VARSTR(buf, "");
6✔
235
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
6✔
236
      }
237
    }
238

239
    fillTagCol = 0;
74,912✔
240

241
    ++(pBlock->info.rows);
74,912✔
242
  }
243
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
1,460!
244
    STR_TO_VARSTR(buf, "tbname");
×
245
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
×
246
    STR_TO_VARSTR(buf, "VARCHAR");
×
247
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
×
248
    int32_t bytes = TSDB_TABLE_NAME_LEN - 1;
×
249
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
×
250
    STR_TO_VARSTR(buf, "TAG");
×
251
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
×
252
    ++(pBlock->info.rows);
×
253
  }
254
  if (pBlock->info.rows <= 0) {
1,460!
255
    qError("no permission to view any columns");
×
256
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
257
  }
258
  return TSDB_CODE_SUCCESS;
1,460✔
259
}
260

261
static int32_t execDescribe(bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode) {
1,460✔
262
  SDescribeStmt* pDesc = (SDescribeStmt*)pStmt;
1,460✔
263
  if (NULL == pDesc || NULL == pDesc->pMeta) {
1,460!
264
    return TSDB_CODE_INVALID_PARA;
×
265
  }
266
  int32_t numOfRows = TABLE_TOTAL_COL_NUM(pDesc->pMeta);
1,460✔
267

268
  SSDataBlock* pBlock = NULL;
1,460✔
269
  int32_t      code = buildDescResultDataBlock(&pBlock);
1,460✔
270
  if (TSDB_CODE_SUCCESS == code) {
1,460!
271
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
1,460✔
272
  }
273
  if (TSDB_CODE_SUCCESS == code) {
1,460!
274
    if (pDesc->pMeta) {
1,460!
275
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
1,460!
276
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
1,432✔
277
      } else if (hasRefCol(pDesc->pMeta->tableType) && pDesc->pMeta->colRef) {
28!
278
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_REF, pRsp);
2✔
279
      } else {
280
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
26✔
281
      }
282
    } else {
283
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
×
284
    }
285
  }
286
  (void)blockDataDestroy(pBlock);
1,460✔
287
  return code;
1,460✔
288
}
289

290
static int32_t execResetQueryCache() { return catalogClearCache(); }
6,320✔
291

292
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
21✔
293
  QRY_PARAM_CHECK(pOutput);
21!
294

295
  SSDataBlock* pBlock = NULL;
21✔
296
  int32_t      code = createDataBlock(&pBlock);
21✔
297
  if (code) {
21!
298
    return code;
×
299
  }
300

301
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_COLS, 1);
21✔
302
  code = blockDataAppendColInfo(pBlock, &infoData);
21✔
303
  if (TSDB_CODE_SUCCESS == code) {
21!
304
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_FIELD2_LEN, 2);
21✔
305
    code = blockDataAppendColInfo(pBlock, &infoData);
21✔
306
  }
307

308
  if (TSDB_CODE_SUCCESS == code) {
21!
309
    *pOutput = pBlock;
21✔
310
  } else {
311
    (void)blockDataDestroy(pBlock);
×
312
  }
313
  return code;
21✔
314
}
315

316
int64_t getValOfDiffPrecision(int8_t unit, int64_t val) {
×
317
  int64_t v = 0;
×
318
  switch (unit) {
×
319
    case 's':
×
320
      v = val / 1000;
×
321
      break;
×
322
    case 'm':
×
323
      v = val / tsTickPerMin[TSDB_TIME_PRECISION_MILLI];
×
324
      break;
×
325
    case 'h':
×
326
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 60);
×
327
      break;
×
328
    case 'd':
×
329
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60);
×
330
      break;
×
331
    case 'w':
×
332
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60 * 7);
×
333
      break;
×
334
    default:
×
335
      break;
×
336
  }
337

338
  return v;
×
339
}
340

341
static int32_t buildRetension(SArray* pRetension, char** ppRetentions) {
21✔
342
  size_t size = taosArrayGetSize(pRetension);
21✔
343
  if (size == 0) {
21!
344
    *ppRetentions = NULL;
21✔
345
    return TSDB_CODE_SUCCESS;
21✔
346
  }
347

348
  const int lMaxLen = 128;
×
349
  char*     p1 = taosMemoryCalloc(1, lMaxLen);
×
350
  if (NULL == p1) {
×
351
    return terrno;
×
352
  }
353
  int32_t len = 0;
×
354

355
  for (int32_t i = 0; i < size; ++i) {
×
356
    SRetention* p = TARRAY_GET_ELEM(pRetension, i);
×
357
    int64_t     v1 = getValOfDiffPrecision(p->freqUnit, p->freq);
×
358
    int64_t     v2 = getValOfDiffPrecision(p->keepUnit, p->keep);
×
359
    if (i == 0) {
×
360
      len += tsnprintf(p1 + len, lMaxLen - len, "-:%" PRId64 "%c", v2, p->keepUnit);
×
361
    } else {
362
      len += tsnprintf(p1 + len, lMaxLen - len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit);
×
363
    }
364

365
    if (i < size - 1) {
×
366
      len += tsnprintf(p1 + len, lMaxLen - len, ",");
×
367
    }
368
  }
369

370
  *ppRetentions = p1;
×
371
  return TSDB_CODE_SUCCESS;
×
372
}
373

374
static const char* cacheModelStr(int8_t cacheModel) {
21✔
375
  switch (cacheModel) {
21!
376
    case TSDB_CACHE_MODEL_NONE:
21✔
377
      return TSDB_CACHE_MODEL_NONE_STR;
21✔
378
    case TSDB_CACHE_MODEL_LAST_ROW:
×
379
      return TSDB_CACHE_MODEL_LAST_ROW_STR;
×
380
    case TSDB_CACHE_MODEL_LAST_VALUE:
×
381
      return TSDB_CACHE_MODEL_LAST_VALUE_STR;
×
382
    case TSDB_CACHE_MODEL_BOTH:
×
383
      return TSDB_CACHE_MODEL_BOTH_STR;
×
384
    default:
×
385
      break;
×
386
  }
387
  return TSDB_CACHE_MODEL_NONE_STR;
×
388
}
389

390
static const char* encryptAlgorithmStr(int8_t encryptAlgorithm) {
21✔
391
  switch (encryptAlgorithm) {
21!
392
    case TSDB_ENCRYPT_ALGO_NONE:
21✔
393
      return TSDB_ENCRYPT_ALGO_NONE_STR;
21✔
394
    case TSDB_ENCRYPT_ALGO_SM4:
×
395
      return TSDB_ENCRYPT_ALGO_SM4_STR;
×
396
    default:
×
397
      break;
×
398
  }
399
  return TSDB_CACHE_MODEL_NONE_STR;
×
400
}
401

402
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
4,437,040✔
403
  if (buffer == NULL || bufSize <= 0) {
4,437,040!
404
    return 0;
×
405
  }
406
  int32_t len = 0;
4,440,341✔
407
  if (timeInMinutes % 1440 == 0) {
4,440,341!
408
    int32_t days = timeInMinutes / 1440;
4,443,254✔
409
    len = tsnprintf(buffer, bufSize, "%dd", days);
4,443,254✔
410
  } else if (timeInMinutes % 60 == 0) {
×
411
    int32_t hours = timeInMinutes / 60;
3✔
412
    len = tsnprintf(buffer, bufSize, "%dh", hours);
3✔
413
  } else {
414
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
×
415
  }
416
  return len;
4,473,418✔
417
}
418

419
static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, char* dbFName, SDbCfgInfo* pCfg) {
21✔
420
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
21!
421
  pBlock->info.rows = 1;
21✔
422

423
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
21✔
424
  char             buf1[SHOW_CREATE_DB_RESULT_FIELD1_LEN] = {0};
21✔
425
  STR_TO_VARSTR(buf1, dbName);
21✔
426
  COL_DATA_SET_VAL_AND_CHECK(pCol1, 0, buf1, false);
21!
427

428
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
21✔
429
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
21✔
430
  int32_t          len = 0;
21✔
431
  char*            prec = NULL;
21✔
432
  switch (pCfg->precision) {
21!
433
    case TSDB_TIME_PRECISION_MILLI:
21✔
434
      prec = TSDB_TIME_PRECISION_MILLI_STR;
21✔
435
      break;
21✔
436
    case TSDB_TIME_PRECISION_MICRO:
×
437
      prec = TSDB_TIME_PRECISION_MICRO_STR;
×
438
      break;
×
439
    case TSDB_TIME_PRECISION_NANO:
×
440
      prec = TSDB_TIME_PRECISION_NANO_STR;
×
441
      break;
×
442
    default:
×
443
      prec = "none";
×
444
      break;
×
445
  }
446

447
  char* pRetentions = NULL;
21✔
448
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
21!
449
  int32_t dbFNameLen = strlen(dbFName);
21✔
450
  int32_t hashPrefix = 0;
21✔
451
  if (pCfg->hashPrefix > 0) {
21✔
452
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
2✔
453
  } else if (pCfg->hashPrefix < 0) {
19✔
454
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
3✔
455
  }
456
  char durationStr[128] = {0};
21✔
457
  char keep0Str[128] = {0};
21✔
458
  char keep1Str[128] = {0};
21✔
459
  char keep2Str[128] = {0};
21✔
460
  char compactIntervalStr[13] = {0};
21✔
461
  char compactStartTimeStr[13] = {0};
21✔
462
  char compactEndTimeStr[13] = {0};
21✔
463

464
  int32_t lenDuration = formatDurationOrKeep(durationStr, sizeof(durationStr), pCfg->daysPerFile);
21✔
465
  int32_t lenKeep0 = formatDurationOrKeep(keep0Str, sizeof(keep0Str), pCfg->daysToKeep0);
21✔
466
  int32_t lenKeep1 = formatDurationOrKeep(keep1Str, sizeof(keep1Str), pCfg->daysToKeep1);
21✔
467
  int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pCfg->daysToKeep2);
21✔
468
  UNUSED(formatDurationOrKeep(compactIntervalStr, sizeof(compactIntervalStr), pCfg->compactInterval));
21✔
469
  UNUSED(formatDurationOrKeep(compactStartTimeStr, sizeof(compactStartTimeStr), pCfg->compactStartTime));
21✔
470
  UNUSED(formatDurationOrKeep(compactEndTimeStr, sizeof(compactEndTimeStr), pCfg->compactEndTime));
21✔
471

472
  if (IS_SYS_DBNAME(dbName)) {
21!
473
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
474
                     "CREATE DATABASE `%s`", dbName);
×
475
  } else {
476
    len +=
21✔
477
        tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
21✔
478
                  "CREATE DATABASE `%s` BUFFER %d CACHESIZE %d CACHEMODEL '%s' COMP %d DURATION %s "
479
                  "WAL_FSYNC_PERIOD %d MAXROWS %d MINROWS %d STT_TRIGGER %d KEEP %s,%s,%s PAGES %d PAGESIZE %d "
480
                  "PRECISION '%s' REPLICA %d "
481
                  "WAL_LEVEL %d VGROUPS %d SINGLE_STABLE %d TABLE_PREFIX %d TABLE_SUFFIX %d TSDB_PAGESIZE %d "
482
                  "WAL_RETENTION_PERIOD %d WAL_RETENTION_SIZE %" PRId64
483
                  " KEEP_TIME_OFFSET %d ENCRYPT_ALGORITHM '%s' S3_CHUNKPAGES %d S3_KEEPLOCAL %dm S3_COMPACT %d "
484
                  "COMPACT_INTERVAL %s COMPACT_TIME_RANGE %s,%s COMPACT_TIME_OFFSET %"PRIi8 "h",
485
                  dbName, pCfg->buffer, pCfg->cacheSize, cacheModelStr(pCfg->cacheLast), pCfg->compression, durationStr,
21✔
486
                  pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str, keep1Str, keep2Str,
21✔
487
                  pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel, pCfg->numOfVgroups,
21✔
488
                  1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize, pCfg->walRetentionPeriod,
21✔
489
                  pCfg->walRetentionSize, pCfg->keepTimeOffset, encryptAlgorithmStr(pCfg->encryptAlgorithm),
21✔
490
                  pCfg->s3ChunkSize, pCfg->s3KeepLocal, pCfg->s3Compact, compactIntervalStr, compactStartTimeStr,
21✔
491
                  compactEndTimeStr, pCfg->compactTimeOffset);
21✔
492

493
    if (pRetentions) {
21!
494
      len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
495
                       " RETENTIONS %s", pRetentions);
496
    }
497
  }
498

499
  taosMemoryFree(pRetentions);
21!
500

501
  (varDataLen(buf2)) = len;
21✔
502

503
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
21!
504

505
  return TSDB_CODE_SUCCESS;
21✔
506
}
507

508
static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt, SRetrieveTableRsp** pRsp) {
21✔
509
  SSDataBlock* pBlock = NULL;
21✔
510
  int32_t      code = buildCreateDBResultDataBlock(&pBlock);
21✔
511
  if (TSDB_CODE_SUCCESS == code) {
21!
512
    code = setCreateDBResultIntoDataBlock(pBlock, pStmt->dbName, pStmt->dbFName, pStmt->pCfg);
21✔
513
  }
514
  if (TSDB_CODE_SUCCESS == code) {
21!
515
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_DB_RESULT_COLS, pRsp);
21✔
516
  }
517
  (void)blockDataDestroy(pBlock);
21✔
518
  return code;
21✔
519
}
520

521
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
169✔
522
  QRY_PARAM_CHECK(pOutput);
169!
523

524
  SSDataBlock* pBlock = NULL;
169✔
525
  int32_t      code = createDataBlock(&pBlock);
169✔
526
  if (code) {
169!
527
    return code;
×
528
  }
529

530
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD1_LEN, 1);
169✔
531
  code = blockDataAppendColInfo(pBlock, &infoData);
169✔
532
  if (TSDB_CODE_SUCCESS == code) {
169!
533
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD2_LEN, 2);
169✔
534
    code = blockDataAppendColInfo(pBlock, &infoData);
169✔
535
  }
536

537
  if (TSDB_CODE_SUCCESS == code) {
169!
538
    *pOutput = pBlock;
169✔
539
  } else {
540
    (void)blockDataDestroy(pBlock);
×
541
  }
542
  return code;
169✔
543
}
544

545
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
15✔
546
  QRY_PARAM_CHECK(pOutput);
15!
547

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

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

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

569
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
130✔
570
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
1,335✔
571
    SSchema* pSchema = pCfg->pSchemas + i;
1,205✔
572
    SColRef* pRef = pCfg->pColRefs + i;
1,205✔
573
#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
574
    char type[LTYPE_LEN];
575
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
1,205✔
576
    int typeLen = strlen(type);
1,205✔
577
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
1,205✔
578
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
1,106✔
579
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
102✔
580
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
1,103✔
581
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
51✔
582
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
51✔
583
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
1,052✔
584
      uint8_t precision, scale;
585
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
80✔
586
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
80✔
587
    }
588

589
    if (withExtSchema(pCfg->tableType) && pCfg->pSchemaExt) {
1,205!
590
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " ENCODE \'%s\'",
1,185✔
591
                           columnEncodeStr(COMPRESS_L1_TYPE_U32(pCfg->pSchemaExt[i].compress)));
1,185✔
592
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " COMPRESS \'%s\'",
1,185✔
593
                           columnCompressStr(COMPRESS_L2_TYPE_U32(pCfg->pSchemaExt[i].compress)));
1,185✔
594
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " LEVEL \'%s\'",
1,185✔
595
                           columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pCfg->pSchemaExt[i].compress)));
1,185✔
596
    }
597

598
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
1,205!
599
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " FROM `%s`", pRef->refDbName);
10✔
600
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
10✔
601
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", pRef->refTableName);
10✔
602
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
10✔
603
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", pRef->refColName);
10✔
604
    }
605

606
    if (!(pSchema->flags & COL_IS_KEY)) {
1,205✔
607
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1,204✔
608
                        "%s`%s` %s", ((i > 0) ? ", " : ""), pSchema->name, type);
1,204✔
609
    } else {
610
      char* pk = "PRIMARY KEY";
1✔
611
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
1✔
612
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), pSchema->name, type, pk);
1!
613
    }
614
  }
615
}
130✔
616

617
static void appendColRefFields(char* buf, int32_t* len, STableCfg* pCfg) {
1✔
618
  for (int32_t i = 1; i < pCfg->numOfColumns; ++i) {
20✔
619
    SSchema* pSchema = pCfg->pSchemas + i;
19✔
620
    SColRef* pRef = pCfg->pColRefs + i;
19✔
621
    char type[TSDB_COL_NAME_LEN + 10 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN];
622
    int typeLen = 0;
19✔
623

624
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
19!
625
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
5✔
626
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
5✔
627
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", pRef->refTableName);
5✔
628
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
5✔
629
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", pRef->refColName);
5✔
630
    } else {
631
      continue;
14✔
632
    }
633

634
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
5✔
635
                      "%s`%s` %s", ((i > 1) ? ", " : ""), pSchema->name, type);
5✔
636

637
  }
638
}
1✔
639

640
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
88✔
641
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
625✔
642
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
537✔
643
    char     type[32];
644
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
537✔
645
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
537✔
646
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
467✔
647
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
71✔
648
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
71✔
649
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
466✔
650
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
26✔
651
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
26✔
652
    }
653

654
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
537✔
655
                      "%s`%s` %s", ((i > 0) ? ", " : ""), pSchema->name, type);
537✔
656
  }
657
}
88✔
658

659
static void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
39✔
660
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
93✔
661
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
54✔
662
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
54✔
663
                      "%s`%s`", ((i > 0) ? ", " : ""), pSchema->name);
54✔
664
  }
665
}
39✔
666

667
static int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg, void* charsetCxt) {
39✔
668
  int32_t code = TSDB_CODE_SUCCESS;
39✔
669
  SArray* pTagVals = NULL;
39✔
670
  STag*   pTag = (STag*)pCfg->pTags;
39✔
671

672
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
39!
673
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
674
    return TSDB_CODE_APP_ERROR;
×
675
  }
676

677
  if (tTagIsJson(pTag)) {
39✔
678
    char* pJson = NULL;
4✔
679
    parseTagDatatoJson(pTag, &pJson, charsetCxt);
4✔
680
    if (NULL == pJson) {
4!
681
      qError("failed to parse tag to json, pJson is NULL");
×
682
      return terrno;
×
683
    }
684
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
4✔
685
                      "%s", pJson);
686
    taosMemoryFree(pJson);
4!
687

688
    return TSDB_CODE_SUCCESS;
4✔
689
  }
690

691
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
35!
692
  int16_t valueNum = taosArrayGetSize(pTagVals);
35✔
693
  int32_t num = 0;
35✔
694
  int32_t j = 0;
35✔
695
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
85✔
696
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
50✔
697
    if (i > 0) {
50✔
698
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
15✔
699
                        ", ");
700
    }
701

702
    if (j >= valueNum) {
50✔
703
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
21✔
704
                        "NULL");
705
      continue;
21✔
706
    }
707

708
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
29✔
709
    if (pSchema->colId > pTagVal->cid) {
29!
710
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
711
      code = TSDB_CODE_APP_ERROR;
×
712
      TAOS_CHECK_ERRNO(code);
×
713
    } else if (pSchema->colId == pTagVal->cid) {
29✔
714
      char    type = pTagVal->type;
27✔
715
      int32_t tlen = 0;
27✔
716

717
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
27✔
718
      if (leftSize <= 0) {
27!
719
        qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
×
720
        code = TSDB_CODE_APP_ERROR;
×
721
        TAOS_CHECK_ERRNO(code);
×
722
      }
723
      if (IS_VAR_DATA_TYPE(type)) {
27!
724
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
6✔
725
        TAOS_CHECK_ERRNO(code);
6!
726
      } else {
727
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
21✔
728
                               &tlen);
729
        TAOS_CHECK_ERRNO(code);
21!
730
      }
731
      *len += tlen;
27✔
732
      j++;
27✔
733
    } else {
734
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2✔
735
                        "NULL");
736
    }
737
  }
738
_exit:
35✔
739
  taosArrayDestroy(pTagVals);
35✔
740

741
  return code;
35✔
742
}
743

744
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
167✔
745
  if (pCfg->commentLen > 0) {
167!
746
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
747
                      " COMMENT '%s'", pCfg->pComment);
748
  } else if (0 == pCfg->commentLen) {
167!
749
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
750
                      " COMMENT ''");
751
  }
752

753
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
167!
754
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
755
                      " WATERMARK %" PRId64 "a", pCfg->watermark1);
756
    if (pCfg->watermark2 > 0) {
×
757
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
758
                        ", %" PRId64 "a", pCfg->watermark2);
759
    }
760
  }
761

762
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
167!
763
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
764
                      " MAX_DELAY %" PRId64 "a", pCfg->delay1);
765
    if (pCfg->delay2 > 0) {
×
766
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
767
                        ", %" PRId64 "a", pCfg->delay2);
768
    }
769
  }
770

771
  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
167✔
772
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
167!
773
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
774
                      " ROLLUP(");
775
    for (int32_t i = 0; i < funcNum; ++i) {
×
776
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
×
777
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
778
                        "%s%s", ((i > 0) ? ", " : ""), pFunc);
779
    }
780
    *len +=
×
781
        snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
×
782
  }
783

784
  if (pCfg->ttl > 0) {
167!
785
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
786
                      " TTL %d", pCfg->ttl);
787
  }
788

789
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
167✔
790
    int32_t nSma = 0;
129✔
791
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
1,314✔
792
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
1,185!
793
        ++nSma;
1,185✔
794
      }
795
    }
796

797
    if (nSma < pCfg->numOfColumns && nSma > 0) {
129!
798
      bool smaOn = false;
×
799
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
800
                        " SMA(");
801
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
802
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
803
          if (smaOn) {
×
804
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
805
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ",`%s`",
×
806
                              (pCfg->pSchemas + i)->name);
×
807
          } else {
808
            smaOn = true;
×
809
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
810
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), "`%s`",
×
811
                              (pCfg->pSchemas + i)->name);
×
812
          }
813
        }
814
      }
815
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, ")");
×
816
    }
817
  }
818

819
  if (pCfg->virtualStb) {
167!
820
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
821
                      " VIRTUAL %d", pCfg->virtualStb);
×
822
  }
823
}
167✔
824

825
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
169✔
826
                                              void* charsetCxt) {
827
  int32_t code = TSDB_CODE_SUCCESS;
169✔
828
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
169!
829
  pBlock->info.rows = 1;
169✔
830

831
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
169✔
832
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN] = {0};
169✔
833
  STR_TO_VARSTR(buf1, tbName);
169✔
834
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
169!
835

836
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
169✔
837
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
169!
838
  if (NULL == buf2) {
169!
839
    QRY_ERR_RET(terrno);
×
840
  }
841

842
  int32_t len = 0;
169✔
843

844
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
169✔
845
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
88✔
846
                     "CREATE STABLE `%s` (", tbName);
847
    appendColumnFields(buf2, &len, pCfg);
88✔
848
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
88✔
849
                     ") TAGS (");
850
    appendTagFields(buf2, &len, pCfg);
88✔
851
    len +=
88✔
852
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
88✔
853
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
88✔
854
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
81✔
855
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
76✔
856
                     "CREATE TABLE `%s` USING `%s` (", tbName, pCfg->stbName);
38✔
857
    appendTagNameFields(buf2, &len, pCfg);
38✔
858
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
38✔
859
                     ") TAGS (");
860
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
38✔
861
    TAOS_CHECK_ERRNO(code);
38!
862
    len +=
38✔
863
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
38✔
864
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
38✔
865
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType){
43✔
866
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
41✔
867
                     "CREATE TABLE `%s` (", tbName);
868
    appendColumnFields(buf2, &len, pCfg);
41✔
869
    len +=
41✔
870
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
41✔
871
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
41✔
872
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
2✔
873
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1✔
874
                     "CREATE VTABLE `%s` (", tbName);
875
    appendColumnFields(buf2, &len, pCfg);
1✔
876
    len +=
1✔
877
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1✔
878
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
1!
879
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1✔
880
                     "CREATE VTABLE `%s` (", tbName);
881
    appendColRefFields(buf2, &len, pCfg);
1✔
882
    len +=
1✔
883
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1✔
884
                 ") USING `%s` (", pCfg->stbName);
1✔
885
    appendTagNameFields(buf2, &len, pCfg);
1✔
886
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1✔
887
                     ") TAGS (");
888
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
1✔
889
    TAOS_CHECK_ERRNO(code);
1!
890
    len +=
1✔
891
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1✔
892
  }
893

894
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
169✔
895

896
  code = colDataSetVal(pCol2, 0, buf2, false);
169✔
897
  TAOS_CHECK_ERRNO(code);
169!
898

899
_exit:
169✔
900
  taosMemoryFree(buf2);
169!
901

902
  return code;
169✔
903
}
904

905
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
15✔
906
  int32_t code = 0;
15✔
907
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
15!
908
  pBlock->info.rows = 1;
15✔
909

910
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
15✔
911
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
15✔
912
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
15✔
913
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
15✔
914
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
15!
915

916
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
15✔
917
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
15!
918
  if (NULL == buf2) {
15!
919
    return terrno;
×
920
  }
921

922
  SViewMeta* pMeta = pStmt->pViewMeta;
15✔
923
  if (NULL == pMeta) {
15!
924
    qError("exception: view meta is null");
×
925
    return TSDB_CODE_APP_ERROR;
×
926
  }
927
  snprintf(varDataVal(buf2), SHOW_CREATE_VIEW_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE VIEW `%s`.`%s` AS %s",
15✔
928
           pStmt->dbName, pStmt->viewName, pMeta->querySql);
15✔
929
  int32_t len = strlen(varDataVal(buf2));
15✔
930
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
15✔
931
  code = colDataSetVal(pCol2, 0, buf2, false);
15✔
932
  taosMemoryFree(buf2);
15!
933

934
  return code;
15✔
935
}
936

937
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
167✔
938
  SSDataBlock* pBlock = NULL;
167✔
939
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
167✔
940
  if (TSDB_CODE_SUCCESS == code) {
167!
941
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
167✔
942
  }
943
  if (TSDB_CODE_SUCCESS == code) {
167!
944
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
167✔
945
  }
946
  (void)blockDataDestroy(pBlock);
167✔
947
  return code;
167✔
948
}
949

950
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
2✔
951
  SSDataBlock* pBlock = NULL;
2✔
952
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
2✔
953
  if (TSDB_CODE_SUCCESS == code) {
2!
954
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
2✔
955
  }
956
  if (TSDB_CODE_SUCCESS == code) {
2!
957
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
2✔
958
  }
959
  (void)blockDataDestroy(pBlock);
2✔
960
  return code;
2✔
961
}
962

963
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
2✔
964
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
2✔
965
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
2✔
966
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
1✔
967
    return terrno;
1✔
968
  }
969

970
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
1✔
971
}
972

973
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
15,724✔
974
  int32_t code = 0;
15,724✔
975

976
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
15,724✔
977
    taosResetLog();
1✔
978
    cfgDumpCfg(tsCfg, 0, false);
1✔
979
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
15,723!
980
    int32_t tmp = 0;
×
981
    code = taosStr2int32(value, &tmp);
×
982
    if (code) {
×
983
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
984
      return code;
×
985
    }
986
    code = schedulerUpdatePolicy(tmp);
×
987
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
15,723!
988
    int32_t tmp = 0;
×
989
    code = taosStr2int32(value, &tmp);
×
990
    if (code) {
×
991
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
992
      return code;
×
993
    }
994
    code = schedulerEnableReSchedule(tmp != 0);
×
995
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
15,723!
996
    code = ctgdHandleDbgCommand(value);
×
997
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
15,723!
998
    code = taosMemoryDbgInit();
×
999
    if (code) {
×
1000
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
1001
      return code;
×
1002
    }
1003
    tsAsyncLog = false;
×
1004
    qInfo("memory dbg enabled");
×
1005
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
15,723!
1006
    code = taosMemoryDbgInitRestore();
×
1007
    if (code) {
×
1008
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
1009
      return code;
×
1010
    }
1011
    qInfo("memory dbg disabled");
×
1012
  } else {
1013
    goto _return;
15,723✔
1014
  }
1015

1016
  *processed = true;
1✔
1017

1018
_return:
15,724✔
1019

1020
  if (code) {
15,724!
1021
    terrno = code;
×
1022
  }
1023

1024
  return code;
15,724✔
1025
}
1026

1027
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
15,724✔
1028
  bool processed = false;
15,724✔
1029

1030
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
15,724!
1031
    return terrno;
×
1032
  }
1033

1034
  if (processed) {
15,724✔
1035
    goto _return;
1✔
1036
  }
1037

1038
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
15,723✔
1039
    return terrno;
2✔
1040
  }
1041

1042
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
15,721!
1043
    return terrno;
×
1044
  }
1045

1046
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
15,721!
1047

1048
_return:
15,721✔
1049

1050
  return TSDB_CODE_SUCCESS;
15,722✔
1051
}
1052

1053
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
514✔
1054
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
514!
1055
  if (NULL == pBlock) {
514!
1056
    return terrno;
×
1057
  }
1058

1059
  pBlock->info.hasVarCol = true;
514✔
1060

1061
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
514✔
1062
  if (NULL == pBlock->pDataBlock) {
514!
1063
    taosMemoryFree(pBlock);
×
1064
    return terrno;
×
1065
  }
1066

1067
  SColumnInfoData infoData = {0};
514✔
1068

1069
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
514✔
1070
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
514✔
1071
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,028!
1072
    goto _exit;
×
1073
  }
1074

1075
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
514✔
1076
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
514✔
1077
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,028!
1078
    goto _exit;
×
1079
  }
1080

1081
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
514✔
1082
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
514✔
1083
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,028!
1084
    goto _exit;
×
1085
  }
1086

1087
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
514✔
1088
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
514✔
1089
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,028!
1090
    goto _exit;
×
1091
  }
1092

1093
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
514✔
1094
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
514✔
1095
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
1,028!
1096
    goto _exit;
×
1097
  }
1098

1099
  *pOutput = pBlock;
514✔
1100

1101
_exit:
514✔
1102
  if (terrno != TSDB_CODE_SUCCESS) {
514!
1103
    taosArrayDestroy(pBlock->pDataBlock);
×
1104
    taosMemoryFree(pBlock);
×
1105
  }
1106
  return terrno;
514✔
1107
}
1108

1109
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
514✔
1110
  SSDataBlock* pBlock = NULL;
514✔
1111
  char*        likePattern = NULL;
514✔
1112
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
514✔
1113
  if (TSDB_CODE_SUCCESS == code) {
514!
1114
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
514!
1115
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
×
1116
    }
1117
  }
1118
  if (TSDB_CODE_SUCCESS == code) {
514!
1119
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
514✔
1120
  }
1121
  if (TSDB_CODE_SUCCESS == code) {
514!
1122
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
514✔
1123
  }
1124
  (void)blockDataDestroy(pBlock);
514✔
1125
  return code;
514✔
1126
}
1127

1128
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
93,867✔
1129
  QRY_PARAM_CHECK(pOutput);
93,867!
1130

1131
  SSDataBlock* pBlock = NULL;
93,867✔
1132
  int32_t      code = createDataBlock(&pBlock);
93,867✔
1133
  if (code) {
93,867!
1134
    return code;
×
1135
  }
1136

1137
  SNode* pProj = NULL;
93,867✔
1138
  FOREACH(pProj, pProjects) {
191,771!
1139
    SExprNode*      pExpr = (SExprNode*)pProj;
97,904✔
1140
    SColumnInfoData infoData = {0};
97,904✔
1141
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
97,904!
1142
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1143
      infoData.info.bytes = 0;
×
1144
    } else {
1145
      infoData.info.type = pExpr->resType.type;
97,904✔
1146
      infoData.info.bytes = pExpr->resType.bytes;
97,904✔
1147
      infoData.info.precision = pExpr->resType.precision;
97,904✔
1148
      infoData.info.scale = pExpr->resType.scale;
97,904✔
1149
    }
1150
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
97,904!
1151
  }
1152

1153
  *pOutput = pBlock;
93,867✔
1154
  return code;
93,867✔
1155
}
1156

1157
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
93,867✔
1158
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
93,867!
1159

1160
  int32_t index = 0;
93,867✔
1161
  SNode*  pProj = NULL;
93,867✔
1162
  FOREACH(pProj, pProjects) {
191,771!
1163
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
97,904!
1164
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1165
    } else {
1166
      if (((SValueNode*)pProj)->isNull) {
97,904✔
1167
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
135!
1168
      } else {
1169
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
97,769!
1170
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1171
      }
1172
    }
1173
  }
1174

1175
  pBlock->info.rows = 1;
93,867✔
1176
  return TSDB_CODE_SUCCESS;
93,867✔
1177
}
1178

1179
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
93,867✔
1180
  SSDataBlock* pBlock = NULL;
93,867✔
1181
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
93,867✔
1182
  if (TSDB_CODE_SUCCESS == code) {
93,867!
1183
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
93,867✔
1184
  }
1185
  if (TSDB_CODE_SUCCESS == code) {
93,867!
1186
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
93,867!
1187
  }
1188
  (void)blockDataDestroy(pBlock);
93,867✔
1189
  return code;
93,867✔
1190
}
1191

1192
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
15✔
1193
  SSDataBlock* pBlock = NULL;
15✔
1194
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
15✔
1195
  if (TSDB_CODE_SUCCESS == code) {
15!
1196
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
15✔
1197
  }
1198
  if (TSDB_CODE_SUCCESS == code) {
15!
1199
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
15✔
1200
  }
1201
  (void)blockDataDestroy(pBlock);
15✔
1202
  return code;
15✔
1203
}
1204

1205
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
118,091✔
1206
                     void* charsetCxt) {
1207
  switch (nodeType(pStmt)) {
118,091!
1208
    case QUERY_NODE_DESCRIBE_STMT:
1,460✔
1209
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
1,460✔
1210
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
6,320✔
1211
      return execResetQueryCache();
6,320✔
1212
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
21✔
1213
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
21✔
1214
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
166✔
1215
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
166✔
1216
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
2✔
1217
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
2✔
1218
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
2✔
1219
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
2✔
1220
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
15✔
1221
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
15✔
1222
    case QUERY_NODE_ALTER_LOCAL_STMT:
15,724✔
1223
      return execAlterLocal((SAlterLocalStmt*)pStmt);
15,724✔
1224
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
514✔
1225
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
514✔
1226
    case QUERY_NODE_SELECT_STMT:
93,867✔
1227
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
93,867✔
1228
    default:
×
1229
      break;
×
1230
  }
1231
  return TSDB_CODE_FAILED;
×
1232
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc