command.c 30.6 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"
D
dapan1121 已提交
18 19
#include "commandInt.h"
#include "scheduler.h"
20
#include "systable.h"
21 22
#include "tdatablock.h"
#include "tglobal.h"
wafwerar's avatar
wafwerar 已提交
23
#include "tgrant.h"
24
#include "taosdef.h"
25

26
extern SConfig* tsCfg;
27 28 29 30 31 32 33 34 35 36 37 38 39

static int32_t buildRetrieveTableRsp(SSDataBlock* pBlock, int32_t numOfCols, SRetrieveTableRsp** pRsp) {
  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;
40
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
41 42
  (*pRsp)->numOfCols = htonl(numOfCols);

H
Haojun Liao 已提交
43
  int32_t len = blockEncode(pBlock, (*pRsp)->data, numOfCols);
44 45 46 47

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
48 49 50
static int32_t getSchemaBytes(const SSchema* pSchema) {
  switch (pSchema->type) {
    case TSDB_DATA_TYPE_BINARY:
D
Dingle Zhang 已提交
51
    case TSDB_DATA_TYPE_GEOMETRY:
X
Xiaoyu Wang 已提交
52 53
      return (pSchema->bytes - VARSTR_HEADER_SIZE);
    case TSDB_DATA_TYPE_NCHAR:
wmmhello's avatar
wmmhello 已提交
54
    case TSDB_DATA_TYPE_JSON:
X
Xiaoyu Wang 已提交
55 56 57 58 59
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
    default:
      return pSchema->bytes;
  }
}
60

X
Xiaoyu Wang 已提交
61
static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) {
62
  SSDataBlock* pBlock = createDataBlock();
X
Xiaoyu Wang 已提交
63 64 65
  if (NULL == pBlock) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
66

67
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_FIELD_LEN, 1);
X
Xiaoyu Wang 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80
  int32_t         code = blockDataAppendColInfo(pBlock, &infoData);
  if (TSDB_CODE_SUCCESS == code) {
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_TYPE_LEN, 2);
    code = blockDataAppendColInfo(pBlock, &infoData);
  }
  if (TSDB_CODE_SUCCESS == code) {
    infoData = createColumnInfoData(TSDB_DATA_TYPE_INT, tDataTypes[TSDB_DATA_TYPE_INT].bytes, 3);
    code = blockDataAppendColInfo(pBlock, &infoData);
  }
  if (TSDB_CODE_SUCCESS == code) {
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, DESCRIBE_RESULT_NOTE_LEN, 4);
    code = blockDataAppendColInfo(pBlock, &infoData);
  }
81

X
Xiaoyu Wang 已提交
82 83 84 85 86 87
  if (TSDB_CODE_SUCCESS == code) {
    *pOutput = pBlock;
  } else {
    blockDataDestroy(pBlock);
  }
  return code;
88 89
}

90
static int32_t setDescResultIntoDataBlock(bool sysInfoUser, SSDataBlock* pBlock, int32_t numOfRows, STableMeta* pMeta) {
91
  blockDataEnsureCapacity(pBlock, numOfRows);
92
  pBlock->info.rows = 0;
93 94 95

  // field
  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
96
  // Type
97
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
98
  // Length
99
  SColumnInfoData* pCol3 = taosArrayGet(pBlock->pDataBlock, 2);
100
  // Note
101
  SColumnInfoData* pCol4 = taosArrayGet(pBlock->pDataBlock, 3);
102
  char             buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
103
  for (int32_t i = 0; i < numOfRows; ++i) {
104 105 106 107
    if (invisibleColumn(sysInfoUser, pMeta->tableType, pMeta->schema[i].flags)) {
      continue;
    }
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
108
    colDataSetVal(pCol1, pBlock->info.rows, buf, false);
109
    STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
110
    colDataSetVal(pCol2, pBlock->info.rows, buf, false);
111
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
112
    colDataSetVal(pCol3, pBlock->info.rows, (const char*)&bytes, false);
113
    STR_TO_VARSTR(buf, i >= pMeta->tableInfo.numOfColumns ? "TAG" : "");
114
    colDataSetVal(pCol4, pBlock->info.rows, buf, false);
115
    ++(pBlock->info.rows);
116
  }
117 118 119 120 121
  if (pBlock->info.rows <= 0) {
    qError("no permission to view any columns");
    return TSDB_CODE_PAR_PERMISSION_DENIED;
  }
  return TSDB_CODE_SUCCESS;
122 123
}

