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

taosdata / TDengine / #3660

15 Mar 2025 09:06AM UTC coverage: 62.039% (-1.3%) from 63.314%
#3660

push

travis-ci

web-flow
feat(stream): support stream processing for virtual tables (#30144)

* enh: add client processing

* enh: add mnode vtables processing

* enh: add mnode vtable processing

* enh: add normal child vtable support

* fix: compile issues

* fix: compile issues

* fix: create stream issues

* fix: multi stream scan issue

* fix: remove debug info

* fix: agg task and task level issues

* fix: correct task output type

* fix: split vtablescan from agg

* fix: memory leak issues

* fix: add limitations

* Update 09-error-code.md

* Update 09-error-code.md

* fix: remove usless case

* feat(stream): extract original table data in source scan task

Implemented functionality in the source task to extract data
corresponding to the virtual table from the original table using WAL.
The extracted data is then sent to the downstream merge task for further
processing.

* feat(stream): multi-way merge using loser tree in virtual merge task

Implemented multi-way merge in the merge task using a loser tree to
combine data from multiple original table into a single virtual table.
The merged virtual table data is then pushed downstream for further
processing.  Introduced memory limit handling during the merge process
with configurable behavior when the memory limit is reached.

* fix(test): remove useless cases

---------

Co-authored-by: dapan1121 <wpan@taosdata.com>
Co-authored-by: Pan Wei <72057773+dapan1121@users.noreply.github.com>

154078 of 317582 branches covered (48.52%)

Branch coverage included in aggregate %.

313 of 2391 new or added lines in 34 files covered. (13.09%)

26134 existing lines in 205 files now uncovered.

240261 of 318051 relevant lines covered (75.54%)

16655189.27 hits per line

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

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

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

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

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

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

73
  return TSDB_CODE_SUCCESS;
97,090✔
74
}
75

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

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

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

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

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

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

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

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

178
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
75,662!
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);
74,497✔
186
    }
187
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
75,662!
188
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
75,661✔
189
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
75,660!
190
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
75,661✔
191
      if (i >= pMeta->tableInfo.numOfColumns) {
75,601✔
192
        STR_TO_VARSTR(buf, "TAG");
9,688✔
193
        fillTagCol = 1;
9,688✔
194
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
65,913✔
195
        STR_TO_VARSTR(buf, "PRIMARY KEY")
13✔
196
      } else {
197
        STR_TO_VARSTR(buf, "");
65,900✔
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);
75,661!
203
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
75,662!
204
      if (i < pMeta->tableInfo.numOfColumns) {
75,506✔
205
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
65,825✔
206
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
65,824!
207
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
65,823✔
208
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
65,822!
209
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
65,825✔
210
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
65,823!
211
      } else {
212
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
9,681!
213
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
9,681!
214
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
9,682!
215
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
9,682!
216
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
9,682!
217
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
9,682!
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;
75,660✔
240

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

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

268
  SSDataBlock* pBlock = NULL;
1,479✔
269
  int32_t      code = buildDescResultDataBlock(&pBlock);
1,479✔
270
  if (TSDB_CODE_SUCCESS == code) {
1,479!
271
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
1,479✔
272
  }
273
  if (TSDB_CODE_SUCCESS == code) {
1,479!
274
    if (pDesc->pMeta) {
1,479!
275
      if (withExtSchema(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
1,479!
276
        code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
1,451✔
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,478✔
287
  return code;
1,478✔
288
}
289

290
static int32_t execResetQueryCache() { return catalogClearCache(); }
6,319✔
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!
UNCOV
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) {
×
UNCOV
319
    case 's':
×
320
      v = val / 1000;
×
UNCOV
321
      break;
×
UNCOV
322
    case 'm':
×
323
      v = val / tsTickPerMin[TSDB_TIME_PRECISION_MILLI];
×
324
      break;
×
UNCOV
325
    case 'h':
×
UNCOV
326
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 60);
×
UNCOV
327
      break;
×
328
    case 'd':
×
329
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60);
×
UNCOV
330
      break;
×
UNCOV
331
    case 'w':
×
UNCOV
332
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60 * 7);
×
UNCOV
333
      break;
×
UNCOV
334
    default:
×
UNCOV
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

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

355
  for (int32_t i = 0; i < size; ++i) {
×
UNCOV
356
    SRetention* p = TARRAY_GET_ELEM(pRetension, i);
×
357
    int64_t     v1 = getValOfDiffPrecision(p->freqUnit, p->freq);
×
UNCOV
358
    int64_t     v2 = getValOfDiffPrecision(p->keepUnit, p->keep);
×
UNCOV
359
    if (i == 0) {
×
UNCOV
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

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

UNCOV
370
  *ppRetentions = p1;
×
UNCOV
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✔
UNCOV
378
    case TSDB_CACHE_MODEL_LAST_ROW:
×
UNCOV
379
      return TSDB_CACHE_MODEL_LAST_ROW_STR;
×
UNCOV
380
    case TSDB_CACHE_MODEL_LAST_VALUE:
×
UNCOV
381
      return TSDB_CACHE_MODEL_LAST_VALUE_STR;
×
UNCOV
382
    case TSDB_CACHE_MODEL_BOTH:
×
UNCOV
383
      return TSDB_CACHE_MODEL_BOTH_STR;
×
UNCOV
384
    default:
×
UNCOV
385
      break;
×
386
  }
UNCOV
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,583,701✔
403
  if (buffer == NULL || bufSize <= 0) {
4,583,701!
UNCOV
404
    return 0;
×
405
  }
406
  int32_t len = 0;
4,584,241✔
407
  if (timeInMinutes % 1440 == 0) {
4,584,241!
408
    int32_t days = timeInMinutes / 1440;
4,584,595✔
409
    len = tsnprintf(buffer, bufSize, "%dd", days);
4,584,595✔
UNCOV
410
  } else if (timeInMinutes % 60 == 0) {
×
411
    int32_t hours = timeInMinutes / 60;
3✔
412
    len = tsnprintf(buffer, bufSize, "%dh", hours);
3✔
413
  } else {
UNCOV
414
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
×
415
  }
416
  return len;
4,588,319✔
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✔
UNCOV
436
    case TSDB_TIME_PRECISION_MICRO:
×
UNCOV
437
      prec = TSDB_TIME_PRECISION_MICRO_STR;
×
UNCOV
438
      break;
×
UNCOV
439
    case TSDB_TIME_PRECISION_NANO:
×
UNCOV
440
      prec = TSDB_TIME_PRECISION_NANO_STR;
×
UNCOV
441
      break;
×
UNCOV
442
    default:
×
UNCOV
443
      prec = "none";
×
UNCOV
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!
UNCOV
473
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
UNCOV
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!
UNCOV
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!
UNCOV
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 {
UNCOV
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!
UNCOV
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 {
UNCOV
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!
UNCOV
673
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
UNCOV
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!
UNCOV
681
      qError("failed to parse tag to json, pJson is NULL");
×
UNCOV
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!
UNCOV
710
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
UNCOV
711
      code = TSDB_CODE_APP_ERROR;
×
UNCOV
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;
×
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
754
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
755
                      " WATERMARK %" PRId64 "a", pCfg->watermark1);
UNCOV
756
    if (pCfg->watermark2 > 0) {
×
UNCOV
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!
UNCOV
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);
UNCOV
765
    if (pCfg->delay2 > 0) {
×
UNCOV
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!
UNCOV
773
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
774
                      " ROLLUP(");
UNCOV
775
    for (int32_t i = 0; i < funcNum; ++i) {
×
UNCOV
776
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
×
UNCOV
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
    }
UNCOV
780
    *len +=
×
UNCOV
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!
UNCOV
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!
UNCOV
798
      bool smaOn = false;
×
UNCOV
799
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
800
                        " SMA(");
UNCOV
801
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
UNCOV
802
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
UNCOV
803
          if (smaOn) {
×
UNCOV
804
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
UNCOV
805
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ",`%s`",
×
UNCOV
806
                              (pCfg->pSchemas + i)->name);
×
807
          } else {
UNCOV
808
            smaOn = true;
×
UNCOV
809
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
UNCOV
810
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), "`%s`",
×
UNCOV
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!
UNCOV
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!
UNCOV
919
    return terrno;
×
920
  }
