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

taosdata / TDengine / #4897

25 Dec 2025 10:17AM UTC coverage: 65.717% (-0.2%) from 65.929%
#4897

push

travis-ci

web-flow
fix: [6622889291] Fix invalid rowSize. (#34043)

186011 of 283047 relevant lines covered (65.72%)

113853896.64 hits per line

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

80.26
/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) {
2,893,872✔
42
  if (NULL == pBlock || NULL == pRsp) {
2,893,872✔
43
    return TSDB_CODE_INVALID_PARA;
×
44
  }
45
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
2,893,872✔
46
  size_t rspSize = sizeof(SRetrieveTableRsp) + dataEncodeBufSize + PAYLOAD_PREFIX_LEN;
2,893,872✔
47
  *pRsp = taosMemoryCalloc(1, rspSize);
2,893,872✔
48
  if (NULL == *pRsp) {
2,893,872✔
49
    return terrno;
×
50
  }
51

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

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

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

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

75
  return TSDB_CODE_SUCCESS;
2,893,872✔
76
}
77

78
static int32_t getSchemaBytes(const SSchema* pSchema) {
77,310,176✔
79
  switch (pSchema->type) {
77,310,176✔
80
    case TSDB_DATA_TYPE_BINARY:
3,711,832✔
81
    case TSDB_DATA_TYPE_VARBINARY:
82
    case TSDB_DATA_TYPE_GEOMETRY:
83
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
3,711,832✔
84
    case TSDB_DATA_TYPE_NCHAR:
5,403,764✔
85
    case TSDB_DATA_TYPE_JSON:
86
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
5,403,764✔
87
    default:
68,194,580✔
88
      return pSchema->bytes;
68,194,580✔
89
  }
90
}
91

92
static const char* expandIdentifier(const char* name, char* output) {
912,425✔
93
  if (NULL == name) return "";
912,425✔
94
  bool containsEscapeChar = false;
912,425✔
95
  for (const char* p = name; *p != '\0'; ++p) {
4,444,892✔
96
    if (*p == TS_ESCAPE_CHAR) {
3,552,717✔
97
      containsEscapeChar = true;
20,250✔
98
      break;
20,250✔
99
    }
100
  }
101
  if (!containsEscapeChar) return name;
912,425✔
102
  if (NULL == output) return "";
20,250✔
103
  char* out_ptr = output;
20,250✔
104
  for (const char* src = name; *src != '\0'; ++src) {
124,650✔
105
    if (*src == TS_ESCAPE_CHAR) {
104,400✔
106
      *out_ptr++ = TS_ESCAPE_CHAR;
50,850✔
107
      *out_ptr++ = TS_ESCAPE_CHAR;
50,850✔
108
    } else {
109
      *out_ptr++ = *src;
53,550✔
110
    }
111
  }
112
  *out_ptr = '\0';
20,250✔
113
  return output;
20,250✔
114
}
115

