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

#include "command.h"
D
dapan1121 已提交
17
#include "catalog.h"
18
#include "tdatablock.h"
D
dapan1121 已提交
19
#include "tglobal.h"
20

21 22
extern SConfig* tsCfg;
static int32_t  getSchemaBytes(const SSchema* pSchema) {
X
Xiaoyu Wang 已提交
23 24 25 26
  switch (pSchema->type) {
    case TSDB_DATA_TYPE_BINARY:
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
    case TSDB_DATA_TYPE_NCHAR:
wmmhello's avatar
wmmhello 已提交
27
    case TSDB_DATA_TYPE_JSON:
X
Xiaoyu Wang 已提交
28 29 30 31 32
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
    default:
      return pSchema->bytes;
  }
}
33

34
static SSDataBlock* buildDescResultDataBlock() {
35
  SSDataBlock* pBlock = createDataBlock();
36

37 38
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_FIELD_LEN, 1);
  blockDataAppendColInfo(pBlock, &infoData);
39

40 41
  infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_TYPE_LEN, 2);
  blockDataAppendColInfo(pBlock, &infoData);
42

43 44
  infoData = createColumnInfoData(TSDB_DATA_TYPE_INT, tDataTypes[TSDB_DATA_TYPE_INT].bytes, 3);
  blockDataAppendColInfo(pBlock, &infoData);
45

46 47
  infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_NOTE_LEN, 4);
  blockDataAppendColInfo(pBlock, &infoData);
48 49 50 51 52 53 54 55 56
  return pBlock;
}

static void setDescResultIntoDataBlock(SSDataBlock* pBlock, int32_t numOfRows, STableMeta* pMeta) {
  blockDataEnsureCapacity(pBlock, numOfRows);
  pBlock->info.rows = numOfRows;

  // field
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
57
  char             buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
58
  for (int32_t i = 0; i < numOfRows; ++i) {
59 60
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
    colDataAppend(pCol1, i, buf, false);
61
  }
62

63
  // Type
64
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
65
  for (int32_t i = 0; i < numOfRows; ++i) {
66 67
    STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
    colDataAppend(pCol2, i, buf, false);
68 69 70
  }

  // Length
71
  SColumnInfoData* pCol3 = taosArrayGet(pBlock->pDataBlock, 2);
72
  for (int32_t i = 0; i < numOfRows; ++i) {
73 74
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
    colDataAppend(pCol3, i, (const char*)&bytes, false);
75 76 77
  }

  // Note
78
  SColumnInfoData* pCol4 = taosArrayGet(pBlock->pDataBlock, 3);
79
  for (int32_t i = 0; i < numOfRows; ++i) {
80 81
    STR_TO_VARSTR(buf, i >= pMeta->tableInfo.numOfColumns ? "TAG" : "");
    colDataAppend(pCol4, i, buf, false);
82 83 84 85
  }
}

static int32_t execDescribe(SNode* pStmt, SRetrieveTableRsp** pRsp) {
86 87
  SDescribeStmt* pDesc = (SDescribeStmt*)pStmt;
  int32_t        numOfRows = TABLE_TOTAL_COL_NUM(pDesc->pMeta);
88 89 90 91 92 93

  SSDataBlock* pBlock = buildDescResultDataBlock();
  setDescResultIntoDataBlock(pBlock, numOfRows, pDesc->pMeta);

  size_t rspSize = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock);
  *pRsp = taosMemoryCalloc(1, rspSize);
94 95 96
  if (NULL == *pRsp) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
97

98 99 100 101 102
  (*pRsp)->useconds = 0;
  (*pRsp)->completed = 1;
  (*pRsp)->precision = 0;
  (*pRsp)->compressed = 0;
  (*pRsp)->compLen = 0;
103 104 105 106
  (*pRsp)->numOfRows = htonl(numOfRows);
  (*pRsp)->numOfCols = htonl(DESCRIBE_RESULT_COLS);

  int32_t len = 0;
107
  blockEncode(pBlock, (*pRsp)->data, &len, DESCRIBE_RESULT_COLS, false);
