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

taosdata / TDengine / #3530

16 Nov 2024 07:44AM UTC coverage: 60.219% (-0.7%) from 60.888%
#3530

push

travis-ci

web-flow
Update 03-ad.md

118417 of 252124 branches covered (46.97%)

Branch coverage included in aggregate %.

198982 of 274951 relevant lines covered (72.37%)

6072359.98 hits per line

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

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

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

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

35
extern SConfig* tsCfg;
36

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

48
  (*pRsp)->useconds = 0;
74,080✔
49
  (*pRsp)->completed = 1;
74,080✔
50
  (*pRsp)->precision = 0;
74,080✔
51
  (*pRsp)->compressed = 0;
74,080✔
52

53
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
74,080✔
54
  (*pRsp)->numOfCols = htonl(numOfCols);
74,081✔
55

56
  int32_t len = blockEncode(pBlock, (*pRsp)->data + PAYLOAD_PREFIX_LEN, dataEncodeBufSize, numOfCols);
74,081✔
57
  if(len < 0) {
74,081!
58
    taosMemoryFree(*pRsp);
×
59
    *pRsp = NULL;
×
60
    return terrno;
×
61
  }
62
  SET_PAYLOAD_LEN((*pRsp)->data, len, len);
74,081✔
63

64
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
74,081✔
65
  (*pRsp)->payloadLen = htonl(payloadLen);
74,081✔
66
  (*pRsp)->compLen = htonl(payloadLen);
74,081✔
67

68
  return TSDB_CODE_SUCCESS;
74,081✔
69
}
70

71
static int32_t getSchemaBytes(const SSchema* pSchema) {
116,803✔
72
  switch (pSchema->type) {
116,803✔
73
    case TSDB_DATA_TYPE_BINARY:
8,073✔
74
    case TSDB_DATA_TYPE_VARBINARY:
75
    case TSDB_DATA_TYPE_GEOMETRY:
76
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
8,073✔
77
    case TSDB_DATA_TYPE_NCHAR:
8,767✔
78
    case TSDB_DATA_TYPE_JSON:
79
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
8,767✔
80
    default:
99,963✔
81
      return pSchema->bytes;
99,963✔
82
  }
83
}
84

85
static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) {
2,787✔
86
  QRY_PARAM_CHECK(pOutput);
2,787!
87

88
  SSDataBlock* pBlock = NULL;
2,787✔
89
  int32_t      code = createDataBlock(&pBlock);
2,787✔
90
  if (code) {
2,787!
91
    return code;
×
92
  }
93

94
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_FIELD_LEN, 1);
2,787✔
95
  code = blockDataAppendColInfo(pBlock, &infoData);
2,787✔
96
  if (TSDB_CODE_SUCCESS == code) {
2,787!
97
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_TYPE_LEN, 2);
2,787✔
98
    code = blockDataAppendColInfo(pBlock, &infoData);
2,787✔
99
  }
100
  if (TSDB_CODE_SUCCESS == code) {
2,787!
101
    infoData = createColumnInfoData(TSDB_DATA_TYPE_INT, tDataTypes[TSDB_DATA_TYPE_INT].bytes, 3);
2,787✔
102
    code = blockDataAppendColInfo(pBlock, &infoData);
2,787✔
103
  }
104
  if (TSDB_CODE_SUCCESS == code) {
2,787!
105
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_NOTE_LEN, 4);
2,787✔
106
    code = blockDataAppendColInfo(pBlock, &infoData);
2,787✔
107
  }
108
  if (TSDB_CODE_SUCCESS == code) {
2,787!
109
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 5);
2,787✔
110
    code = blockDataAppendColInfo(pBlock, &infoData);
2,787✔
111
  }
112
  if (TSDB_CODE_SUCCESS == code) {
2,787!
113
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 6);
2,787✔
114
    code = blockDataAppendColInfo(pBlock, &infoData);
2,787✔
115
  }
116
  if (TSDB_CODE_SUCCESS == code) {
2,787!
117
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_COPRESS_OPTION_LEN, 7);
2,787✔
118
    code = blockDataAppendColInfo(pBlock, &infoData);
2,787✔
119
  }
120

121
  if (TSDB_CODE_SUCCESS == code) {
2,787!
122
    *pOutput = pBlock;
2,787✔
123
  } else {
124
    (void)blockDataDestroy(pBlock);
×
125
  }
126
  return code;
2,787✔
127
}
128

129
static int32_t setDescResultIntoDataBlock(bool sysInfoUser, SSDataBlock* pBlock, int32_t numOfRows, STableMeta* pMeta,
2,787✔
130
                                          int8_t biMode) {
131
  int32_t blockCap = (biMode != 0) ? numOfRows + 1 : numOfRows;
2,787!
132
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, blockCap));
2,787!
133
  pBlock->info.rows = 0;
2,787✔
134

135
  // field
136
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
2,787✔
137
  // Type
138
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
2,787✔
139
  // Length