116
static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) {
586,600✔
117
  QRY_PARAM_CHECK(pOutput);
586,600✔
118

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

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

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

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

191
  if (hasRefCol(pMeta->tableType)) {
586,858✔
192
    pCol5 = taosArrayGet(pBlock->pDataBlock, 4);
5,356✔
193
  }
194

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

204
    if (IS_DECIMAL_TYPE(pMeta->schema[i].type) && withExtSchema(pMeta->tableType)) {
77,310,176✔
205
      uint8_t prec = 0, scale = 0;
274,038✔
206
      decimalFromTypeMod(pMeta->schemaExt[i].typeMod, &prec, &scale);
274,038✔
207
      size_t len = snprintf(buf + VARSTR_HEADER_SIZE, DESCRIBE_RESULT_FIELD_LEN - VARSTR_HEADER_SIZE, "%s(%hhu, %hhu)",
274,038✔
208
                            tDataTypes[pMeta->schema[i].type].name, prec, scale);
274,038✔
209
      varDataSetLen(buf, len);
274,038✔
210
    } else {
211
      STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
77,036,138✔
212
    }
213
    COL_DATA_SET_VAL_AND_CHECK(pCol2, pBlock->info.rows, buf, false);
77,310,176✔
214
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
77,310,176✔
215
    COL_DATA_SET_VAL_AND_CHECK(pCol3, pBlock->info.rows, (const char*)&bytes, false);
77,310,176✔
216
    if (TSDB_VIEW_TABLE != pMeta->tableType) {
77,310,176✔
217
      if (i >= pMeta->tableInfo.numOfColumns) {
77,272,576✔
218
        STR_TO_VARSTR(buf, "TAG");
4,348,157✔
219
        fillTagCol = 1;
4,348,157✔
220
      } else if (i == 1 && pMeta->schema[i].flags & COL_IS_KEY) {
72,924,419✔
221
        STR_TO_VARSTR(buf, "COMPOSITE KEY")
14,500✔
222
      } else {
223
        STR_TO_VARSTR(buf, "");
72,909,919✔
224
      }
225
    } else {
226
      STR_TO_VARSTR(buf, "VIEW COL");
37,600✔
227
    }
228
    COL_DATA_SET_VAL_AND_CHECK(pCol4, pBlock->info.rows, buf, false);
77,310,176✔
229
    if (withExtSchema(pMeta->tableType) && pMeta->schemaExt) {
77,310,176✔
230
      if (i < pMeta->tableInfo.numOfColumns) {
77,170,298✔
231
        STR_TO_VARSTR(buf, columnEncodeStr(COMPRESS_L1_TYPE_U32(pMeta->schemaExt[i].compress)));
72,830,289✔
232
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
72,830,289✔
233
        STR_TO_VARSTR(buf, columnCompressStr(COMPRESS_L2_TYPE_U32(pMeta->schemaExt[i].compress)));
72,830,289✔
234
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
72,830,289✔
235
        STR_TO_VARSTR(buf, columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pMeta->schemaExt[i].compress)));
72,830,289✔
236
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
72,830,034✔
237
      } else {
238
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
4,340,009✔
239
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
4,340,009✔
240
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
4,340,009✔
241
        COL_DATA_SET_VAL_AND_CHECK(pCol6, pBlock->info.rows, buf, false);
4,340,009✔
242
        STR_TO_VARSTR(buf, fillTagCol == 0 ? "" : "disabled");
4,340,009✔
243
        COL_DATA_SET_VAL_AND_CHECK(pCol7, pBlock->info.rows, buf, false);
4,340,009✔
244
      }
245
    } else if (hasRefCol(pMeta->tableType) && pMeta->colRef) {
139,878✔
246
      if (i < pMeta->numOfColRefs) {
47,948✔
247
        if (pMeta->colRef[i].hasRef) {
39,800✔
248
          char refColName[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_COL_FNAME_LEN] = {0};
18,390✔
249
          strcat(refColName, pMeta->colRef[i].refDbName);
18,390✔
250
          strcat(refColName, ".");
18,390✔
251
          strcat(refColName, pMeta->colRef[i].refTableName);
18,390✔
252
          strcat(refColName, ".");
18,390✔
253
          strcat(refColName, pMeta->colRef[i].refColName);
18,390✔
254
          STR_TO_VARSTR(buf, refColName);
18,390✔
255
        } else {
256
          STR_TO_VARSTR(buf, "");
21,410✔
257
        }
258
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
39,800✔
259
      } else {
260
        STR_TO_VARSTR(buf, "");
8,148✔
261
        COL_DATA_SET_VAL_AND_CHECK(pCol5, pBlock->info.rows, buf, false);
8,148✔
262
      }
263
    }
264

265
    fillTagCol = 0;
77,310,176✔
266

267
    ++(pBlock->info.rows);
77,310,176✔
268
  }
269
  if (pMeta->tableType == TSDB_SUPER_TABLE && biMode != 0) {
586,858✔
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) {
586,858✔
281
    qError("no permission to view any columns");
×
282
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
283
  }
284
  return TSDB_CODE_SUCCESS;
586,858✔
285
}
286

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

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

316
static int32_t execResetQueryCache() { return catalogClearCache(); }
2,207,964✔
317

318
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
96,539✔
319
  QRY_PARAM_CHECK(pOutput);
96,539✔
320

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

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

334
  if (TSDB_CODE_SUCCESS == code) {
96,539✔
335
    *pOutput = pBlock;
96,539✔
336
  } else {
337
    (void)blockDataDestroy(pBlock);
×
338
  }
339
  return code;