108 109 110
  ASSERT(len == rspSize - sizeof(SRetrieveTableRsp));

  blockDataDestroy(pBlock);
111 112 113
  return TSDB_CODE_SUCCESS;
}

114 115
static int32_t execResetQueryCache() { return catalogClearCache(); }

D
dapan1121 已提交
116
static SSDataBlock* buildCreateDBResultDataBlock() {
117
  SSDataBlock*    pBlock = createDataBlock();
H
Haojun Liao 已提交
118 119
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_COLS, 1);
  blockDataAppendColInfo(pBlock, &infoData);
D
dapan1121 已提交
120

H
Haojun Liao 已提交
121 122
  infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_FIELD2_LEN, 2);
  blockDataAppendColInfo(pBlock, &infoData);
D
dapan1121 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  return pBlock;
}

int64_t getValOfDiffPrecision(int8_t unit, int64_t val) {
  int64_t v = 0;
  switch (unit) {
    case 's':
      v = val / 1000;
      break;
    case 'm':
      v = val / tsTickPerMin[TSDB_TIME_PRECISION_MILLI];
      break;
    case 'h':
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 60);
      break;
    case 'd':
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60);
      break;
    case 'w':
      v = val / (tsTickPerMin[TSDB_TIME_PRECISION_MILLI] * 24 * 60 * 7);
      break;
    default:
      break;
  }

  return v;
}

151
char* buildRetension(SArray* pRetension) {
D
dapan1121 已提交
152 153 154 155 156
  size_t size = taosArrayGetSize(pRetension);
  if (size == 0) {
    return NULL;
  }

157 158
  char*       p1 = taosMemoryCalloc(1, 100);
  SRetention* p = taosArrayGet(pRetension, 0);
D
dapan1121 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

  int32_t len = 0;

  int64_t v1 = getValOfDiffPrecision(p->freqUnit, p->freq);
  int64_t v2 = getValOfDiffPrecision(p->keepUnit, p->keep);
  len += sprintf(p1 + len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit);

  if (size > 1) {
    len += sprintf(p1 + len, ",");
    p = taosArrayGet(pRetension, 1);

    v1 = getValOfDiffPrecision(p->freqUnit, p->freq);
    v2 = getValOfDiffPrecision(p->keepUnit, p->keep);
    len += sprintf(p1 + len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit);
  }

  if (size > 2) {
    len += sprintf(p1 + len, ",");
    p = taosArrayGet(pRetension, 2);

    v1 = getValOfDiffPrecision(p->freqUnit, p->freq);
    v2 = getValOfDiffPrecision(p->keepUnit, p->keep);
    len += sprintf(p1 + len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit);
  }

  return p1;
}

187
static void setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbFName, SDbCfgInfo* pCfg) {
D
dapan1121 已提交
188 189 190 191 192 193 194 195 196 197 198
  blockDataEnsureCapacity(pBlock, 1);
  pBlock->info.rows = 1;

  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
  char             buf1[SHOW_CREATE_DB_RESULT_FIELD1_LEN] = {0};
  STR_TO_VARSTR(buf1, dbFName);
  colDataAppend(pCol1, 0, buf1, false);

  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
  int32_t          len = 0;
199
  char*            prec = NULL;
D
dapan1121 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  switch (pCfg->precision) {
    case TSDB_TIME_PRECISION_MILLI:
      prec = TSDB_TIME_PRECISION_MILLI_STR;
      break;
    case TSDB_TIME_PRECISION_MICRO:
      prec = TSDB_TIME_PRECISION_MICRO_STR;
      break;
    case TSDB_TIME_PRECISION_NANO:
      prec = TSDB_TIME_PRECISION_NANO_STR;
      break;
    default:
      prec = "none";
      break;
  }

215 216 217 218 219 220 221 222 223 224
  char* retentions = buildRetension(pCfg->pRetensions);

  len += sprintf(buf2 + VARSTR_HEADER_SIZE,
                 "CREATE DATABASE `%s` BUFFER %d CACHELAST %d COMP %d DURATION %dm "
                 "FSYNC %d MAXROWS %d MINROWS %d KEEP %dm,%dm,%dm PAGES %d PAGESIZE %d PRECISION '%s' REPLICA %d "
                 "STRICT %d WAL %d VGROUPS %d SINGLE_STABLE %d",
                 dbFName, pCfg->buffer, pCfg->cacheLastRow, pCfg->compression, pCfg->daysPerFile, pCfg->fsyncPeriod,
                 pCfg->maxRows, pCfg->minRows, pCfg->daysToKeep0, pCfg->daysToKeep1, pCfg->daysToKeep2, pCfg->pages,
                 pCfg->pageSize, prec, pCfg->replications, pCfg->strict, pCfg->walLevel, pCfg->numOfVgroups,
                 1 == pCfg->numOfStables);
D
dapan1121 已提交
225 226 227 228 229 230 231

  if (retentions) {
    len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, " RETENTIONS %s", retentions);
    taosMemoryFree(retentions);
  }

  (varDataLen(buf2)) = len;