140
  SColumnInfoData* pCol3 = taosArrayGet(pBlock->pDataBlock, 2);
2,787✔
141
  // Note
142
  SColumnInfoData* pCol4 = taosArrayGet(pBlock->pDataBlock, 3);
2,787✔
143
  // encode
144
  SColumnInfoData* pCol5 = NULL;
2,786✔
145
  // compress
146
  SColumnInfoData* pCol6 = NULL;
2,786✔
147
  // level
148
  SColumnInfoData* pCol7 = NULL;
2,786✔
149
  if (useCompress(pMeta->tableType)) {
2,786✔
150
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
2,769✔
151
    pCol6 = taosArrayGet(pBlock->pDataBlock, 5);
2,769✔
152
    pCol7 = taosArrayGet(pBlock->pDataBlock, 6);
2,769✔
153
  }
154

155
  int32_t fillTagCol = 0;
2,786✔
156
  char    buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
2,786✔
157
  for (int32_t i = 0; i < numOfRows; ++i) {
119,589✔
158
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
116,802!
159
      continue;
×
160
    }
161
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
116,803✔
162
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
116,803!
163

164
    STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
116,802✔
165
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
116,802!
166
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
116,803✔
167
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
116,803!
168
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
116,803✔
169
      if (i >= pMeta->tableInfo.numOfColumns) {
116,743✔
170
        STR_TO_VARSTR(buf, "TAG");
6,721✔
171
        fillTagCol = 1;
6,721✔
172
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
110,022✔
173
        STR_TO_VARSTR(buf, "PRIMARY KEY")
12✔
174
      } else {
175
        STR_TO_VARSTR(buf, "");
110,010✔
176
      }
177
    } else {
178
      STR_TO_VARSTR(buf, "VIEW COL");
60✔
179
    }
180
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
116,803!
181
    if (useCompress(pMeta->tableType) && pMeta->schemaExt) {
116,804!
182
      if (i < pMeta->tableInfo.numOfColumns) {
116,735✔
183
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
110,014✔
184
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
110,014!
185
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
110,014✔
186
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
110,013!
187
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
110,014✔
188
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
110,014!
189
      } else {
190
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
6,721!
191
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
6,721!
192
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
6,721!
193
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
6,721!
194
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
6,721!
195
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
6,721✔
196
      }
197
    }
198

199
    fillTagCol = 0;
116,803✔
200

201
    ++(pBlock->info.rows);
116,803✔
202
  }
203
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
2,787!
204
    STR_TO_VARSTR(buf, "tbname");
×
205
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
×
206
    STR_TO_VARSTR(buf, "VARCHAR");
×
207
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
×
208
    int32_t bytes = TSDB_TABLE_NAME_LEN - 1;
×
209
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
×
210
    STR_TO_VARSTR(buf, "TAG");
×
211
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
×
212
    ++(pBlock->info.rows);
×
213
  }
214
  if (pBlock->info.rows <= 0) {
2,787!
215
    qError("no permission to view any columns");
×
216
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
217
  }
218
  return TSDB_CODE_SUCCESS;
2,787✔
219
}
220

221
static int32_t execDescribe(bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode) {
2,787✔
222
  SDescribeStmt* pDesc = (SDescribeStmt*)pStmt;
2,787✔
223
  if (NULL == pDesc || NULL == pDesc->pMeta) {
2,787!
224
    return TSDB_CODE_INVALID_PARA;
×
225
  }
226
  int32_t        numOfRows = TABLE_TOTAL_COL_NUM(pDesc->pMeta);
2,787✔
227

228
  SSDataBlock* pBlock = NULL;
2,787✔
229
  int32_t      code = buildDescResultDataBlock(&pBlock);
2,787✔
230
  if (TSDB_CODE_SUCCESS == code) {
2,787!
231
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta, biMode);
2,787✔
232
  }
233
  if (TSDB_CODE_SUCCESS == code) {
2,787!
234
    if (pDesc->pMeta && useCompress(pDesc->pMeta->tableType) && pDesc->pMeta->schemaExt) {
2,787!
235
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS_COMPRESS, pRsp);
2,770✔
236
    } else {
237
      code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
17✔
238
    }
239
  }
240
  (void)blockDataDestroy(pBlock);
2,786✔
241
  return code;
2,787✔
242
}
243

244
static int32_t execResetQueryCache() { return catalogClearCache(); }
7,732✔
245

246
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
21✔
247
  QRY_PARAM_CHECK(pOutput);
21!
248

249
  SSDataBlock* pBlock = NULL;
21✔
250
  int32_t      code = createDataBlock(&pBlock);
21✔
251
  if (code) {
21!
252
    return code;
×
253
  }
254

255
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_COLS, 1);
21✔
256
  code = blockDataAppendColInfo(pBlock, &infoData);
21✔
257
  if (TSDB_CODE_SUCCESS == code) {
21!
258
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_FIELD2_LEN, 2);
21✔
259
    code = blockDataAppendColInfo(pBlock, &infoData);