96,539✔
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) {
96,539✔
368
  size_t size = taosArrayGetSize(pRetension);
96,539✔
369
  if (size == 0) {
96,539✔
370
    *ppRetentions = NULL;
96,539✔
371
    return TSDB_CODE_SUCCESS;
96,539✔
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) {
95,805✔
401
  switch (cacheModel) {
95,805✔
402
    case TSDB_CACHE_MODEL_NONE:
95,805✔
403
      return TSDB_CACHE_MODEL_NONE_STR;
95,805✔
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) {
95,805✔
417
  if (algorithmsId[0] != '\0') {
95,805✔
418
    return algorithmsId;
×
419
  } else if (encryptAlgorithm != 0) {
95,805✔
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;
95,805✔
432
}
433

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

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

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

460
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
96,539✔
461
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
96,539✔
462
  int32_t          len = 0;
96,539✔
463
  char*            prec = NULL;
96,539✔
464
  switch (pCfg->precision) {
96,539✔
465
    case TSDB_TIME_PRECISION_MILLI:
96,539✔
466
      prec = TSDB_TIME_PRECISION_MILLI_STR;
96,539✔
467
      break;
96,539✔
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;
96,539✔
480
  QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions));
96,539✔
481
  int32_t dbFNameLen = strlen(dbFName);
96,539✔
482
  int32_t hashPrefix = 0;
96,539✔
483
  if (pCfg->hashPrefix > 0) {
96,539✔
484
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
1,578✔
485
  } else if (pCfg->hashPrefix < 0) {
94,961✔
486
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
2,367✔
487
  }
488
  char durationStr[128] = {0};
96,539✔
489
  char keep0Str[128] = {0};
96,539✔
490
  char keep1Str[128] = {0};
96,539✔
491
  char keep2Str[128] = {0};
96,539✔
492
  char compactIntervalStr[13] = {0};
96,539✔
493
  char compactStartTimeStr[13] = {0};
96,539✔
494
  char compactEndTimeStr[13] = {0};
96,539✔
495

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

504
  if (IS_SYS_DBNAME(dbName)) {
96,539✔
505
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
734✔
506
                     "CREATE DATABASE `%s`", dbName);
734✔
507
  } else {
508
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
574,830✔
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,
95,805✔
517
                     durationStr, pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows, pCfg->sstTrigger, keep0Str,
95,805✔
518
                     keep1Str, keep2Str, pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel,
95,805✔
519
                     pCfg->numOfVgroups, 1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize,
95,805✔
520
                     pCfg->walRetentionPeriod, pCfg->walRetentionSize, pCfg->keepTimeOffset,
521
                     encryptAlgorithmStr(pCfg->encryptAlgr, pCfg->algorithmsId), pCfg->ssChunkSize, pCfg->ssKeepLocal,
95,805✔
522
                     pCfg->ssCompact, compactIntervalStr, compactStartTimeStr, compactEndTimeStr,
95,805✔
523
                     pCfg->compactTimeOffset, pCfg->isAudit);
95,805✔
524

525
    if (pRetentions) {
95,805✔
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);
96,539✔
532

533
  (varDataLen(buf2)) = len;
96,539✔
534

535
  COL_DATA_SET_VAL_AND_CHECK(pCol2, 0, buf2, false);
96,539✔
536

537
  return TSDB_CODE_SUCCESS;
96,539✔
538
}
539

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

553
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
120,570✔
554
  QRY_PARAM_CHECK(pOutput);
120,570✔
555

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

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

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

577
static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) {
7,544✔
578
  QRY_PARAM_CHECK(pOutput);
7,544✔
579

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

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

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

601
static void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
69,302✔
602
  char expandName[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1) + 1] = {0};
69,302✔
603
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
533,328✔
604
    SSchema* pSchema = pCfg->pSchemas + i;
464,026✔
605
    SColRef* pRef = pCfg->pColRefs + i;
464,026✔
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];
464,026✔
610
    snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
464,026✔
611
    int typeLen = strlen(type);
464,026✔
612
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
464,026✔
613
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
418,460✔
614
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
52,375✔
615
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
411,651✔
616
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
15,358✔
617
                           (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
15,358✔
618
    } else if (IS_DECIMAL_TYPE(pSchema->type)) {
396,293✔
619
      uint8_t precision, scale;
19,032✔
620
      decimalFromTypeMod(pCfg->pSchemaExt[i].typeMod, &precision, &scale);
19,032✔
621
      typeLen += tsnprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d,%d)", precision, scale);
19,032✔
622
    }
623

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

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

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

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

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

671
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
4,390✔
672
                      "%s`%s` %s", ((i > 1) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
4,390✔
673

674
  }
675
}
1,148✔
676

677
static void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
42,200✔
678
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
42,200✔
679
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
241,050✔
680
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
198,850✔
681
    char     type[32];
198,850✔
682
    snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
198,850✔
683
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
198,850✔
684
        TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