124
static int32_t execDescribe(bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp) {
125 126
  SDescribeStmt* pDesc = (SDescribeStmt*)pStmt;
  int32_t        numOfRows = TABLE_TOTAL_COL_NUM(pDesc->pMeta);
127

X
Xiaoyu Wang 已提交
128 129 130
  SSDataBlock* pBlock = NULL;
  int32_t      code = buildDescResultDataBlock(&pBlock);
  if (TSDB_CODE_SUCCESS == code) {
131
    code = setDescResultIntoDataBlock(sysInfoUser, pBlock, numOfRows, pDesc->pMeta);
X
Xiaoyu Wang 已提交
132 133 134 135 136 137
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = buildRetrieveTableRsp(pBlock, DESCRIBE_RESULT_COLS, pRsp);
  }
  blockDataDestroy(pBlock);
  return code;
138 139
}

140 141
static int32_t execResetQueryCache() { return catalogClearCache(); }

X
Xiaoyu Wang 已提交
142 143 144 145 146 147
static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) {
  SSDataBlock* pBlock = createDataBlock();
  if (NULL == pBlock) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

H
Haojun Liao 已提交
148
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_COLS, 1);
X
Xiaoyu Wang 已提交
149 150 151 152 153
  int32_t         code = blockDataAppendColInfo(pBlock, &infoData);
  if (TSDB_CODE_SUCCESS == code) {
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_DB_RESULT_FIELD2_LEN, 2);
    code = blockDataAppendColInfo(pBlock, &infoData);
  }
D
dapan1121 已提交
154

X
Xiaoyu Wang 已提交
155 156 157 158 159 160
  if (TSDB_CODE_SUCCESS == code) {
    *pOutput = pBlock;
  } else {
    blockDataDestroy(pBlock);
  }
  return code;
D
dapan1121 已提交
161 162
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static int32_t buildAliveResultDataBlock(SSDataBlock** pOutput) {
  SSDataBlock* pBlock = createDataBlock();
  if (NULL == pBlock) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_INT, sizeof(int32_t), 1);
  int32_t         code = blockDataAppendColInfo(pBlock, &infoData);

  if (TSDB_CODE_SUCCESS == code) {
    *pOutput = pBlock;
  } else {
    blockDataDestroy(pBlock);
  }
  return code;
}

D
dapan1121 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
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;
}

205
char* buildRetension(SArray* pRetension) {
D
dapan1121 已提交
206 207 208 209 210
  size_t size = taosArrayGetSize(pRetension);
  if (size == 0) {
    return NULL;
  }

211 212
  char*       p1 = taosMemoryCalloc(1, 100);
  SRetention* p = taosArrayGet(pRetension, 0);
D
dapan1121 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

  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;
}

X
Xiaoyu Wang 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
static const char* cacheModelStr(int8_t cacheModel) {
  switch (cacheModel) {
    case TSDB_CACHE_MODEL_NONE:
      return TSDB_CACHE_MODEL_NONE_STR;
    case TSDB_CACHE_MODEL_LAST_ROW:
      return TSDB_CACHE_MODEL_LAST_ROW_STR;
    case TSDB_CACHE_MODEL_LAST_VALUE:
      return TSDB_CACHE_MODEL_LAST_VALUE_STR;
    case TSDB_CACHE_MODEL_BOTH:
      return TSDB_CACHE_MODEL_BOTH_STR;
    default:
      break;
  }
  return TSDB_CACHE_MODEL_NONE_STR;
}

257
static void setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, char* dbFName, SDbCfgInfo* pCfg) {
D
dapan1121 已提交
258 259 260 261 262
  blockDataEnsureCapacity(pBlock, 1);
  pBlock->info.rows = 1;

  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
  char             buf1[SHOW_CREATE_DB_RESULT_FIELD1_LEN] = {0};
263
  STR_TO_VARSTR(buf1, dbName);
264
  colDataSetVal(pCol1, 0, buf1, false);
D
dapan1121 已提交
265 266 267 268

  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
  char             buf2[SHOW_CREATE_DB_RESULT_FIELD2_LEN] = {0};
  int32_t          len = 0;
269
  char*            prec = NULL;
D
dapan1121 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  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;
  }

285
  char* retentions = buildRetension(pCfg->pRetensions);
286
  int32_t dbFNameLen = strlen(dbFName);
287 288 289 290 291 292
  int32_t hashPrefix = 0;
  if (pCfg->hashPrefix > 0) {
    hashPrefix = pCfg->hashPrefix - dbFNameLen - 1;
  } else if (pCfg->hashPrefix < 0) {
    hashPrefix = pCfg->hashPrefix + dbFNameLen + 1;
  }
293

