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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

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

16
#include "command.h"
17
#include "catalog.h"
18
#include "commandInt.h"
19
#include "decimal.h"
20
#include "osMemory.h"
21
#include "osString.h"
22
#include "scheduler.h"
23
#include "systable.h"
24
#include "taosdef.h"
25
#include "tdatablock.h"
26
#include "tdataformat.h"
27
#include "tglobal.h"
28
#include "tgrant.h"
29

30
#define COL_DATA_SET_VAL_AND_CHECK(pCol, rows, buf, isNull) \
31
  do {                                                      \
32
    int _code = colDataSetVal(pCol, rows, buf, isNull);     \
33
    if (TSDB_CODE_SUCCESS != _code) {                       \
34
      terrno = _code;                                       \
35
      return _code;                                         \
36
    }                                                       \
37
  } while (0)
38

39
extern SConfig* tsCfg;
40

41
static int32_t buildRetrieveTableRsp(SSDataBlock* pBlock, int32_t numOfCols, SRetrieveTableRsp** pRsp) {
329✔
42
  if (NULL == pBlock || NULL == pRsp) {
329✔
43
    return TSDB_CODE_INVALID_PARA;
×
44
  }
45
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
329✔
46
  size_t rspSize = sizeof(SRetrieveTableRsp) + dataEncodeBufSize + PAYLOAD_PREFIX_LEN;
329✔
47
  *pRsp = taosMemoryCalloc(1, rspSize);
329✔
48
  if (NULL == *pRsp) {
329✔
49
    return terrno;
×
50
  }
51

52
  (*pRsp)->useconds = 0;
329✔
53
  (*pRsp)->completed = 1;
329✔
54
  (*pRsp)->precision = 0;
329✔
55
  (*pRsp)->compressed = 0;
329✔
56

57
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
329✔
58
  (*pRsp)->numOfCols = htonl(numOfCols);
329✔
59

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

71
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
329✔
72
  (*pRsp)->payloadLen = htonl(payloadLen);
329✔
73
  (*pRsp)->compLen = htonl(payloadLen);
329✔
74

75
  return TSDB_CODE_SUCCESS;
329✔
76
}
77

78
static int32_t getSchemaBytes(const SSchema* pSchema) {
×
79
  switch (pSchema->type) {
×
80
    case TSDB_DATA_TYPE_BINARY:
×
81
    case TSDB_DATA_TYPE_VARBINARY:
82
    case TSDB_DATA_TYPE_GEOMETRY:
83
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
×
84
    case TSDB_DATA_TYPE_NCHAR:
×
85
    case TSDB_DATA_TYPE_JSON:
86
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
87
    default:
×
88
      return pSchema->bytes;
×
89
  }
90
}
91

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

116
static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) {
×
117
  QRY_PARAM_CHECK(pOutput);
×
118

119
  SSDataBlock* pBlock = NULL;
×
120
  int32_t      code = createDataBlock(&pBlock);
×
121
  if (code) {
×
122
    return code;
×
123
  }
124

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

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

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

191
  if (hasRefCol(pMeta->tableType)) {
×
192
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
×
193
  }
194

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

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

265
    fillTagCol = 0;
×
266

267
    ++(pBlock->info.rows);
×
268
  }
269
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
×
270
    STR_TO_VARSTR(buf, "tbname");
×
271
    COL_DATA_SET_VAL_AND_CHECK(pCol1, pBlock->info.rows, buf, false);
×
272
    STR_TO_VARSTR(buf, "VARCHAR");
×
273
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
×
274
    int32_t bytes = TSDB_TABLE_NAME_LEN - 1;
×
275
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
×
276
    STR_TO_VARSTR(buf, "TAG");
×
277
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
×
278
    ++(pBlock->info.rows);
×
279
  }
280
  if (pBlock->info.rows <= 0) {
×
281
    qError("no permission to view any columns");
×
282
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
283
  }
284
  return TSDB_CODE_SUCCESS;
×
285
}
286

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

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

316
static int32_t execResetQueryCache() { return catalogClearCache(); }
×
317

318
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
×
319
  QRY_PARAM_CHECK(pOutput);
×
320

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

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

334
  if (TSDB_CODE_SUCCESS == code) {
×
335
    *pOutput = pBlock;
×
336
  } else {
337
    (void)blockDataDestroy(pBlock);
×
338
  }
339
  return code;
×
340
}
341

342
int64_t getValOfDiffPrecision(int8_t unit, int64_t val) {
×
343
  int64_t v = 0;
×
344
  switch (unit) {
×
345
    case 's':
×
346
      v = val / 1000;
×
347
      break;
×
348
    case 'm':
×
349
      v = val / tsTickPerMin[TSDB_TIME_PRECISION_MILLI];
×
350
      break;
×
351
    case 'h':
×
352
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 60);
×
353
      break;
×
354
    case 'd':
×
355
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60);
×
356
      break;
×
357
    case 'w':
×
358
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60 * 7);
×
359
      break;
×
360
    default:
×
361
      break;
×
362
  }
363

364
  return v;
×
365
}
366

