diff --git a/include/util/tdef.h b/include/util/tdef.h index ebfb7b696aa5d156dbfa1f07f74470c37c4c63f9..e66cc0b7bb7d4bd86927c43f1f701fdd7ffc538e 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -356,8 +356,8 @@ typedef enum ELogicConditionType { #define TSDB_DEFAULT_EXPLAIN_VERBOSE false -#define TSDB_EXPLAIN_RESULT_ROW_SIZE 1024 -#define TSDB_EXPLAIN_RESULT_COLUMN_NAME "QUERY PLAN" +#define TSDB_EXPLAIN_RESULT_ROW_SIZE 512 +#define TSDB_EXPLAIN_RESULT_COLUMN_NAME "QUERY_PLAN" #define TSDB_MAX_FIELD_LEN 16384 #define TSDB_MAX_BINARY_LEN (TSDB_MAX_FIELD_LEN - TSDB_KEYSIZE) // keep 16384 diff --git a/source/client/test/clientTests.cpp b/source/client/test/clientTests.cpp index f16baba0e7e9f2788bcdb697596f4887cba39afd..f9825c50ffaf77133702cf9b1afaae0242f5bbb5 100644 --- a/source/client/test/clientTests.cpp +++ b/source/client/test/clientTests.cpp @@ -780,31 +780,28 @@ TEST(testCase, async_api_test) { taos_query(pConn, "use test"); - TAOS_RES* pRes = taos_query(pConn, "select * from t1"); - - taos_query(pConn, "alter table t1 add column b int"); - pRes = taos_query(pConn, "insert into t1 values(now, 1, 2)"); + TAOS_RES* pRes = taos_query(pConn, "desc abc1.tu"); if (taos_errno(pRes) != 0) { printf("failed, reason:%s\n", taos_errstr(pRes)); } -// int32_t n = 0; -// TAOS_ROW pRow = NULL; -// TAOS_FIELD* pFields = taos_fetch_fields(pRes); -// int32_t numOfFields = taos_num_fields(pRes); -// -// char str[512] = {0}; -// while ((pRow = taos_fetch_row(pRes)) != NULL) { -// int32_t* length = taos_fetch_lengths(pRes); -// for(int32_t i = 0; i < numOfFields; ++i) { -// printf("(%d):%d " , i, length[i]); -// } -// printf("\n"); -// -// int32_t code = taos_print_row(str, pRow, pFields, numOfFields); -// printf("%s\n", str); -// memset(str, 0, sizeof(str)); -// } + int32_t n = 0; + TAOS_ROW pRow = NULL; + TAOS_FIELD* pFields = taos_fetch_fields(pRes); + int32_t numOfFields = taos_num_fields(pRes); + + char str[512] = {0}; + while ((pRow = taos_fetch_row(pRes)) != NULL) { + int32_t* length = taos_fetch_lengths(pRes); + for(int32_t i = 0; i < numOfFields; ++i) { + printf("(%d):%d " , i, length[i]); + } + printf("\n"); + + int32_t code = taos_print_row(str, pRow, pFields, numOfFields); + printf("%s\n", str); + memset(str, 0, sizeof(str)); + } taos_query_a(pConn, "alter table test.m1 comment 'abcde' ", queryCallback, pConn); getchar(); diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 8c51f1d50c383593499de1999047974d46ee63fa..aee0e15a7be661bc700b0375436771a32b022696 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -1902,7 +1902,7 @@ void blockCompressEncode(const SSDataBlock* pBlock, char* data, int32_t* dataLen int32_t* colSizes = (int32_t*)data; data += numOfCols * sizeof(int32_t); - *dataLen = blockDataGetSerialMetaSize(numOfCols);; + *dataLen = blockDataGetSerialMetaSize(numOfCols); int32_t numOfRows = pBlock->info.rows; for (int32_t col = 0; col < numOfCols; ++col) { diff --git a/source/libs/command/src/command.c b/source/libs/command/src/command.c index 3034b4b02a293dd4cce91814bde367682db6ac8d..f330b7ce162f9c79189a66c0772fffbc96cb8dcc 100644 --- a/source/libs/command/src/command.c +++ b/source/libs/command/src/command.c @@ -28,91 +28,94 @@ static int32_t getSchemaBytes(const SSchema* pSchema) { } } -// todo : to convert data according to SSDatablock -static void buildRspData(const STableMeta* pMeta, char* pData) { - int32_t* payloadLen = (int32_t*) pData; - uint64_t* groupId = (uint64_t*)(pData + sizeof(int32_t)); +static SSDataBlock* buildDescResultDataBlock() { + SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock)); + pBlock->info.numOfCols = DESCRIBE_RESULT_COLS; + pBlock->info.hasVarCol = true; - int32_t* pColSizes = (int32_t*)(pData + sizeof(int32_t) + sizeof(uint64_t)); - pData = (char*) pColSizes + DESCRIBE_RESULT_COLS * sizeof(int32_t); + pBlock->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData)); - int32_t numOfRows = TABLE_TOTAL_COL_NUM(pMeta); + SColumnInfoData infoData = {0}; + infoData.info.type = TSDB_DATA_TYPE_VARCHAR; + infoData.info.bytes = DESCRIBE_RESULT_FIELD_LEN; - // Field - int32_t* pOffset = (int32_t*)pData; - pData += numOfRows * sizeof(int32_t); + 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; + infoData.info.bytes = tDataTypes[TSDB_DATA_TYPE_INT].bytes;; + 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); + char buf[DESCRIBE_RESULT_FIELD_LEN] = {0}; 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; + STR_TO_VARSTR(buf, pMeta->schema[i].name); + colDataAppend(pCol1, i, buf, false); } - + // Type - pOffset = (int32_t*)pData; - pData += numOfRows * sizeof(int32_t); + SColumnInfoData* pCol2 = taosArrayGet(pBlock->pDataBlock, 1); 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; + STR_TO_VARSTR(buf, tDataTypes[pMeta->schema[i].type].name); + colDataAppend(pCol2, i, buf, false); } // Length - pData += BitmapLen(numOfRows); + SColumnInfoData* pCol3 = taosArrayGet(pBlock->pDataBlock, 2); for (int32_t i = 0; i < numOfRows; ++i) { - *(int32_t*)pData = getSchemaBytes(pMeta->schema + i); - pData += sizeof(int32_t); + int32_t bytes = getSchemaBytes(pMeta->schema + i); + colDataAppend(pCol3, i, (const char*)&bytes, false); } - pColSizes[2] = sizeof(int32_t) * numOfRows; // Note - pOffset = (int32_t*)pData; - pData += numOfRows * sizeof(int32_t); + SColumnInfoData* pCol4 = taosArrayGet(pBlock->pDataBlock, 3); 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]); + STR_TO_VARSTR(buf, i >= pMeta->tableInfo.numOfColumns ? "TAG" : ""); + colDataAppend(pCol4, i, buf, false); } - - - *payloadLen = (int32_t)(pData - (char*)payloadLen); -} - -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)) + - (numOfRows * sizeof(int32_t) + numOfRows * DESCRIBE_RESULT_NOTE_LEN) + - sizeof(int32_t) + sizeof(uint64_t); } static int32_t execDescribe(SNode* pStmt, SRetrieveTableRsp** pRsp) { - SDescribeStmt* pDesc = (SDescribeStmt*)pStmt; - *pRsp = taosMemoryCalloc(1, calcRspSize(pDesc->pMeta)); + SDescribeStmt* pDesc = (SDescribeStmt*) pStmt; + int32_t numOfRows = TABLE_TOTAL_COL_NUM(pDesc->pMeta); + + SSDataBlock* pBlock = buildDescResultDataBlock(); + setDescResultIntoDataBlock(pBlock, numOfRows, pDesc->pMeta); + + 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(TABLE_TOTAL_COL_NUM(pDesc->pMeta)); - buildRspData(pDesc->pMeta, (*pRsp)->data); + (*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); return TSDB_CODE_SUCCESS; } diff --git a/source/libs/command/src/explain.c b/source/libs/command/src/explain.c index 99ff4d406c2c89ce6e44789fb7a3c88d6a2f69ca..56b041af40988184b5c34ceec9daba6666284604 100644 --- a/source/libs/command/src/explain.c +++ b/source/libs/command/src/explain.c @@ -13,6 +13,7 @@ * along with this program. If not, see . */ +#include "tdatablock.h" #include "commandInt.h" #include "plannodes.h" #include "query.h" @@ -916,9 +917,32 @@ int32_t qExplainGetRspFromCtx(void *ctx, SRetrieveTableRsp **pRsp) { QRY_ERR_RET(TSDB_CODE_QRY_APP_ERROR); } - int32_t colNum = 1; - int32_t rspSize = sizeof(SRetrieveTableRsp) + sizeof(int32_t) + sizeof(uint64_t) + sizeof(int32_t) * colNum + - sizeof(int32_t) * rowNum + pCtx->dataSize; + SSDataBlock *pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock)); + SColumnInfoData infoData = {0}; + infoData.info.type = TSDB_DATA_TYPE_VARCHAR; + infoData.info.bytes = TSDB_EXPLAIN_RESULT_ROW_SIZE; + + pBlock->pDataBlock = taosArrayInit(1, sizeof(SColumnInfoData)); + taosArrayPush(pBlock->pDataBlock, &infoData); + + SColumnInfoData* pInfoData = taosArrayGet(pBlock->pDataBlock, 0); + pInfoData->hasNull = false; + colInfoDataEnsureCapacity(pInfoData, 0, rowNum); + + char buf[1024] = {0}; + for (int32_t i = 0; i < rowNum; ++i) { + SQueryExplainRowInfo *row = taosArrayGet(pCtx->rows, i); + varDataCopy(buf, row->buf); + ASSERT(varDataTLen(row->buf) == row->len); + colDataAppend(pInfoData, i, buf, false); + } + + pBlock->info.numOfCols = 1; + pBlock->info.rows = rowNum; + pBlock->info.hasVarCol = true; + + int32_t rspSize = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock); + SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)taosMemoryCalloc(1, rspSize); if (NULL == rsp) { qError("malloc SRetrieveTableRsp failed, size:%d", rspSize); @@ -928,34 +952,13 @@ int32_t qExplainGetRspFromCtx(void *ctx, SRetrieveTableRsp **pRsp) { rsp->completed = 1; rsp->numOfRows = htonl(rowNum); - // payload length - *(int32_t *)rsp->data = - sizeof(int32_t) + sizeof(uint64_t) + sizeof(int32_t) * colNum + sizeof(int32_t) * rowNum + pCtx->dataSize; + int32_t len = 0; + blockCompressEncode(pBlock, rsp->data, &len, pBlock->info.numOfCols, 0); + ASSERT(len == rspSize - sizeof(SRetrieveTableRsp)); - // group id - *(uint64_t *)(rsp->data + sizeof(int32_t)) = 0; - - // column length - int32_t *colLength = (int32_t *)(rsp->data + sizeof(int32_t) + sizeof(uint64_t)); - - // varchar column offset segment - int32_t *offset = (int32_t *)((char *)colLength + sizeof(int32_t)); - - // varchar data real payload - char *data = (char *)(offset + rowNum); - - char *start = data; - for (int32_t i = 0; i < rowNum; ++i) { - SQueryExplainRowInfo *row = taosArrayGet(pCtx->rows, i); - offset[i] = data - start; - - varDataCopy(data, row->buf); - ASSERT(varDataTLen(row->buf) == row->len); - data += row->len; - } + rsp->compLen = htonl(len); - *colLength = htonl(data - start); - rsp->compLen = htonl(rspSize); + blockDataDestroy(pBlock); *pRsp = rsp; return TSDB_CODE_SUCCESS;