G
Ganlin Zhao 已提交
294
  if (IS_SYS_DBNAME(dbName)) {
G
Ganlin Zhao 已提交
295
    len += sprintf(buf2 + VARSTR_HEADER_SIZE, "CREATE DATABASE `%s`", dbName);
G
Ganlin Zhao 已提交
296 297 298 299 300 301
  } else {
    len += sprintf(
        buf2 + VARSTR_HEADER_SIZE,
        "CREATE DATABASE `%s` BUFFER %d CACHESIZE %d CACHEMODEL '%s' COMP %d DURATION %dm "
        "WAL_FSYNC_PERIOD %d MAXROWS %d MINROWS %d STT_TRIGGER %d KEEP %dm,%dm,%dm PAGES %d PAGESIZE %d PRECISION '%s' REPLICA %d "
        "WAL_LEVEL %d VGROUPS %d SINGLE_STABLE %d TABLE_PREFIX %d TABLE_SUFFIX %d TSDB_PAGESIZE %d "
302
        "WAL_RETENTION_PERIOD %d WAL_RETENTION_SIZE %" PRId64,
G
Ganlin Zhao 已提交
303 304 305
        dbName, pCfg->buffer, pCfg->cacheSize, cacheModelStr(pCfg->cacheLast), pCfg->compression, pCfg->daysPerFile,
        pCfg->walFsyncPeriod, pCfg->maxRows, pCfg->minRows,  pCfg->sstTrigger, pCfg->daysToKeep0, pCfg->daysToKeep1, pCfg->daysToKeep2,
        pCfg->pages, pCfg->pageSize, prec, pCfg->replications, pCfg->walLevel, pCfg->numOfVgroups,
306
        1 == pCfg->numOfStables, hashPrefix, pCfg->hashSuffix, pCfg->tsdbPageSize, pCfg->walRetentionPeriod, pCfg->walRetentionSize);
G
Ganlin Zhao 已提交
307 308 309 310 311

    if (retentions) {
      len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, " RETENTIONS %s", retentions);
      taosMemoryFree(retentions);
    }
D
dapan1121 已提交
312 313 314
  }

  (varDataLen(buf2)) = len;
315

316
  colDataSetVal(pCol2, 0, buf2, false);
D
dapan1121 已提交
317 318
}

319 320 321 322 323
#define CHECK_LEADER(n) (row[n] && (fields[n].type == TSDB_DATA_TYPE_VARCHAR && strncasecmp(row[n], "leader", varDataLen((char *)row[n] - VARSTR_HEADER_SIZE)) == 0))
// on this row, if have leader return true else return false
bool existLeaderRole(TAOS_ROW row, TAOS_FIELD* fields, int nFields) {
  // vgroup_id | db_name | tables | v1_dnode | v1_status | v2_dnode | v2_status | v3_dnode | v3_status | v4_dnode |
  // v4_status |  cacheload  | tsma |
D
dapan1121 已提交
324
  if (nFields != 14) {
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    return false;
  }

  // check have leader on cloumn v*_status on 4 6 8 10
  if (CHECK_LEADER(4) || CHECK_LEADER(6) || CHECK_LEADER(8) || CHECK_LEADER(10)) {
    return true;
  }

  return false;
}

// get db alive status, return 1 is alive else return 0
int32_t getAliveStatusFromApi(int64_t* pConnId, char* dbName, int32_t* pStatus) {
  char    sql[128 + TSDB_DB_NAME_LEN] = "select * from information_schema.ins_vgroups";
  int32_t code;

  // filter with db name
  if (dbName && dbName[0] != 0) {
    char str[64 + TSDB_DB_NAME_LEN] = "";
    // test db name exist
    sprintf(str, "show create database %s ;", dbName);
    TAOS_RES* dbRes = taos_query(pConnId, str);
    code = taos_errno(dbRes);
    if (code != TSDB_CODE_SUCCESS) {
      taos_free_result(dbRes);
      return code;
    }
    taos_free_result(dbRes);

    sprintf(str, " where db_name='%s' ;", dbName);
    strcat(sql, str);
  }

  TAOS_RES* res = taos_query(pConnId, sql);
  code = taos_errno(res);
  if (code != TSDB_CODE_SUCCESS) {
    taos_free_result(res);
    return code;
  }

  TAOS_ROW    row = NULL;
  TAOS_FIELD* fields = taos_fetch_fields(res);
  int32_t     nFields = taos_num_fields(res);
  int32_t     nAvailble = 0;
  int32_t     nUnAvailble = 0;

  while ((row = taos_fetch_row(res)) != NULL) {
    if (existLeaderRole(row, fields, nFields)) {
      nAvailble++;
    } else {
      nUnAvailble++;
    }
  }
  taos_free_result(res);

  int32_t status = 0;
  if (nAvailble + nUnAvailble == 0 || nUnAvailble == 0) {
    status = SHOW_STATUS_AVAILABLE;
  } else if (nAvailble > 0 && nUnAvailble > 0) {
    status = SHOW_STATUS_HALF_AVAILABLE;
  } else {
    status = SHOW_STATUS_NOT_AVAILABLE;
  }

  if (pStatus) {
    *pStatus = status;
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t setAliveResultIntoDataBlock(int64_t* pConnId, SSDataBlock* pBlock, char* dbName) {
  blockDataEnsureCapacity(pBlock, 1);
  pBlock->info.rows = 1;

  SColumnInfoData* pCol1 = taosArrayGet(pBlock->pDataBlock, 0);
  int32_t          status = 0;
  int32_t          code = getAliveStatusFromApi(pConnId, dbName, &status);
  if (code == TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
403
    colDataSetVal(pCol1, 0, (const char*)&status, false);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
  }
  return code;
}

static int32_t execShowAliveStatus(int64_t* pConnId, SShowAliveStmt* pStmt, SRetrieveTableRsp** pRsp) {
  SSDataBlock* pBlock = NULL;
  int32_t      code = buildAliveResultDataBlock(&pBlock);
  if (TSDB_CODE_SUCCESS == code) {
    code = setAliveResultIntoDataBlock(pConnId, pBlock, pStmt->dbName);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = buildRetrieveTableRsp(pBlock, SHOW_ALIVE_RESULT_COLS, pRsp);
  }
  blockDataDestroy(pBlock);
  return code;
}

D
dapan1121 已提交
421
static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt, SRetrieveTableRsp** pRsp) {
X
Xiaoyu Wang 已提交
422 423 424
  SSDataBlock* pBlock = NULL;
  int32_t      code = buildCreateDBResultDataBlock(&pBlock);
  if (TSDB_CODE_SUCCESS == code) {
425
    setCreateDBResultIntoDataBlock(pBlock, pStmt->dbName, pStmt->dbFName, pStmt->pCfg);
X
Xiaoyu Wang 已提交
426 427 428 429 430 431
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_DB_RESULT_COLS, pRsp);
  }
  blockDataDestroy(pBlock);
  return code;
D
dapan1121 已提交
432
}
433