232

D
dapan1121 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  colDataAppend(pCol2, 0, buf2, false);
}

static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt, SRetrieveTableRsp** pRsp) {
  SSDataBlock* pBlock = buildCreateDBResultDataBlock();
  setCreateDBResultIntoDataBlock(pBlock, pStmt->dbName, pStmt->pCfg);

  size_t rspSize = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock);
  *pRsp = taosMemoryCalloc(1, rspSize);
  if (NULL == *pRsp) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  (*pRsp)->useconds = 0;
  (*pRsp)->completed = 1;
  (*pRsp)->precision = 0;
  (*pRsp)->compressed = 0;
  (*pRsp)->compLen = 0;
  (*pRsp)->numOfRows = htonl(1);
  (*pRsp)->numOfCols = htonl(SHOW_CREATE_DB_RESULT_COLS);

  int32_t len = 0;
255
  blockEncode(pBlock, (*pRsp)->data, &len, SHOW_CREATE_DB_RESULT_COLS, false);
D
dapan1121 已提交
256 257 258 259 260
  ASSERT(len == rspSize - sizeof(SRetrieveTableRsp));

  blockDataDestroy(pBlock);
  return TSDB_CODE_SUCCESS;
}
261

D
dapan1121 已提交
262
static SSDataBlock* buildCreateTbResultDataBlock() {
H
Haojun Liao 已提交
263
  SSDataBlock* pBlock = createDataBlock();
D
dapan1121 已提交
264

H
Haojun Liao 已提交
265 266
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD1_LEN, 1);
  blockDataAppendColInfo(pBlock, &infoData);
D
dapan1121 已提交
267

H
Haojun Liao 已提交
268 269
  infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD2_LEN, 2);
  blockDataAppendColInfo(pBlock, &infoData);
D
dapan1121 已提交
270 271 272 273 274 275 276

  return pBlock;
}

void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
    SSchema* pSchema = pCfg->pSchemas + i;
277
    char     type[32];
D
dapan1121 已提交
278 279 280 281
    sprintf(type, "%s", tDataTypes[pSchema->type].name);
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type) {
      sprintf(type + strlen(type), "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
282
      sprintf(type + strlen(type), "(%d)", (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
D
dapan1121 已提交
283
    }
284

D
dapan1121 已提交
285 286 287 288 289 290 291
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s`%s` %s", ((i > 0) ? ", " : ""), pSchema->name, type);
  }
}

void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
292
    char     type[32];
D
dapan1121 已提交
293 294 295 296
    sprintf(type, "%s", tDataTypes[pSchema->type].name);
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type) {
      sprintf(type + strlen(type), "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
297
      sprintf(type + strlen(type), "(%d)", (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
D
dapan1121 已提交
298 299 300 301 302 303
    }

    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s`%s` %s", ((i > 0) ? ", " : ""), pSchema->name, type);
  }
}

D
dapan1121 已提交
304 305 306 307 308 309 310
void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) {
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s`%s`", ((i > 0) ? ", " : ""), pSchema->name);
  }
}