367
static int32_t buildRetension(SArray* pRetension, char** ppRetentions) {
×
368
  size_t size = taosArrayGetSize(pRetension);
×
369
  if (size == 0) {
×
370
    *ppRetentions = NULL;
×
371
    return TSDB_CODE_SUCCESS;
×
372
  }
373

374
  const int lMaxLen = 128;
×
375
  char*     p1 = taosMemoryCalloc(1, lMaxLen);
×
376
  if (NULL == p1) {
×
377
    return terrno;
×
378
  }
379
  int32_t len = 0;
×
380

381
  for (int32_t i = 0; i < size; ++i) {
×
382
    SRetention* p = TARRAY_GET_ELEM(pRetension, i);
×
383
    int64_t     v1 = getValOfDiffPrecision(p->freqUnit, p->freq);
×
384
    int64_t     v2 = getValOfDiffPrecision(p->keepUnit, p->keep);
×
385
    if (i == 0) {
×
386
      len += tsnprintf(p1 + len, lMaxLen - len, "-:%" PRId64 "%c", v2, p->keepUnit);
×
387
    } else {
388
      len += tsnprintf(p1 + len, lMaxLen - len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit);
×
389
    }
390

391
    if (i < size - 1) {
×
392
      len += tsnprintf(p1 + len, lMaxLen - len, ",");
×
393
    }
394
  }
395

396
  *ppRetentions = p1;
×
397
  return TSDB_CODE_SUCCESS;
×
398
}
399

400
static const char* cacheModelStr(int8_t cacheModel) {
×
401
  switch (cacheModel) {
×
402
    case TSDB_CACHE_MODEL_NONE:
×
403
      return TSDB_CACHE_MODEL_NONE_STR;
×
404
    case TSDB_CACHE_MODEL_LAST_ROW:
×
405
      return TSDB_CACHE_MODEL_LAST_ROW_STR;
×
406
    case TSDB_CACHE_MODEL_LAST_VALUE:
×
407
      return TSDB_CACHE_MODEL_LAST_VALUE_STR;
×
408
    case TSDB_CACHE_MODEL_BOTH:
×
409
      return TSDB_CACHE_MODEL_BOTH_STR;
×
410
    default:
×
411
      break;
×
412
  }
413
  return TSDB_CACHE_MODEL_NONE_STR;
×
414
}
415

416
static const char* encryptAlgorithmStr(int8_t encryptAlgorithm, char* algorithmsId) {
×
417
  if (algorithmsId[0] != '\0') {
×
418
    return algorithmsId;
×
419
  } else if (encryptAlgorithm != 0) {
×
420
    switch (encryptAlgorithm) {
×
421
      case TSDB_ENCRYPT_ALGO_NONE:
×
422
        return TSDB_ENCRYPT_ALGO_NONE_STR;
×
423
      case TSDB_ENCRYPT_ALGO_SM4:
×
424
        return TSDB_ENCRYPT_ALGO_SM4_STR;
×
425
      default:
×
426
        break;
×
427
    }
428
    return TSDB_CACHE_MODEL_NONE_STR;
×
429
  }
430

431
  return TSDB_CACHE_MODEL_NONE_STR;
×
432
}
433

434
int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
×
435
  if (buffer == NULL || bufSize <= 0) {
×
436
    return 0;
×
437
  }
438
  int32_t len = 0;
×
439
  if (timeInMinutes % 1440 == 0) {
×
440
    int32_t days = timeInMinutes / 1440;
×
441
    len = tsnprintf(buffer, bufSize, "%dd", days);
×
442
  } else if (timeInMinutes % 60 == 0) {
×
443
    int32_t hours = timeInMinutes / 60;
×
444
    len = tsnprintf(buffer, bufSize, "%dh", hours);
×
445
  } else {
446
    len = tsnprintf(buffer, bufSize, "%dm", timeInMinutes);
×
447
  }
448
  return len;
×
449
}
450

451
static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, char* dbFName, SDbCfgInfo* pCfg) {
×
452
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
×
453
  pBlock->info.rows = 1;
×
454

455
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
×
456
  char             buf1[SHOW_CREATE_DB_RESULT_FIELD1_LEN] = {0};
×
457
  STR_TO_VARSTR(buf1, dbName);
×
458
  COL_DATA_SET_VAL_AND_CHECK(pCol1, 0, buf1, false);
×
459

460
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
×
461
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
×
462
  int32_t          len = 0;
×
463
  char*            prec = NULL;
×
464
  switch (pCfg->precision) {
×
465
    case TSDB_TIME_PRECISION_MILLI:
×
466
      prec = TSDB_TIME_PRECISION_MILLI_STR;
×
467
      break;
×
468
    case TSDB_TIME_PRECISION_MICRO:
×
469
      prec = TSDB_TIME_PRECISION_MICRO_STR;
×
470
      break;
×
471
    case TSDB_TIME_PRECISION_NANO:
×
472
      prec = TSDB_TIME_PRECISION_NANO_STR;
×
473
      break;
×
474
    default:
×
475
      prec = "none";
×
476
      break;
×
477
  }
478

479
  char* pRetentions = NULL;
×
480
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
×
481
  int32_t dbFNameLen = strlen(dbFName);
×
482
  int32_t hashPrefix = 0;
×
483
  if (pCfg->hashPrefix > 0) {
×
484
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
×
485
  } else if (pCfg->hashPrefix < 0) {
×
486
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
×
487
  }
488
  char durationStr[128] = {0};
×
489
  char keep0Str[128] = {0};
×
490
  char keep1Str[128] = {0};
×
491
  char keep2Str[128] = {0};
×
492
  char compactIntervalStr[13] = {0};
×
493
  char compactStartTimeStr[13] = {0};
×
494
  char compactEndTimeStr[13] = {0};
×
495

496
  int32_t lenDuration = formatDurationOrKeep(durationStr, sizeof(durationStr), pCfg->daysPerFile);
×
497
  int32_t lenKeep0 = formatDurationOrKeep(keep0Str, sizeof(keep0Str), pCfg->daysToKeep0);
×
498
  int32_t lenKeep1 = formatDurationOrKeep(keep1Str, sizeof(keep1Str), pCfg->daysToKeep1);
×
499
  int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pCfg->daysToKeep2);