X
Xiaoyu Wang 已提交
434
static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) {
H
Haojun Liao 已提交
435
  SSDataBlock* pBlock = createDataBlock();
X
Xiaoyu Wang 已提交
436 437 438
  if (NULL == pBlock) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
D
dapan1121 已提交
439

H
Haojun Liao 已提交
440
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD1_LEN, 1);
X
Xiaoyu Wang 已提交
441 442 443 444 445
  int32_t         code = blockDataAppendColInfo(pBlock, &infoData);
  if (TSDB_CODE_SUCCESS == code) {
    infoData = createColumnInfoData(TSDB_DATA_TYPE_VARCHAR, SHOW_CREATE_TB_RESULT_FIELD2_LEN, 2);
    code = blockDataAppendColInfo(pBlock, &infoData);
  }
D
dapan1121 已提交
446

X
Xiaoyu Wang 已提交
447 448 449 450 451 452
  if (TSDB_CODE_SUCCESS == code) {
    *pOutput = pBlock;
  } else {
    blockDataDestroy(pBlock);
  }
  return code;
D
dapan1121 已提交
453 454 455 456 457
}

void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
  for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
    SSchema* pSchema = pCfg->pSchemas + i;
458
    char     type[32];
D
dapan1121 已提交
459
    sprintf(type, "%s", tDataTypes[pSchema->type].name);
D
Dingle Zhang 已提交
460
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
D
dapan1121 已提交
461 462
      sprintf(type + strlen(type), "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
463
      sprintf(type + strlen(type), "(%d)", (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
D
dapan1121 已提交
464
    }
465

D
dapan1121 已提交
466
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s`%s` %s", ((i > 0) ? ", " : ""), pSchema->name, type);
D
dapan1121 已提交
467 468 469 470 471 472
  }
}

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;
473
    char     type[32];
D
dapan1121 已提交
474
    sprintf(type, "%s", tDataTypes[pSchema->type].name);
D
Dingle Zhang 已提交
475
    if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
D
dapan1121 已提交
476 477
      sprintf(type + strlen(type), "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
    } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
478
      sprintf(type + strlen(type), "(%d)", (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
D
dapan1121 已提交
479 480
    }

D
dapan1121 已提交
481
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s`%s` %s", ((i > 0) ? ", " : ""), pSchema->name, type);
D
dapan1121 已提交
482 483 484
  }
}

D
dapan1121 已提交
485 486 487
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;
D
dapan1121 已提交
488
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s`%s`", ((i > 0) ? ", " : ""), pSchema->name);
D
dapan1121 已提交
489 490 491
  }
}