D
dapan1121 已提交
311
int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) {
312 313 314
  SArray* pTagVals = NULL;
  STag*   pTag = (STag*)pCfg->pTags;

D
dapan1121 已提交
315
  if (pCfg->pTags && pTag->flags & TD_TAG_JSON) {
316
    char* pJson = parseTagDatatoJson(pTag);
D
dapan1121 已提交
317 318
    if (pJson) {
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s", pJson);
D
dapan1121 已提交
319
      taosMemoryFree(pJson);
D
dapan1121 已提交
320 321 322 323
    }

    return TSDB_CODE_SUCCESS;
  }
324 325

  int32_t code = tTagToValArray((const STag*)pCfg->pTags, &pTagVals);
D
dapan1121 已提交
326 327 328 329
  if (code) {
    return code;
  }

D
dapan1121 已提交
330
  int16_t valueNum = taosArrayGetSize(pTagVals);
D
dapan1121 已提交
331
  int32_t num = 0;
D
dapan1121 已提交
332 333 334 335 336 337
  int32_t j = 0;
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
    if (i > 0) {
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", ");
    }
338

D
dapan1121 已提交
339 340 341 342
    if (j >= valueNum) {
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "NULL");
      continue;
    }
343 344

    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
D
dapan1121 已提交
345 346 347 348 349
    if (pSchema->colId > pTagVal->cid) {
      qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid);
      taosArrayDestroy(pTagVals);
      return TSDB_CODE_APP_ERROR;
    } else if (pSchema->colId == pTagVal->cid) {
350 351
      char    type = pTagVal->type;
      int32_t tlen = 0;
D
dapan1121 已提交
352 353 354 355 356 357 358 359 360 361 362

      if (IS_VAR_DATA_TYPE(type)) {
        dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, type, pTagVal->pData, pTagVal->nData, &tlen);
      } else {
        dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, type, &pTagVal->i64, tDataTypes[type].bytes, &tlen);
      }
      *len += tlen;
      j++;
    } else {
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "NULL");
    }
D
dapan1121 已提交
363 364 365 366 367 368 369

    /*
    if (type == TSDB_DATA_TYPE_BINARY) {
      if (pTagVal->nData > 0) {
        if (num) {
          *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", ");
        }
370

D
dapan1121 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
        memcpy(buf + VARSTR_HEADER_SIZE + *len, pTagVal->pData, pTagVal->nData);
        *len += pTagVal->nData;
      }
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
      if (pTagVal->nData > 0) {
        if (num) {
          *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", ");
        }
        int32_t tlen = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, buf + VARSTR_HEADER_SIZE + *len);
      }
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
      double val = *(double *)(&pTagVal->i64);
      int    len = 0;
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
    } else if (type == TSDB_DATA_TYPE_BOOL) {
      int val = *(int *)(&pTagVal->i64);
      int len = 0;
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_INT, key, nKey, (const char *)&val, len);
    }
    */
  }

D
dapan1121 已提交
393 394
  taosArrayDestroy(pTagVals);

395
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
}

void appendTableOptions(char* buf, int32_t* len, STableCfg* pCfg) {
  if (pCfg->commentLen > 0) {
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " COMMENT '%s'", pCfg->pComment);
  } else if (0 == pCfg->commentLen) {
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " COMMENT ''");
  }

  if (pCfg->watermark1 > 0) {
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " WATERMARK %" PRId64 "a", pCfg->watermark1);
    if (pCfg->watermark2 > 0) {
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", %" PRId64 "a", pCfg->watermark2);
    }
  }

  if (pCfg->delay1 > 0) {
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " MAX_DELAY %" PRId64 "a", pCfg->delay1);
    if (pCfg->delay2 > 0) {
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", %" PRId64 "a", pCfg->delay2);
    }
  }

  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
  if (funcNum > 0) {
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " ROLLUP(");
    for (int32_t i = 0; i < funcNum; ++i) {
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
424
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s%s", ((i > 0) ? ", " : ""), pFunc);
D
dapan1121 已提交
425 426 427
    }
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ")");
  }