×
500
  UNUSED(formatDurationOrKeep(compactIntervalStr, sizeof(compactIntervalStr), pCfg->compactInterval));
×
501
  UNUSED(formatDurationOrKeep(compactStartTimeStr, sizeof(compactStartTimeStr), pCfg->compactStartTime));
×
502
  UNUSED(formatDurationOrKeep(compactEndTimeStr, sizeof(compactEndTimeStr), pCfg->compactEndTime));
×
503

504
  if (IS_SYS_DBNAME(dbName)) {
×
505
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
506
                     "CREATE DATABASE `%s`", dbName);
×
507
  } else {
508
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
509
                     "CREATE DATABASE `%s` BUFFER %d CACHESIZE %d CACHEMODEL '%s' COMP %d DURATION %s "
510
                     "WAL_FSYNC_PERIOD %d MAXROWS %d MINROWS %d STT_TRIGGER %d KEEP %s,%s,%s PAGES %d PAGESIZE %d "
511
                     "PRECISION '%s' REPLICA %d "
512
                     "WAL_LEVEL %d VGROUPS %d SINGLE_STABLE %d TABLE_PREFIX %d TABLE_SUFFIX %d TSDB_PAGESIZE %d "
513
                     "WAL_RETENTION_PERIOD %d WAL_RETENTION_SIZE %" PRId64
514
                     " KEEP_TIME_OFFSET %d ENCRYPT_ALGORITHM '%s' SS_CHUNKPAGES %d SS_KEEPLOCAL %dm SS_COMPACT %d "
515
                     "COMPACT_INTERVAL %s COMPACT_TIME_RANGE %s,%s COMPACT_TIME_OFFSET %" PRIi8 "h IS_AUDIT %d",
516
                     dbName, pCfg->buffer, pCfg->cacheSize, cacheModelStr(pCfg->cacheLast), pCfg->compression,
×
517
                     durationStr, pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str,
×
518
                     keep1Str, keep2Str, pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel,
×
519
                     pCfg->numOfVgroups, 1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize,
×
520
                     pCfg->walRetentionPeriod, pCfg->walRetentionSize, pCfg->keepTimeOffset,
521
                     encryptAlgorithmStr(pCfg->encryptAlgr, pCfg->algorithmsId), pCfg->ssChunkSize, pCfg->ssKeepLocal,
×
522
                     pCfg->ssCompact, compactIntervalStr, compactStartTimeStr, compactEndTimeStr,
×
523
                     pCfg->compactTimeOffset, pCfg->isAudit);
×
524

525
    if (pRetentions) {
×
526
      len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
527
                       " RETENTIONS %s", pRetentions);
528
    }
529
  }
530

531
  taosMemoryFree(pRetentions);
×
532

533
  (varDataLen(buf2)) = len;
×
534

535
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
×
536

537
  return TSDB_CODE_SUCCESS;
×
538
}
539

540
static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt, SRetrieveTableRsp** pRsp) {
×
541
  SSDataBlock* pBlock = NULL;
×
542
  int32_t      code = buildCreateDBResultDataBlock(&pBlock);
×
543
  if (TSDB_CODE_SUCCESS == code) {
×
544
    code = setCreateDBResultIntoDataBlock(pBlock, pStmt->dbName, pStmt->dbFName, pStmt->pCfg);
×
545
  }
546
  if (TSDB_CODE_SUCCESS == code) {
×
547
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_DB_RESULT_COLS, pRsp);
×
548
  }
549
  (void)blockDataDestroy(pBlock);
×
550
  return code;
×
551
}
552

553
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
×
554
  QRY_PARAM_CHECK(pOutput);
×
555

556
  SSDataBlock* pBlock = NULL;
×
557
  int32_t      code = createDataBlock(&pBlock);
×
558
  if (code) {
×
559
    return code;
×
560
  }
561

562
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD1_LEN, 1);
×
563
  code = blockDataAppendColInfo(pBlock, &infoData);
×
564
  if (TSDB_CODE_SUCCESS == code) {
×
565
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD2_LEN, 2);
×
566
    code = blockDataAppendColInfo(pBlock, &infoData);
