command.c 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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"
#include "tdatablock.h"

X
Xiaoyu Wang 已提交
19 20 21 22 23 24 25 26 27 28
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:
      return (pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
    default:
      return pSchema->bytes;
  }
}
29

30
// todo : to convert data according to SSDatablock
31
static void buildRspData(const STableMeta* pMeta, char* pData) {
32 33 34 35 36 37
  int32_t* payloadLen = (int32_t*) pData;
  uint64_t* groupId = (uint64_t*)(pData + sizeof(int32_t));

  int32_t* pColSizes = (int32_t*)(pData + sizeof(int32_t) + sizeof(uint64_t));
  pData = (char*) pColSizes + DESCRIBE_RESULT_COLS * sizeof(int32_t);

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  int32_t numOfRows = TABLE_TOTAL_COL_NUM(pMeta);

  // Field
  int32_t* pOffset = (int32_t*)pData;
  pData += numOfRows * sizeof(int32_t);
  for (int32_t i = 0; i < numOfRows; ++i) {
    STR_TO_VARSTR(pData, pMeta->schema[i].name);
    int16_t len = varDataTLen(pData);
    pData += len;
    *pOffset = pColSizes[0];
    pOffset += 1;
    pColSizes[0] += len;
  }
  
  // Type
  pOffset = (int32_t*)pData;
  pData += numOfRows * sizeof(int32_t);
  for (int32_t i = 0; i < numOfRows; ++i) {
    STR_TO_VARSTR(pData, tDataTypes[pMeta->schema[i].type].name);
    int16_t len = varDataTLen(pData);
    pData += len;
    *pOffset = pColSizes[1];
    pOffset += 1;
    pColSizes[1] += len;
  }

  // Length
  pData += BitmapLen(numOfRows);
  for (int32_t i = 0; i < numOfRows; ++i) {
X
Xiaoyu Wang 已提交
67
    *(int32_t*)pData = getSchemaBytes(pMeta->schema + i);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    pData += sizeof(int32_t);
  }
  pColSizes[2] = sizeof(int32_t) * numOfRows;

  // Note
  pOffset = (int32_t*)pData;
  pData += numOfRows * sizeof(int32_t);
  for (int32_t i = 0; i < numOfRows; ++i) {
    STR_TO_VARSTR(pData, i >= pMeta->tableInfo.numOfColumns ? "TAG" : "");
    int16_t len = varDataTLen(pData);
    pData += len;
    *pOffset = pColSizes[3];
    pOffset += 1;
    pColSizes[3] += len;
  }

  for (int32_t i = 0; i < DESCRIBE_RESULT_COLS; ++i) {
    pColSizes[i] = htonl(pColSizes[i]);
  }
87 88 89


  *payloadLen = (int32_t)(pData - (char*)payloadLen);
90 91 92 93 94 95 96 97
}

static int32_t calcRspSize(const STableMeta* pMeta) {
  int32_t numOfRows = TABLE_TOTAL_COL_NUM(pMeta);
  return sizeof(SRetrieveTableRsp) + 
      (numOfRows * sizeof(int32_t) + numOfRows * DESCRIBE_RESULT_FIELD_LEN) + 
      (numOfRows * sizeof(int32_t) + numOfRows * DESCRIBE_RESULT_TYPE_LEN) +
      (BitmapLen(numOfRows) + numOfRows * sizeof(int32_t)) + 
98 99
      (numOfRows * sizeof(int32_t) + numOfRows * DESCRIBE_RESULT_NOTE_LEN) +
      sizeof(int32_t) + sizeof(uint64_t);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
}

static int32_t execDescribe(SNode* pStmt, SRetrieveTableRsp** pRsp) {
  SDescribeStmt* pDesc = (SDescribeStmt*)pStmt;
  *pRsp = taosMemoryCalloc(1, calcRspSize(pDesc->pMeta));
  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(TABLE_TOTAL_COL_NUM(pDesc->pMeta));
  buildRspData(pDesc->pMeta, (*pRsp)->data);
  return TSDB_CODE_SUCCESS;
}

static int32_t execResetQueryCache() {
  // todo
  return TSDB_CODE_SUCCESS;
}

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();
    default:
      break;
  }
  return TSDB_CODE_FAILED;
}