170,767✔
685
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
32,990✔
686
               (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
32,990✔
687
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
165,860✔
688
      snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
8,384✔
689
               (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
8,384✔
690
    }
691

692
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
198,850✔
693
                      "%s`%s` %s", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName), type);
198,850✔
694
  }
695
}
42,200✔
696

697
static void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
38,634✔
698
  char expandName[(TSDB_COL_NAME_LEN << 1) + 1] = {0};
38,634✔
699
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
95,631✔
700
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
56,997✔
701
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
56,997✔
702
                      "%s`%s`", ((i > 0) ? ", " : ""), expandIdentifier(pSchema->name, expandName));
56,997✔
703
  }
704
}
38,634✔
705

706
static int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg, void* charsetCxt) {
38,634✔
707
  int32_t code = TSDB_CODE_SUCCESS;
38,634✔
708
  SArray* pTagVals = NULL;
38,634✔
709
  STag*   pTag = (STag*)pCfg->pTags;
38,634✔
710

711
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
38,634✔
712
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
×
713
    return TSDB_CODE_APP_ERROR;
×
714
  }
715

716
  if (tTagIsJson(pTag)) {
38,634✔
717
    char* pJson = NULL;
2,795✔
718
    parseTagDatatoJson(pTag, &pJson, charsetCxt);
2,795✔
719
    if (NULL == pJson) {
2,795✔
720
      qError("failed to parse tag to json, pJson is NULL");
×
721
      return terrno;
×
722
    }
723
    char* escapedJson = taosMemCalloc(sizeof(char), strlen(pJson) * 2 + 1);  // taosMemoryAlloc(strlen(pJson) * 2 + 1);
2,795✔
724
    if (escapedJson) {
2,795✔
725
      char* writer = escapedJson;
2,795✔
726
      for (char* reader = pJson; *reader; ++reader) {
55,900✔
727
        if (*reader == '\'') {
53,105✔
728
          *writer++ = '\'';  // Escape single quote by doubling it
559✔
729
        }
730
        *writer++ = *reader;
53,105✔
731
      }
732
      *writer = '\0';
2,795✔
733

734
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,795✔
735
                        "'%s'", escapedJson);
736
      taosMemoryFree(escapedJson);
2,795✔
737
    } else {
738
      taosMemoryFree(pJson);
×
739
      return terrno;
×
740
    }
741
    taosMemoryFree(pJson);
2,795✔
742

743
    return TSDB_CODE_SUCCESS;
2,795✔
744
  }
745

746
  QRY_ERR_RET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
35,839✔
747
  int16_t valueNum = taosArrayGetSize(pTagVals);
35,839✔
748
  int32_t num = 0;
35,839✔
749
  int32_t j = 0;
35,839✔
750
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
90,041✔
751
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
54,202✔
752
    if (i > 0) {
54,202✔
753
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
18,363✔
754
                        ", ");
755
    }
756

757
    if (j >= valueNum) {
54,202✔
758
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
16,452✔
759
                        "NULL");
760
      continue;
16,452✔
761
    }
762

763
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
37,750✔
764
    if (pSchema->colId > pTagVal->cid) {
37,750✔
765
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
×
766
      code = TSDB_CODE_APP_ERROR;
×
767
      TAOS_CHECK_ERRNO(code);
×
768
    } else if (pSchema->colId == pTagVal->cid) {
37,750✔
769
      char    type = pTagVal->type;
36,126✔
770
      int32_t tlen = 0;
36,126✔
771

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

796
  return code;
35,839✔
797
}
798

799
static void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
105,640✔
800
  if (pCfg->commentLen > 0) {
105,640✔
801
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
802
                      " COMMENT '%s'", pCfg->pComment);
803
  } else if (0 == pCfg->commentLen) {
105,640✔
804
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
3,960✔
805
                      " COMMENT ''");
806
  }
807

808
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
105,640✔
809
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
810
                      " WATERMARK %" PRId64 "a", pCfg->watermark1);
811
    if (pCfg->watermark2 > 0) {
×
812
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
813
                        ", %" PRId64 "a", pCfg->watermark2);
814
    }
815
  }
816

817
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
105,640✔
818
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
819
                      " MAX_DELAY %" PRId64 "a", pCfg->delay1);
820
    if (pCfg->delay2 > 0) {
×
821
      *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
822
                        ", %" PRId64 "a", pCfg->delay2);
823
    }
824
  }
825

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

839
  if (pCfg->ttl > 0) {
105,640✔
840
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
×
841
                      " TTL %d", pCfg->ttl);
842
  }