×
567
  }
568

569
  if (TSDB_CODE_SUCCESS == code) {
×
570
    *pOutput = pBlock;
×
571
  } else {
572
    (void)blockDataDestroy(pBlock);
×
573
  }
574
  return code;
×
575
}
576

577
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
×
578
  QRY_PARAM_CHECK(pOutput);
×
579

580
  SSDataBlock* pBlock = NULL;
×
581
  int32_t      code = createDataBlock(&pBlock);
×
582
  if (code) {
×
583
    return code;
×
584
  }
585

586
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD1_LEN, 1);
×
587
  code = blockDataAppendColInfo(pBlock, &infoData);
×
588
  if (TSDB_CODE_SUCCESS == code) {
×
589
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_VIEW_RESULT_FIELD2_LEN, 2);
×
590
    code = blockDataAppendColInfo(pBlock, &infoData);
×
591
  }
592

593
  if (TSDB_CODE_SUCCESS == code) {
×
594
    *pOutput = pBlock;
×
595
  } else {
596
    (void)blockDataDestroy(pBlock);
×
597
  }
598
  return code;
×
599
}
600

601
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
×
602
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
×
603
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
604
    SSchema* pSchema = pCfg->pSchemas + i;
×
605
    SColRef* pRef = pCfg->pColRefs + i;
×
606
#define LTYPE_LEN                                    \
607
  (32 + 60 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN + \
608
   10)  // 60 byte for compress info, TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN for column ref
609
    char type[LTYPE_LEN];
610
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
×
611
    int typeLen = strlen(type);
×
612
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
×
613
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
×
614
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
×
615
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
×
616
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
×
617
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
×
618
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
×
619
      uint8_t precision, scale;
620
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
×
621
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
×
622
    }
623

624
    if (withExtSchema(pCfg->tableType) && pCfg->pSchemaExt && tsShowFullCreateTableColumn) {
×
625
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " ENCODE \'%s\'",
×
626
                           columnEncodeStr(COMPRESS_L1_TYPE_U32(pCfg->pSchemaExt[i].compress)));
×
627
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " COMPRESS \'%s\'",
×
628
                           columnCompressStr(COMPRESS_L2_TYPE_U32(pCfg->pSchemaExt[i].compress)));
×
629
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " LEVEL \'%s\'",
×
630
                           columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pCfg->pSchemaExt[i].compress)));
×
631
    }
632

633
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
×
634
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, " FROM `%s`", pRef->refDbName);
×
635
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
×
636
      typeLen +=
×
637
          tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
×
638
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, ".");
×
639
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
×
640
    }
641

642
    if (!(pSchema->flags & COL_IS_KEY)) {
×
643
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
644
                        "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
×
645
    } else {
646
      char* pk = "COMPOSITE KEY";
×
647
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
648
                        "%s`%s` %s %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type, pk);
×
649
    }
650
  }
651
}
×
652

653
static void appendColRefFields(char* buf, int32_t* len, STableCfg* pCfg) {
×
654
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
×
655
  bool firstRef = true;
×
656
  for (int32_t i = 1; i < pCfg->numOfColumns; ++i) {
×
657
    SSchema* pSchema = pCfg->pSchemas + i;
×
658
    SColRef* pRef = pCfg->pColRefs + i;
×
659
    char     type[TSDB_COL_NAME_LEN + 10 + TSDB_COL_FNAME_LEN + TSDB_DB_NAME_LEN];
660
    int      typeLen = 0;
×
661

662
    if (hasRefCol(pCfg->tableType) && pCfg->pColRefs && pRef->hasRef) {
×
663
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "FROM `%s`", pRef->refDbName);
×
664
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
×
665
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refTableName, expandName));
×
666
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, ".");
×
667
      typeLen += tsnprintf(type + typeLen, sizeof(type) - typeLen, "`%s`", expandIdentifier(pRef->refColName, expandName));
×
668
    } else {
669
      continue;
×
670
    }
671

672
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
673
                      "%s`%s` %s", (!firstRef ? ", " : " ("), expandIdentifier(pSchema->name, expandName), type);
×
674
    firstRef = false;
×
675
  }
676
  if (!firstRef) {
×
677
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
×
678
  }
679
}
×
680

681
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
×
682
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
×
683
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
×
684
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
×
685
    char     type[32];
686
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
×
687
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
×
688
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
×
689
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
×
690
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
×
691
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
×
692
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
×
693
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
×
694
    }
695

696
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
697
                      "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
×
698
  }
699
}
×
700

701
static void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
×
702
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
×
703
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
×
704
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
×
705
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
706
                      "%s`%s`", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName));
×
707
  }
708
}
×
709

710
static int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg, void* charsetCxt) {
×
711
  int32_t code = TSDB_CODE_SUCCESS;
×
712
  SArray* pTagVals = NULL;
×
713
  STag*   pTag = (STag*)pCfg->pTags;
×
714

715
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
×
716
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
717
    return TSDB_CODE_APP_ERROR;
×
718
  }
719