D
dapan1121 已提交
492
int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) {
493 494 495
  SArray* pTagVals = NULL;
  STag*   pTag = (STag*)pCfg->pTags;

D
dapan1121 已提交
496 497 498 499 500 501
  if (NULL == pCfg->pTags || pCfg->numOfTags <= 0) {
    qError("tag missed in table cfg, pointer:%p, numOfTags:%d", pCfg->pTags, pCfg->numOfTags);
    return TSDB_CODE_APP_ERROR;
  }

  if (tTagIsJson(pTag)) {
502
    char* pJson = parseTagDatatoJson(pTag);
D
dapan1121 已提交
503
    if (pJson) {
D
dapan1121 已提交
504
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s", pJson);
D
dapan1121 已提交
505
      taosMemoryFree(pJson);
D
dapan1121 已提交
506 507 508 509
    }

    return TSDB_CODE_SUCCESS;
  }
510 511

  int32_t code = tTagToValArray((const STag*)pCfg->pTags, &pTagVals);
D
dapan1121 已提交
512 513 514 515
  if (code) {
    return code;
  }

D
dapan1121 已提交
516
  int16_t valueNum = taosArrayGetSize(pTagVals);
D
dapan1121 已提交
517
  int32_t num = 0;
D
dapan1121 已提交
518 519 520 521
  int32_t j = 0;
  for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
    SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
    if (i > 0) {
D
dapan1121 已提交
522
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", ");
D
dapan1121 已提交
523
    }
524

D
dapan1121 已提交
525
    if (j >= valueNum) {
D
dapan1121 已提交
526
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "NULL");
D
dapan1121 已提交
527 528
      continue;
    }
529 530

    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
D
dapan1121 已提交
531 532 533 534 535
    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) {
536 537
      char    type = pTagVal->type;
      int32_t tlen = 0;
D
dapan1121 已提交
538 539

      if (IS_VAR_DATA_TYPE(type)) {
D
dapan1121 已提交
540
        dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, type, pTagVal->pData, pTagVal->nData, &tlen);
D
dapan1121 已提交
541
      } else {
D
dapan1121 已提交
542
        dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, type, &pTagVal->i64, tDataTypes[type].bytes, &tlen);
D
dapan1121 已提交
543 544 545 546
      }
      *len += tlen;
      j++;
    } else {
D
dapan1121 已提交
547
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "NULL");
D
dapan1121 已提交
548
    }