D
dapan1121 已提交
428 429 430 431

  if (pCfg->ttl > 0) {
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " TTL %d", pCfg->ttl);
  }
D
dapan1121 已提交
432 433
}

434
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, char* tbName, STableCfg* pCfg) {
D
dapan1121 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
  int32_t code = 0;
  blockDataEnsureCapacity(pBlock, 1);
  pBlock->info.rows = 1;

  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
  char             buf1[SHOW_CREATE_TB_RESULT_FIELD1_LEN] = {0};
  STR_TO_VARSTR(buf1, tbName);
  colDataAppend(pCol1, 0, buf1, false);

  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
  char             buf2[SHOW_CREATE_TB_RESULT_FIELD2_LEN] = {0};
  int32_t          len = 0;

  if (TSDB_SUPER_TABLE == pCfg->tableType) {
    len += sprintf(buf2 + VARSTR_HEADER_SIZE, "CREATE STABLE `%s` (", tbName);
    appendColumnFields(buf2, &len, pCfg);
    len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ") TAGS (");
452
    appendTagFields(buf2, &len, pCfg);
D
dapan1121 已提交
453
    len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ")");
D
dapan1121 已提交
454
    appendTableOptions(buf2, &len, pCfg);
D
dapan1121 已提交
455
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
D
dapan1121 已提交
456 457 458
    len += sprintf(buf2 + VARSTR_HEADER_SIZE, "CREATE TABLE `%s` USING `%s` (", tbName, pCfg->stbName);
    appendTagNameFields(buf2, &len, pCfg);
    len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ") TAGS (");
D
dapan1121 已提交
459 460 461 462 463
    code = appendTagValues(buf2, &len, pCfg);
    if (code) {
      return code;
    }
    len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ")");
D
dapan1121 已提交
464
    appendTableOptions(buf2, &len, pCfg);
D
dapan1121 已提交
465 466 467 468 469 470
  } else {
    len += sprintf(buf2 + VARSTR_HEADER_SIZE, "CREATE TABLE `%s` (", tbName);
    appendColumnFields(buf2, &len, pCfg);
    len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ")");
  }

D
dapan1121 已提交
471
  varDataLen(buf2) = len;
472

D
dapan1121 已提交
473 474 475 476 477 478 479
  colDataAppend(pCol2, 0, buf2, false);

  return TSDB_CODE_SUCCESS;
}

static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp) {
  SSDataBlock* pBlock = buildCreateTbResultDataBlock();
480
  int32_t      code = setCreateTBResultIntoDataBlock(pBlock, pStmt->tableName, pStmt->pCfg);
D
dapan1121 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  if (code) {
    return code;
  }

  size_t rspSize = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock);
  *pRsp = taosMemoryCalloc(1, rspSize);
  if (NULL == *pRsp) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  (*pRsp)->useconds = 0;
  (*pRsp)->completed = 1;
  (*pRsp)->precision = 0;
  (*pRsp)->compressed = 0;
  (*pRsp)->compLen = 0;
  (*pRsp)->numOfRows = htonl(1);
  (*pRsp)->numOfCols = htonl(SHOW_CREATE_TB_RESULT_COLS);
498

D
dapan1121 已提交
499
  int32_t len = 0;
500
  blockEncode(pBlock, (*pRsp)->data, &len, SHOW_CREATE_TB_RESULT_COLS, false);
D
dapan1121 已提交
501 502 503 504 505 506 507 508 509 510 511 512
  ASSERT(len == rspSize - sizeof(SRetrieveTableRsp));

  blockDataDestroy(pBlock);
  return TSDB_CODE_SUCCESS;
}

static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp) {
  STableCfg* pCfg = (STableCfg*)pStmt->pCfg;
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
    return terrno;
  }
513

D
dapan1121 已提交
514 515
  return execShowCreateTable(pStmt, pRsp);
}
516

517
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
D
dapan1121 已提交
518
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CMD)) {
519
    return terrno;
D
dapan1121 已提交
520 521
  }

D
dapan1121 已提交
522
  if (taosSetCfg(tsCfg, pStmt->config)) {
523
    return terrno;
D
dapan1121 已提交
524 525
  }