720
  if (tTagIsJson(pTag)) {
×
721
    char* pJson = NULL;
×
722
    parseTagDatatoJson(pTag, &pJson, charsetCxt);
×
723
    if (NULL == pJson) {
×
724
      qError("failed to parse tag to json, pJson is NULL");
×
725
      return terrno;
×
726
    }
727
    char* escapedJson = taosMemCalloc(sizeof(char), strlen(pJson) * 2 + 1);  // taosMemoryAlloc(strlen(pJson) * 2 + 1);
×
728
    if (escapedJson) {
×
729
      char* writer = escapedJson;
×
730
      for (char* reader = pJson; *reader; ++reader) {
×
731
        if (*reader == '\'') {
×
732
          *writer++ = '\'';  // Escape single quote by doubling it
×
733
        }
734
        *writer++ = *reader;
×
735
      }
736
      *writer = '\0';
×
737

738
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
739
                        "'%s'", escapedJson);
740
      taosMemoryFree(escapedJson);
×
741
    } else {
742
      taosMemoryFree(pJson);
×
743
      return terrno;
×
744
    }
745
    taosMemoryFree(pJson);
×
746

747
    return TSDB_CODE_SUCCESS;
×
748
  }
749

750
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
×
751
  int16_t valueNum = taosArrayGetSize(pTagVals);
×
752
  int32_t num = 0;
×
753
  int32_t j = 0;
×
754
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
×
755
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
×
756
    if (i > 0) {
×
757
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
758
                        ", ");
759
    }
760

761
    if (j >= valueNum) {
×
762
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
763
                        "NULL");
764
      continue;
×
765
    }
766

767
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
×
768
    if (pSchema->colId > pTagVal->cid) {
×
769
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
770
      code = TSDB_CODE_APP_ERROR;
×
771
      TAOS_CHECK_ERRNO(code);
×
772
    } else if (pSchema->colId == pTagVal->cid) {
×
773
      char    type = pTagVal->type;
×
774
      int32_t tlen = 0;
×
775

776
      int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
×
777
      if (leftSize <= 0) {
×
778
        qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
×
779
        code = TSDB_CODE_APP_ERROR;
×
780
        TAOS_CHECK_ERRNO(code);
×
781
      }
782
      if (IS_VAR_DATA_TYPE(type)) {
×
783
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
×
784
        TAOS_CHECK_ERRNO(code);
×
785
      } else {
786
        code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes,
×
787
                               &tlen);
788
        TAOS_CHECK_ERRNO(code);
×
789
      }
790
      *len += tlen;
×
791
      j++;
×
792
    } else {
793
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
794
                        "NULL");
795
    }
796
  }
797
_exit:
×
798
  taosArrayDestroy(pTagVals);
×
799

800
  return code;
×
801
}
802

803
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
×
804
  if (pCfg->commentLen > 0) {
×
805
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
806
                      " COMMENT '%s'", pCfg->pComment);
807
  } else if (0 == pCfg->commentLen) {
×
808
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
809
                      " COMMENT ''");
810
  }
811

812
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
×
813
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
814
                      " WATERMARK %" PRId64 "a", pCfg->watermark1);
815
    if (pCfg->watermark2 > 0) {
×
816
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
817
                        ", %" PRId64 "a", pCfg->watermark2);
818
    }
819
  }
820

821
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
×
822
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
823
                      " MAX_DELAY %" PRId64 "a", pCfg->delay1);
824
    if (pCfg->delay2 > 0) {
×
825
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
826
                        ", %" PRId64 "a", pCfg->delay2);
827
    }
828
  }
829

830
  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
×
831
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
×
832
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
833
                      " ROLLUP(");
834
    for (int32_t i = 0; i < funcNum; ++i) {
×
835
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
×
836
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
837
                        "%s%s", ((i > 0) ? ", " : ""), pFunc);
838
    }
839
    *len +=
×
840
        snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ")");
×
841
  }
842

843
  if (pCfg->ttl > 0) {
×
844
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
845
                      " TTL %d", pCfg->ttl);
846
  }
847

848
  if (pCfg->keep > 0) {
×
849
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
850
                      " KEEP %dm", pCfg->keep);
851
  }
852

853
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
×
854
    int32_t nSma = 0;
×
855
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
856
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
857
        ++nSma;
×
858
      }
859
    }
860

861
    if (nSma < pCfg->numOfColumns && nSma > 0) {
×
862
      bool smaOn = false;
×
863
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
864
                        " SMA(");
865
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
×
866
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
×
867
          if (smaOn) {
×
868
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
869
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), ",`%s`",
×
870
                              (pCfg->pSchemas + i)->name);
×
871
          } else {
872
            smaOn = true;
×
873
            *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len,
×
874
                              SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), "`%s`",
×
875
                              (pCfg->pSchemas + i)->name);
×
876
          }
877
        }
878
      }
879
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, ")");
×
880
    }
881
  }
882

883
  if (pCfg->virtualStb) {
×
884
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
885
                      " VIRTUAL %d", pCfg->virtualStb);
×
886
  }
887
}
×
888

889
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
×
890
                                              void* charsetCxt) {
891
  int32_t code = TSDB_CODE_SUCCESS;
×
892
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
×
893
  pBlock->info.rows = 1;
×
894

895
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
×
896
  char             buf1[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1)] = {0};