21✔
260
  }
261

262
  if (TSDB_CODE_SUCCESS == code) {
21!
263
    *pOutput = pBlock;
21✔
264
  } else {
265
    (void)blockDataDestroy(pBlock);
×
266
  }
267
  return code;
21✔
268
}
269

270
int64_t getValOfDiffPrecision(int8_t unit, int64_t val) {
×
271
  int64_t v = 0;
×
272
  switch (unit) {
×
273
    case 's':
×
274
      v = val / 1000;
×
275
      break;
×
276
    case 'm':
×
277
      v = val / tsTickPerMin[TSDB_TIME_PRECISION_MILLI];
×
278
      break;
×
279
    case 'h':
×
280
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 60);
×
281
      break;
×
282
    case 'd':
×
283
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60);
×
284
      break;
×
285
    case 'w':
×
286
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60 * 7);
×
287
      break;
×
288
    default:
×
289
      break;
×
290
  }
291

292
  return v;
×
293
}
294

295
static int32_t buildRetension(SArray* pRetension, char** ppRetentions) {
21✔
296
  size_t size = taosArrayGetSize(pRetension);
21✔
297
  if (size == 0) {
21!
298
    *ppRetentions = NULL;
21✔
299
    return TSDB_CODE_SUCCESS;
21✔
300
  }
301

302
  const int lMaxLen = 128;
×
303
  char*     p1 = taosMemoryCalloc(1, lMaxLen);
×
304
  if (NULL == p1) {
×
305
    return terrno;
×
306
  }
307
  int32_t len = 0;
×
308

309
  for (int32_t i = 0; i < size; ++i) {
×
310
    SRetention* p = TARRAY_GET_ELEM(pRetension, i);
×
311
    int64_t     v1 = getValOfDiffPrecision(p->freqUnit, p->freq);
×
312
    int64_t     v2 = getValOfDiffPrecision(p->keepUnit, p->keep);
×
313
    if (i == 0) {
×
314
      len += tsnprintf(p1 + len, lMaxLen - len, "-:%" PRId64 "%c", v2, p->keepUnit);
×
315
    } else {
316
      len += tsnprintf(p1 + len, lMaxLen - len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit);
×
317
    }
318

319
    if (i < size - 1) {
×
320
      len += tsnprintf(p1 + len, lMaxLen - len, ",");
×
321
    }
322
  }
323

324
  *ppRetentions = p1;
×
325
  return TSDB_CODE_SUCCESS;
×
326
}
327

328
static const char* cacheModelStr(int8_t cacheModel) {
19✔
329
  switch (cacheModel) {
19!
330
    case TSDB_CACHE_MODEL_NONE:
19✔
331
      return TSDB_CACHE_MODEL_NONE_STR;
19✔
332
    case TSDB_CACHE_MODEL_LAST_ROW:
×
333
      return TSDB_CACHE_MODEL_LAST_ROW_STR;
×
334
    case TSDB_CACHE_MODEL_LAST_VALUE:
×
335
      return TSDB_CACHE_MODEL_LAST_VALUE_STR;
×
336
    case TSDB_CACHE_MODEL_BOTH:
×
337
      return TSDB_CACHE_MODEL_BOTH_STR;
×
338
    default:
×
339
      break;
×
340
  }
341
  return TSDB_CACHE_MODEL_NONE_STR;
×
342
}
343

344
static const char* encryptAlgorithmStr(int8_t encryptAlgorithm) {
19✔
345
  switch (encryptAlgorithm) {
19!
346
    case TSDB_ENCRYPT_ALGO_NONE:
19✔
347
      return TSDB_ENCRYPT_ALGO_NONE_STR;
19✔
348
    case TSDB_ENCRYPT_ALGO_SM4:
×
349
      return TSDB_ENCRYPT_ALGO_SM4_STR;
×
350
    default:
×
351
      break;
×
352
  }
353
  return TSDB_CACHE_MODEL_NONE_STR;
×
354
}
355

356
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
4,272✔
357
  if (buffer == NULL || bufSize <= 0) {
4,272!
358
    return 0;
×
359
  }
360
  int32_t len = 0;
4,272✔
361
  if (timeInMinutes % 1440 == 0) {
4,272!
362
    int32_t days = timeInMinutes / 1440;
4,272✔
363
    len = tsnprintf(buffer, bufSize, "%dd", days);
4,272✔
364
  } else if (timeInMinutes % 60 == 0) {
×
365
    int32_t hours = timeInMinutes / 60;
×
366
    len = tsnprintf(buffer, bufSize, "%dh", hours);
×
367
  } else {
368
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
×
369
  }
370
  return len;
4,272✔
371
}
372