843

844
  if (pCfg->keep > 0) {
105,640✔
845
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
2,916✔
846
                      " KEEP %dm", pCfg->keep);
847
  }
848

849
  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
105,640✔
850
    int32_t nSma = 0;
68,154✔
851
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
516,870✔
852
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
448,716✔
853
        ++nSma;
448,716✔
854
      }
855
    }
856

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

879
  if (pCfg->virtualStb) {
105,640✔
880
    *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
450✔
881
                      " VIRTUAL %d", pCfg->virtualStb);
450✔
882
  }
883
}
105,640✔
884

885
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg,
118,946✔
886
                                              void* charsetCxt) {
887
  int32_t code = TSDB_CODE_SUCCESS;
118,946✔
888
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
118,946✔
889
  pBlock->info.rows = 1;
118,946✔
890

891
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
118,946✔
892
  char             buf1[(SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1)] = {0};
118,946✔
893
  STR_TO_VARSTR(buf1, tbName);
118,946✔
894
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
118,946✔
895

896
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
118,946✔
897
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
118,946✔
898
  if (NULL == buf2) {
118,946✔
899
    QRY_ERR_RET(terrno);
×
900
  }
901

902
  int32_t len = 0;
118,946✔
903

904
  if (TSDB_SUPER_TABLE == pCfg->tableType) {
118,946✔
905
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
42,200✔
906
                     "CREATE STABLE `%s` (", expandIdentifier(tbName, buf1));
907
    appendColumnFields(buf2, &len, pCfg);
42,200✔
908
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
42,200✔
909
                     ") TAGS (");
910
    appendTagFields(buf2, &len, pCfg);
42,200✔
911
    len +=
42,200✔
912
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
42,200✔
913
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
42,200✔
914
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
76,746✔
915
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
37,486✔
916
                     "CREATE TABLE `%s` ", expandIdentifier(tbName, buf1));
917
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
37,486✔
918
                     "USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
37,486✔
919
    appendTagNameFields(buf2, &len, pCfg);
37,486✔
920
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
37,486✔
921
                     ") TAGS (");
922
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
37,486✔
923
    TAOS_CHECK_ERRNO(code);
37,486✔
924
    len +=
37,486✔
925
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
37,486✔
926
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
37,486✔
927
  } else if (TSDB_NORMAL_TABLE == pCfg->tableType) {
39,260✔
928
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
25,954✔
929
                     "CREATE TABLE `%s` (", expandIdentifier(tbName, buf1));
930
    appendColumnFields(buf2, &len, pCfg);
25,954✔
931
    len +=
25,954✔
932
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
25,954✔
933
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
25,954✔
934
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pCfg->tableType) {
13,306✔
935
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1,148✔
936
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
937
    appendColumnFields(buf2, &len, pCfg);
1,148✔
938
    len +=
1,148✔
939
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,148✔
940
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pCfg->tableType) {
12,158✔
941
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_TB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE,
1,148✔
942
                     "CREATE VTABLE `%s` (", expandIdentifier(tbName, buf1));
943
    appendColRefFields(buf2, &len, pCfg);
1,148✔
944
    len += snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,148✔
945
                    ") USING `%s` (", expandIdentifier(pCfg->stbName, buf1));
1,148✔
946
    appendTagNameFields(buf2, &len, pCfg);
1,148✔
947
    len += tsnprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len),
1,148✔
948
                     ") TAGS (");
949
    code = appendTagValues(buf2, &len, pCfg, charsetCxt);
1,148✔
950
    TAOS_CHECK_ERRNO(code);
1,148✔
951
    len +=
1,148✔
952
        snprintf(buf2 + VARSTR_HEADER_SIZE + len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + len), ")");
1,148✔
953
  }
954

955
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
118,946✔
956

957
  code = colDataSetVal(pCol2, 0, buf2, false);
118,946✔
958
  TAOS_CHECK_ERRNO(code);
118,946✔
959

960
_exit:
118,946✔
961
  taosMemoryFree(buf2);
118,946✔
962

963
  return code;
118,946✔
964
}
965