×
897
  STR_TO_VARSTR(buf1, tbName);
×
898
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
×
899

900
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
×
901
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
×
902
  if (NULL == buf2) {
×
903
    QRY_ERR_RET(terrno);
×
904
  }
905

906
  int32_t len = 0;
×
907

908
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
×
909
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
910
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
911
    appendColumnFields(buf2, &len, pCfg);
×
912
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
×
913
                     ") TAGS (");
914
    appendTagFields(buf2, &len, pCfg);
×
915
    len +=
×
916
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
×
917
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
×
918
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
×
919
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
920
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
921
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
×
922
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
×
923
    appendTagNameFields(buf2, &len, pCfg);
×
924
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
×
925
                     ") TAGS (");
926
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
×
927
    TAOS_CHECK_ERRNO(code);
×
928
    len +=
×
929
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
×
930
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
×
931
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
×
932
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
933
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
934
    appendColumnFields(buf2, &len, pCfg);
×
935
    len +=
×
936
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
×
937
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
×
938
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
×
939
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
940
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
941
    appendColumnFields(buf2, &len, pCfg);
×
942
    len +=
×
943
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
×
944
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
×
945
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
946
                     "CREATE VTABLE `%s`", expandIdentifier(tbName, buf1));
947
    appendColRefFields(buf2, &len, pCfg);
×
948
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
×
949
                    " USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
×
950
    appendTagNameFields(buf2, &len, pCfg);
×
951
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
×
952
                     ") TAGS (");
953
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
×
954
    TAOS_CHECK_ERRNO(code);
×
955
    len +=
×
956
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
×
957
  }
958

959
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
×
960

961
  code = colDataSetVal(pCol2, 0, buf2, false);
×
962
  TAOS_CHECK_ERRNO(code);
×
963

964
_exit:
×
965
  taosMemoryFree(buf2);
×
966

967
  return code;
×
968
}
969

970
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
×
971
  int32_t code = 0;
×
972
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
×
973
  pBlock->info.rows = 1;
×
974

975
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
×
976
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
×
977
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
×
978
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
×
979
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
×
980

981
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
×
982
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
×
983
  if (NULL == buf2) {
×
984
    return terrno;
×
985
  }
986

987
  SViewMeta* pMeta = pStmt->pViewMeta;
×
988
  if (NULL == pMeta) {
×
989
    qError("exception: view meta is null");
×
990
    taosMemoryFree(buf2);
×
991
    return TSDB_CODE_APP_ERROR;
×
992
  }
993
  snprintf(varDataVal(buf2), SHOW_CREATE_VIEW_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE VIEW `%s`.`%s` AS %s",
×
994
           pStmt->dbName, pStmt->viewName, pMeta->querySql);
×
995
  int32_t len = strlen(varDataVal(buf2));
×
996
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
×
997
  code = colDataSetVal(pCol2, 0, buf2, false);
×
998
  taosMemoryFree(buf2);
×
999

1000
  return code;
×
1001
}
1002

1003
extern const char* fmGetFuncName(int32_t funcId);
1004
static int32_t setCreateRsmaResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateRsmaStmt* pStmt) {
×
1005
  int32_t       code = 0, lino = 0;
×
1006
  char*         buf2 = NULL;
×
1007
  SRsmaInfoRsp* pMeta = pStmt->pRsmaMeta;
×
1008

1009
  if (pMeta->nFuncs != pMeta->nColNames) {
×
1010
    qError("exception: rsma meta is invalid, nFuncs:%d != nColNames:%d", pMeta->nFuncs, pMeta->nColNames);
×
1011
    return TSDB_CODE_APP_ERROR;
×
1012
  }
1013

1014
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, 1));
×
1015
  pBlock->info.rows = 1;
×
1016

1017
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
×
1018
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1] = {0};
×
1019
  snprintf(varDataVal(buf1), TSDB_TABLE_FNAME_LEN + 4, "`%s`", expandIdentifier(pStmt->rsmaName, buf1));
×
1020
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
×
1021
  TAOS_CHECK_EXIT(colDataSetVal(pCol1, 0, buf1, false));
×
1022

1023
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
×
1024
  if (!(buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN))) {
×
1025
    return terrno;
×
1026
  }
1027

1028
  SName name = {0};
×
1029
  TAOS_CHECK_EXIT(tNameFromString(&name, pMeta->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
×
1030

1031
  int32_t len = 0;
×
1032
  len += tsnprintf(varDataVal(buf2), SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
×
1033
                       "CREATE RSMA `%s` ON `%s`.`%s` FUNCTION(", expandIdentifier(pStmt->rsmaName, buf1),
×
1034
                       expandIdentifier(name.dbname, buf1), expandIdentifier(name.tname, buf1));
1035
  for (int32_t i = 0; i < pMeta->nFuncs; ++i) {
×
1036
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
×
1037
                         "%s%s(`%s`)", (i > 0) ? "," : "", fmGetFuncName(pMeta->funcIds[i]),
×
1038
                         expandIdentifier(*(char**)TARRAY_GET_ELEM(pMeta->colNames, i), buf1));
×
1039
  }
1040
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
×
1041
                       ") INTERVAL(%d%c", pMeta->interval[0], pMeta->intervalUnit);