373
static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, char* dbFName, SDbCfgInfo* pCfg) {
21✔
374
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
21!
375
  pBlock->info.rows = 1;
21✔
376

377
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
21✔
378
  char             buf1[SHOW_CREATE_DB_RESULT_FIELD1_LEN] = {0};
21✔
379
  STR_TO_VARSTR(buf1, dbName);
21✔
380
  COL_DATA_SET_VAL_AND_CHECK(pCol1, 0, buf1, false);
21!
381

382
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
21✔
383
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
21✔
384
  int32_t          len = 0;
21✔
385
  char*            prec = NULL;
21✔
386
  switch (pCfg->precision) {
21!
387
    case TSDB_TIME_PRECISION_MILLI:
21✔
388
      prec = TSDB_TIME_PRECISION_MILLI_STR;
21✔
389
      break;
21✔
390
    case TSDB_TIME_PRECISION_MICRO:
×
391
      prec = TSDB_TIME_PRECISION_MICRO_STR;
×
392
      break;
×
393
    case TSDB_TIME_PRECISION_NANO:
×
394
      prec = TSDB_TIME_PRECISION_NANO_STR;
×
395
      break;
×
396
    default:
×
397
      prec = "none";
×
398
      break;
×
399
  }
400

401
  char* pRetentions = NULL;
21✔
402
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
21!
403
  int32_t dbFNameLen = strlen(dbFName);
21✔
404
  int32_t hashPrefix = 0;
21✔
405
  if (pCfg->hashPrefix > 0) {
21✔
406
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
2✔
407
  } else if (pCfg->hashPrefix < 0) {
19✔
408
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
3✔
409
  }
410
  char durationStr[128] = {0};
21✔
411
  char keep0Str[128] = {0};
21✔
412
  char keep1Str[128] = {0};
21✔
413
  char keep2Str[128] = {0};
21✔
414

415
  int32_t lenDuration = formatDurationOrKeep(durationStr, sizeof(durationStr), pCfg->daysPerFile);
21✔
416
  int32_t lenKeep0 = formatDurationOrKeep(keep0Str, sizeof(keep0Str), pCfg->daysToKeep0);
21✔
417
  int32_t lenKeep1 = formatDurationOrKeep(keep1Str, sizeof(keep1Str), pCfg->daysToKeep1);
21✔
418
  int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pCfg->daysToKeep2);
21✔
419

420
  if (IS_SYS_DBNAME(dbName)) {
21!
421
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
2✔
422
                     "CREATE DATABASE `%s`", dbName);
2✔
423
  } else {
424
    len +=
19✔
425
        tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
19✔
426
                  "CREATE DATABASE `%s` BUFFER %d CACHESIZE %d CACHEMODEL '%s' COMP %d DURATION %s "
427
                  "WAL_FSYNC_PERIOD %d MAXROWS %d MINROWS %d STT_TRIGGER %d KEEP %s,%s,%s PAGES %d PAGESIZE %d "
428
                  "PRECISION '%s' REPLICA %d "
429
                  "WAL_LEVEL %d VGROUPS %d SINGLE_STABLE %d TABLE_PREFIX %d TABLE_SUFFIX %d TSDB_PAGESIZE %d "
430
                  "WAL_RETENTION_PERIOD %d WAL_RETENTION_SIZE %" PRId64
431
                  " KEEP_TIME_OFFSET %d ENCRYPT_ALGORITHM '%s' S3_CHUNKPAGES %d S3_KEEPLOCAL %dm S3_COMPACT %d",
432
                  dbName, pCfg->buffer, pCfg->cacheSize, cacheModelStr(pCfg->cacheLast), pCfg->compression, durationStr,
19✔
433
                  pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str, keep1Str, keep2Str,
19✔
434
                  pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel, pCfg->numOfVgroups,
19✔
435
                  1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize, pCfg->walRetentionPeriod,
19✔
436
                  pCfg->walRetentionSize, pCfg->keepTimeOffset, encryptAlgorithmStr(pCfg->encryptAlgorithm),
19✔
437
                  pCfg->s3ChunkSize, pCfg->s3KeepLocal, pCfg->s3Compact);
19✔
438

439
    if (pRetentions) {
19!
440
      len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
441
                       " RETENTIONS %s", pRetentions);
442
    }
443
  }
444

445
  taosMemoryFree(pRetentions);
21✔
446

447
  (varDataLen(buf2)) = len;
21✔
448

449
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
21!
450

451
  return TSDB_CODE_SUCCESS;
21✔
452
}
453

454
static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt, SRetrieveTableRsp** pRsp) {
21✔
455
  SSDataBlock* pBlock = NULL;
21✔
456
  int32_t      code = buildCreateDBResultDataBlock(&pBlock);
21✔
457
  if (TSDB_CODE_SUCCESS == code) {
21!
458
    code = setCreateDBResultIntoDataBlock(pBlock, pStmt->dbName, pStmt->dbFName, pStmt->pCfg);
21✔
459
  }
460
  if (TSDB_CODE_SUCCESS == code) {
21!
461
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_DB_RESULT_COLS, pRsp);
21✔
462
  }