966
static int32_t setCreateViewResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateViewStmt* pStmt) {
7,544✔
967
  int32_t code = 0;
7,544✔
968
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
7,544✔
969
  pBlock->info.rows = 1;
7,544✔
970

971
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
7,544✔
972
  char             buf1[SHOW_CREATE_VIEW_RESULT_FIELD1_LEN + 1] = {0};
7,544✔
973
  snprintf(varDataVal(buf1), TSDB_VIEW_FNAME_LEN + 4, "`%s`.`%s`", pStmt->dbName, pStmt->viewName);
7,544✔
974
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
7,544✔
975
  QRY_ERR_RET(colDataSetVal(pCol1, 0, buf1, false));
7,544✔
976

977
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
7,544✔
978
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_VIEW_RESULT_FIELD2_LEN);
7,544✔
979
  if (NULL == buf2) {
7,544✔
980
    return terrno;
×
981
  }
982

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

996
  return code;
7,544✔
997
}
998

999
extern const char* fmGetFuncName(int32_t funcId);
1000
static int32_t setCreateRsmaResultIntoDataBlock(SSDataBlock* pBlock, SShowCreateRsmaStmt* pStmt) {
1,624✔
1001
  int32_t       code = 0, lino = 0;
1,624✔
1002
  char*         buf2 = NULL;
1,624✔
1003
  SRsmaInfoRsp* pMeta = pStmt->pRsmaMeta;
1,624✔
1004

1005
  if (pMeta->nFuncs != pMeta->nColNames) {
1,624✔
1006
    qError("exception: rsma meta is invalid, nFuncs:%d != nColNames:%d", pMeta->nFuncs, pMeta->nColNames);
×
1007
    return TSDB_CODE_APP_ERROR;
×
1008
  }
1009

1010
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, 1));
1,624✔
1011
  pBlock->info.rows = 1;
1,624✔
1012

1013
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
1,624✔
1014
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN << 1] = {0};
1,624✔
1015
  snprintf(varDataVal(buf1), TSDB_TABLE_FNAME_LEN + 4, "`%s`", expandIdentifier(pStmt->rsmaName, buf1));
1,624✔
1016
  varDataSetLen(buf1, strlen(varDataVal(buf1)));
1,624✔
1017
  TAOS_CHECK_EXIT(colDataSetVal(pCol1, 0, buf1, false));
1,624✔
1018

1019
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
1,624✔
1020
  if (!(buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN))) {
1,624✔
1021
    return terrno;
×
1022
  }
1023

1024
  SName name = {0};
1,624✔
1025
  TAOS_CHECK_EXIT(tNameFromString(&name, pMeta->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
1,624✔
1026

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

1050
static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
116,875✔
1051
  SSDataBlock* pBlock = NULL;
116,875✔
1052
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
116,875✔
1053
  if (TSDB_CODE_SUCCESS == code) {
116,875✔
1054
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
116,875✔
1055
  }
1056
  if (TSDB_CODE_SUCCESS == code) {
116,875✔
1057
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
116,875✔
1058
  }
1059
  (void)blockDataDestroy(pBlock);
116,875✔
1060
  return code;
116,875✔
1061
}
1062

1063
static int32_t execShowCreateVTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
2,071✔
1064
  SSDataBlock* pBlock = NULL;
2,071✔
1065
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
2,071✔
1066
  if (TSDB_CODE_SUCCESS == code) {
2,071✔
1067
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg, charsetCxt);
2,071✔
1068
  }
1069
  if (TSDB_CODE_SUCCESS == code) {
2,071✔
1070
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
2,071✔
1071
  }
1072
  (void)blockDataDestroy(pBlock);
2,071✔
1073
  return code;
2,071✔
1074
}
1075

1076
static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp, void* charsetCxt) {
1,101✔
1077
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
1,101✔
1078
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
1,101✔
1079
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
367✔
1080
    return terrno;
367✔
1081
  }
1082

1083
  return execShowCreateTable(pStmt, pRsp, charsetCxt);
734✔
1084
}
1085

1086
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
254,587✔
1087
  int32_t code = 0;
254,587✔
1088

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

1129
  *processed = true;
1,186✔
1130

1131
_return:
254,587✔
1132

1133
  if (code) {
254,587✔
1134
    terrno = code;
×
1135
  }
1136

1137
  return code;
254,587✔
1138
}
1139

1140
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
254,587✔
1141
  bool processed = false;
254,587✔
1142

1143
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
254,587✔
1144
    return terrno;
×
1145
  }
1146

1147
  if (processed) {
254,587✔
1148
    goto _return;
1,186✔
1149
  }
1150

1151
  if (cfgCheckRangeForDynUpdate(tsCfg, pStmt->config, pStmt->value, false, CFG_ALTER_LOCAL)) {
253,401✔
1152
    return terrno;
3,633✔
1153
  }
1154

1155
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CLIENT_CMD, true)) {
249,768✔
1156
    return terrno;
×
1157
  }