×
1042
  if (pMeta->interval[1] > 0) {
×
1043
    len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
×
1044
                         ",%d%c", pMeta->interval[1], pMeta->intervalUnit);
×
1045
  }
1046
  len += tsnprintf(varDataVal(buf2) + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
×
1047
  varDataLen(buf2) = len;
×
1048
  code = colDataSetVal(pCol2, 0, buf2, false);
×
1049
_exit:
×
1050
  taosMemoryFree(buf2);
×
1051
  return code;
×
1052
}
1053

1054
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
×
1055
  SSDataBlock* pBlock = NULL;
×
1056
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
×
1057
  if (TSDB_CODE_SUCCESS == code) {
×
1058
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
×
1059
  }
1060
  if (TSDB_CODE_SUCCESS == code) {
×
1061
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
×
1062
  }
1063
  (void)blockDataDestroy(pBlock);
×
1064
  return code;
×
1065
}
1066

1067
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
×
1068
  SSDataBlock* pBlock = NULL;
×
1069
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
×
1070
  if (TSDB_CODE_SUCCESS == code) {
×
1071
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
×
1072
  }
1073
  if (TSDB_CODE_SUCCESS == code) {
×
1074
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
×
1075
  }
1076
  (void)blockDataDestroy(pBlock);
×
1077
  return code;
×
1078
}
1079

1080
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
×
1081
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
×
1082
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
×
1083
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
×
1084
    return terrno;
×
1085
  }
1086

1087
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
×
1088
}
1089

1090
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
8✔
1091
  int32_t code = 0;
8✔
1092

1093
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
8✔
1094
    taosResetLog();
×
1095
    cfgDumpCfg(tsCfg, 0, false);
×
1096
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
8✔
1097
    int32_t tmp = 0;
×
1098
    code = taosStr2int32(value, &tmp);
×
1099
    if (code) {
×
1100
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1101
      return code;
×
1102
    }
1103
    code = schedulerUpdatePolicy(tmp);
×
1104
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
8✔
1105
    int32_t tmp = 0;
×
1106
    code = taosStr2int32(value, &tmp);
×
1107
    if (code) {
×
1108
      qError("invalid value:%s, error:%s", value, tstrerror(code));
×
1109
      return code;
×
1110
    }
1111
    code = schedulerEnableReSchedule(tmp != 0);
×
1112
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
8✔
1113
    code = ctgdHandleDbgCommand(value);
×
1114
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
8✔
1115
    code = taosMemoryDbgInit();
×
1116
    if (code) {
×
1117
      qError("failed to init memory dbg, error:%s", tstrerror(code));
×
1118
      return code;
×
1119
    }
1120
    tsAsyncLog = false;
×
1121
    qInfo("memory dbg enabled");
×
1122
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
8✔
1123
    code = taosMemoryDbgInitRestore();
×
1124
    if (code) {
×
1125
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
×
1126
      return code;
×
1127
    }
1128
    qInfo("memory dbg disabled");
×
1129
  } else {
1130
    goto _return;
8✔
1131
  }
1132

1133
  *processed = true;
×
1134

1135
_return:
8✔
1136

1137
  if (code) {
8✔
1138
    terrno = code;
×
1139
  }
1140

1141
  return code;
8✔
1142
}
1143

1144
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
8✔
1145
  bool processed = false;
8✔
1146

1147
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
8✔
1148
    return terrno;
×
1149
  }
1150

1151
  if (processed) {
8✔
1152
    goto _return;
×
1153
  }
1154

1155
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
8✔
1156
    return terrno;
×
1157
  }
1158

1159
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
8✔
1160
    return terrno;
×
1161
  }
1162

1163
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
8✔
1164

1165
_return:
8✔
1166

1167
  return TSDB_CODE_SUCCESS;
8✔
1168
}
1169

1170
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
36✔
1171
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
36✔
1172
  if (NULL == pBlock) {
36✔
1173
    return terrno;
×
1174
  }
1175

1176
  pBlock->info.hasVarCol = true;
36✔
1177

1178
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
36✔
1179
  if (NULL == pBlock->pDataBlock) {
36✔
1180
    taosMemoryFree(pBlock);
×
1181
    return terrno;
×
1182
  }
1183

1184
  SColumnInfoData infoData = {0};
36✔
1185

1186
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
36✔
1187
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
36✔
1188
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
72✔
1189
    goto _exit;
×
1190
  }
1191

1192
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
36✔
1193
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
36✔
1194
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
72✔
1195
    goto _exit;
×
1196
  }
1197

1198
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
36✔
1199
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
36✔
1200
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
72✔
1201
    goto _exit;
×
1202
  }
1203

1204
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
36✔
1205
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
36✔
1206
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
72✔
1207
    goto _exit;
×
1208
  }
1209

1210
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
36✔
1211
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
36✔
1212
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
72✔
1213
    goto _exit;
×
1214
  }
1215