921

922
  SViewMeta* pMeta = pStmt->pViewMeta;
15✔
923
  if (NULL == pMeta) {
15!
UNCOV
924
    qError("exception: view meta is null");
×
UNCOV
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,492✔
974
  int32_t code = 0;
15,492✔
975

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

1016
  *processed = true;
1✔
1017

1018
_return:
15,492✔
1019

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

1024
  return code;
15,492✔
1025
}
1026

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

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

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

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

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

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

1048
_return:
15,489✔
1049

1050
  return TSDB_CODE_SUCCESS;
15,490✔
1051
}
1052

1053
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
514✔
1054
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
514!
1055
  if (NULL == pBlock) {
514!
UNCOV
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!
UNCOV
1063
    taosMemoryFree(pBlock);
×
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
1096
    goto _exit;
×
1097
  }
1098

1099
  *pOutput = pBlock;
514✔
1100

1101
_exit:
514✔
1102
  if (terrno != TSDB_CODE_SUCCESS) {
514!
UNCOV
1103
    taosArrayDestroy(pBlock->pDataBlock);
×
UNCOV
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!
UNCOV
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) {
94,893✔
1129
  QRY_PARAM_CHECK(pOutput);
94,893!
1130

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

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

1153
  *pOutput = pBlock;
94,893✔
1154
  return code;
94,893✔
1155
}
1156

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

1160
  int32_t index = 0;
94,893✔
1161
  SNode*  pProj = NULL;
94,893✔
1162
  FOREACH(pProj, pProjects) {
193,823!
1163
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
98,930!
UNCOV
1164
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1165
    } else {
1166
      if (((SValueNode*)pProj)->isNull) {
98,930✔
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,
98,795!
1170
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1171
      }
1172
    }
1173
  }
1174

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

1179
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
94,893✔
1180
  SSDataBlock* pBlock = NULL;
94,893✔
1181
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
94,893✔
1182
  if (TSDB_CODE_SUCCESS == code) {
94,893!
1183
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
94,893✔
1184
  }
1185
  if (TSDB_CODE_SUCCESS == code) {
94,893!
1186
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
94,893!
1187
  }
1188
  (void)blockDataDestroy(pBlock);
94,893✔
1189
  return code;
94,893✔
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,903✔
1206
                     void* charsetCxt) {
1207
  switch (nodeType(pStmt)) {
118,903!
1208
    case QUERY_NODE_DESCRIBE_STMT:
1,479✔
1209
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
1,479✔
1210
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
6,319✔
1211
      return execResetQueryCache();
6,319✔
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,492✔
1223
      return execAlterLocal((SAlterLocalStmt*)pStmt);
15,492✔
1224
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
514✔
1225
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
514✔
1226
    case QUERY_NODE_SELECT_STMT:
94,893✔
1227
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
94,893✔
UNCOV
1228
    default:
×
UNCOV
1229
      break;
×
1230
  }
UNCOV
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