1158

1159
  TAOS_CHECK_RETURN(taosCfgDynamicOptions(tsCfg, pStmt->config, false));
249,768✔
1160

1161
_return:
250,856✔
1162

1163
  return TSDB_CODE_SUCCESS;
250,954✔
1164
}
1165

1166
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
32,678✔
1167
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
32,678✔
1168
  if (NULL == pBlock) {
32,678✔
1169
    return terrno;
×
1170
  }
1171

1172
  pBlock->info.hasVarCol = true;
32,678✔
1173

1174
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
32,678✔
1175
  if (NULL == pBlock->pDataBlock) {
32,678✔
1176
    taosMemoryFree(pBlock);
×
1177
    return terrno;
×
1178
  }
1179

1180
  SColumnInfoData infoData = {0};
32,678✔
1181

1182
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
32,678✔
1183
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;
32,678✔
1184
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
65,356✔
1185
    goto _exit;
×
1186
  }
1187

1188
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
32,678✔
1189
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
32,678✔
1190
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
65,356✔
1191
    goto _exit;
×
1192
  }
1193

1194
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
32,678✔
1195
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN;
32,678✔
1196
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
65,356✔
1197
    goto _exit;
×
1198
  }
1199

1200
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
32,678✔
1201
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD4_LEN;
32,678✔
1202
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
65,356✔
1203
    goto _exit;
×
1204
  }
1205

1206
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
32,678✔
1207
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD5_LEN;
32,678✔
1208
  if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) {
65,356✔
1209
    goto _exit;
×
1210
  }
1211

1212
  *pOutput = pBlock;
32,678✔
1213

1214
_exit:
32,678✔
1215
  if (terrno != TSDB_CODE_SUCCESS) {
32,678✔
1216
    taosArrayDestroy(pBlock->pDataBlock);
×
1217
    taosMemoryFree(pBlock);
×
1218
  }
1219
  return terrno;
32,678✔
1220
}
1221

1222
static int32_t execShowLocalVariables(SShowStmt* pStmt, SRetrieveTableRsp** pRsp) {
32,678✔
1223
  SSDataBlock* pBlock = NULL;
32,678✔
1224
  char*        likePattern = NULL;
32,678✔
1225
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
32,678✔
1226
  if (TSDB_CODE_SUCCESS == code) {
32,678✔
1227
    if (pStmt->tableCondType == OP_TYPE_LIKE) {
32,678✔
1228
      likePattern = ((SValueNode*)pStmt->pTbName)->literal;
2,612✔
1229
    }
1230
  }
1231
  if (TSDB_CODE_SUCCESS == code) {
32,678✔
1232
    code = dumpConfToDataBlock(pBlock, 0, likePattern);
32,678✔
1233
  }
1234
  if (TSDB_CODE_SUCCESS == code) {
32,678✔
1235
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
32,678✔
1236
  }
1237
  (void)blockDataDestroy(pBlock);
32,678✔
1238
  return code;
32,678✔
1239
}
1240

1241
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
2,049,683✔
1242
  QRY_PARAM_CHECK(pOutput);
2,049,683✔
1243

1244
  SSDataBlock* pBlock = NULL;
2,049,683✔
1245
  int32_t      code = createDataBlock(&pBlock);
2,049,683✔
1246
  if (code) {
2,049,683✔
1247
    return code;
×
1248
  }
1249

1250
  SNode* pProj = NULL;
2,049,683✔
1251
  FOREACH(pProj, pProjects) {
4,844,272✔
1252
    SExprNode*      pExpr = (SExprNode*)pProj;
2,794,589✔
1253
    SColumnInfoData infoData = {0};
2,794,589✔
1254
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
2,794,589✔
1255
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
×
1256
      infoData.info.bytes = 0;
×
1257
    } else {
1258
      infoData.info.type = pExpr->resType.type;
2,794,589✔
1259
      infoData.info.bytes = pExpr->resType.bytes;
2,794,589✔
1260
      infoData.info.precision = pExpr->resType.precision;
2,794,589✔
1261
      infoData.info.scale = pExpr->resType.scale;
2,794,589✔
1262
    }
1263
    QRY_ERR_RET(blockDataAppendColInfo(pBlock, &infoData));
2,794,589✔
1264
  }
1265

1266
  *pOutput = pBlock;
2,049,683✔
1267
  return code;
2,049,683✔
1268
}
1269

1270
static int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
2,049,683✔
1271
  QRY_ERR_RET(blockDataEnsureCapacity(pBlock, 1));