463
  (void)blockDataDestroy(pBlock);
21✔
464
  return code;
21✔
465
}
466

467
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
115✔
468
  QRY_PARAM_CHECK(pOutput);
115!
469

470
  SSDataBlock* pBlock = NULL;
115✔
471
  int32_t      code = createDataBlock(&pBlock);
115✔
472
  if (code) {
115!
473
    return code;
×
474
  }
475

476
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD1_LEN, 1);
115✔
477
  code = blockDataAppendColInfo(pBlock, &infoData);
115✔
478
  if (TSDB_CODE_SUCCESS == code) {
115!
479
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD2_LEN, 2);
115✔
480
    code = blockDataAppendColInfo(pBlock, &infoData);
115✔
481
  }
482

483
  if (TSDB_CODE_SUCCESS == code) {
115!
484
    *pOutput = pBlock;
115✔
485
  } else {
486
    (void)blockDataDestroy(pBlock);
×
487
  }
488
  return code;
115✔
489
}
490

491
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
11✔
492
  QRY_PARAM_CHECK(pOutput);
11!
493

494
  SSDataBlock* pBlock = NULL;
11✔
495
  int32_t      code = createDataBlock(&pBlock);
11✔
496
  if (code) {
11!
497
    return code;
×
498
  }
499

500
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD1_LEN, 1);
11✔
501
  code = blockDataAppendColInfo(pBlock, &infoData);
11✔
502
  if (TSDB_CODE_SUCCESS == code) {
11!
503
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD2_LEN, 2);
11✔
504
    code = blockDataAppendColInfo(pBlock, &infoData);
11✔
505
  }
506

507
  if (TSDB_CODE_SUCCESS == code) {
11!
508
    *pOutput = pBlock;
11✔
509
  } else {
510
    (void)blockDataDestroy(pBlock);
×
511
  }
512
  return code;
11✔
513
}
514

515
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
77✔
516
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
515✔
517
    SSchema* pSchema = pCfg->pSchemas + i;
438✔
518
#define LTYPE_LEN (32 + 60)  // 60 byte for compress info
519
    char type[LTYPE_LEN];
520
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
438✔
521
    int typeLen = strlen(type);
438✔
522
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
438!
523
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
271!
524
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
167✔
525
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
271✔
526
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
10✔
527
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
10✔
528
    }
529

530
    if (useCompress(pCfg->tableType) && pCfg->pSchemaExt) {
438!
531
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " ENCODE \'%s\'",
168✔
532
                           columnEncodeStr(COMPRESS_L1_TYPE_U32(pCfg->pSchemaExt[i].compress)));
168✔
533
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " COMPRESS \'%s\'",
168✔
534
                           columnCompressStr(COMPRESS_L2_TYPE_U32(pCfg->pSchemaExt[i].compress)));
168✔
535
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " LEVEL \'%s\'",
168✔
536
                           columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pCfg->pSchemaExt[i].compress)));
168✔
537
    }
538
    if (!(pSchema->flags & COL_IS_KEY)) {
438✔
539
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
426✔
540
                        "%s`%s` %s", ((i > 0) ? ", " : ""), pSchema->name, type);
426✔
541
    } else {
542
      char* pk = "PRIMARY KEY";
12✔
543
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
12✔
544
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), pSchema->name, type, pk);
12!
545
    }
546
  }
547
}
77✔
548

549
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
30✔
550
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
96✔
551
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
66✔
552
    char     type[32];
553
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
66✔
554
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
66!
555
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
58!
556
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
8✔
557
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
8✔
558
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
58✔
559
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
5✔
560
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
5✔
561
    }
562

563
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
66✔
564
                      "%s`%s` %s", ((i > 0) ? ", " : ""), pSchema->name, type);
66✔
565
  }
566
}
30✔
567

568
static void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
38✔
569
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
99✔
570
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
61✔
571
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
61✔
572
                      "%s`%s`", ((i > 0) ? ", " : ""), pSchema->name);
61✔
573
  }
574
}
38✔
575

576
static int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) {
38✔
577
  int32_t code = TSDB_CODE_SUCCESS;
38✔
578
  SArray* pTagVals = NULL;
38✔
579
  STag*   pTag = (STag*)pCfg->pTags;
38✔
580

581
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
38!
582
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
583
    return TSDB_CODE_APP_ERROR;
×
584
  }
585

586
  if (tTagIsJson(pTag)) {
38✔
587
    char* pJson = NULL;
4✔
588
    parseTagDatatoJson(pTag, &pJson);
4✔
589
    if (NULL == pJson) {
4!
590
      qError("failed to parse tag to json, pJson is NULL");
×
591
      return terrno;
×
592
    }
593
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
4✔
594
                      "%s", pJson);
595
    taosMemoryFree(pJson);
4✔
596

597
    return TSDB_CODE_SUCCESS;
4✔
598
  }
599

600
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
34!
601
  int16_t valueNum = taosArrayGetSize(pTagVals);