D
dapan1121 已提交
549 550

    /*
D
Dingle Zhang 已提交
551
    if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_GEOMETRY) {
D
dapan1121 已提交
552 553 554 555
      if (pTagVal->nData > 0) {
        if (num) {
          *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", ");
        }
556

D
dapan1121 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
        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 已提交
579 580
  taosArrayDestroy(pTagVals);

581
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
582 583
}

X
Xiaoyu Wang 已提交
584
void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg* pCfg) {
D
dapan1121 已提交
585
  if (pCfg->commentLen > 0) {
D
dapan1121 已提交
586
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " COMMENT '%s'", pCfg->pComment);
D
dapan1121 已提交
587
  } else if (0 == pCfg->commentLen) {
D
dapan1121 已提交
588
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " COMMENT ''");
D
dapan1121 已提交
589 590
  }

X
Xiaoyu Wang 已提交
591
  if (NULL != pDbCfg->pRetensions && pCfg->watermark1 > 0) {
D
dapan1121 已提交
592
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " WATERMARK %" PRId64 "a", pCfg->watermark1);
D
dapan1121 已提交
593
    if (pCfg->watermark2 > 0) {
D
dapan1121 已提交
594
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", %" PRId64 "a", pCfg->watermark2);
D
dapan1121 已提交
595 596 597
    }
  }

X
Xiaoyu Wang 已提交
598
  if (NULL != pDbCfg->pRetensions && pCfg->delay1 > 0) {
D
dapan1121 已提交
599
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " MAX_DELAY %" PRId64 "a", pCfg->delay1);
D
dapan1121 已提交
600
    if (pCfg->delay2 > 0) {
D
dapan1121 已提交
601
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", %" PRId64 "a", pCfg->delay2);
D
dapan1121 已提交
602 603 604 605
    }
  }

  int32_t funcNum = taosArrayGetSize(pCfg->pFuncs);
X
Xiaoyu Wang 已提交
606
  if (NULL != pDbCfg->pRetensions && funcNum > 0) {
D
dapan1121 已提交
607
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " ROLLUP(");
D
dapan1121 已提交
608 609
    for (int32_t i = 0; i < funcNum; ++i) {
      char* pFunc = taosArrayGet(pCfg->pFuncs, i);
D
dapan1121 已提交
610
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s%s", ((i > 0) ? ", " : ""), pFunc);
D
dapan1121 已提交
611
    }
D
dapan1121 已提交
612
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ")");
D
dapan1121 已提交
613
  }
D
dapan1121 已提交
614 615

  if (pCfg->ttl > 0) {
D
dapan1121 已提交
616
    *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " TTL %d", pCfg->ttl);
D
dapan1121 已提交
617
  }
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641

  if (TSDB_SUPER_TABLE == pCfg->tableType || TSDB_NORMAL_TABLE == pCfg->tableType) {
    int32_t nSma = 0;
    for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
      if (IS_BSMA_ON(pCfg->pSchemas + i)) {
        ++nSma;
      }
    }

    if (nSma < pCfg->numOfColumns && nSma > 0) {
      bool smaOn = false;
      for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
        if (IS_BSMA_ON(pCfg->pSchemas + i)) {
          if (smaOn) {
            *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ",`%s`", (pCfg->pSchemas + i)->name);
          } else {
            smaOn = true;
            *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, " SMA(`%s`", (pCfg->pSchemas + i)->name);
          }
        }
      }
      *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ")");
    }
  }
D
dapan1121 已提交
642 643
}

X
Xiaoyu Wang 已提交
644
static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, SDbCfgInfo* pDbCfg, char* tbName, STableCfg* pCfg) {
D
dapan1121 已提交
645 646 647 648 649 650 651
  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);
652
  colDataSetVal(pCol1, 0, buf1, false);
D
dapan1121 已提交
653 654

  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
D
dapan1121 已提交
655 656
  char*            buf2 = taosMemoryMalloc(SHOW_CREATE_TB_RESULT_FIELD2_LEN);
  if (NULL == buf2) {
S
Shengliang Guan 已提交
657
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
658 659
    return terrno;
  }
X
Xiaoyu Wang 已提交
660 661

  int32_t len = 0;
D
dapan1121 已提交
662 663

  if (TSDB_SUPER_TABLE == pCfg->tableType) {
D
dapan1121 已提交
664
    len += sprintf(buf2 + VARSTR_HEADER_SIZE, "CREATE STABLE `%s` (", tbName);
D
dapan1121 已提交
665
    appendColumnFields(buf2, &len, pCfg);
D
dapan1121 已提交
666
    len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ") TAGS (");
667
    appendTagFields(buf2, &len, pCfg);
D
dapan1121 已提交
668
    len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ")");
X
Xiaoyu Wang 已提交
669
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
D
dapan1121 已提交
670
  } else if (TSDB_CHILD_TABLE == pCfg->tableType) {
D
dapan1121 已提交
671
    len += sprintf(buf2 + VARSTR_HEADER_SIZE, "CREATE TABLE `%s` USING `%s` (", tbName, pCfg->stbName);
D
dapan1121 已提交
672
    appendTagNameFields(buf2, &len, pCfg);
D
dapan1121 已提交
673
    len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ") TAGS (");
D
dapan1121 已提交
674 675
    code = appendTagValues(buf2, &len, pCfg);
    if (code) {
D
dapan1121 已提交
676
      taosMemoryFree(buf2);
D
dapan1121 已提交
677 678
      return code;
    }
D
dapan1121 已提交
679
    len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ")");
X
Xiaoyu Wang 已提交
680
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
D
dapan1121 已提交
681
  } else {
D
dapan1121 已提交
682
    len += sprintf(buf2 + VARSTR_HEADER_SIZE, "CREATE TABLE `%s` (", tbName);
D
dapan1121 已提交
683
    appendColumnFields(buf2, &len, pCfg);
D
dapan1121 已提交
684
    len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ")");
685
    appendTableOptions(buf2, &len, pDbCfg, pCfg);
D
dapan1121 已提交
686 687
  }

D
dapan1121 已提交
688
  varDataLen(buf2) = (len > 65535) ? 65535 : len;
689

690
  colDataSetVal(pCol2, 0, buf2, false);
D
dapan1121 已提交
691

D
dapan1121 已提交
692
  taosMemoryFree(buf2);
X
Xiaoyu Wang 已提交
693

D
dapan1121 已提交
694 695 696 697
  return TSDB_CODE_SUCCESS;
}

static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp) {
X
Xiaoyu Wang 已提交
698 699 700 701
  SSDataBlock* pBlock = NULL;
  int32_t      code = buildCreateTbResultDataBlock(&pBlock);
  if (TSDB_CODE_SUCCESS == code) {
    code = setCreateTBResultIntoDataBlock(pBlock, pStmt->pDbCfg, pStmt->tableName, pStmt->pTableCfg);
D
dapan1121 已提交
702
  }
X
Xiaoyu Wang 已提交
703 704 705 706 707
  if (TSDB_CODE_SUCCESS == code) {
    code = buildRetrieveTableRsp(pBlock, SHOW_CREATE_TB_RESULT_COLS, pRsp);
  }
  blockDataDestroy(pBlock);
  return code;
D
dapan1121 已提交
708 709 710
}

static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt, SRetrieveTableRsp** pRsp) {
X
Xiaoyu Wang 已提交
711
  STableCfg* pCfg = (STableCfg*)pStmt->pTableCfg;
D
dapan1121 已提交
712 713 714 715
  if (TSDB_SUPER_TABLE != pCfg->tableType) {
    terrno = TSDB_CODE_TSC_NOT_STABLE_ERROR;
    return terrno;
  }
716

D
dapan1121 已提交
717 718
  return execShowCreateTable(pStmt, pRsp);
}
719

D
dapan1121 已提交
720 721
static int32_t execAlterCmd(char* cmd, char* value, bool* processed) {
  int32_t code = 0;
722

D
dapan1121 已提交
723 724 725 726 727 728 729
  if (0 == strcasecmp(cmd, COMMAND_RESET_LOG)) {
    taosResetLog();
    cfgDumpCfg(tsCfg, 0, false);
  } else if (0 == strcasecmp(cmd, COMMAND_SCHEDULE_POLICY)) {
    code = schedulerUpdatePolicy(atoi(value));
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_RESCHEDULE)) {
    code = schedulerEnableReSchedule(atoi(value));
D
dapan1121 已提交
730 731
  } else if (0 == strcasecmp(cmd, COMMAND_CATALOG_DEBUG)) {
    code = ctgdHandleDbgCommand(value);
D
dapan1121 已提交
732 733 734 735 736 737
  } else if (0 == strcasecmp(cmd, COMMAND_ENABLE_MEM_DEBUG)) {
    code = taosMemoryDbgInit();
    if (code) {
      qError("failed to init memory dbg, error:%s", tstrerror(code));
      return code;
    }
D
dapan1121 已提交
738
    tsAsyncLog = false;
D
dapan1121 已提交
739 740 741 742 743 744 745 746
    qInfo("memory dbg enabled");
  } else if (0 == strcasecmp(cmd, COMMAND_DISABLE_MEM_DEBUG)) {
    code = taosMemoryDbgInitRestore();
    if (code) {
      qError("failed to restore from memory dbg, error:%s", tstrerror(code));
      return code;
    }
    qInfo("memory dbg disabled");
D
dapan1121 已提交
747 748 749 750 751 752 753 754 755 756 757
  } else {
    goto _return;
  }

  *processed = true;

_return:

  if (code) {
    terrno = code;
  }
758 759

  return code;
D
dapan1121 已提交
760 761
}

762
static int32_t execAlterLocal(SAlterLocalStmt* pStmt) {
D
dapan1121 已提交
763
  bool processed = false;
764

D
dapan1121 已提交
765 766 767 768 769 770 771
  if (execAlterCmd(pStmt->config, pStmt->value, &processed)) {
    return terrno;
  }

  if (processed) {
    goto _return;
  }
772

773 774 775 776 777 778 779
  bool forbidden = false;
  taosLocalCfgForbiddenToChange(pStmt->config, &forbidden);
  if (forbidden) {
    terrno = TSDB_CODE_OPS_NOT_SUPPORT;
    return terrno;
  }

D
dapan1121 已提交
780
  if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CMD)) {
781
    return terrno;
D
dapan1121 已提交
782 783
  }

D
dapan1121 已提交
784
  if (taosApplyLocalCfg(tsCfg, pStmt->config)) {
785
    return terrno;
D
dapan1121 已提交
786 787
  }

D
dapan1121 已提交
788 789
_return:

D
dapan1121 已提交
790 791
  return TSDB_CODE_SUCCESS;
}
792

X
Xiaoyu Wang 已提交
793
static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) {
D
dapan1121 已提交
794
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
X
Xiaoyu Wang 已提交
795 796 797 798
  if (NULL == pBlock) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

D
dapan1121 已提交
799 800
  pBlock->info.hasVarCol = true;

S
shenglian zhou 已提交
801
  pBlock->pDataBlock = taosArrayInit(SHOW_LOCAL_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
D
dapan1121 已提交
802 803 804 805 806 807 808 809 810 811 812

  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);

X
Xiaoyu Wang 已提交
813 814
  *pOutput = pBlock;
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
815 816 817 818 819 820 821 822
}

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) {
823
    SConfigItem* pItem = taosArrayGet(tsCfg->array, i);
wafwerar's avatar
wafwerar 已提交
824
    GRANT_CFG_SKIP;
D
dapan1121 已提交
825 826
    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);
827
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, c++);
828
    colDataSetVal(pColInfo, i, name, false);
829 830

    char    value[TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
D
dapan1121 已提交
831 832 833 834
    int32_t valueLen = 0;
    cfgDumpItemValue(pItem, &value[VARSTR_HEADER_SIZE], TSDB_CONFIG_VALUE_LEN, &valueLen);
    varDataSetLen(value, valueLen);
    pColInfo = taosArrayGet(pBlock->pDataBlock, c++);
835
    colDataSetVal(pColInfo, i, value, false);
D
dapan1121 已提交
836 837 838 839 840

    numOfRows++;
  }

  pBlock->info.rows = numOfRows;
841

D
dapan1121 已提交
842 843 844 845
  return TSDB_CODE_SUCCESS;
}

static int32_t execShowLocalVariables(SRetrieveTableRsp** pRsp) {
X
Xiaoyu Wang 已提交
846 847 848 849
  SSDataBlock* pBlock = NULL;
  int32_t      code = buildLocalVariablesResultDataBlock(&pBlock);
  if (TSDB_CODE_SUCCESS == code) {
    code = setLocalVariablesResultIntoDataBlock(pBlock);
D
dapan1121 已提交
850
  }
X
Xiaoyu Wang 已提交
851 852 853 854 855
  if (TSDB_CODE_SUCCESS == code) {
    code = buildRetrieveTableRsp(pBlock, SHOW_LOCAL_VARIABLES_RESULT_COLS, pRsp);
  }
  blockDataDestroy(pBlock);
  return code;
856
}
D
dapan1121 已提交
857

858
static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** pOutput) {
859
  SSDataBlock* pBlock = createDataBlock();
860
  if (NULL == pBlock) {
D
dapan1121 已提交
861 862 863
    return TSDB_CODE_OUT_OF_MEMORY;
  }

864 865
  SNode* pProj = NULL;
  FOREACH(pProj, pProjects) {
X
Xiaoyu Wang 已提交
866
    SExprNode*      pExpr = (SExprNode*)pProj;
867
    SColumnInfoData infoData = {0};
X
Xiaoyu Wang 已提交
868 869 870 871 872 873 874
    if (TSDB_DATA_TYPE_NULL == pExpr->resType.type) {
      infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
      infoData.info.bytes = 0;
    } else {
      infoData.info.type = pExpr->resType.type;
      infoData.info.bytes = pExpr->resType.bytes;
    }
875
    blockDataAppendColInfo(pBlock, &infoData);
876 877 878 879 880 881 882 883 884 885 886
  }
  *pOutput = pBlock;
  return TSDB_CODE_SUCCESS;
}

int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) {
  blockDataEnsureCapacity(pBlock, 1);

  int32_t index = 0;
  SNode*  pProj = NULL;
  FOREACH(pProj, pProjects) {
D
dapan1121 已提交
887 888
    if (QUERY_NODE_VALUE != nodeType(pProj)) {
      return TSDB_CODE_PAR_INVALID_SELECTED_EXPR;
889
    } else {
D
dapan1121 已提交
890
      if (((SValueNode*)pProj)->isNull) {
891
        colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true);
D
dapan1121 已提交
892
      } else {
893
        colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, nodesGetValueFromNode((SValueNode*)pProj), false);
D
dapan1121 已提交
894
      }
895 896 897 898
    }
  }

  pBlock->info.rows = 1;
D
dapan1121 已提交
899 900
  return TSDB_CODE_SUCCESS;
}
901

902 903 904 905 906 907 908 909 910
static int32_t execSelectWithoutFrom(SSelectStmt* pSelect, SRetrieveTableRsp** pRsp) {
  SSDataBlock* pBlock = NULL;
  int32_t      code = createSelectResultDataBlock(pSelect->pProjectionList, &pBlock);
  if (TSDB_CODE_SUCCESS == code) {
    code = buildSelectResultDataBlock(pSelect->pProjectionList, pBlock);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = buildRetrieveTableRsp(pBlock, LIST_LENGTH(pSelect->pProjectionList), pRsp);
  }
X
Xiaoyu Wang 已提交
911
  blockDataDestroy(pBlock);
912 913 914
  return code;
}

915
int32_t qExecCommand(int64_t* pConnId, bool sysInfoUser, SNode* pStmt, SRetrieveTableRsp** pRsp) {
916 917
  switch (nodeType(pStmt)) {
    case QUERY_NODE_DESCRIBE_STMT:
918
      return execDescribe(sysInfoUser, pStmt, pRsp);
919 920
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
      return execResetQueryCache();
921
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
D
dapan1121 已提交
922
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt, pRsp);
923
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
D
dapan1121 已提交
924
      return execShowCreateTable((SShowCreateTableStmt*)pStmt, pRsp);
925
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
D
dapan1121 已提交
926
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt, pRsp);
927 928
    case QUERY_NODE_ALTER_LOCAL_STMT:
      return execAlterLocal((SAlterLocalStmt*)pStmt);
929
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
D
dapan1121 已提交
930
      return execShowLocalVariables(pRsp);
931 932
    case QUERY_NODE_SELECT_STMT:
      return execSelectWithoutFrom((SSelectStmt*)pStmt, pRsp);
933 934 935
    case QUERY_NODE_SHOW_DB_ALIVE_STMT:
    case QUERY_NODE_SHOW_CLUSTER_ALIVE_STMT:
      return execShowAliveStatus(pConnId, (SShowAliveStmt*)pStmt, pRsp);       
936 937 938 939 940
    default:
      break;
  }
  return TSDB_CODE_FAILED;
}