2,049,683✔
1272

1273
  int32_t index = 0;
2,049,683✔
1274
  SNode*  pProj = NULL;
2,049,683✔
1275
  FOREACH(pProj, pProjects) {
4,844,272✔
1276
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
2,794,589✔
1277
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
×
1278
    } else {
1279
      if (((SValueNode*)pProj)->isNull) {
2,794,589✔
1280
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true));
94,285✔
1281
      } else {
1282
        QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0,
2,700,304✔
1283
                                  nodesGetValueFromNode((SValueNode*)pProj), false));
1284
      }
1285
    }
1286
  }
1287

1288
  pBlock->info.rows = 1;
2,049,683✔
1289
  return TSDB_CODE_SUCCESS;
2,049,683✔
1290
}
1291

1292
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
2,049,683✔
1293
  SSDataBlock* pBlock = NULL;
2,049,683✔
1294
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
2,049,683✔
1295
  if (TSDB_CODE_SUCCESS == code) {
2,049,683✔
1296
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
2,049,683✔
1297
  }
1298
  if (TSDB_CODE_SUCCESS == code) {
2,049,683✔
1299
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
2,049,683✔
1300
  }
1301
  (void)blockDataDestroy(pBlock);
2,049,683✔
1302
  return code;
2,049,683✔
1303
}
1304

1305
static int32_t execShowCreateView(SShowCreateViewStmt* pStmt, SRetrieveTableRsp** pRsp) {
7,544✔
1306
  SSDataBlock* pBlock = NULL;
7,544✔
1307
  int32_t      code = buildCreateViewResultDataBlock(&pBlock);
7,544✔
1308
  if (TSDB_CODE_SUCCESS == code) {
7,544✔
1309
    code = setCreateViewResultIntoDataBlock(pBlock, pStmt);
7,544✔
1310
  }
1311
  if (TSDB_CODE_SUCCESS == code) {
7,544✔
1312
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_VIEW_RESULT_COLS, pRsp);
7,544✔
1313
  }
1314
  (void)blockDataDestroy(pBlock);
7,544✔
1315
  return code;
7,544✔
1316
}
1317

1318
static int32_t execShowCreateRsma(SShowCreateRsmaStmt* pStmt, SRetrieveTableRsp** pRsp) {
1,624✔
1319
  SSDataBlock* pBlock = NULL;
1,624✔
1320
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
1,624✔
1321
  if (TSDB_CODE_SUCCESS == code) {
1,624✔
1322
    code = setCreateRsmaResultIntoDataBlock(pBlock, pStmt);
1,624✔
1323
  }
1324
  if (TSDB_CODE_SUCCESS == code) {
1,624✔
1325
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
1,624✔
1326
  }
1327
  (void)blockDataDestroy(pBlock);
1,624✔
1328
  return code;
1,624✔
1329
}
1330

1331
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp, int8_t biMode,
5,356,532✔
1332
                     void* charsetCxt) {
1333
  switch (nodeType(pStmt)) {
5,356,532✔
1334
    case QUERY_NODE_DESCRIBE_STMT:
586,600✔
1335
      return execDescribe(sysInfoUser, pStmt, pRsp, biMode);
586,600✔
1336
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
2,207,964✔
1337
      return execResetQueryCache();
2,207,964✔
1338
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
96,539✔
1339
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
96,539✔
1340
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
116,141✔
1341
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
116,141✔
1342
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
2,071✔
1343
      return execShowCreateVTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
2,071✔
1344
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
1,101✔
1345
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp, charsetCxt);
1,101✔
1346
    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
7,544✔
1347
      return execShowCreateView((SShowCreateViewStmt*)pStmt, pRsp);
7,544✔
1348
    case QUERY_NODE_SHOW_CREATE_RSMA_STMT:
1,624✔
1349
      return execShowCreateRsma((SShowCreateRsmaStmt*)pStmt, pRsp);
1,624✔
1350
    case QUERY_NODE_ALTER_LOCAL_STMT:
254,587✔
1351
      return execAlterLocal((SAlterLocalStmt*)pStmt);
254,587✔
1352
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
32,678✔
1353
      return execShowLocalVariables((SShowStmt*)pStmt, pRsp);
32,678✔
1354
    case QUERY_NODE_SELECT_STMT:
2,049,941✔
1355
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
2,049,941✔
1356
    default:
×
1357
      break;
×
1358
  }
1359
  return TSDB_CODE_FAILED;
×
1360
}
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