34✔
602
  int32_t num = 0;
34✔
603
  int32_t j = 0;
34✔
604
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
91✔
605
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
57✔
606
    if (i > 0) {
57✔
607
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
23✔
608
                        ", ");
609
    }
610

611
    if (j >= valueNum) {
57✔
612
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
21✔
613
                        "NULL");
614
      continue;
21✔
615
    }
616

617
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
36✔
618
    if (pSchema->colId > pTagVal->cid) {
36!
619
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
620
      code = TSDB_CODE_APP_ERROR;
×
621
      TAOS_CHECK_ERRNO(code);
×
622
    } else if (pSchema->colId == pTagVal->cid) {
36✔
623
      char    type = pTagVal->type;
34✔
624
      int32_t tlen = 0;
34✔
625

626
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
34✔
627
      if (leftSize <= 0) {
34!
628
        qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
×
629
        code = TSDB_CODE_APP_ERROR;
×
630
        TAOS_CHECK_ERRNO(code);
×
631
      }
632
      if (IS_VAR_DATA_TYPE(type)) {
34!
633
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
6✔
634
        TAOS_CHECK_ERRNO(code);
6!
635
      } else {
636
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
28✔
637
                               &tlen);
638
        TAOS_CHECK_ERRNO(code);
28!
639
      }
640
      *len += tlen;
34✔
641
      j++;
34✔
642
    } else {
643
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2✔
644
                        "NULL");
645
    }
646
  }
647
_exit:
34✔
648
  taosArrayDestroy(pTagVals);
34✔
649

650
  return code;
34✔
651
}
652

653
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
115✔
654
  if (pCfg->commentLen > 0) {
115!
655
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
656
                      " COMMENT '%s'", pCfg->pComment);
657
  } else if (0 == pCfg->commentLen) {
115✔
658
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
30✔
659
                      " COMMENT ''");
660
  }
661

662
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
115!
663
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
664
                      " WATERMARK %" PRId64 "a", pCfg->watermark1);
665
    if (pCfg->watermark2 > 0) {
×
666
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
667
                        ", %" PRId64 "a", pCfg->watermark2);
668
    }
669
  }
670

671
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
115!
672
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
673
                      " MAX_DELAY %" PRId64 "a", pCfg->delay1);
674
    if (pCfg->delay2 > 0) {
×
675
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
676
                        ", %" PRId64 "a", pCfg->delay2);
677
    }
678
  }
679

680
  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
115✔
681
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
115!
682
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
683
                      " ROLLUP(");
684
    for (int32_t i = 0; i < funcNum; ++i) {
×
685
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
×
686
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
687
                        "%s%s", ((i > 0) ? ", " : ""), pFunc);
688
    }
689
    *len +=
×
690
        snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
×
691
  }
692

693
  if (pCfg->ttl > 0) {
115!
694
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
695
                      " TTL %d", pCfg->ttl);
696
  }
697

698
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
115✔
699
    int32_t nSma = 0;
47✔
700
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
215✔
701
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
168!
702
        ++nSma;
168✔
703
      }
704
    }
705

706
    if (nSma < pCfg->numOfColumns && nSma > 0) {
47!
707
      bool smaOn = false;
×
708
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
709
                        " SMA(");
710
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
711
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
712
          if (smaOn) {
×
713
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
714
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ",`%s`",
×
715
                              (pCfg->pSchemas + i)->name);
×
716
          } else {
717
            smaOn = true;
×
718
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
719
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), "`%s`",
×
720
                              (pCfg->pSchemas + i)->name);
×
721
          }
722
        }
723
      }
724
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, ")");
×
725
    }
726
  }
727
}
115✔
728

729
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg) {
115✔
730
  int32_t code = TSDB_CODE_SUCCESS;
115✔
731
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
115!
732
  pBlock->info.rows = 1;
115✔
733

734
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
115✔
735
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN] = {0};
115✔
736
  STR_TO_VARSTR(buf1, tbName);
115✔
737
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
115!
738

739
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
115✔
740
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
115✔
741
  if (NULL == buf2) {
115!
742
    QRY_ERR_RET(terrno);
×
743
  }
744

745
  int32_t len = 0;
115✔
746

747
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
115✔
748
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
30✔
749
                     "CREATE STABLE `%s` (", tbName);
750
    appendColumnFields(buf2, &len, pCfg);
30✔
751
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
30✔
752
                     ") TAGS (");
753
    appendTagFields(buf2, &len, pCfg);
30✔
754
    len +=
30✔
755
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
30✔
756
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
30✔
757
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
85✔
758
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
76✔
759
                     "CREATE TABLE `%s` USING `%s` (", tbName, pCfg->stbName);
38✔
760
    appendTagNameFields(buf2, &len, pCfg);
38✔
761
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
38✔
762
                     ") TAGS (");
763
    code = appendTagValues(buf2, &len, pCfg);
38✔
764
    TAOS_CHECK_ERRNO(code);