D
dapan1121 已提交
526 527
  return TSDB_CODE_SUCCESS;
}
528

D
dapan1121 已提交
529 530 531 532
static SSDataBlock* buildLocalVariablesResultDataBlock() {
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
  pBlock->info.hasVarCol = true;

S
shenglian zhou 已提交
533
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
D
dapan1121 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553

  SColumnInfoData infoData = {0};
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN;

  taosArrayPush(pBlock->pDataBlock, &infoData);

  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
  infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN;
  taosArrayPush(pBlock->pDataBlock, &infoData);

  return pBlock;
}

int32_t setLocalVariablesResultIntoDataBlock(SSDataBlock* pBlock) {
  int32_t numOfCfg = taosArrayGetSize(tsCfg->array);
  int32_t numOfRows = 0;
  blockDataEnsureCapacity(pBlock, numOfCfg);

  for (int32_t i = 0, c = 0; i < numOfCfg; ++i, c = 0) {
554
    SConfigItem* pItem = taosArrayGet(tsCfg->array, i);
D
dapan1121 已提交
555 556 557

    char name[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(name, pItem->name, TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE);
558
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, c++);
D
dapan1121 已提交
559
    colDataAppend(pColInfo, i, name, false);
560 561

    char    value[TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
D
dapan1121 已提交
562 563 564 565 566 567 568 569 570 571
    int32_t valueLen = 0;
    cfgDumpItemValue(pItem, &value[VARSTR_HEADER_SIZE], TSDB_CONFIG_VALUE_LEN, &valueLen);
    varDataSetLen(value, valueLen);
    pColInfo = taosArrayGet(pBlock->pDataBlock, c++);
    colDataAppend(pColInfo, i, value, false);

    numOfRows++;
  }

  pBlock->info.rows = numOfRows;
572

D
dapan1121 已提交
573 574 575 576 577
  return TSDB_CODE_SUCCESS;
}

static int32_t execShowLocalVariables(SRetrieveTableRsp** pRsp) {
  SSDataBlock* pBlock = buildLocalVariablesResultDataBlock();
578
  int32_t      code = setLocalVariablesResultIntoDataBlock(pBlock);
D
dapan1121 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
  if (code) {
    return code;
  }

  size_t rspSize = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock);
  *pRsp = taosMemoryCalloc(1, rspSize);
  if (NULL == *pRsp) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  (*pRsp)->useconds = 0;
  (*pRsp)->completed = 1;
  (*pRsp)->precision = 0;
  (*pRsp)->compressed = 0;
  (*pRsp)->compLen = 0;
  (*pRsp)->numOfRows = htonl(pBlock->info.rows);
  (*pRsp)->numOfCols = htonl(SHOW_LOCAL_VARIABLES_RESULT_COLS);

  int32_t len = 0;
598
  blockEncode(pBlock, (*pRsp)->data, &len, SHOW_LOCAL_VARIABLES_RESULT_COLS, false);
D
dapan1121 已提交
599 600 601 602 603
  ASSERT(len == rspSize - sizeof(SRetrieveTableRsp));

  blockDataDestroy(pBlock);
  return TSDB_CODE_SUCCESS;
}
604

605 606 607 608 609 610
int32_t qExecCommand(SNode* pStmt, SRetrieveTableRsp** pRsp) {
  switch (nodeType(pStmt)) {
    case QUERY_NODE_DESCRIBE_STMT:
      return execDescribe(pStmt, pRsp);
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
      return execResetQueryCache();
611
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
D
dapan1121 已提交
612
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
613
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
D
dapan1121 已提交
614
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp);
615
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
D
dapan1121 已提交
616
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp);
617 618
    case QUERY_NODE_ALTER_LOCAL_STMT:
      return execAlterLocal((SAlterLocalStmt*)pStmt);
619
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
D
dapan1121 已提交
620
      return execShowLocalVariables(pRsp);
621 622 623 624 625
    default:
      break;
  }
  return TSDB_CODE_FAILED;
}