command.c 5.1 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 19
#include "tdatablock.h"

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

32 33 34 35
static SSDataBlock* buildDescResultDataBlock() {
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
  pBlock->info.numOfCols = DESCRIBE_RESULT_COLS;
  pBlock->info.hasVarCol = true;
36

37
  pBlock->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData));
38

39 40 41
  SColumnInfoData infoData = {0};
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
  infoData.info.bytes = DESCRIBE_RESULT_FIELD_LEN;
42

43 44 45 46 47 48 49
  taosArrayPush(pBlock->pDataBlock, &infoData);

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

  infoData.info.type = TSDB_DATA_TYPE_INT;
50 51
  infoData.info.bytes = tDataTypes[TSDB_DATA_TYPE_INT].bytes;

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  taosArrayPush(pBlock->pDataBlock, &infoData);

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

  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);
67
  char             buf[DESCRIBE_RESULT_FIELD_LEN] = {0};
68
  for (int32_t i = 0; i < numOfRows; ++i) {
69 70
    STR_TO_VARSTR(buf, pMeta->schema[i].name);
    colDataAppend(pCol1, i, buf, false);
71
  }
72

73
  // Type
74
  SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1);
75
  for (int32_t i = 0; i < numOfRows; ++i) {
76 77
    STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name);
    colDataAppend(pCol2, i, buf, false);
78 79 80
  }

  // Length
81
  SColumnInfoData* pCol3 = taosArrayGet(pBlock->pDataBlock, 2);
82
  for (int32_t i = 0; i < numOfRows; ++i) {
83 84
    int32_t bytes = getSchemaBytes(pMeta->schema + i);
    colDataAppend(pCol3, i, (const char*)&bytes, false);
85 86 87
  }

  // Note
88
  SColumnInfoData* pCol4 = taosArrayGet(pBlock->pDataBlock, 3);
89
  for (int32_t i = 0; i < numOfRows; ++i) {
90 91
    STR_TO_VARSTR(buf, i >= pMeta->tableInfo.numOfColumns ? "TAG" : "");
    colDataAppend(pCol4, i, buf, false);
92 93 94 95
  }
}

static int32_t execDescribe(SNode* pStmt, SRetrieveTableRsp** pRsp) {
96 97
  SDescribeStmt* pDesc = (SDescribeStmt*)pStmt;
  int32_t        numOfRows = TABLE_TOTAL_COL_NUM(pDesc->pMeta);
98 99 100 101 102 103

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

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

108 109 110 111 112
  (*pRsp)->useconds = 0;
  (*pRsp)->completed = 1;
  (*pRsp)->precision = 0;
  (*pRsp)->compressed = 0;
  (*pRsp)->compLen = 0;
113 114 115 116 117 118 119 120
  (*pRsp)->numOfRows = htonl(numOfRows);
  (*pRsp)->numOfCols = htonl(DESCRIBE_RESULT_COLS);

  int32_t len = 0;
  blockCompressEncode(pBlock, (*pRsp)->data, &len, DESCRIBE_RESULT_COLS, false);
  ASSERT(len == rspSize - sizeof(SRetrieveTableRsp));

  blockDataDestroy(pBlock);
121 122 123
  return TSDB_CODE_SUCCESS;
}

124 125 126 127 128 129 130 131 132
static int32_t execResetQueryCache() { return catalogClearCache(); }

static int32_t execShowCreateDatabase(SShowCreateDatabaseStmt* pStmt) { return TSDB_CODE_FAILED; }

static int32_t execShowCreateTable(SShowCreateTableStmt* pStmt) { return TSDB_CODE_FAILED; }

static int32_t execShowCreateSTable(SShowCreateTableStmt* pStmt) { return TSDB_CODE_FAILED; }

static int32_t execAlterLocal(SAlterLocalStmt* pStmt) { return TSDB_CODE_FAILED; }
133 134 135 136 137 138 139

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();
140 141 142 143 144 145 146 147
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
      return execShowCreateDatabase((SShowCreateDatabaseStmt*)pStmt);
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
      return execShowCreateTable((SShowCreateTableStmt*)pStmt);
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
      return execShowCreateSTable((SShowCreateTableStmt*)pStmt);
    case QUERY_NODE_ALTER_LOCAL_STMT:
      return execAlterLocal((SAlterLocalStmt*)pStmt);
148 149 150 151 152
    default:
      break;
  }
  return TSDB_CODE_FAILED;
}