38!
765
    len +=
38✔
766
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
38✔
767
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
38✔
768
  } else {
769
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
47✔
770
                     "CREATE TABLE `%s` (", tbName);
771
    appendColumnFields(buf2, &len, pCfg);
47✔
772
    len +=
47✔
773
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
47✔
774
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
47✔
775
  }
776

777
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
115✔
778

779
  code = colDataSetVal(pCol2, 0, buf2, false);
115✔
780
  TAOS_CHECK_ERRNO(code);
115!
781

782
_exit:
115✔
783
  taosMemoryFree(buf2);
115✔
784

785
  return code;
115✔
786
}
787

788
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
11✔
789
  int32_t code = 0;
11✔
790
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
11!
791
  pBlock->info.rows = 1;
11✔
792

793
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
11✔
794
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
11✔
795
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
11✔
796
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
11✔
797
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
11!
798

799
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
11✔
800
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
11✔
801
  if (NULL == buf2) {
11!
802
    return terrno;
×
803
  }
804

805
  SViewMeta* pMeta = pStmt->pViewMeta;
11✔
806
  if (NULL == pMeta) {
11!
807
    qError("exception: view meta is null");
×
808
    return TSDB_CODE_APP_ERROR;
×
809
  }
810
  snprintf(varDataVal(buf2), SHOW_CREATE_VIEW_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE VIEW `%s`.`%s` AS %s",
11✔
811
           pStmt->dbName, pStmt->viewName, pMeta->querySql);
11✔
812
  int32_t len = strlen(varDataVal(buf2));
11✔
813
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
11✔
814
  code = colDataSetVal(pCol2, 0, buf2, false);
11✔
815
  taosMemoryFree(buf2);
11✔
816

817
  return code;
11✔
818
}
819

820
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp) {
115✔
821
  SSDataBlock* pBlock = NULL;
115✔
822
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
115✔
823
  if (TSDB_CODE_SUCCESS == code) {
115!
824
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg);
115✔
825
  }
826
  if (TSDB_CODE_SUCCESS == code) {
115!
827
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
115✔
828
  }
829
  (void)blockDataDestroy(pBlock);
115✔
830
  return code;
115✔
831
}
832

833
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp) {
3✔
834
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
3✔
835
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
3✔
836
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
1✔
837
    return terrno;
1✔
838
  }
839

840
  return execShowCreateTable(pStmt, pRsp);
2✔
841
}
842

843
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
15,910✔
844
  int32_t code = 0;
15,910✔
845

846
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
15,910!
847
    taosResetLog();
×
848
    cfgDumpCfg(tsCfg, 0, false);
×
849
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
15,910✔
850
    int32_t tmp = 0;
40✔
851
    code = taosStr2int32(value, &tmp);
40✔
852
    if (code) {
40!
853
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
854
      return code;
×
855
    }
856
    code = schedulerUpdatePolicy(tmp);
40✔
857
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
15,870!
858
    int32_t tmp = 0;
×
859
    code = taosStr2int32(value, &tmp);
×
860
    if (code) {
×
861
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
862
      return code;
×
863
    }
864
    code = schedulerEnableReSchedule(tmp != 0);
×
865
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
15,870!
866
    code = ctgdHandleDbgCommand(value);
×
867
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
15,870!
868
    code = taosMemoryDbgInit();
×
869
    if (code) {
×
870
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
871
      return code;
×
872
    }
873
    tsAsyncLog = false;
×
874
    qInfo("memory dbg enabled");
×
875
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
15,870!
876
    code = taosMemoryDbgInitRestore();
×
877
    if (code) {
×
878
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
879
      return code;
×
880
    }
881
    qInfo("memory dbg disabled");
×
882
  } else {
883
    goto _return;
15,870✔
884
  }
885

886
  *processed = true;
40✔
887

888
_return:
15,910✔
889

890
  if (code) {
15,910!
891
    terrno = code;
×
892
  }
893

894
  return code;
15,910✔
895
}
896

897
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
15,910✔
898
  bool processed = false;
15,910✔
899

900
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
15,910!
901
    return terrno;
×
902
  }
903

904
  if (processed) {
15,910✔
905
    goto _return;
40✔
906
  }
907

908
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false)) {
15,870✔
909
    return terrno;
11✔
910
  }
911

912
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CMD, true)) {
15,859!
913
    return terrno;
×
914
  }
915

916
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
15,859✔
917

918
_return:
15,858✔
919

920
  return TSDB_CODE_SUCCESS;
15,898✔
921
}
922

923
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
443✔
924
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
443✔
925
  if (NULL == pBlock) {
443!
926
    return terrno;
×
927
  }
928

929
  pBlock->info.hasVarCol = true;
443✔
930

931
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
443✔
932
  if (NULL == pBlock->pDataBlock) {
443!
933
    taosMemoryFree(pBlock);
×
934
    return terrno;
×
935
  }
936

937
  SColumnInfoData infoData = {0};