1216
  *pOutput = pBlock;
36✔
1217

1218
_exit:
36✔
1219
  if (terrno != TSDB_CODE_SUCCESS) {
36✔
1220
    taosArrayDestroy(pBlock->pDataBlock);
×
1221
    taosMemoryFree(pBlock);
×
1222
  }
1223
  return terrno;
36✔
1224
}
1225

1226
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
36✔
1227
  SSDataBlock* pBlock = NULL;
36✔
1228
  char*        likePattern = NULL;
36✔
1229
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
36✔
1230
  if (TSDB_CODE_SUCCESS == code) {
36✔
1231
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
36✔
1232
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
×
1233
    }
1234
  }
1235
  if (TSDB_CODE_SUCCESS == code) {
36✔
1236
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
36✔
1237
  }
1238
  if (TSDB_CODE_SUCCESS == code) {
36✔
1239
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
36✔
1240
  }
1241
  (void)blockDataDestroy(pBlock);
36✔
1242
  return code;
36✔
1243
}
1244

1245
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
293✔
1246
  QRY_PARAM_CHECK(pOutput);
293✔
1247

1248
  SSDataBlock* pBlock = NULL;
293✔
1249
  int32_t      code = createDataBlock(&pBlock);
293✔
1250
  if (code) {
293✔
1251
    return code;
×
1252
  }
1253

1254
  SNode* pProj = NULL;
293✔
1255
  FOREACH(pProj, pProjects) {
586✔
1256
    SExprNode*      pExpr = (SExprNode*)pProj;
293✔
1257
    SColumnInfoData infoData = {0};
293✔
1258
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
293✔
1259
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1260
      infoData.info.bytes = 0;
×
1261
    } else {
1262
      infoData.info.type = pExpr->resType.type;
293✔
1263
      infoData.info.bytes = pExpr->resType.bytes;
293✔
1264
      infoData.info.precision = pExpr->resType.precision;
293✔
1265
      infoData.info.scale = pExpr->resType.scale;
293✔
1266
    }
1267
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
293✔
1268
  }
1269

1270
  *pOutput = pBlock;
293✔
1271
  return code;
293✔
1272
}
1273

1274
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
293✔
1275
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
293✔
1276

1277
  int32_t index = 0;
293✔
1278
  SNode*  pProj = NULL;
293✔
1279
  FOREACH(pProj, pProjects) {
586✔
1280
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
293✔
1281
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1282
    } else {
1283
      if (((SValueNode*)pProj)->isNull) {
293✔
1284
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
×
1285
      } else {
1286
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
293✔
1287
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1288
      }
1289
    }
1290
  }
1291

1292
  pBlock->info.rows = 1;
293✔
1293
  return TSDB_CODE_SUCCESS;
293✔
1294
}
1295

1296
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
293✔
1297
  SSDataBlock* pBlock = NULL;
293✔
1298
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
293✔
1299
  if (TSDB_CODE_SUCCESS == code) {
293✔
1300
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
293✔
1301
  }
1302
  if (TSDB_CODE_SUCCESS == code) {
293✔
1303
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
293✔
1304
  }
1305
  (void)blockDataDestroy(pBlock);
293✔
1306
  return code;
293✔
1307
}
1308

1309
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
×
1310
  SSDataBlock* pBlock = NULL;
×
1311
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
×
1312
  if (TSDB_CODE_SUCCESS == code) {
×
1313
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
×
1314
  }
1315
  if (TSDB_CODE_SUCCESS == code) {
×
1316
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
×
1317
  }
1318
  (void)blockDataDestroy(pBlock);
×
1319
  return code;
×
1320
}
1321

1322
static int32_t execShowCreateRsma(SShowCreateRsmaStmt* pStmt, SRetrieveTableRsp** pRsp) {
×
1323
  SSDataBlock* pBlock = NULL;
×
1324
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
×
1325
  if (TSDB_CODE_SUCCESS == code) {
×
1326
    code = setCreateRsmaResultIntoDataBlock(pBlock, pStmt);
×
1327
  }
1328
  if (TSDB_CODE_SUCCESS == code) {
×
1329
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
×
1330
  }
1331
  (void)blockDataDestroy(pBlock);
×
1332
  return code;
×
1333
}
1334

1335
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
337✔
1336
                     void* charsetCxt) {
1337
  switch (nodeType(pStmt)) {
337✔
1338
    case QUERY_NODE_DESCRIBE_STMT:
×
1339
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
×
1340
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
×
1341
      return execResetQueryCache();
×
1342
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
×
1343
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
×
1344
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
×
1345
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
×
1346
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
×
1347
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
×
1348
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
×
1349
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
×
1350
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
×
1351
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
×
1352
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
×
1353
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
×
1354
    case QUERY_NODE_ALTER_LOCAL_STMT:
8✔
1355
      return execAlterLocal((SAlterLocalStmt*)pStmt);
8✔
1356
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
36✔
1357
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
36✔
1358
    case QUERY_NODE_SELECT_STMT:
293✔
1359
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
293✔
1360
    default:
×
1361
      break;
×
1362
  }
1363
  return TSDB_CODE_FAILED;
×
1364
}
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