443✔
938

939
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
443✔
940
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
443✔
941
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
886!
942
    goto _exit;
×
943
  }
944

945
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
443✔
946
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
443✔
947
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
886!
948
    goto _exit;
×
949
  }
950

951
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
443✔
952
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
443✔
953
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
886!
954
    goto _exit;
×
955
  }
956

957
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
443✔
958
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
443✔
959
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
886!
960
    goto _exit;
×
961
  }
962

963
  *pOutput = pBlock;
443✔
964

965
_exit:
443✔
966
  if (terrno != TSDB_CODE_SUCCESS) {
443!
967
    taosArrayDestroy(pBlock->pDataBlock);
×
968
    taosMemoryFree(pBlock);
×
969
  }
970
  return terrno;
443✔
971
}
972

973
static int32_t execShowLocalVariables(SRetrieveTableRsp** pRsp) {
443✔
974
  SSDataBlock* pBlock = NULL;
443✔
975
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
443✔
976
  if (TSDB_CODE_SUCCESS == code) {
443!
977
    code = dumpConfToDataBlock(pBlock, 0);
443✔
978
  }
979
  if (TSDB_CODE_SUCCESS == code) {
443!
980
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
443✔
981
  }
982
  (void)blockDataDestroy(pBlock);
443✔
983
  return code;
443✔
984
}
985

986
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
70,705✔
987
  QRY_PARAM_CHECK(pOutput);
70,705!
988

989
  SSDataBlock* pBlock = NULL;
70,705✔
990
  int32_t      code = createDataBlock(&pBlock);
70,705✔
991
  if (code) {
70,705!
992
    return code;
×
993
  }
994

995
  SNode* pProj = NULL;
70,705✔
996
  FOREACH(pProj, pProjects) {
141,447!
997
    SExprNode*      pExpr = (SExprNode*)pProj;
70,742✔
998
    SColumnInfoData infoData = {0};
70,742✔
999
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
70,742!
1000
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1001
      infoData.info.bytes = 0;
×
1002
    } else {
1003
      infoData.info.type = pExpr->resType.type;
70,742✔
1004
      infoData.info.bytes = pExpr->resType.bytes;
70,742✔
1005
    }
1006
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
70,742!
1007
  }
1008

1009
  *pOutput = pBlock;
70,705✔
1010
  return code;
70,705✔
1011
}
1012

1013
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
70,705✔
1014
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
70,705!
1015

1016
  int32_t index = 0;
70,705✔
1017
  SNode*  pProj = NULL;
70,705✔
1018
  FOREACH(pProj, pProjects) {
141,447!
1019
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
70,742!
1020
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1021
    } else {
1022
      if (((SValueNode*)pProj)->isNull) {
70,742✔
1023
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
128!
1024
      } else {
1025
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
70,614!
1026
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1027
      }
1028
    }
1029
  }
1030

1031
  pBlock->info.rows = 1;
70,705✔
1032
  return TSDB_CODE_SUCCESS;
70,705✔
1033
}
1034

1035
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
70,705✔
1036
  SSDataBlock* pBlock = NULL;
70,705✔
1037
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
70,705✔
1038
  if (TSDB_CODE_SUCCESS == code) {
70,705!
1039
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
70,705✔
1040
  }
1041
  if (TSDB_CODE_SUCCESS == code) {
70,705!
1042
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
70,705!
1043
  }
1044
  (void)blockDataDestroy(pBlock);
70,705✔
1045
  return code;
70,705✔
1046
}
1047

1048
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
11✔
1049
  SSDataBlock* pBlock = NULL;
11✔
1050
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
11✔
1051
  if (TSDB_CODE_SUCCESS == code) {
11!
1052
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
11✔
1053
  }
1054
  if (TSDB_CODE_SUCCESS == code) {
11!
1055
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
11✔
1056
  }
1057
  (void)blockDataDestroy(pBlock);
11✔
1058
  return code;
11✔
1059
}
1060

1061
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode) {
97,725✔
1062
  switch (nodeType(pStmt)) {
97,725!
1063
    case QUERY_NODE_DESCRIBE_STMT:
2,787✔
1064
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
2,787✔
1065
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
7,732✔
1066
      return execResetQueryCache();
7,732✔
1067
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
21✔
1068
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
21✔
1069
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
113✔
1070
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp);
113✔
1071
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
3✔
1072
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp);
3✔
1073
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
11✔
1074
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
11✔
1075
    case QUERY_NODE_ALTER_LOCAL_STMT:
15,910✔
1076
      return execAlterLocal((SAlterLocalStmt*)pStmt);
15,910✔
1077
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
443✔
1078
      return execShowLocalVariables(pRsp);
443✔
1079
    case QUERY_NODE_SELECT_STMT:
70,705✔
1080
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
70,705✔
1081
    default:
×
1082
      break;
×
1083
  }
1084
  return TSDB_CODE_FAILED;
×
1085
}
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