TDengine/source/libs/executor/src/executil.c

3455 lines
114 KiB
C
Raw Normal View History

/*
* 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 "function.h"
#include "functionMgt.h"
2022-06-25 12:36:38 +00:00
#include "index.h"
#include "os.h"
2023-03-18 10:01:45 +00:00
#include "query.h"
2024-12-19 02:48:40 +00:00
#include "querynodes.h"
#include "tarray.h"
#include "tdatablock.h"
#include "thash.h"
#include "tmsg.h"
#include "ttime.h"
#include "executil.h"
2023-04-28 03:42:34 +00:00
#include "executorInt.h"
2023-04-27 16:23:38 +00:00
#include "querytask.h"
#include "storageapi.h"
2023-06-26 10:43:00 +00:00
#include "tcompression.h"
2022-10-30 14:13:49 +00:00
typedef struct tagFilterAssist {
SHashObj* colHash;
int32_t index;
SArray* cInfoList;
2024-07-21 10:20:30 +00:00
int32_t code;
2022-10-30 14:13:49 +00:00
} tagFilterAssist;
2024-07-21 10:20:30 +00:00
typedef struct STransTagExprCtx {
int32_t code;
SMetaReader* pReader;
} STransTagExprCtx;
2023-01-31 14:43:59 +00:00
typedef enum {
FILTER_NO_LOGIC = 1,
FILTER_AND,
FILTER_OTHER,
} FilterCondType;
static FilterCondType checkTagCond(SNode* cond);
2023-06-26 10:43:00 +00:00
static int32_t optimizeTbnameInCond(void* metaHandle, int64_t suid, SArray* list, SNode* pTagCond, SStorageAPI* pAPI);
2025-03-28 10:10:57 +00:00
static int32_t optimizeTbnameInCondImpl(void* metaHandle, SArray* list, SNode* pTagCond, SStorageAPI* pStoreAPI,
uint64_t suid);
2023-02-20 09:21:37 +00:00
2024-04-03 09:11:49 +00:00
static int32_t getTableList(void* pVnode, SScanPhysiNode* pScanNode, SNode* pTagCond, SNode* pTagIndexCond,
STableListInfo* pListInfo, uint8_t* digest, const char* idstr, SStorageAPI* pStorageAPI);
2022-09-28 14:07:16 +00:00
2025-03-28 10:10:57 +00:00
static int64_t getLimit(const SNode* pLimit) {
return (NULL == pLimit || NULL == ((SLimitNode*)pLimit)->limit) ? -1 : ((SLimitNode*)pLimit)->limit->datum.i;
}
static int64_t getOffset(const SNode* pLimit) {
return (NULL == pLimit || NULL == ((SLimitNode*)pLimit)->offset) ? -1 : ((SLimitNode*)pLimit)->offset->datum.i;
}
static void releaseColInfoData(void* pCol);
2022-09-28 14:07:16 +00:00
2022-06-25 12:36:38 +00:00
void initResultRowInfo(SResultRowInfo* pResultRowInfo) {
pResultRowInfo->size = 0;
pResultRowInfo->cur.pageId = -1;
}
2022-06-25 12:36:38 +00:00
void closeResultRow(SResultRow* pResultRow) { pResultRow->closed = true; }
void resetResultRow(SResultRow* pResultRow, size_t entrySize) {
pResultRow->numOfRows = 0;
pResultRow->closed = false;
pResultRow->endInterp = false;
pResultRow->startInterp = false;
if (entrySize > 0) {
memset(pResultRow->pEntryInfo, 0, entrySize);
}
}
2021-02-23 06:01:44 +00:00
// TODO refactor: use macro
SResultRowEntryInfo* getResultEntryInfo(const SResultRow* pRow, int32_t index, const int32_t* offset) {
2022-06-25 12:36:38 +00:00
return (SResultRowEntryInfo*)((char*)pRow->pEntryInfo + offset[index]);
2020-11-02 06:10:15 +00:00
}
size_t getResultRowSize(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
int32_t rowSize = (numOfOutput * sizeof(SResultRowEntryInfo)) + sizeof(SResultRow);
2022-06-25 12:36:38 +00:00
for (int32_t i = 0; i < numOfOutput; ++i) {
rowSize += pCtx[i].resDataInfo.interBufSize;
}
return rowSize;
}
// Convert buf read from rocksdb to result row
2025-03-28 10:10:57 +00:00
int32_t getResultRowFromBuf(SExprSupp* pSup, const char* inBuf, size_t inBufSize, char** outBuf, size_t* outBufSize) {
if (inBuf == NULL || pSup == NULL) {
qError("invalid input parameters, inBuf:%p, pSup:%p", inBuf, pSup);
return TSDB_CODE_INVALID_PARA;
}
2025-03-28 10:10:57 +00:00
SqlFunctionCtx* pCtx = pSup->pCtx;
int32_t* offset = pSup->rowEntryInfoOffset;
SResultRow* pResultRow = NULL;
size_t processedSize = 0;
int32_t code = TSDB_CODE_SUCCESS;
// calculate the size of output buffer
*outBufSize = getResultRowSize(pCtx, pSup->numOfExprs);
*outBuf = taosMemoryMalloc(*outBufSize);
if (*outBuf == NULL) {
qError("failed to allocate memory for output buffer, size:%zu", *outBufSize);
return terrno;
}
2024-10-10 01:04:44 +00:00
pResultRow = (SResultRow*)*outBuf;
(void)memcpy(pResultRow, inBuf, sizeof(SResultRow));
inBuf += sizeof(SResultRow);
processedSize += sizeof(SResultRow);
for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
int32_t len = *(int32_t*)inBuf;
inBuf += sizeof(int32_t);
processedSize += sizeof(int32_t);
if (pResultRow->version != FUNCTION_RESULT_INFO_VERSION && pCtx->fpSet.decode) {
code = pCtx->fpSet.decode(&pCtx[i], inBuf, getResultEntryInfo(pResultRow, i, offset), pResultRow->version);
if (code != TSDB_CODE_SUCCESS) {
qError("failed to decode result row, code:%d", code);
return code;
}
} else {
(void)memcpy(getResultEntryInfo(pResultRow, i, offset), inBuf, len);
}
inBuf += len;
processedSize += len;
}
if (processedSize < inBufSize) {
// stream stores extra data after result row
size_t leftLen = inBufSize - processedSize;
TAOS_MEMORY_REALLOC(*outBuf, *outBufSize + leftLen);
if (*outBuf == NULL) {
qError("failed to reallocate memory for output buffer, size:%zu", *outBufSize + leftLen);
return terrno;
}
(void)memcpy(*outBuf + *outBufSize, inBuf, leftLen);
inBuf += leftLen;
processedSize += leftLen;
*outBufSize += leftLen;
}
2025-03-28 10:10:57 +00:00
qTrace("[StreamInternal] get result inBufSize:%zu, outBufSize:%zu", inBufSize, *outBufSize);
return TSDB_CODE_SUCCESS;
}
// Convert result row to buf for rocksdb
2025-03-28 10:10:57 +00:00
int32_t putResultRowToBuf(SExprSupp* pSup, const char* inBuf, size_t inBufSize, char** outBuf, size_t* outBufSize) {
if (pSup == NULL || inBuf == NULL || outBuf == NULL || outBufSize == NULL) {
qError("invalid input parameters, inBuf:%p, pSup:%p, outBufSize:%p, outBuf:%p", inBuf, pSup, outBufSize, outBuf);
return TSDB_CODE_INVALID_PARA;
}
2025-03-28 10:10:57 +00:00
SqlFunctionCtx* pCtx = pSup->pCtx;
int32_t* offset = pSup->rowEntryInfoOffset;
SResultRow* pResultRow = (SResultRow*)inBuf;
size_t rowSize = getResultRowSize(pCtx, pSup->numOfExprs);
if (rowSize > inBufSize) {
qError("invalid input buffer size, rowSize:%zu, inBufSize:%zu", rowSize, inBufSize);
return TSDB_CODE_INVALID_PARA;
}
// calculate the size of output buffer
*outBufSize = rowSize + sizeof(int32_t) * pSup->numOfExprs;
if (rowSize < inBufSize) {
*outBufSize += inBufSize - rowSize;
}
*outBuf = taosMemoryMalloc(*outBufSize);
if (*outBuf == NULL) {
qError("failed to allocate memory for output buffer, size:%zu", *outBufSize);
return terrno;
}
2025-03-28 10:10:57 +00:00
char* pBuf = *outBuf;
pResultRow->version = FUNCTION_RESULT_INFO_VERSION;
2024-10-10 01:04:44 +00:00
(void)memcpy(pBuf, pResultRow, sizeof(SResultRow));
pBuf += sizeof(SResultRow);
for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
size_t len = sizeof(SResultRowEntryInfo) + pCtx[i].resDataInfo.interBufSize;
2025-03-28 10:10:57 +00:00
*(int32_t*)pBuf = (int32_t)len;
2024-10-10 01:04:44 +00:00
pBuf += sizeof(int32_t);
(void)memcpy(pBuf, getResultEntryInfo(pResultRow, i, offset), len);
pBuf += len;
}
if (rowSize < inBufSize) {
// stream stores extra data after result row
size_t leftLen = inBufSize - rowSize;
2024-10-10 01:04:44 +00:00
(void)memcpy(pBuf, inBuf + rowSize, leftLen);
pBuf += leftLen;
}
2025-03-28 10:10:57 +00:00
qTrace("[StreamInternal] put result inBufSize:%zu, outBufSize:%zu", inBufSize, *outBufSize);
return TSDB_CODE_SUCCESS;
}
2023-02-20 09:21:37 +00:00
static void freeEx(void* p) { taosMemoryFree(*(void**)p); }
2021-02-03 10:29:40 +00:00
void cleanupGroupResInfo(SGroupResInfo* pGroupResInfo) {
2023-01-11 10:47:45 +00:00
taosMemoryFreeClear(pGroupResInfo->pBuf);
2023-02-14 17:28:50 +00:00
if (pGroupResInfo->freeItem) {
2023-02-20 09:21:37 +00:00
// taosArrayDestroy(pGroupResInfo->pRows);
taosArrayDestroyEx(pGroupResInfo->pRows, freeEx);
pGroupResInfo->freeItem = false;
2023-02-14 17:28:50 +00:00
pGroupResInfo->pRows = NULL;
} else {
2024-07-12 07:22:39 +00:00
taosArrayDestroy(pGroupResInfo->pRows);
pGroupResInfo->pRows = NULL;
2022-07-18 05:52:33 +00:00
}
2022-06-25 12:36:38 +00:00
pGroupResInfo->index = 0;
pGroupResInfo->delIndex = 0;
2021-02-03 10:29:40 +00:00
}
2022-07-08 11:55:06 +00:00
int32_t resultrowComparAsc(const void* p1, const void* p2) {
2022-06-25 12:36:38 +00:00
SResKeyPos* pp1 = *(SResKeyPos**)p1;
SResKeyPos* pp2 = *(SResKeyPos**)p2;
if (pp1->groupId == pp2->groupId) {
2022-06-25 12:36:38 +00:00
int64_t pts1 = *(int64_t*)pp1->key;
int64_t pts2 = *(int64_t*)pp2->key;
if (pts1 == pts2) {
return 0;
} else {
2022-06-25 12:36:38 +00:00
return pts1 < pts2 ? -1 : 1;
}
} else {
2022-06-25 12:36:38 +00:00
return pp1->groupId < pp2->groupId ? -1 : 1;
}
}
2022-06-25 12:36:38 +00:00
static int32_t resultrowComparDesc(const void* p1, const void* p2) { return resultrowComparAsc(p2, p1); }
2024-07-26 12:48:06 +00:00
int32_t initGroupedResultInfo(SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap, int32_t order) {
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
2021-02-03 10:29:40 +00:00
if (pGroupResInfo->pRows != NULL) {
taosArrayDestroy(pGroupResInfo->pRows);
}
if (pGroupResInfo->pBuf) {
taosMemoryFree(pGroupResInfo->pBuf);
pGroupResInfo->pBuf = NULL;
}
2021-02-03 10:29:40 +00:00
// extract the result rows information from the hash map
2023-01-11 10:47:45 +00:00
int32_t size = tSimpleHashGetSize(pHashmap);
void* pData = NULL;
2023-01-11 10:47:45 +00:00
pGroupResInfo->pRows = taosArrayInit(size, POINTER_BYTES);
2024-08-05 04:22:25 +00:00
QUERY_CHECK_NULL(pGroupResInfo->pRows, code, lino, _end, terrno);
size_t keyLen = 0;
int32_t iter = 0;
2023-05-18 02:25:10 +00:00
int64_t bufLen = 0, offset = 0;
2023-01-11 10:47:45 +00:00
2023-02-10 17:56:28 +00:00
// todo move away and record this during create window
while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
2024-07-23 02:50:16 +00:00
/*void* key = */ (void)tSimpleHashGetKey(pData, &keyLen);
2023-02-10 17:56:28 +00:00
bufLen += keyLen + sizeof(SResultRowPosition);
}
2023-02-10 17:56:28 +00:00
pGroupResInfo->pBuf = taosMemoryMalloc(bufLen);
2024-07-26 12:48:06 +00:00
QUERY_CHECK_NULL(pGroupResInfo->pBuf, code, lino, _end, terrno);
2023-01-11 10:47:45 +00:00
2023-02-10 17:56:28 +00:00
iter = 0;
while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
void* key = tSimpleHashGetKey(pData, &keyLen);
2023-02-20 09:21:37 +00:00
SResKeyPos* p = (SResKeyPos*)(pGroupResInfo->pBuf + offset);
2022-06-25 12:36:38 +00:00
p->groupId = *(uint64_t*)key;
p->pos = *(SResultRowPosition*)pData;
memcpy(p->key, (char*)key + sizeof(uint64_t), keyLen - sizeof(uint64_t));
2024-07-26 12:48:06 +00:00
void* tmp = taosArrayPush(pGroupResInfo->pRows, &p);
QUERY_CHECK_NULL(pGroupResInfo->pBuf, code, lino, _end, terrno);
2023-02-10 17:56:28 +00:00
offset += keyLen + sizeof(struct SResultRowPosition);
}
if (order == TSDB_ORDER_ASC || order == TSDB_ORDER_DESC) {
2022-06-25 12:36:38 +00:00
__compar_fn_t fn = (order == TSDB_ORDER_ASC) ? resultrowComparAsc : resultrowComparDesc;
size = POINTER_BYTES;
2022-10-18 03:43:58 +00:00
taosSort(pGroupResInfo->pRows->pData, taosArrayGetSize(pGroupResInfo->pRows), size, fn);
}
2021-03-04 10:12:52 +00:00
pGroupResInfo->index = 0;
2024-07-26 12:48:06 +00:00
_end:
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
}
return code;
2021-02-03 10:29:40 +00:00
}
2022-03-30 09:09:06 +00:00
void initMultiResInfoFromArrayList(SGroupResInfo* pGroupResInfo, SArray* pArrayList) {
if (pGroupResInfo->pRows != NULL) {
taosArrayDestroy(pGroupResInfo->pRows);
2022-03-30 09:09:06 +00:00
}
2023-02-14 17:28:50 +00:00
pGroupResInfo->freeItem = true;
pGroupResInfo->pRows = pArrayList;
2022-03-30 09:09:06 +00:00
pGroupResInfo->index = 0;
pGroupResInfo->delIndex = 0;
2022-03-30 09:09:06 +00:00
}
bool hasRemainResults(SGroupResInfo* pGroupResInfo) {
2021-02-03 10:29:40 +00:00
if (pGroupResInfo->pRows == NULL) {
return false;
}
return pGroupResInfo->index < taosArrayGetSize(pGroupResInfo->pRows);
}
int32_t getNumOfTotalRes(SGroupResInfo* pGroupResInfo) {
if (pGroupResInfo->pRows == 0) {
return 0;
}
2022-06-25 12:36:38 +00:00
return (int32_t)taosArrayGetSize(pGroupResInfo->pRows);
2021-02-03 10:29:40 +00:00
}
SArray* createSortInfo(SNodeList* pNodeList) {
size_t numOfCols = 0;
if (pNodeList != NULL) {
numOfCols = LIST_LENGTH(pNodeList);
} else {
numOfCols = 0;
}
SArray* pList = taosArrayInit(numOfCols, sizeof(SBlockOrderInfo));
if (pList == NULL) {
return pList;
}
for (int32_t i = 0; i < numOfCols; ++i) {
SOrderByExprNode* pSortKey = (SOrderByExprNode*)nodesListGetNode(pNodeList, i);
2024-08-05 08:09:01 +00:00
if (!pSortKey) {
2024-08-05 10:18:13 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2024-08-05 08:09:01 +00:00
taosArrayDestroy(pList);
pList = NULL;
2024-12-11 11:18:50 +00:00
terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
2024-08-05 08:09:01 +00:00
break;
}
2025-03-28 10:10:57 +00:00
SBlockOrderInfo bi = {0};
bi.order = (pSortKey->order == ORDER_ASC) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
bi.nullFirst = (pSortKey->nullOrder == NULL_ORDER_FIRST);
if (nodeType(pSortKey->pExpr) != QUERY_NODE_COLUMN) {
qError("invalid order by expr type:%d", nodeType(pSortKey->pExpr));
taosArrayDestroy(pList);
pList = NULL;
terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
break;
}
SColumnNode* pColNode = (SColumnNode*)pSortKey->pExpr;
bi.slotId = pColNode->slotId;
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pList, &bi);
if (!tmp) {
taosArrayDestroy(pList);
pList = NULL;
break;
}
}
return pList;
}
SSDataBlock* createDataBlockFromDescNode(void* p) {
SDataBlockDescNode* pNode = (SDataBlockDescNode*)p;
2024-07-27 10:55:34 +00:00
int32_t numOfCols = LIST_LENGTH(pNode->pSlots);
SSDataBlock* pBlock = NULL;
int32_t code = createDataBlock(&pBlock);
if (code) {
terrno = code;
return NULL;
}
2021-02-03 10:29:40 +00:00
2022-11-28 04:32:40 +00:00
pBlock->info.id.blockId = pNode->dataBlockId;
pBlock->info.type = STREAM_INVALID;
2022-07-12 07:36:48 +00:00
pBlock->info.calWin = (STimeWindow){.skey = INT64_MIN, .ekey = INT64_MAX};
pBlock->info.watermark = INT64_MIN;
2021-02-03 10:29:40 +00:00
for (int32_t i = 0; i < numOfCols; ++i) {
2025-03-28 10:10:57 +00:00
SSlotDescNode* pDescNode = (SSlotDescNode*)nodesListGetNode(pNode->pSlots, i);
2024-08-05 08:09:01 +00:00
if (!pDescNode) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
blockDataDestroy(pBlock);
pBlock = NULL;
2024-08-07 08:59:19 +00:00
terrno = TSDB_CODE_INVALID_PARA;
2024-08-05 08:09:01 +00:00
break;
}
2022-06-25 12:36:38 +00:00
SColumnInfoData idata =
createColumnInfoData(pDescNode->dataType.type, pDescNode->dataType.bytes, pDescNode->slotId);
idata.info.scale = pDescNode->dataType.scale;
idata.info.precision = pDescNode->dataType.precision;
idata.info.noData = pDescNode->reserve;
2024-07-27 10:55:34 +00:00
code = blockDataAppendColInfo(pBlock, &idata);
2024-07-23 02:50:16 +00:00
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
blockDataDestroy(pBlock);
pBlock = NULL;
terrno = code;
break;
}
2021-02-03 10:29:40 +00:00
}
return pBlock;
}
2024-04-08 10:04:06 +00:00
int32_t prepareDataBlockBuf(SSDataBlock* pDataBlock, SColMatchInfo* pMatchInfo) {
SDataBlockInfo* pBlockInfo = &pDataBlock->info;
for (int32_t i = 0; i < taosArrayGetSize(pMatchInfo->pList); ++i) {
SColMatchItem* pItem = taosArrayGet(pMatchInfo->pList, i);
2024-08-05 08:09:01 +00:00
if (!pItem) {
2024-08-05 10:18:13 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2024-08-05 08:09:01 +00:00
return terrno;
}
2024-04-08 10:04:06 +00:00
if (pItem->isPk) {
SColumnInfoData* pInfoData = taosArrayGet(pDataBlock->pDataBlock, pItem->dstSlotId);
2024-08-05 08:09:01 +00:00
if (!pInfoData) {
2024-08-05 10:18:13 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2024-08-05 08:09:01 +00:00
return terrno;
}
2024-04-08 10:04:06 +00:00
pBlockInfo->pks[0].type = pInfoData->info.type;
pBlockInfo->pks[1].type = pInfoData->info.type;
2024-04-11 14:34:47 +00:00
// allocate enough buffer size, which is pInfoData->info.bytes
2024-04-08 10:04:06 +00:00
if (IS_VAR_DATA_TYPE(pItem->dataType.type)) {
pBlockInfo->pks[0].pData = taosMemoryCalloc(1, pInfoData->info.bytes);
if (pBlockInfo->pks[0].pData == NULL) {
return terrno;
2024-04-08 10:04:06 +00:00
}
pBlockInfo->pks[1].pData = taosMemoryCalloc(1, pInfoData->info.bytes);
if (pBlockInfo->pks[1].pData == NULL) {
taosMemoryFreeClear(pBlockInfo->pks[0].pData);
return terrno;
2024-04-08 10:04:06 +00:00
}
pBlockInfo->pks[0].nData = pInfoData->info.bytes;
pBlockInfo->pks[1].nData = pInfoData->info.bytes;
2024-04-08 10:04:06 +00:00
}
break;
2024-04-08 10:04:06 +00:00
}
}
return TSDB_CODE_SUCCESS;
}
2022-06-21 12:53:19 +00:00
EDealRes doTranslateTagExpr(SNode** pNode, void* pContext) {
2024-07-21 10:20:30 +00:00
STransTagExprCtx* pCtx = pContext;
2024-08-06 08:14:15 +00:00
SMetaReader* mr = pCtx->pReader;
bool isTagCol = false, isTbname = false;
2022-06-25 12:36:38 +00:00
if (nodeType(*pNode) == QUERY_NODE_COLUMN) {
SColumnNode* pCol = (SColumnNode*)*pNode;
if (pCol->colType == COLUMN_TYPE_TBNAME)
isTbname = true;
else
isTagCol = true;
} else if (nodeType(*pNode) == QUERY_NODE_FUNCTION) {
SFunctionNode* pFunc = (SFunctionNode*)*pNode;
2024-07-23 02:50:16 +00:00
if (pFunc->funcType == FUNCTION_TYPE_TBNAME) isTbname = true;
}
if (isTagCol) {
2022-06-21 12:53:19 +00:00
SColumnNode* pSColumnNode = *(SColumnNode**)pNode;
2024-07-21 10:20:30 +00:00
SValueNode* res = NULL;
pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&res);
2022-06-21 12:53:19 +00:00
if (NULL == res) {
return DEAL_RES_ERROR;
}
res->translate = true;
res->node.resType = pSColumnNode->node.resType;
STagVal tagVal = {0};
tagVal.cid = pSColumnNode->colId;
2023-05-25 09:51:03 +00:00
const char* p = mr->pAPI->extractTagVal(mr->me.ctbEntry.pTags, pSColumnNode->node.resType.type, &tagVal);
2022-06-21 12:53:19 +00:00
if (p == NULL) {
res->node.resType.type = TSDB_DATA_TYPE_NULL;
2022-06-25 12:36:38 +00:00
} else if (pSColumnNode->node.resType.type == TSDB_DATA_TYPE_JSON) {
int32_t len = ((const STag*)p)->len;
2022-06-21 12:53:19 +00:00
res->datum.p = taosMemoryCalloc(len + 1, 1);
2024-08-05 04:22:25 +00:00
if (NULL == res->datum.p) {
return DEAL_RES_ERROR;
}
2022-06-21 12:53:19 +00:00
memcpy(res->datum.p, p, len);
} else if (IS_VAR_DATA_TYPE(pSColumnNode->node.resType.type)) {
res->datum.p = taosMemoryCalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1, 1);
2024-08-05 04:22:25 +00:00
if (NULL == res->datum.p) {
return DEAL_RES_ERROR;
}
2022-06-21 12:53:19 +00:00
memcpy(varDataVal(res->datum.p), tagVal.pData, tagVal.nData);
varDataSetLen(res->datum.p, tagVal.nData);
} else {
2024-07-23 02:50:16 +00:00
int32_t code = nodesSetValueNodeValue(res, &(tagVal.i64));
if (code != TSDB_CODE_SUCCESS) {
return DEAL_RES_ERROR;
}
2022-06-21 12:53:19 +00:00
}
nodesDestroyNode(*pNode);
*pNode = (SNode*)res;
} else if (isTbname) {
2024-07-21 10:20:30 +00:00
SValueNode* res = NULL;
pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&res);
if (NULL == res) {
return DEAL_RES_ERROR;
}
2022-06-21 12:53:19 +00:00
res->translate = true;
res->node.resType = ((SExprNode*)(*pNode))->resType;
2022-06-21 12:53:19 +00:00
int32_t len = strlen(mr->me.name);
res->datum.p = taosMemoryCalloc(len + VARSTR_HEADER_SIZE + 1, 1);
2024-08-05 04:22:25 +00:00
if (NULL == res->datum.p) {
return DEAL_RES_ERROR;
}
memcpy(varDataVal(res->datum.p), mr->me.name, len);
varDataSetLen(res->datum.p, len);
nodesDestroyNode(*pNode);
*pNode = (SNode*)res;
2022-06-21 12:53:19 +00:00
}
return DEAL_RES_CONTINUE;
}
2023-06-26 10:43:00 +00:00
int32_t isQualifiedTable(STableKeyInfo* info, SNode* pTagCond, void* metaHandle, bool* pQualified, SStorageAPI* pAPI) {
int32_t code = TSDB_CODE_SUCCESS;
2022-06-25 12:36:38 +00:00
SMetaReader mr = {0};
pAPI->metaReaderFn.initReader(&mr, metaHandle, META_READER_LOCK, &pAPI->metaFn);
code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, info->uid);
if (TSDB_CODE_SUCCESS != code) {
pAPI->metaReaderFn.clearReader(&mr);
2022-07-27 05:55:04 +00:00
*pQualified = false;
2022-07-27 05:55:04 +00:00
return TSDB_CODE_SUCCESS;
}
2022-06-21 12:53:19 +00:00
2024-07-21 10:20:30 +00:00
SNode* pTagCondTmp = NULL;
code = nodesCloneNode(pTagCond, &pTagCondTmp);
if (TSDB_CODE_SUCCESS != code) {
*pQualified = false;
2024-08-27 02:50:28 +00:00
pAPI->metaReaderFn.clearReader(&mr);
2024-07-21 10:20:30 +00:00
return code;
}
STransTagExprCtx ctx = {.code = 0, .pReader = &mr};
nodesRewriteExprPostOrder(&pTagCondTmp, doTranslateTagExpr, &ctx);
pAPI->metaReaderFn.clearReader(&mr);
2024-07-21 10:20:30 +00:00
if (TSDB_CODE_SUCCESS != ctx.code) {
*pQualified = false;
terrno = code;
return code;
}
2022-06-21 12:53:19 +00:00
SNode* pNew = NULL;
code = scalarCalculateConstants(pTagCondTmp, &pNew);
2022-06-21 12:53:19 +00:00
if (TSDB_CODE_SUCCESS != code) {
2022-06-27 08:17:58 +00:00
terrno = code;
2022-06-21 12:53:19 +00:00
nodesDestroyNode(pTagCondTmp);
*pQualified = false;
return code;
2022-06-21 12:53:19 +00:00
}
2022-06-25 12:36:38 +00:00
SValueNode* pValue = (SValueNode*)pNew;
*pQualified = pValue->datum.b;
2022-06-21 12:53:19 +00:00
nodesDestroyNode(pNew);
return TSDB_CODE_SUCCESS;
2022-06-21 12:53:19 +00:00
}
2022-08-12 13:57:29 +00:00
static EDealRes getColumn(SNode** pNode, void* pContext) {
2024-07-21 10:20:30 +00:00
tagFilterAssist* pData = (tagFilterAssist*)pContext;
2024-08-06 08:14:15 +00:00
SColumnNode* pSColumnNode = NULL;
2022-08-12 13:57:29 +00:00
if (QUERY_NODE_COLUMN == nodeType((*pNode))) {
pSColumnNode = *(SColumnNode**)pNode;
2022-08-25 03:01:36 +00:00
} else if (QUERY_NODE_FUNCTION == nodeType((*pNode))) {
2022-08-12 13:57:29 +00:00
SFunctionNode* pFuncNode = *(SFunctionNode**)(pNode);
if (pFuncNode->funcType == FUNCTION_TYPE_TBNAME) {
2024-07-21 10:20:30 +00:00
pData->code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pSColumnNode);
2022-08-12 13:57:29 +00:00
if (NULL == pSColumnNode) {
return DEAL_RES_ERROR;
}
pSColumnNode->colId = -1;
pSColumnNode->colType = COLUMN_TYPE_TBNAME;
pSColumnNode->node.resType.type = TSDB_DATA_TYPE_VARCHAR;
pSColumnNode->node.resType.bytes = TSDB_TABLE_FNAME_LEN - 1 + VARSTR_HEADER_SIZE;
nodesDestroyNode(*pNode);
*pNode = (SNode*)pSColumnNode;
2022-08-25 03:01:36 +00:00
} else {
return DEAL_RES_CONTINUE;
2022-08-10 12:18:54 +00:00
}
2022-08-25 03:01:36 +00:00
} else {
2022-08-12 13:57:29 +00:00
return DEAL_RES_CONTINUE;
2022-08-10 12:18:54 +00:00
}
2022-08-12 13:57:29 +00:00
2024-08-06 08:14:15 +00:00
void* data = taosHashGet(pData->colHash, &pSColumnNode->colId, sizeof(pSColumnNode->colId));
2022-08-25 03:01:36 +00:00
if (!data) {
2024-07-23 10:32:03 +00:00
int32_t tempRes =
2024-07-23 02:50:16 +00:00
taosHashPut(pData->colHash, &pSColumnNode->colId, sizeof(pSColumnNode->colId), pNode, sizeof((*pNode)));
2024-07-23 10:32:03 +00:00
if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
2024-07-23 02:50:16 +00:00
return DEAL_RES_ERROR;
}
2022-08-12 13:57:29 +00:00
pSColumnNode->slotId = pData->index++;
2022-08-25 03:01:36 +00:00
SColumnInfo cInfo = {.colId = pSColumnNode->colId,
.type = pSColumnNode->node.resType.type,
2024-03-21 01:37:46 +00:00
.bytes = pSColumnNode->node.resType.bytes,
.pk = pSColumnNode->isPk};
#if TAG_FILTER_DEBUG
qDebug("tagfilter build column info, slotId:%d, colId:%d, type:%d", pSColumnNode->slotId, cInfo.colId, cInfo.type);
#endif
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pData->cInfoList, &cInfo);
2024-07-23 06:19:04 +00:00
if (!tmp) {
2024-07-23 02:50:16 +00:00
return DEAL_RES_ERROR;
}
2022-08-25 03:01:36 +00:00
} else {
SColumnNode* col = *(SColumnNode**)data;
pSColumnNode->slotId = col->slotId;
2022-08-12 13:57:29 +00:00
}
2022-08-10 12:18:54 +00:00
return DEAL_RES_CONTINUE;
}
static int32_t createResultData(SDataType* pType, int32_t numOfRows, SScalarParam* pParam) {
SColumnInfoData* pColumnData = taosMemoryCalloc(1, sizeof(SColumnInfoData));
if (pColumnData == NULL) {
return terrno;
}
2022-08-25 03:01:36 +00:00
pColumnData->info.type = pType->type;
pColumnData->info.bytes = pType->bytes;
pColumnData->info.scale = pType->scale;
2022-08-10 12:18:54 +00:00
pColumnData->info.precision = pType->precision;
2022-11-04 10:46:48 +00:00
int32_t code = colInfoDataEnsureCapacity(pColumnData, numOfRows, true);
2022-08-10 12:18:54 +00:00
if (code != TSDB_CODE_SUCCESS) {
2022-08-24 02:24:19 +00:00
terrno = code;
releaseColInfoData(pColumnData);
2022-08-10 12:18:54 +00:00
return terrno;
}
pParam->columnData = pColumnData;
pParam->colAlloced = true;
return TSDB_CODE_SUCCESS;
}
2022-08-25 03:01:36 +00:00
static void releaseColInfoData(void* pCol) {
if (pCol) {
SColumnInfoData* col = (SColumnInfoData*)pCol;
colDataDestroy(col);
taosMemoryFree(col);
}
}
2023-02-10 10:28:35 +00:00
void freeItem(void* p) {
2023-02-20 09:21:37 +00:00
STUidTagInfo* pInfo = p;
2023-02-10 10:28:35 +00:00
if (pInfo->pTagVal != NULL) {
taosMemoryFree(pInfo->pTagVal);
}
}
2024-07-23 02:50:16 +00:00
static int32_t genTagFilterDigest(const SNode* pTagCond, T_MD5_CTX* pContext) {
2023-04-17 07:14:06 +00:00
if (pTagCond == NULL) {
2024-07-23 02:50:16 +00:00
return TSDB_CODE_SUCCESS;
2023-04-17 07:14:06 +00:00
}
char* payload = NULL;
int32_t len = 0;
2024-07-23 02:50:16 +00:00
int32_t code = nodesNodeToMsg(pTagCond, &payload, &len);
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
return code;
}
2023-04-17 07:14:06 +00:00
tMD5Init(pContext);
tMD5Update(pContext, (uint8_t*)payload, (uint32_t)len);
tMD5Final(pContext);
taosMemoryFree(payload);
2024-07-23 02:50:16 +00:00
return TSDB_CODE_SUCCESS;
2023-04-17 07:14:06 +00:00
}
2024-07-23 02:50:16 +00:00
static int32_t genTbGroupDigest(const SNode* pGroup, uint8_t* filterDigest, T_MD5_CTX* pContext) {
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
2023-05-09 09:04:20 +00:00
char* payload = NULL;
int32_t len = 0;
2024-07-23 02:50:16 +00:00
code = nodesNodeToMsg(pGroup, &payload, &len);
QUERY_CHECK_CODE(code, lino, _end);
2023-05-09 09:04:20 +00:00
if (filterDigest[0]) {
payload = taosMemoryRealloc(payload, len + tListLen(pContext->digest));
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(payload, code, lino, _end, terrno);
2023-05-09 09:04:20 +00:00
memcpy(payload + len, filterDigest + 1, tListLen(pContext->digest));
len += tListLen(pContext->digest);
}
tMD5Init(pContext);
tMD5Update(pContext, (uint8_t*)payload, (uint32_t)len);
tMD5Final(pContext);
2024-07-23 02:50:16 +00:00
_end:
2023-05-09 09:04:20 +00:00
taosMemoryFree(payload);
2024-07-23 02:50:16 +00:00
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
}
return code;
2023-05-09 09:04:20 +00:00
}
int32_t qGetColumnsFromNodeList(void* data, bool isList, SArray** pColList) {
int32_t code = TSDB_CODE_SUCCESS;
2022-08-25 03:01:36 +00:00
tagFilterAssist ctx = {0};
ctx.colHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
if (ctx.colHash == NULL) {
2024-09-20 05:23:44 +00:00
code = terrno;
2022-08-25 03:01:36 +00:00
goto end;
}
2022-08-25 03:01:36 +00:00
ctx.index = 0;
ctx.cInfoList = taosArrayInit(4, sizeof(SColumnInfo));
if (ctx.cInfoList == NULL) {
2024-09-20 05:23:44 +00:00
code = terrno;
2022-08-25 03:01:36 +00:00
goto end;
}
if (isList) {
SNode* pNode = NULL;
FOREACH(pNode, (SNodeList*)data) {
nodesRewriteExprPostOrder(&pNode, getColumn, (void*)&ctx);
if (TSDB_CODE_SUCCESS != ctx.code) {
code = ctx.code;
goto end;
}
REPLACE_NODE(pNode);
}
} else {
SNode* pNode = (SNode*)data;
2022-08-25 03:01:36 +00:00
nodesRewriteExprPostOrder(&pNode, getColumn, (void*)&ctx);
2024-07-21 10:20:30 +00:00
if (TSDB_CODE_SUCCESS != ctx.code) {
code = ctx.code;
goto end;
}
2022-08-25 03:01:36 +00:00
}
if (pColList != NULL) *pColList = ctx.cInfoList;
ctx.cInfoList = NULL;
end:
taosHashCleanup(ctx.colHash);
taosArrayDestroy(ctx.cInfoList);
return code;
}
static int32_t buildGroupInfo(SColumnInfoData* pValue, int32_t i, SArray* gInfo) {
int32_t code = TSDB_CODE_SUCCESS;
SStreamGroupValue* v = taosArrayReserve(gInfo, 1);
if (v == NULL) {
code = terrno;
goto end;
}
if (colDataIsNull_s(pValue, i)) {
v->isNull = true;
} else {
v->isNull = false;
char* data = colDataGetData(pValue, i);
if (pValue->info.type == TSDB_DATA_TYPE_JSON) {
if (tTagIsJson(data)) {
code = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
goto end;
}
if (tTagIsJsonNull(data)) {
v->isNull = true;
goto end;
}
int32_t len = getJsonValueLen(data);
v->data.type = pValue->info.type;
v->data.nData = len;
2025-05-16 01:23:52 +00:00
v->data.pData = taosMemoryCalloc(1, len + 1);
2025-05-27 01:28:17 +00:00
if (v->data.pData == NULL) {
code = terrno;
goto end;
}
memcpy(v->data.pData, data, len);
2025-05-16 01:23:52 +00:00
qDebug("buildGroupInfo:%d add json data len:%d, data:%s", i, len, (char*)v->data.pData);
} else if (IS_VAR_DATA_TYPE(pValue->info.type)) {
if (varDataTLen(data) > pValue->info.bytes) {
code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
goto end;
}
v->data.type = pValue->info.type;
v->data.nData = varDataLen(data);
2025-05-16 01:23:52 +00:00
v->data.pData = taosMemoryCalloc(1, varDataLen(data) + 1);
2025-05-27 01:28:17 +00:00
if (v->data.pData == NULL) {
code = terrno;
goto end;
}
memcpy(v->data.pData, varDataVal(data), varDataLen(data));
2025-05-16 01:23:52 +00:00
qDebug("buildGroupInfo:%d add var data type:%d, len:%d, data:%s", i, pValue->info.type, varDataLen(data), (char*)v->data.pData);
2025-05-27 01:28:17 +00:00
} else if (pValue->info.type == TSDB_DATA_TYPE_DECIMAL) { // reader todo decimal
v->data.type = pValue->info.type;
v->data.nData = pValue->info.bytes;
v->data.pData = taosMemoryCalloc(1, pValue->info.bytes);
if (v->data.pData == NULL) {
code = terrno;
goto end;
}
memcpy(&v->data.pData, data, pValue->info.bytes);
qDebug("buildGroupInfo:%d add data type:%d, data:%"PRId64, i, pValue->info.type, v->data.val);
} else { // reader todo decimal
v->data.type = pValue->info.type;
memcpy(&v->data.val, data, pValue->info.bytes);
2025-05-16 01:23:52 +00:00
qDebug("buildGroupInfo:%d add data type:%d, data:%"PRId64, i, pValue->info.type, v->data.val);
}
}
end:
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
v->isNull = true;
}
return code;
}
static void getColInfoResultForGroupbyForStream(void* pVnode, SNodeList* group, STableListInfo* pTableListInfo,
SStorageAPI* pAPI, SHashObj* groupIdMap) {
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
SArray* pBlockList = NULL;
SSDataBlock* pResBlock = NULL;
SArray* groupData = NULL;
SArray* pUidTagList = NULL;
SArray* gInfo = NULL;
int32_t tbNameIndex = 0;
int32_t rows = taosArrayGetSize(pTableListInfo->pTableList);
if (rows == 0) {
return;
}
pUidTagList = taosArrayInit(8, sizeof(STUidTagInfo));
QUERY_CHECK_NULL(pUidTagList, code, lino, end, terrno);
for (int32_t i = 0; i < rows; ++i) {
STableKeyInfo* pkeyInfo = taosArrayGet(pTableListInfo->pTableList, i);
QUERY_CHECK_NULL(pkeyInfo, code, lino, end, terrno);
STUidTagInfo info = {.uid = pkeyInfo->uid};
void* tmp = taosArrayPush(pUidTagList, &info);
QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
}
code = pAPI->metaFn.getTableTags(pVnode, pTableListInfo->idInfo.suid, pUidTagList);
if (code != TSDB_CODE_SUCCESS) {
goto end;
}
SArray* pColList = NULL;
code = qGetColumnsFromNodeList(group, true, &pColList);
if (code != TSDB_CODE_SUCCESS) {
goto end;
}
for (int32_t i = 0; i < taosArrayGetSize(pColList); ++i) {
SColumnInfo* tmp = (SColumnInfo*)taosArrayGet(pColList, i);
if (tmp != NULL && tmp->colId == -1) {
tbNameIndex = i;
}
}
int32_t numOfTables = taosArrayGetSize(pUidTagList);
pResBlock = createTagValBlockForFilter(pColList, numOfTables, pUidTagList, pVnode, pAPI);
taosArrayDestroy(pColList);
if (pResBlock == NULL) {
code = terrno;
goto end;
}
pBlockList = taosArrayInit(2, POINTER_BYTES);
QUERY_CHECK_NULL(pBlockList, code, lino, end, terrno);
void* tmp = taosArrayPush(pBlockList, &pResBlock);
QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
groupData = taosArrayInit(2, POINTER_BYTES);
QUERY_CHECK_NULL(groupData, code, lino, end, terrno);
SNode* pNode = NULL;
FOREACH(pNode, group) {
SScalarParam output = {0};
switch (nodeType(pNode)) {
case QUERY_NODE_VALUE:
break;
case QUERY_NODE_COLUMN:
case QUERY_NODE_OPERATOR:
case QUERY_NODE_FUNCTION: {
SExprNode* expNode = (SExprNode*)pNode;
code = createResultData(&expNode->resType, rows, &output);
if (code != TSDB_CODE_SUCCESS) {
goto end;
}
break;
}
default:
code = TSDB_CODE_OPS_NOT_SUPPORT;
goto end;
}
if (nodeType(pNode) == QUERY_NODE_COLUMN) {
SColumnNode* pSColumnNode = (SColumnNode*)pNode;
SColumnInfoData* pColInfo = (SColumnInfoData*)taosArrayGet(pResBlock->pDataBlock, pSColumnNode->slotId);
QUERY_CHECK_NULL(pColInfo, code, lino, end, terrno);
code = colDataAssign(output.columnData, pColInfo, rows, NULL);
} else if (nodeType(pNode) == QUERY_NODE_VALUE) {
continue;
} else {
code = scalarCalculate(pNode, pBlockList, &output, NULL, NULL);
}
if (code != TSDB_CODE_SUCCESS) {
releaseColInfoData(output.columnData);
goto end;
}
void* tmp = taosArrayPush(groupData, &output.columnData);
QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
}
for (int i = 0; i < rows; i++) {
gInfo = taosArrayInit(rows, sizeof(SStreamGroupValue));
QUERY_CHECK_NULL(gInfo, code, lino, end, terrno);
STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
QUERY_CHECK_NULL(info, code, lino, end, terrno);
for (int j = 0; j < taosArrayGetSize(groupData); j++) {
SColumnInfoData* pValue = (SColumnInfoData*)taosArrayGetP(groupData, j);
int32_t ret = buildGroupInfo(pValue, i, gInfo);
if (ret != TSDB_CODE_SUCCESS) {
qError("buildGroupInfo failed at line %d since %s", __LINE__, tstrerror(ret));
goto end;
}
if (j == tbNameIndex) {
SStreamGroupValue* v = taosArrayGetLast(gInfo);
if (v != NULL){
v->isTbname = true;
v->uid = info->uid;
}
}
}
int32_t ret = taosHashPut(groupIdMap, &info->uid, sizeof(info->uid), &gInfo, POINTER_BYTES);
if (ret != TSDB_CODE_SUCCESS) {
qError("put groupid to map failed at line %d since %s", __LINE__, tstrerror(ret));
goto end;
}
qDebug("put groupid to map gid:%" PRIu64, info->uid);
gInfo = NULL;
}
end:
blockDataDestroy(pResBlock);
taosArrayDestroy(pBlockList);
taosArrayDestroyEx(pUidTagList, freeItem);
taosArrayDestroyP(groupData, releaseColInfoData);
taosArrayDestroyEx(gInfo, tDestroySStreamGroupValue);
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
}
}
int32_t getColInfoResultForGroupby(void* pVnode, SNodeList* group, STableListInfo* pTableListInfo, uint8_t* digest,
SStorageAPI* pAPI, bool initRemainGroups, SHashObj* groupIdMap) {
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
SArray* pBlockList = NULL;
SSDataBlock* pResBlock = NULL;
void* keyBuf = NULL;
SArray* groupData = NULL;
SArray* pUidTagList = NULL;
SArray* tableList = NULL;
SArray* gInfo = NULL;
int32_t rows = taosArrayGetSize(pTableListInfo->pTableList);
if (rows == 0) {
return TSDB_CODE_SUCCESS;
}
2022-08-25 03:01:36 +00:00
2023-04-17 07:14:06 +00:00
T_MD5_CTX context = {0};
2023-05-09 09:04:20 +00:00
if (tsTagFilterCache) {
2024-07-21 10:20:30 +00:00
SNodeListNode* listNode = NULL;
code = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&listNode);
if (TSDB_CODE_SUCCESS != code) {
goto end;
}
2023-05-09 09:04:20 +00:00
listNode->pNodeList = group;
2024-07-23 02:50:16 +00:00
code = genTbGroupDigest((SNode*)listNode, digest, &context);
QUERY_CHECK_CODE(code, lino, end);
2023-05-09 09:04:20 +00:00
nodesFree(listNode);
2024-07-23 02:50:16 +00:00
code = pAPI->metaFn.metaGetCachedTbGroup(pVnode, pTableListInfo->idInfo.suid, context.digest,
tListLen(context.digest), &tableList);
QUERY_CHECK_CODE(code, lino, end);
2023-05-09 09:04:20 +00:00
if (tableList) {
taosArrayDestroy(pTableListInfo->pTableList);
pTableListInfo->pTableList = tableList;
2023-05-23 03:46:02 +00:00
qDebug("retrieve tb group list from cache, numOfTables:%d",
(int32_t)taosArrayGetSize(pTableListInfo->pTableList));
2023-05-09 09:04:20 +00:00
goto end;
}
2023-04-17 07:14:06 +00:00
}
2023-05-23 03:46:02 +00:00
2023-04-17 07:50:25 +00:00
pUidTagList = taosArrayInit(8, sizeof(STUidTagInfo));
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pUidTagList, code, lino, end, terrno);
2024-07-23 02:50:16 +00:00
2022-08-25 03:01:36 +00:00
for (int32_t i = 0; i < rows; ++i) {
STableKeyInfo* pkeyInfo = taosArrayGet(pTableListInfo->pTableList, i);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(pkeyInfo, code, lino, end, terrno);
2025-03-28 10:10:57 +00:00
STUidTagInfo info = {.uid = pkeyInfo->uid};
void* tmp = taosArrayPush(pUidTagList, &info);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
2022-08-25 03:01:36 +00:00
}
code = pAPI->metaFn.getTableTags(pVnode, pTableListInfo->idInfo.suid, pUidTagList);
2022-08-25 03:01:36 +00:00
if (code != TSDB_CODE_SUCCESS) {
goto end;
}
SArray* pColList = NULL;
code = qGetColumnsFromNodeList(group, true, &pColList);
if (code != TSDB_CODE_SUCCESS) {
goto end;
}
int32_t numOfTables = taosArrayGetSize(pUidTagList);
pResBlock = createTagValBlockForFilter(pColList, numOfTables, pUidTagList, pVnode, pAPI);
taosArrayDestroy(pColList);
2023-02-10 10:28:35 +00:00
if (pResBlock == NULL) {
code = terrno;
2022-08-25 03:01:36 +00:00
goto end;
}
// int64_t st1 = taosGetTimestampUs();
// qDebug("generate tag block rows:%d, cost:%ld us", rows, st1-st);
pBlockList = taosArrayInit(2, POINTER_BYTES);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pBlockList, code, lino, end, terrno);
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pBlockList, &pResBlock);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
2022-08-25 03:01:36 +00:00
groupData = taosArrayInit(2, POINTER_BYTES);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(groupData, code, lino, end, terrno);
2024-07-23 02:50:16 +00:00
SNode* pNode = NULL;
2022-08-25 03:01:36 +00:00
FOREACH(pNode, group) {
SScalarParam output = {0};
switch (nodeType(pNode)) {
case QUERY_NODE_VALUE:
break;
case QUERY_NODE_COLUMN:
case QUERY_NODE_OPERATOR:
case QUERY_NODE_FUNCTION: {
SExprNode* expNode = (SExprNode*)pNode;
code = createResultData(&expNode->resType, rows, &output);
if (code != TSDB_CODE_SUCCESS) {
goto end;
}
break;
}
2022-08-25 03:01:36 +00:00
default:
code = TSDB_CODE_OPS_NOT_SUPPORT;
goto end;
}
2022-08-25 03:01:36 +00:00
if (nodeType(pNode) == QUERY_NODE_COLUMN) {
SColumnNode* pSColumnNode = (SColumnNode*)pNode;
SColumnInfoData* pColInfo = (SColumnInfoData*)taosArrayGet(pResBlock->pDataBlock, pSColumnNode->slotId);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(pColInfo, code, lino, end, terrno);
2022-08-25 03:01:36 +00:00
code = colDataAssign(output.columnData, pColInfo, rows, NULL);
} else if (nodeType(pNode) == QUERY_NODE_VALUE) {
continue;
} else {
code = scalarCalculate(pNode, pBlockList, &output, NULL, NULL);
2022-08-25 03:01:36 +00:00
}
2022-08-25 03:01:36 +00:00
if (code != TSDB_CODE_SUCCESS) {
releaseColInfoData(output.columnData);
goto end;
}
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(groupData, &output.columnData);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
2022-08-25 03:01:36 +00:00
}
int32_t keyLen = 0;
SNode* node;
FOREACH(node, group) {
SExprNode* pExpr = (SExprNode*)node;
keyLen += pExpr->resType.bytes;
}
int32_t nullFlagSize = sizeof(int8_t) * LIST_LENGTH(group);
keyLen += nullFlagSize;
keyBuf = taosMemoryCalloc(1, keyLen);
if (keyBuf == NULL) {
code = terrno;
2022-08-25 03:01:36 +00:00
goto end;
}
2024-01-02 07:45:03 +00:00
if (initRemainGroups) {
pTableListInfo->remainGroups =
taosHashInit(rows, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
if (pTableListInfo->remainGroups == NULL) {
2024-09-20 05:23:44 +00:00
code = terrno;
2024-01-02 07:45:03 +00:00
goto end;
}
}
2022-08-25 03:01:36 +00:00
for (int i = 0; i < rows; i++) {
STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(info, code, lino, end, terrno);
2022-08-25 03:01:36 +00:00
if (groupIdMap != NULL){
gInfo = taosArrayInit(rows, sizeof(SStreamGroupValue));
}
2022-08-25 03:01:36 +00:00
char* isNull = (char*)keyBuf;
char* pStart = (char*)keyBuf + sizeof(int8_t) * LIST_LENGTH(group);
for (int j = 0; j < taosArrayGetSize(groupData); j++) {
SColumnInfoData* pValue = (SColumnInfoData*)taosArrayGetP(groupData, j);
if (groupIdMap != NULL && gInfo != NULL) {
int32_t ret = buildGroupInfo(pValue, i, gInfo);
if (ret != TSDB_CODE_SUCCESS) {
qError("buildGroupInfo failed at line %d since %s", __LINE__, tstrerror(ret));
taosArrayDestroyEx(gInfo, tDestroySStreamGroupValue);
gInfo = NULL;
}
}
2022-08-25 03:01:36 +00:00
if (colDataIsNull_s(pValue, i)) {
isNull[j] = 1;
} else {
isNull[j] = 0;
char* data = colDataGetData(pValue, i);
if (pValue->info.type == TSDB_DATA_TYPE_JSON) {
if (tTagIsJson(data)) {
code = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
goto end;
}
if (tTagIsJsonNull(data)) {
isNull[j] = 1;
continue;
}
int32_t len = getJsonValueLen(data);
memcpy(pStart, data, len);
pStart += len;
} else if (IS_VAR_DATA_TYPE(pValue->info.type)) {
2023-03-29 11:10:10 +00:00
if (varDataTLen(data) > pValue->info.bytes) {
code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
goto end;
}
2022-08-25 03:01:36 +00:00
memcpy(pStart, data, varDataTLen(data));
pStart += varDataTLen(data);
} else {
memcpy(pStart, data, pValue->info.bytes);
pStart += pValue->info.bytes;
}
}
}
int32_t len = (int32_t)(pStart - (char*)keyBuf);
info->groupId = calcGroupId(keyBuf, len);
if (groupIdMap != NULL && gInfo != NULL) {
int32_t ret = taosHashPut(groupIdMap, &info->groupId, sizeof(info->groupId), &gInfo, POINTER_BYTES);
if (ret != TSDB_CODE_SUCCESS) {
2025-05-17 03:41:46 +00:00
qError("put groupid to map failed at line %d since %s", __LINE__, tstrerror(ret));
taosArrayDestroyEx(gInfo, tDestroySStreamGroupValue);
}
2025-05-17 03:41:46 +00:00
qDebug("put groupid to map gid:%" PRIu64, info->groupId);
gInfo = NULL;
}
2024-01-02 07:45:03 +00:00
if (initRemainGroups) {
// groupId ~ table uid
2024-07-23 02:50:16 +00:00
code = taosHashPut(pTableListInfo->remainGroups, &(info->groupId), sizeof(info->groupId), &(info->uid),
sizeof(info->uid));
2024-07-23 10:32:03 +00:00
if (code == TSDB_CODE_DUP_KEY) {
code = TSDB_CODE_SUCCESS;
2024-07-23 06:19:04 +00:00
}
2024-07-23 10:32:03 +00:00
QUERY_CHECK_CODE(code, lino, end);
2024-01-02 07:45:03 +00:00
}
}
2023-05-09 09:04:20 +00:00
if (tsTagFilterCache) {
tableList = taosArrayDup(pTableListInfo->pTableList, NULL);
2024-08-06 06:02:58 +00:00
QUERY_CHECK_NULL(tableList, code, lino, end, terrno);
2024-07-23 02:50:16 +00:00
code = pAPI->metaFn.metaPutTbGroupToCache(pVnode, pTableListInfo->idInfo.suid, context.digest,
tListLen(context.digest), tableList,
taosArrayGetSize(tableList) * sizeof(STableKeyInfo));
QUERY_CHECK_CODE(code, lino, end);
2023-04-17 08:33:55 +00:00
}
2023-05-23 03:46:02 +00:00
2022-08-25 03:01:36 +00:00
// int64_t st2 = taosGetTimestampUs();
// qDebug("calculate tag block rows:%d, cost:%ld us", rows, st2-st1);
end:
taosMemoryFreeClear(keyBuf);
blockDataDestroy(pResBlock);
taosArrayDestroy(pBlockList);
2023-02-10 10:28:35 +00:00
taosArrayDestroyEx(pUidTagList, freeItem);
2022-08-25 03:01:36 +00:00
taosArrayDestroyP(groupData, releaseColInfoData);
taosArrayDestroyEx(gInfo, tDestroySStreamGroupValue);
2024-07-23 10:32:03 +00:00
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
}
2022-08-25 03:01:36 +00:00
return code;
}
2022-10-29 19:01:40 +00:00
static int32_t nameComparFn(const void* p1, const void* p2) {
const char* pName1 = *(const char**)p1;
const char* pName2 = *(const char**)p2;
int32_t ret = strcmp(pName1, pName2);
if (ret == 0) {
return 0;
} else {
return (ret > 0) ? 1 : -1;
}
}
static SArray* getTableNameList(const SNodeListNode* pList) {
2024-07-23 02:50:16 +00:00
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
2022-10-29 19:01:40 +00:00
int32_t len = LIST_LENGTH(pList->pNodeList);
SListCell* cell = pList->pNodeList->pHead;
SArray* pTbList = taosArrayInit(len, POINTER_BYTES);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pTbList, code, lino, _end, terrno);
2024-07-23 02:50:16 +00:00
2022-10-29 19:01:40 +00:00
for (int i = 0; i < pList->pNodeList->length; i++) {
SValueNode* valueNode = (SValueNode*)cell->pNode;
if (!IS_VAR_DATA_TYPE(valueNode->node.resType.type)) {
terrno = TSDB_CODE_INVALID_PARA;
taosArrayDestroy(pTbList);
return NULL;
}
char* name = varDataVal(valueNode->datum.p);
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pTbList, &name);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2022-10-29 19:01:40 +00:00
cell = cell->pNext;
}
size_t numOfTables = taosArrayGetSize(pTbList);
// order the name
taosArraySort(pTbList, nameComparFn);
// remove the duplicates
SArray* pNewList = taosArrayInit(taosArrayGetSize(pTbList), sizeof(void*));
2024-08-05 04:22:25 +00:00
QUERY_CHECK_NULL(pNewList, code, lino, _end, terrno);
2025-03-28 10:10:57 +00:00
void* tmpTbl = taosArrayGet(pTbList, 0);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(tmpTbl, code, lino, _end, terrno);
2025-03-28 10:10:57 +00:00
void* tmp = taosArrayPush(pNewList, tmpTbl);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2022-10-29 19:01:40 +00:00
for (int32_t i = 1; i < numOfTables; ++i) {
char** name = taosArrayGetLast(pNewList);
char** nameInOldList = taosArrayGet(pTbList, i);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(nameInOldList, code, lino, _end, terrno);
2022-10-29 19:01:40 +00:00
if (strcmp(*name, *nameInOldList) == 0) {
continue;
}
2024-07-23 02:50:16 +00:00
tmp = taosArrayPush(pNewList, nameInOldList);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2022-10-29 19:01:40 +00:00
}
2024-07-23 02:50:16 +00:00
_end:
2022-10-29 19:01:40 +00:00
taosArrayDestroy(pTbList);
2024-07-23 02:50:16 +00:00
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
return NULL;
}
2022-10-29 19:01:40 +00:00
return pNewList;
}
2022-09-29 10:35:12 +00:00
static int tableUidCompare(const void* a, const void* b) {
2022-10-29 19:01:40 +00:00
uint64_t u1 = *(uint64_t*)a;
uint64_t u2 = *(uint64_t*)b;
2022-09-29 10:35:12 +00:00
if (u1 == u2) {
return 0;
}
2022-10-29 19:01:40 +00:00
2022-09-29 10:35:12 +00:00
return u1 < u2 ? -1 : 1;
}
2022-10-29 19:01:40 +00:00
2023-02-02 02:11:36 +00:00
static int32_t filterTableInfoCompare(const void* a, const void* b) {
2023-02-20 09:21:37 +00:00
STUidTagInfo* p1 = (STUidTagInfo*)a;
STUidTagInfo* p2 = (STUidTagInfo*)b;
2023-02-02 02:11:36 +00:00
if (p1->uid == p2->uid) {
return 0;
}
2023-02-20 09:21:37 +00:00
return p1->uid < p2->uid ? -1 : 1;
}
2023-01-31 14:43:59 +00:00
static FilterCondType checkTagCond(SNode* cond) {
if (nodeType(cond) == QUERY_NODE_OPERATOR) {
return FILTER_NO_LOGIC;
}
if (nodeType(cond) != QUERY_NODE_LOGIC_CONDITION || ((SLogicConditionNode*)cond)->condType != LOGIC_COND_TYPE_AND) {
return FILTER_AND;
}
return FILTER_OTHER;
2023-02-02 02:11:36 +00:00
}
static int32_t optimizeTbnameInCond(void* pVnode, int64_t suid, SArray* list, SNode* cond, SStorageAPI* pAPI) {
2022-10-08 07:48:52 +00:00
int32_t ret = -1;
2023-02-02 02:17:39 +00:00
int32_t ntype = nodeType(cond);
if (ntype == QUERY_NODE_OPERATOR) {
ret = optimizeTbnameInCondImpl(pVnode, list, cond, pAPI, suid);
2022-09-29 10:35:12 +00:00
}
2023-02-02 02:17:39 +00:00
if (ntype != QUERY_NODE_LOGIC_CONDITION || ((SLogicConditionNode*)cond)->condType != LOGIC_COND_TYPE_AND) {
2022-10-08 07:48:52 +00:00
return ret;
2022-09-29 10:35:12 +00:00
}
2022-09-29 12:30:32 +00:00
bool hasTbnameCond = false;
2022-09-29 10:35:12 +00:00
SLogicConditionNode* pNode = (SLogicConditionNode*)cond;
2022-09-29 13:35:14 +00:00
SNodeList* pList = (SNodeList*)pNode->pParameterList;
2022-09-29 10:35:12 +00:00
2022-09-29 13:35:14 +00:00
int32_t len = LIST_LENGTH(pList);
2022-10-29 19:01:40 +00:00
if (len <= 0) {
return ret;
}
2022-09-29 10:35:12 +00:00
2022-09-29 13:35:14 +00:00
SListCell* cell = pList->pHead;
2022-09-29 10:35:12 +00:00
for (int i = 0; i < len; i++) {
2022-09-29 12:30:32 +00:00
if (cell == NULL) break;
if (optimizeTbnameInCondImpl(pVnode, list, cell->pNode, pAPI, suid) == 0) {
2022-09-29 10:35:12 +00:00
hasTbnameCond = true;
2022-10-08 07:48:52 +00:00
break;
2022-09-29 10:35:12 +00:00
}
cell = cell->pNext;
}
2022-10-29 19:01:40 +00:00
2023-02-20 09:21:37 +00:00
taosArraySort(list, filterTableInfoCompare);
taosArrayRemoveDuplicate(list, filterTableInfoCompare, NULL);
2022-09-29 10:35:12 +00:00
2022-10-08 07:48:52 +00:00
if (hasTbnameCond) {
ret = pAPI->metaFn.getTableTagsByUid(pVnode, suid, list);
2022-10-08 07:48:52 +00:00
}
2022-10-29 19:01:40 +00:00
2022-10-08 07:48:52 +00:00
return ret;
}
2023-01-14 15:17:57 +00:00
// only return uid that does not contained in pExistedUidList
2025-03-28 10:10:57 +00:00
static int32_t optimizeTbnameInCondImpl(void* pVnode, SArray* pExistedUidList, SNode* pTagCond, SStorageAPI* pStoreAPI,
uint64_t suid) {
2022-09-28 14:07:16 +00:00
if (nodeType(pTagCond) != QUERY_NODE_OPERATOR) {
return -1;
}
2022-09-28 14:07:16 +00:00
SOperatorNode* pNode = (SOperatorNode*)pTagCond;
if (pNode->opType != OP_TYPE_IN) {
return -1;
}
2022-09-28 14:07:16 +00:00
if ((pNode->pLeft != NULL && nodeType(pNode->pLeft) == QUERY_NODE_COLUMN &&
((SColumnNode*)pNode->pLeft)->colType == COLUMN_TYPE_TBNAME) &&
(pNode->pRight != NULL && nodeType(pNode->pRight) == QUERY_NODE_NODE_LIST)) {
SNodeListNode* pList = (SNodeListNode*)pNode->pRight;
int32_t len = LIST_LENGTH(pList->pNodeList);
2022-10-29 19:01:40 +00:00
if (len <= 0) {
return -1;
2022-09-28 14:07:16 +00:00
}
2022-12-01 12:34:06 +00:00
SArray* pTbList = getTableNameList(pList);
int32_t numOfTables = taosArrayGetSize(pTbList);
SHashObj* uHash = NULL;
2023-02-02 02:11:36 +00:00
2023-02-20 09:21:37 +00:00
size_t numOfExisted = taosArrayGetSize(pExistedUidList); // len > 0 means there already have uids
2023-01-14 15:17:57 +00:00
if (numOfExisted > 0) {
uHash = taosHashInit(numOfExisted / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
2024-07-23 02:50:16 +00:00
if (!uHash) {
2024-12-11 11:18:50 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2024-09-20 05:23:44 +00:00
return terrno;
2024-07-23 02:50:16 +00:00
}
2023-01-14 15:17:57 +00:00
for (int i = 0; i < numOfExisted; i++) {
STUidTagInfo* pTInfo = taosArrayGet(pExistedUidList, i);
2024-08-05 08:09:01 +00:00
if (!pTInfo) {
2024-08-05 10:18:13 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2024-08-05 08:09:01 +00:00
return terrno;
}
2025-03-28 10:10:57 +00:00
int32_t tempRes = taosHashPut(uHash, &pTInfo->uid, sizeof(uint64_t), &i, sizeof(i));
2024-07-23 10:32:03 +00:00
if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(tempRes));
return tempRes;
2024-07-23 02:50:16 +00:00
}
2022-11-03 02:09:36 +00:00
}
}
2022-09-28 14:07:16 +00:00
for (int i = 0; i < numOfTables; i++) {
char* name = taosArrayGetP(pTbList, i);
2022-09-28 14:07:16 +00:00
uint64_t uid = 0, csuid = 0;
if (pStoreAPI->metaFn.getTableUidByName(pVnode, name, &uid) == 0) {
2022-09-30 01:31:19 +00:00
ETableType tbType = TSDB_TABLE_MAX;
2025-03-28 10:10:57 +00:00
if (pStoreAPI->metaFn.getTableTypeSuidByName(pVnode, name, &tbType, &csuid) == 0 &&
tbType == TSDB_CHILD_TABLE) {
if (suid != csuid) {
continue;
}
2022-11-03 02:09:36 +00:00
if (NULL == uHash || taosHashGet(uHash, &uid, sizeof(uid)) == NULL) {
STUidTagInfo s = {.uid = uid, .name = name, .pTagVal = NULL};
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pExistedUidList, &s);
if (!tmp) {
2024-09-20 05:23:44 +00:00
return terrno;
2024-07-23 02:50:16 +00:00
}
2022-11-03 02:09:36 +00:00
}
2022-09-30 01:31:19 +00:00
} else {
taosArrayDestroy(pTbList);
2022-11-03 02:09:36 +00:00
taosHashCleanup(uHash);
2022-09-30 01:31:19 +00:00
return -1;
}
2022-09-28 14:07:16 +00:00
} else {
2023-01-31 09:00:19 +00:00
// qWarn("failed to get tableIds from by table name: %s, reason: %s", name, tstrerror(terrno));
2022-09-28 14:07:16 +00:00
terrno = 0;
}
}
2022-11-03 02:09:36 +00:00
taosHashCleanup(uHash);
2022-09-28 14:07:16 +00:00
taosArrayDestroy(pTbList);
2022-09-29 13:53:39 +00:00
return 0;
2022-09-28 14:07:16 +00:00
}
2022-10-29 19:01:40 +00:00
2022-09-29 13:53:39 +00:00
return -1;
2022-09-28 14:07:16 +00:00
}
2022-10-29 19:01:40 +00:00
2023-08-10 09:30:01 +00:00
SSDataBlock* createTagValBlockForFilter(SArray* pColList, int32_t numOfTables, SArray* pUidTagList, void* pVnode,
2024-04-03 09:11:49 +00:00
SStorageAPI* pStorageAPI) {
2024-07-23 02:50:16 +00:00
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
2024-07-27 10:55:34 +00:00
SSDataBlock* pResBlock = NULL;
code = createDataBlock(&pResBlock);
QUERY_CHECK_CODE(code, lino, _end);
for (int32_t i = 0; i < taosArrayGetSize(pColList); ++i) {
SColumnInfoData colInfo = {0};
2025-03-28 10:10:57 +00:00
void* tmp = taosArrayGet(pColList, i);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
colInfo.info = *(SColumnInfo*)tmp;
2024-07-23 02:50:16 +00:00
code = blockDataAppendColInfo(pResBlock, &colInfo);
QUERY_CHECK_CODE(code, lino, _end);
}
2024-07-23 02:50:16 +00:00
code = blockDataEnsureCapacity(pResBlock, numOfTables);
if (code != TSDB_CODE_SUCCESS) {
terrno = code;
2024-08-07 08:59:19 +00:00
blockDataDestroy(pResBlock);
return NULL;
}
pResBlock->info.rows = numOfTables;
int32_t numOfCols = taosArrayGetSize(pResBlock->pDataBlock);
for (int32_t i = 0; i < numOfTables; i++) {
STUidTagInfo* p1 = taosArrayGet(pUidTagList, i);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(p1, code, lino, _end, terrno);
for (int32_t j = 0; j < numOfCols; j++) {
SColumnInfoData* pColInfo = (SColumnInfoData*)taosArrayGet(pResBlock->pDataBlock, j);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(pColInfo, code, lino, _end, terrno);
if (pColInfo->info.colId == -1) { // tbname
char str[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
2023-02-10 10:28:35 +00:00
if (p1->name != NULL) {
STR_TO_VARSTR(str, p1->name);
2023-02-20 09:21:37 +00:00
} else { // name is not retrieved during filter
2024-07-23 02:50:16 +00:00
code = pStorageAPI->metaFn.getTableNameByUid(pVnode, p1->uid, str);
QUERY_CHECK_CODE(code, lino, _end);
2023-02-10 10:28:35 +00:00
}
2024-07-23 02:50:16 +00:00
code = colDataSetVal(pColInfo, i, str, false);
QUERY_CHECK_CODE(code, lino, _end);
#if TAG_FILTER_DEBUG
qDebug("tagfilter uid:%ld, tbname:%s", *uid, str + 2);
#endif
} else {
STagVal tagVal = {0};
tagVal.cid = pColInfo->info.colId;
if (p1->pTagVal == NULL) {
2023-02-20 02:04:08 +00:00
colDataSetNULL(pColInfo, i);
2023-02-28 01:34:16 +00:00
} else {
const char* p = pStorageAPI->metaFn.extractTagVal(p1->pTagVal, pColInfo->info.type, &tagVal);
2023-02-28 01:34:16 +00:00
if (p == NULL || (pColInfo->info.type == TSDB_DATA_TYPE_JSON && ((STag*)p)->nTag == 0)) {
colDataSetNULL(pColInfo, i);
} else if (pColInfo->info.type == TSDB_DATA_TYPE_JSON) {
2024-07-23 02:50:16 +00:00
code = colDataSetVal(pColInfo, i, p, false);
QUERY_CHECK_CODE(code, lino, _end);
2023-02-28 01:34:16 +00:00
} else if (IS_VAR_DATA_TYPE(pColInfo->info.type)) {
char* tmp = taosMemoryMalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2023-02-28 01:34:16 +00:00
varDataSetLen(tmp, tagVal.nData);
memcpy(tmp + VARSTR_HEADER_SIZE, tagVal.pData, tagVal.nData);
2024-07-23 02:50:16 +00:00
code = colDataSetVal(pColInfo, i, tmp, false);
#if TAG_FILTER_DEBUG
2023-02-28 01:34:16 +00:00
qDebug("tagfilter varch:%s", tmp + 2);
#endif
2023-02-28 01:34:16 +00:00
taosMemoryFree(tmp);
2024-09-30 05:58:28 +00:00
QUERY_CHECK_CODE(code, lino, _end);
2023-02-28 01:34:16 +00:00
} else {
2024-07-23 02:50:16 +00:00
code = colDataSetVal(pColInfo, i, (const char*)&tagVal.i64, false);
QUERY_CHECK_CODE(code, lino, _end);
#if TAG_FILTER_DEBUG
2023-02-28 01:34:16 +00:00
if (pColInfo->info.type == TSDB_DATA_TYPE_INT) {
qDebug("tagfilter int:%d", *(int*)(&tagVal.i64));
} else if (pColInfo->info.type == TSDB_DATA_TYPE_DOUBLE) {
qDebug("tagfilter double:%f", *(double*)(&tagVal.i64));
}
#endif
2023-02-28 01:34:16 +00:00
}
}
}
}
}
2024-07-23 02:50:16 +00:00
_end:
if (code != TSDB_CODE_SUCCESS) {
2024-08-07 08:59:19 +00:00
blockDataDestroy(pResBlock);
2024-07-23 02:50:16 +00:00
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
2024-07-23 11:31:43 +00:00
terrno = code;
2024-07-23 02:50:16 +00:00
return NULL;
}
return pResBlock;
}
2024-04-03 09:11:49 +00:00
static int32_t doSetQualifiedUid(STableListInfo* pListInfo, SArray* pUidList, const SArray* pUidTagList,
bool* pResultList, bool addUid) {
taosArrayClear(pUidList);
2023-07-19 11:14:30 +00:00
STableKeyInfo info = {.uid = 0, .groupId = 0};
2024-04-03 09:11:49 +00:00
int32_t numOfTables = taosArrayGetSize(pUidTagList);
2023-02-20 09:21:37 +00:00
for (int32_t i = 0; i < numOfTables; ++i) {
if (pResultList[i]) {
2024-08-05 08:09:01 +00:00
STUidTagInfo* tmpTag = (STUidTagInfo*)taosArrayGet(pUidTagList, i);
if (!tmpTag) {
2024-08-05 10:18:13 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2024-08-05 08:09:01 +00:00
return terrno;
}
uint64_t uid = tmpTag->uid;
2023-07-19 11:14:30 +00:00
qDebug("tagfilter get uid:%" PRId64 ", res:%d", uid, pResultList[i]);
info.uid = uid;
void* p = taosArrayPush(pListInfo->pTableList, &info);
if (p == NULL) {
2024-09-20 05:23:44 +00:00
return terrno;
2023-07-19 11:14:30 +00:00
}
if (addUid) {
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pUidList, &uid);
if (tmp == NULL) {
2024-09-20 05:23:44 +00:00
return terrno;
2024-07-23 02:50:16 +00:00
}
2023-07-19 11:14:30 +00:00
}
}
}
2023-07-19 11:14:30 +00:00
return TSDB_CODE_SUCCESS;
}
2024-07-23 02:50:16 +00:00
static int32_t copyExistedUids(SArray* pUidTagList, const SArray* pUidList) {
int32_t code = TSDB_CODE_SUCCESS;
int32_t numOfExisted = taosArrayGetSize(pUidList);
2023-02-10 15:34:03 +00:00
if (numOfExisted == 0) {
2024-07-23 02:50:16 +00:00
return code;
2023-02-10 15:34:03 +00:00
}
2023-02-20 09:21:37 +00:00
for (int32_t i = 0; i < numOfExisted; ++i) {
2025-03-28 10:10:57 +00:00
uint64_t* uid = taosArrayGet(pUidList, i);
2024-08-05 08:09:01 +00:00
if (!uid) {
2024-08-05 10:18:13 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2024-08-05 08:09:01 +00:00
return terrno;
}
2023-02-10 15:34:03 +00:00
STUidTagInfo info = {.uid = *uid};
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pUidTagList, &info);
if (!tmp) {
2024-12-11 11:18:50 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2024-07-23 02:50:16 +00:00
return code;
}
}
2024-07-23 02:50:16 +00:00
return code;
}
static int32_t doFilterByTagCond(STableListInfo* pListInfo, SArray* pUidList, SNode* pTagCond, void* pVnode,
2023-07-19 11:41:54 +00:00
SIdxFltStatus status, SStorageAPI* pAPI, bool addUid, bool* listAdded) {
*listAdded = false;
2022-11-26 17:59:49 +00:00
if (pTagCond == NULL) {
return TSDB_CODE_SUCCESS;
}
terrno = TSDB_CODE_SUCCESS;
2024-07-23 02:50:16 +00:00
int32_t lino = 0;
int32_t code = TSDB_CODE_SUCCESS;
SArray* pBlockList = NULL;
SSDataBlock* pResBlock = NULL;
SScalarParam output = {0};
SArray* pUidTagList = NULL;
SDataType type = {.type = TSDB_DATA_TYPE_BOOL, .bytes = sizeof(bool)};
2022-11-26 17:59:49 +00:00
// int64_t stt = taosGetTimestampUs();
pUidTagList = taosArrayInit(10, sizeof(STUidTagInfo));
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pUidTagList, code, lino, end, terrno);
2024-07-23 02:50:16 +00:00
code = copyExistedUids(pUidTagList, pUidList);
QUERY_CHECK_CODE(code, lino, end);
2023-02-20 09:21:37 +00:00
FilterCondType condType = checkTagCond(pTagCond);
int32_t filter = optimizeTbnameInCond(pVnode, pListInfo->idInfo.suid, pUidTagList, pTagCond, pAPI);
if (filter == 0) { // tbname in filter is activated, do nothing and return
taosArrayClear(pUidList);
int32_t numOfRows = taosArrayGetSize(pUidTagList);
2024-07-23 02:50:16 +00:00
code = taosArrayEnsureCap(pUidList, numOfRows);
QUERY_CHECK_CODE(code, lino, end);
2023-02-20 09:21:37 +00:00
for (int32_t i = 0; i < numOfRows; ++i) {
STUidTagInfo* pInfo = taosArrayGet(pUidTagList, i);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(pInfo, code, lino, end, terrno);
2025-03-28 10:10:57 +00:00
void* tmp = taosArrayPush(pUidList, &pInfo->uid);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
2022-11-26 17:59:49 +00:00
}
terrno = 0;
} else {
2023-02-20 09:21:37 +00:00
if ((condType == FILTER_NO_LOGIC || condType == FILTER_AND) && status != SFLT_NOT_INDEX) {
code = pAPI->metaFn.getTableTagsByUid(pVnode, pListInfo->idInfo.suid, pUidTagList);
2023-02-20 09:21:37 +00:00
} else {
code = pAPI->metaFn.getTableTags(pVnode, pListInfo->idInfo.suid, pUidTagList);
2023-02-20 09:21:37 +00:00
}
if (code != TSDB_CODE_SUCCESS) {
qError("failed to get table tags from meta, reason:%s, suid:%" PRIu64, tstrerror(code), pListInfo->idInfo.suid);
terrno = code;
2024-07-23 10:32:03 +00:00
QUERY_CHECK_CODE(code, lino, end);
2022-11-26 17:59:49 +00:00
}
}
int32_t numOfTables = taosArrayGetSize(pUidTagList);
if (numOfTables == 0) {
goto end;
2022-11-26 17:59:49 +00:00
}
SArray* pColList = NULL;
code = qGetColumnsFromNodeList(pTagCond, false, &pColList);
if (code != TSDB_CODE_SUCCESS) {
goto end;
}
pResBlock = createTagValBlockForFilter(pColList, numOfTables, pUidTagList, pVnode, pAPI);
taosArrayDestroy(pColList);
2023-02-10 10:28:35 +00:00
if (pResBlock == NULL) {
code = terrno;
2024-07-23 10:32:03 +00:00
QUERY_CHECK_CODE(code, lino, end);
}
// int64_t st1 = taosGetTimestampUs();
// qDebug("generate tag block rows:%d, cost:%ld us", rows, st1-st);
pBlockList = taosArrayInit(2, POINTER_BYTES);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pBlockList, code, lino, end, terrno);
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pBlockList, &pResBlock);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
code = createResultData(&type, numOfTables, &output);
if (code != TSDB_CODE_SUCCESS) {
2023-02-10 11:22:38 +00:00
terrno = code;
2024-07-23 10:32:03 +00:00
QUERY_CHECK_CODE(code, lino, end);
}
code = scalarCalculate(pTagCond, pBlockList, &output, NULL, NULL);
if (code != TSDB_CODE_SUCCESS) {
qError("failed to calculate scalar, reason:%s", tstrerror(code));
terrno = code;
2024-07-23 10:32:03 +00:00
QUERY_CHECK_CODE(code, lino, end);
}
2023-07-19 11:14:30 +00:00
code = doSetQualifiedUid(pListInfo, pUidList, pUidTagList, (bool*)output.columnData->pData, addUid);
if (code != TSDB_CODE_SUCCESS) {
terrno = code;
2024-07-23 10:32:03 +00:00
QUERY_CHECK_CODE(code, lino, end);
2023-07-19 11:14:30 +00:00
}
2023-07-19 11:41:54 +00:00
*listAdded = true;
2023-02-20 09:21:37 +00:00
end:
2024-07-23 02:50:16 +00:00
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
}
blockDataDestroy(pResBlock);
taosArrayDestroy(pBlockList);
2023-02-10 10:28:35 +00:00
taosArrayDestroyEx(pUidTagList, freeItem);
colDataDestroy(output.columnData);
taosMemoryFreeClear(output.columnData);
2023-02-10 10:28:35 +00:00
return code;
2022-11-26 17:59:49 +00:00
}
int32_t getTableList(void* pVnode, SScanPhysiNode* pScanNode, SNode* pTagCond, SNode* pTagIndexCond,
STableListInfo* pListInfo, uint8_t* digest, const char* idstr, SStorageAPI* pStorageAPI) {
int32_t code = TSDB_CODE_SUCCESS;
2024-07-23 02:50:16 +00:00
int32_t lino = 0;
2022-11-26 17:59:49 +00:00
size_t numOfTables = 0;
2023-07-19 11:14:30 +00:00
bool listAdded = false;
pListInfo->idInfo.suid = pScanNode->suid;
pListInfo->idInfo.tableType = pScanNode->tableType;
2023-02-10 15:40:53 +00:00
SArray* pUidList = taosArrayInit(8, sizeof(uint64_t));
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pUidList, code, lino, _error, terrno);
2022-06-25 12:36:38 +00:00
2023-01-31 14:43:59 +00:00
SIdxFltStatus status = SFLT_NOT_INDEX;
2022-11-26 17:59:49 +00:00
if (pScanNode->tableType != TSDB_SUPER_TABLE) {
pListInfo->idInfo.uid = pScanNode->uid;
if (pStorageAPI->metaFn.isTableExisted(pVnode, pScanNode->uid)) {
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pUidList, &pScanNode->uid);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
}
2023-07-19 11:41:54 +00:00
code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, status, pStorageAPI, false, &listAdded);
2024-07-23 10:32:03 +00:00
QUERY_CHECK_CODE(code, lino, _end);
2022-11-26 17:59:49 +00:00
} else {
T_MD5_CTX context = {0};
2022-11-16 06:23:12 +00:00
if (tsTagFilterCache) {
// try to retrieve the result from meta cache
2024-07-23 02:50:16 +00:00
code = genTagFilterDigest(pTagCond, &context);
QUERY_CHECK_CODE(code, lino, _error);
bool acquired = false;
2024-07-23 02:50:16 +00:00
code = pStorageAPI->metaFn.getCachedTableList(pVnode, pScanNode->suid, context.digest, tListLen(context.digest),
pUidList, &acquired);
QUERY_CHECK_CODE(code, lino, _error);
if (acquired) {
2023-05-09 09:04:20 +00:00
digest[0] = 1;
memcpy(digest + 1, context.digest, tListLen(context.digest));
qDebug("retrieve table uid list from cache, numOfTables:%d", (int32_t)taosArrayGetSize(pUidList));
goto _end;
}
2022-08-10 12:18:54 +00:00
}
2023-01-14 15:17:57 +00:00
if (!pTagCond) { // no tag filter condition exists, let's fetch all tables of this super table
2024-07-23 02:50:16 +00:00
code = pStorageAPI->metaFn.getChildTableList(pVnode, pScanNode->suid, pUidList);
QUERY_CHECK_CODE(code, lino, _error);
2022-11-26 17:59:49 +00:00
} else {
2022-11-16 06:23:12 +00:00
// failed to find the result in the cache, let try to calculate the results
if (pTagIndexCond) {
void* pIndex = pStorageAPI->metaFn.getInvertIndex(pVnode);
2023-06-26 10:43:00 +00:00
SIndexMetaArg metaArg = {.metaEx = pVnode,
.idx = pStorageAPI->metaFn.storeGetIndexInfo(pVnode),
.ivtIdx = pIndex,
.suid = pScanNode->uid};
2022-11-16 06:23:12 +00:00
2023-03-17 06:28:30 +00:00
status = SFLT_NOT_INDEX;
2023-05-24 13:53:40 +00:00
code = doFilterTag(pTagIndexCond, &metaArg, pUidList, &status, &pStorageAPI->metaFilter);
2023-01-05 10:59:36 +00:00
if (code != 0 || status == SFLT_NOT_INDEX) { // temporarily disable it for performance sake
2023-06-26 10:43:00 +00:00
qDebug("failed to get tableIds from index, suid:%" PRIu64, pScanNode->uid);
2023-02-05 03:49:45 +00:00
} else {
2024-04-03 09:11:49 +00:00
qDebug("succ to get filter result, table num: %d", (int)taosArrayGetSize(pUidList));
2022-11-16 06:23:12 +00:00
}
2022-07-14 02:49:09 +00:00
}
}
2023-07-19 11:41:54 +00:00
code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, status, pStorageAPI, tsTagFilterCache, &listAdded);
2024-07-23 10:32:03 +00:00
QUERY_CHECK_CODE(code, lino, _end);
2022-08-10 12:18:54 +00:00
2022-11-26 17:59:49 +00:00
// let's add the filter results into meta-cache
2023-02-10 15:40:53 +00:00
numOfTables = taosArrayGetSize(pUidList);
if (tsTagFilterCache) {
size_t size = numOfTables * sizeof(uint64_t) + sizeof(int32_t);
char* pPayload = taosMemoryMalloc(size);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pPayload, code, lino, _end, terrno);
2022-11-26 17:59:49 +00:00
*(int32_t*)pPayload = numOfTables;
if (numOfTables > 0) {
2024-08-05 08:09:01 +00:00
void* tmp = taosArrayGet(pUidList, 0);
QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
memcpy(pPayload + sizeof(int32_t), tmp, numOfTables * sizeof(uint64_t));
}
2024-07-23 02:50:16 +00:00
code = pStorageAPI->metaFn.putCachedTableList(pVnode, pScanNode->suid, context.digest, tListLen(context.digest),
pPayload, size, 1);
QUERY_CHECK_CODE(code, lino, _error);
2023-05-09 09:04:20 +00:00
digest[0] = 1;
memcpy(digest + 1, context.digest, tListLen(context.digest));
}
2022-07-14 02:49:09 +00:00
}
2022-11-26 17:59:49 +00:00
_end:
2023-07-19 11:14:30 +00:00
if (!listAdded) {
numOfTables = taosArrayGetSize(pUidList);
for (int i = 0; i < numOfTables; i++) {
2024-08-05 11:10:54 +00:00
void* tmp = taosArrayGet(pUidList, i);
2024-08-05 10:36:45 +00:00
QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
2024-08-05 08:09:01 +00:00
STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
2023-07-19 11:14:30 +00:00
void* p = taosArrayPush(pListInfo->pTableList, &info);
if (p == NULL) {
taosArrayDestroy(pUidList);
2024-09-20 05:23:44 +00:00
return terrno;
2023-07-19 11:14:30 +00:00
}
2022-09-13 10:22:58 +00:00
2023-07-19 11:14:30 +00:00
qTrace("tagfilter get uid:%" PRIu64 ", %s", info.uid, idstr);
}
}
2024-07-23 02:50:16 +00:00
_error:
2023-02-10 15:40:53 +00:00
taosArrayDestroy(pUidList);
2024-07-23 02:50:16 +00:00
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
}
return code;
}
2021-02-03 10:29:40 +00:00
2023-06-26 10:43:00 +00:00
int32_t qGetTableList(int64_t suid, void* pVnode, void* node, SArray** tableList, void* pTaskInfo) {
2024-08-05 09:08:21 +00:00
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
2023-06-26 10:43:00 +00:00
SSubplan* pSubplan = (SSubplan*)node;
SScanPhysiNode pNode = {0};
pNode.suid = suid;
pNode.uid = suid;
pNode.tableType = TSDB_SUPER_TABLE;
2024-08-05 10:34:56 +00:00
STableListInfo* pTableListInfo = tableListCreate();
2024-08-05 09:08:21 +00:00
QUERY_CHECK_NULL(pTableListInfo, code, lino, _end, terrno);
2025-03-28 10:10:57 +00:00
uint8_t digest[17] = {0};
code = getTableList(pVnode, &pNode, pSubplan ? pSubplan->pTagCond : NULL, pSubplan ? pSubplan->pTagIndexCond : NULL,
pTableListInfo, digest, "qGetTableList", &((SExecTaskInfo*)pTaskInfo)->storageAPI);
2024-08-05 09:08:21 +00:00
QUERY_CHECK_CODE(code, lino, _end);
*tableList = pTableListInfo->pTableList;
pTableListInfo->pTableList = NULL;
tableListDestroy(pTableListInfo);
2024-08-05 09:08:21 +00:00
_end:
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
}
return code;
}
2022-07-22 06:38:28 +00:00
size_t getTableTagsBufLen(const SNodeList* pGroups) {
size_t keyLen = 0;
SNode* node;
FOREACH(node, pGroups) {
SExprNode* pExpr = (SExprNode*)node;
keyLen += pExpr->resType.bytes;
}
keyLen += sizeof(int8_t) * LIST_LENGTH(pGroups);
return keyLen;
}
int32_t getGroupIdFromTagsVal(void* pVnode, uint64_t uid, SNodeList* pGroupNode, char* keyBuf, uint64_t* pGroupId,
2023-06-26 10:43:00 +00:00
SStorageAPI* pAPI) {
2022-07-27 05:55:04 +00:00
SMetaReader mr = {0};
pAPI->metaReaderFn.initReader(&mr, pVnode, META_READER_LOCK, &pAPI->metaFn);
if (pAPI->metaReaderFn.getEntryGetUidCache(&mr, uid) != 0) { // table not exist
pAPI->metaReaderFn.clearReader(&mr);
return TSDB_CODE_PAR_TABLE_NOT_EXIST;
}
2022-07-22 06:38:28 +00:00
2024-07-21 10:20:30 +00:00
SNodeList* groupNew = NULL;
2024-08-06 08:14:15 +00:00
int32_t code = nodesCloneList(pGroupNode, &groupNew);
2024-07-21 10:20:30 +00:00
if (TSDB_CODE_SUCCESS != code) {
pAPI->metaReaderFn.clearReader(&mr);
return code;
}
2022-07-22 06:38:28 +00:00
2024-07-21 10:20:30 +00:00
STransTagExprCtx ctx = {.code = 0, .pReader = &mr};
nodesRewriteExprsPostOrder(groupNew, doTranslateTagExpr, &ctx);
if (TSDB_CODE_SUCCESS != ctx.code) {
nodesDestroyList(groupNew);
pAPI->metaReaderFn.clearReader(&mr);
return code;
}
2022-07-22 06:38:28 +00:00
char* isNull = (char*)keyBuf;
2022-07-27 05:55:04 +00:00
char* pStart = (char*)keyBuf + sizeof(int8_t) * LIST_LENGTH(pGroupNode);
2022-07-22 06:38:28 +00:00
SNode* pNode;
int32_t index = 0;
FOREACH(pNode, groupNew) {
SNode* pNew = NULL;
int32_t code = scalarCalculateConstants(pNode, &pNew);
if (TSDB_CODE_SUCCESS == code) {
REPLACE_NODE(pNew);
} else {
nodesDestroyList(groupNew);
pAPI->metaReaderFn.clearReader(&mr);
2022-07-22 06:38:28 +00:00
return code;
}
if (nodeType(pNew) != QUERY_NODE_VALUE) {
nodesDestroyList(groupNew);
pAPI->metaReaderFn.clearReader(&mr);
2024-08-23 03:13:36 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
}
2022-07-22 06:38:28 +00:00
SValueNode* pValue = (SValueNode*)pNew;
if (pValue->node.resType.type == TSDB_DATA_TYPE_NULL || pValue->isNull) {
isNull[index++] = 1;
continue;
} else {
isNull[index++] = 0;
char* data = nodesGetValueFromNode(pValue);
if (pValue->node.resType.type == TSDB_DATA_TYPE_JSON) {
if (tTagIsJson(data)) {
terrno = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
nodesDestroyList(groupNew);
pAPI->metaReaderFn.clearReader(&mr);
2022-07-22 06:38:28 +00:00
return terrno;
}
int32_t len = getJsonValueLen(data);
memcpy(pStart, data, len);
pStart += len;
} else if (IS_VAR_DATA_TYPE(pValue->node.resType.type)) {
memcpy(pStart, data, varDataTLen(data));
pStart += varDataTLen(data);
} else {
memcpy(pStart, data, pValue->node.resType.bytes);
pStart += pValue->node.resType.bytes;
}
}
}
2022-07-27 05:55:04 +00:00
int32_t len = (int32_t)(pStart - (char*)keyBuf);
2022-07-22 06:38:28 +00:00
*pGroupId = calcGroupId(keyBuf, len);
nodesDestroyList(groupNew);
pAPI->metaReaderFn.clearReader(&mr);
2025-03-28 10:10:57 +00:00
2022-07-22 06:38:28 +00:00
return TSDB_CODE_SUCCESS;
}
2023-06-15 11:34:15 +00:00
SArray* makeColumnArrayFromList(SNodeList* pNodeList) {
2022-06-25 12:36:38 +00:00
if (!pNodeList) {
return NULL;
}
2021-02-03 10:29:40 +00:00
size_t numOfCols = LIST_LENGTH(pNodeList);
SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
if (pList == NULL) {
return NULL;
}
for (int32_t i = 0; i < numOfCols; ++i) {
SColumnNode* pColNode = (SColumnNode*)nodesListGetNode(pNodeList, i);
2024-08-05 08:09:01 +00:00
if (!pColNode) {
taosArrayDestroy(pList);
2024-12-11 11:18:50 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
2024-08-05 08:09:01 +00:00
return NULL;
}
// todo extract method
SColumn c = {0};
c.slotId = pColNode->slotId;
c.colId = pColNode->colId;
c.type = pColNode->node.resType.type;
c.bytes = pColNode->node.resType.bytes;
c.precision = pColNode->node.resType.precision;
c.scale = pColNode->node.resType.scale;
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pList, &c);
if (!tmp) {
taosArrayDestroy(pList);
2024-12-11 11:18:50 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2024-07-23 02:50:16 +00:00
return NULL;
}
}
2021-02-03 10:29:40 +00:00
return pList;
2021-02-03 10:29:40 +00:00
}
2022-10-24 08:44:44 +00:00
int32_t extractColMatchInfo(SNodeList* pNodeList, SDataBlockDescNode* pOutputNodeList, int32_t* numOfOutputCols,
int32_t type, SColMatchInfo* pMatchInfo) {
2022-10-29 10:32:17 +00:00
size_t numOfCols = LIST_LENGTH(pNodeList);
2024-07-23 02:50:16 +00:00
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
2022-10-24 08:44:44 +00:00
pMatchInfo->matchType = type;
SArray* pList = taosArrayInit(numOfCols, sizeof(SColMatchItem));
if (pList == NULL) {
2024-09-20 05:23:44 +00:00
code = terrno;
2022-10-24 08:44:44 +00:00
return code;
}
for (int32_t i = 0; i < numOfCols; ++i) {
STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
2022-10-24 08:44:44 +00:00
SColMatchItem c = {.needOutput = true};
c.colId = pColNode->colId;
c.srcSlotId = pColNode->slotId;
2022-10-24 08:44:44 +00:00
c.dstSlotId = pNode->slotId;
2024-03-20 05:09:59 +00:00
c.isPk = pColNode->isPk;
c.dataType = pColNode->node.resType;
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pList, &c);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
}
}
2022-12-01 09:24:26 +00:00
// set the output flag for each column in SColMatchInfo, according to the
*numOfOutputCols = 0;
int32_t num = LIST_LENGTH(pOutputNodeList->pSlots);
for (int32_t i = 0; i < num; ++i) {
SSlotDescNode* pNode = (SSlotDescNode*)nodesListGetNode(pOutputNodeList->pSlots, i);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
// todo: add reserve flag check
// it is a column reserved for the arithmetic expression calculation
if (pNode->slotId >= numOfCols) {
(*numOfOutputCols) += 1;
continue;
}
2022-10-24 08:44:44 +00:00
SColMatchItem* info = NULL;
for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
info = taosArrayGet(pList, j);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(info, code, lino, _end, terrno);
2022-10-24 08:44:44 +00:00
if (info->dstSlotId == pNode->slotId) {
break;
}
}
if (pNode->output) {
(*numOfOutputCols) += 1;
2022-09-19 06:08:30 +00:00
} else if (info != NULL) {
// select distinct tbname from stb where tbname='abc';
2022-10-24 08:44:44 +00:00
info->needOutput = false;
}
}
2022-10-24 09:24:20 +00:00
pMatchInfo->pList = pList;
2024-07-23 02:50:16 +00:00
_end:
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
}
2022-10-24 08:44:44 +00:00
return code;
}
static SResSchema createResSchema(int32_t type, int32_t bytes, int32_t slotId, int32_t scale, int32_t precision,
const char* name) {
SResSchema s = {0};
s.scale = scale;
s.type = type;
s.bytes = bytes;
s.slotId = slotId;
s.precision = precision;
2022-10-17 06:05:40 +00:00
tstrncpy(s.name, name, tListLen(s.name));
return s;
}
2022-08-25 03:01:36 +00:00
static SColumn* createColumn(int32_t blockId, int32_t slotId, int32_t colId, SDataType* pType, EColumnType colType) {
SColumn* pCol = taosMemoryCalloc(1, sizeof(SColumn));
if (pCol == NULL) {
return NULL;
}
pCol->slotId = slotId;
pCol->colId = colId;
pCol->bytes = pType->bytes;
pCol->type = pType->type;
pCol->scale = pType->scale;
pCol->precision = pType->precision;
pCol->dataBlockId = blockId;
2022-08-25 03:01:36 +00:00
pCol->colType = colType;
return pCol;
}
2024-07-23 02:50:16 +00:00
int32_t createExprFromOneNode(SExprInfo* pExp, SNode* pNode, int16_t slotId) {
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
2024-08-28 02:11:54 +00:00
pExp->base.numOfParams = 0;
pExp->base.pParam = NULL;
pExp->pExpr = taosMemoryCalloc(1, sizeof(tExprNode));
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pExp->pExpr, code, lino, _end, terrno);
2024-07-23 02:50:16 +00:00
pExp->pExpr->_function.num = 1;
pExp->pExpr->_function.functionId = -1;
int32_t type = nodeType(pNode);
// it is a project query, or group by column
if (type == QUERY_NODE_COLUMN) {
pExp->pExpr->nodeType = QUERY_NODE_COLUMN;
SColumnNode* pColNode = (SColumnNode*)pNode;
pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
2024-07-23 02:50:16 +00:00
pExp->base.numOfParams = 1;
SDataType* pType = &pColNode->node.resType;
pExp->base.resSchema =
createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pColNode->colName);
2024-08-05 10:02:32 +00:00
pExp->base.pParam[0].pCol =
createColumn(pColNode->dataBlockId, pColNode->slotId, pColNode->colId, pType, pColNode->colType);
2024-08-05 10:02:32 +00:00
QUERY_CHECK_NULL(pExp->base.pParam[0].pCol, code, lino, _end, terrno);
pExp->base.pParam[0].type = FUNC_PARAM_TYPE_COLUMN;
} else if (type == QUERY_NODE_VALUE) {
pExp->pExpr->nodeType = QUERY_NODE_VALUE;
SValueNode* pValNode = (SValueNode*)pNode;
pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
2024-07-23 02:50:16 +00:00
pExp->base.numOfParams = 1;
SDataType* pType = &pValNode->node.resType;
pExp->base.resSchema =
createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pValNode->node.aliasName);
pExp->base.pParam[0].type = FUNC_PARAM_TYPE_VALUE;
2024-08-09 03:44:13 +00:00
code = nodesValueNodeToVariant(pValNode, &pExp->base.pParam[0].param);
2024-08-28 02:11:54 +00:00
QUERY_CHECK_CODE(code, lino, _end);
} else if (type == QUERY_NODE_FUNCTION) {
pExp->pExpr->nodeType = QUERY_NODE_FUNCTION;
SFunctionNode* pFuncNode = (SFunctionNode*)pNode;
2024-10-14 15:27:11 +00:00
SDataType* pType = &pFuncNode->node.resType;
pExp->base.resSchema =
createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pFuncNode->node.aliasName);
2022-10-16 05:46:21 +00:00
tExprNode* pExprNode = pExp->pExpr;
pExprNode->_function.functionId = pFuncNode->funcId;
pExprNode->_function.pFunctNode = pFuncNode;
2022-11-18 03:53:33 +00:00
pExprNode->_function.functionType = pFuncNode->funcType;
2022-10-16 05:46:21 +00:00
tstrncpy(pExprNode->_function.functionName, pFuncNode->functionName, tListLen(pExprNode->_function.functionName));
pExp->base.pParamList = pFuncNode->pParameterList;
#if 1
// todo refactor: add the parameter for tbname function
2022-10-16 05:46:21 +00:00
const char* name = "tbname";
2022-10-19 09:54:06 +00:00
int32_t len = strlen(name);
2022-10-16 05:46:21 +00:00
if (!pFuncNode->pParameterList && (memcmp(pExprNode->_function.functionName, name, len) == 0) &&
pExprNode->_function.functionName[len] == 0) {
2024-07-21 10:20:30 +00:00
pFuncNode->pParameterList = NULL;
2024-08-06 08:14:15 +00:00
int32_t code = nodesMakeList(&pFuncNode->pParameterList);
2024-07-21 10:20:30 +00:00
SValueNode* res = NULL;
if (TSDB_CODE_SUCCESS == code) {
code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&res);
}
2024-08-09 03:44:13 +00:00
QUERY_CHECK_CODE(code, lino, _end);
res->node.resType = (SDataType){.bytes = sizeof(int64_t), .type = TSDB_DATA_TYPE_BIGINT};
code = nodesListAppend(pFuncNode->pParameterList, (SNode*)res);
2024-08-15 09:03:32 +00:00
if (code != TSDB_CODE_SUCCESS) {
nodesDestroyNode((SNode*)res);
res = NULL;
}
2024-08-09 03:44:13 +00:00
QUERY_CHECK_CODE(code, lino, _end);
}
#endif
int32_t numOfParam = LIST_LENGTH(pFuncNode->pParameterList);
pExp->base.pParam = taosMemoryCalloc(numOfParam, sizeof(SFunctParam));
2024-08-05 04:22:25 +00:00
QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
pExp->base.numOfParams = numOfParam;
2024-08-09 03:44:13 +00:00
for (int32_t j = 0; j < numOfParam && TSDB_CODE_SUCCESS == code; ++j) {
SNode* p1 = nodesListGetNode(pFuncNode->pParameterList, j);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(p1, code, lino, _end, terrno);
if (p1->type == QUERY_NODE_COLUMN) {
SColumnNode* pcn = (SColumnNode*)p1;
pExp->base.pParam[j].type = FUNC_PARAM_TYPE_COLUMN;
pExp->base.pParam[j].pCol =
createColumn(pcn->dataBlockId, pcn->slotId, pcn->colId, &pcn->node.resType, pcn->colType);
2024-08-05 10:02:32 +00:00
QUERY_CHECK_NULL(pExp->base.pParam[j].pCol, code, lino, _end, terrno);
} else if (p1->type == QUERY_NODE_VALUE) {
SValueNode* pvn = (SValueNode*)p1;
pExp->base.pParam[j].type = FUNC_PARAM_TYPE_VALUE;
2024-08-09 03:44:13 +00:00
code = nodesValueNodeToVariant(pvn, &pExp->base.pParam[j].param);
QUERY_CHECK_CODE(code, lino, _end);
}
}
2025-02-20 11:15:22 +00:00
pExp->pExpr->_function.bindExprID = ((SExprNode*)pNode)->bindExprID;
} else if (type == QUERY_NODE_OPERATOR) {
pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
SOperatorNode* pOpNode = (SOperatorNode*)pNode;
pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
2024-08-05 04:22:25 +00:00
QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
pExp->base.numOfParams = 1;
SDataType* pType = &pOpNode->node.resType;
pExp->base.resSchema =
createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pOpNode->node.aliasName);
pExp->pExpr->_optrRoot.pRootNode = pNode;
2022-09-30 10:13:55 +00:00
} else if (type == QUERY_NODE_CASE_WHEN) {
pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
2022-10-12 08:02:21 +00:00
SCaseWhenNode* pCaseNode = (SCaseWhenNode*)pNode;
2022-10-14 11:54:05 +00:00
2022-09-30 10:13:55 +00:00
pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
2024-08-05 04:22:25 +00:00
QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
2022-09-30 10:13:55 +00:00
pExp->base.numOfParams = 1;
2022-10-14 11:54:05 +00:00
2022-10-12 08:02:21 +00:00
SDataType* pType = &pCaseNode->node.resType;
2022-10-14 11:54:05 +00:00
pExp->base.resSchema =
createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pCaseNode->node.aliasName);
2022-10-12 08:02:21 +00:00
pExp->pExpr->_optrRoot.pRootNode = pNode;
2024-07-12 01:58:48 +00:00
} else if (type == QUERY_NODE_LOGIC_CONDITION) {
pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
SLogicConditionNode* pCond = (SLogicConditionNode*)pNode;
pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
2024-08-05 04:22:25 +00:00
QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
2024-08-09 03:44:13 +00:00
pExp->base.numOfParams = 1;
SDataType* pType = &pCond->node.resType;
2025-03-28 10:10:57 +00:00
pExp->base.resSchema =
createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pCond->node.aliasName);
2024-08-09 03:44:13 +00:00
pExp->pExpr->_optrRoot.pRootNode = pNode;
} else {
code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
QUERY_CHECK_CODE(code, lino, _end);
}
2025-02-15 17:15:12 +00:00
pExp->pExpr->relatedTo = ((SExprNode*)pNode)->relatedTo;
2024-07-23 02:50:16 +00:00
_end:
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
}
return code;
}
2024-07-23 02:50:16 +00:00
int32_t createExprFromTargetNode(SExprInfo* pExp, STargetNode* pTargetNode) {
return createExprFromOneNode(pExp, pTargetNode->pExpr, pTargetNode->slotId);
}
2023-01-10 02:13:56 +00:00
SExprInfo* createExpr(SNodeList* pNodeList, int32_t* numOfExprs) {
*numOfExprs = LIST_LENGTH(pNodeList);
SExprInfo* pExprs = taosMemoryCalloc(*numOfExprs, sizeof(SExprInfo));
2024-08-05 04:22:25 +00:00
if (!pExprs) {
return NULL;
}
2023-01-10 02:13:56 +00:00
for (int32_t i = 0; i < (*numOfExprs); ++i) {
SExprInfo* pExp = &pExprs[i];
2024-07-23 02:50:16 +00:00
int32_t code = createExprFromOneNode(pExp, nodesListGetNode(pNodeList, i), i + UD_TAG_COLUMN_INDEX);
if (code != TSDB_CODE_SUCCESS) {
taosMemoryFreeClear(pExprs);
2024-08-05 08:09:01 +00:00
terrno = code;
2024-07-23 02:50:16 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
return NULL;
}
2023-01-10 02:13:56 +00:00
}
return pExprs;
}
2024-08-05 03:57:18 +00:00
int32_t createExprInfo(SNodeList* pNodeList, SNodeList* pGroupKeys, SExprInfo** pExprInfo, int32_t* numOfExprs) {
QRY_PARAM_CHECK(pExprInfo);
2024-08-05 03:57:18 +00:00
int32_t code = 0;
int32_t numOfFuncs = LIST_LENGTH(pNodeList);
int32_t numOfGroupKeys = 0;
if (pGroupKeys != NULL) {
numOfGroupKeys = LIST_LENGTH(pGroupKeys);
}
*numOfExprs = numOfFuncs + numOfGroupKeys;
2022-08-01 06:05:41 +00:00
if (*numOfExprs == 0) {
2024-08-05 03:57:18 +00:00
return code;
2022-08-01 06:05:41 +00:00
}
SExprInfo* pExprs = taosMemoryCalloc(*numOfExprs, sizeof(SExprInfo));
2024-08-05 03:57:18 +00:00
if (pExprs == NULL) {
return terrno;
}
for (int32_t i = 0; i < (*numOfExprs); ++i) {
STargetNode* pTargetNode = NULL;
if (i < numOfFuncs) {
pTargetNode = (STargetNode*)nodesListGetNode(pNodeList, i);
} else {
pTargetNode = (STargetNode*)nodesListGetNode(pGroupKeys, i - numOfFuncs);
}
2024-08-05 08:09:01 +00:00
if (!pTargetNode) {
2024-08-16 06:30:35 +00:00
destroyExprInfo(pExprs, *numOfExprs);
taosMemoryFreeClear(pExprs);
2024-08-05 08:09:01 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
return terrno;
}
SExprInfo* pExp = &pExprs[i];
2024-08-05 03:57:18 +00:00
code = createExprFromTargetNode(pExp, pTargetNode);
2024-07-23 02:50:16 +00:00
if (code != TSDB_CODE_SUCCESS) {
2024-08-15 09:03:32 +00:00
destroyExprInfo(pExprs, *numOfExprs);
2024-08-16 06:30:35 +00:00
taosMemoryFreeClear(pExprs);
2024-07-23 02:50:16 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
2024-08-05 03:57:18 +00:00
return code;
2024-07-23 02:50:16 +00:00
}
}
2024-08-05 03:57:18 +00:00
*pExprInfo = pExprs;
return code;
}
2024-12-19 02:48:40 +00:00
static void deleteSubsidiareCtx(void* pData) {
SSubsidiaryResInfo* pCtx = (SSubsidiaryResInfo*)pData;
if (pCtx->pCtx) {
taosMemoryFreeClear(pCtx->pCtx);
}
}
// set the output buffer for the selectivity + tag query
static int32_t setSelectValueColumnInfo(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
int32_t num = 0;
2024-08-26 08:53:19 +00:00
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
2024-12-19 02:48:40 +00:00
SArray* pValCtxArray = NULL;
for (int32_t i = numOfOutput - 1; i > 0; --i) { // select Func is at the end of the list
2025-02-15 17:15:12 +00:00
int32_t funcIdx = pCtx[i].pExpr->pExpr->_function.bindExprID;
2024-12-19 02:48:40 +00:00
if (funcIdx > 0) {
if (pValCtxArray == NULL) {
// the end of the list is the select function of biggest index
pValCtxArray = taosArrayInit_s(sizeof(SSubsidiaryResInfo*), funcIdx);
if (pValCtxArray == NULL) {
return terrno;
}
}
if (funcIdx > pValCtxArray->size) {
qError("funcIdx:%d is out of range", funcIdx);
taosArrayDestroyP(pValCtxArray, deleteSubsidiareCtx);
return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
}
SSubsidiaryResInfo* pSubsidiary = &pCtx[i].subsidiaries;
pSubsidiary->pCtx = taosMemoryCalloc(numOfOutput, POINTER_BYTES);
if (pSubsidiary->pCtx == NULL) {
taosArrayDestroyP(pValCtxArray, deleteSubsidiareCtx);
return terrno;
}
pSubsidiary->num = 0;
taosArraySet(pValCtxArray, funcIdx - 1, &pSubsidiary);
}
}
2024-12-19 02:48:40 +00:00
SqlFunctionCtx* p = NULL;
SqlFunctionCtx** pValCtx = NULL;
if (pValCtxArray == NULL) {
pValCtx = taosMemoryCalloc(numOfOutput, POINTER_BYTES);
if (pValCtx == NULL) {
QUERY_CHECK_CODE(terrno, lino, _end);
}
}
2024-08-26 08:53:19 +00:00
for (int32_t i = 0; i < numOfOutput; ++i) {
2022-07-14 11:09:40 +00:00
const char* pName = pCtx[i].pExpr->pExpr->_function.functionName;
2025-02-20 08:29:23 +00:00
if ((strcmp(pName, "_select_value") == 0)) {
2024-12-19 02:48:40 +00:00
if (pValCtxArray == NULL) {
pValCtx[num++] = &pCtx[i];
2023-05-11 05:23:40 +00:00
} else {
2025-02-15 17:15:12 +00:00
int32_t bindFuncIndex = pCtx[i].pExpr->pExpr->relatedTo; // start from index 1;
2024-12-19 02:48:40 +00:00
if (bindFuncIndex > 0) { // 0 is default index related to the select function
bindFuncIndex -= 1;
2024-07-23 02:50:16 +00:00
}
2024-12-19 02:48:40 +00:00
SSubsidiaryResInfo** pSubsidiary = taosArrayGet(pValCtxArray, bindFuncIndex);
if(pSubsidiary == NULL) {
QUERY_CHECK_CODE(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR, lino, _end);
}
(*pSubsidiary)->pCtx[(*pSubsidiary)->num] = &pCtx[i];
(*pSubsidiary)->num++;
2023-05-11 05:23:40 +00:00
}
2024-12-19 02:48:40 +00:00
} else if (fmIsSelectFunc(pCtx[i].functionId)) {
if (pValCtxArray == NULL) {
p = &pCtx[i];
}
}
}
2022-07-14 11:09:40 +00:00
if (p != NULL) {
p->subsidiaries.pCtx = pValCtx;
p->subsidiaries.num = num;
} else {
taosMemoryFreeClear(pValCtx);
}
2024-08-26 08:53:19 +00:00
_end:
if (code != TSDB_CODE_SUCCESS) {
2024-12-19 02:48:40 +00:00
taosArrayDestroyP(pValCtxArray, deleteSubsidiareCtx);
2024-08-26 08:53:19 +00:00
taosMemoryFreeClear(pValCtx);
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
2024-12-29 02:02:20 +00:00
} else {
taosArrayDestroy(pValCtxArray);
2024-08-26 08:53:19 +00:00
}
return code;
}
2023-06-26 10:43:00 +00:00
SqlFunctionCtx* createSqlFunctionCtx(SExprInfo* pExprInfo, int32_t numOfOutput, int32_t** rowEntryInfoOffset,
SFunctionStateStore* pStore) {
2024-07-23 02:50:16 +00:00
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
SqlFunctionCtx* pFuncCtx = (SqlFunctionCtx*)taosMemoryCalloc(numOfOutput, sizeof(SqlFunctionCtx));
if (pFuncCtx == NULL) {
return NULL;
}
2022-06-17 15:23:37 +00:00
*rowEntryInfoOffset = taosMemoryCalloc(numOfOutput, sizeof(int32_t));
if (*rowEntryInfoOffset == 0) {
taosMemoryFreeClear(pFuncCtx);
return NULL;
}
for (int32_t i = 0; i < numOfOutput; ++i) {
SExprInfo* pExpr = &pExprInfo[i];
SExprBasicInfo* pFunct = &pExpr->base;
SqlFunctionCtx* pCtx = &pFuncCtx[i];
pCtx->functionId = -1;
pCtx->pExpr = pExpr;
if (pExpr->pExpr->nodeType == QUERY_NODE_FUNCTION) {
SFuncExecEnv env = {0};
pCtx->functionId = pExpr->pExpr->_function.pFunctNode->funcId;
2025-05-10 07:41:44 +00:00
pCtx->isPseudoFunc = fmIsWindowPseudoColumnFunc(pCtx->functionId) || fmIsPlaceHolderFunc(pCtx->functionId);
pCtx->isNotNullFunc = fmIsNotNullOutputFunc(pCtx->functionId);
2024-07-23 11:31:43 +00:00
bool isUdaf = fmIsUserDefinedFunc(pCtx->functionId);
if (fmIsPlaceHolderFunc(pCtx->functionId)) {
code = fmGetStreamPesudoFuncEnv(pCtx->functionId, pExpr->base.pParamList, &env);
QUERY_CHECK_CODE(code, lino, _end);
} else if (fmIsAggFunc(pCtx->functionId) || fmIsIndefiniteRowsFunc(pCtx->functionId)) {
if (!isUdaf) {
2024-07-23 02:50:16 +00:00
code = fmGetFuncExecFuncs(pCtx->functionId, &pCtx->fpSet);
QUERY_CHECK_CODE(code, lino, _end);
} else {
char* udfName = pExpr->pExpr->_function.pFunctNode->functionName;
pCtx->udfName = taosStrdup(udfName);
2024-08-05 03:17:49 +00:00
QUERY_CHECK_NULL(pCtx->udfName, code, lino, _end, terrno);
2024-07-23 02:50:16 +00:00
code = fmGetUdafExecFuncs(pCtx->functionId, &pCtx->fpSet);
QUERY_CHECK_CODE(code, lino, _end);
}
bool tmp = pCtx->fpSet.getEnv(pExpr->pExpr->_function.pFunctNode, &env);
if (!tmp) {
2024-12-11 11:18:50 +00:00
code = terrno;
2024-07-23 02:50:16 +00:00
QUERY_CHECK_CODE(code, lino, _end);
}
} else {
2024-07-23 10:32:03 +00:00
code = fmGetScalarFuncExecFuncs(pCtx->functionId, &pCtx->sfp);
2024-07-23 11:31:43 +00:00
if (code != TSDB_CODE_SUCCESS && isUdaf) {
code = TSDB_CODE_SUCCESS;
}
2024-07-23 10:32:03 +00:00
QUERY_CHECK_CODE(code, lino, _end);
2024-07-23 02:50:16 +00:00
if (pCtx->sfp.getEnv != NULL) {
2024-07-23 02:50:16 +00:00
bool tmp = pCtx->sfp.getEnv(pExpr->pExpr->_function.pFunctNode, &env);
if (!tmp) {
2024-12-11 11:18:50 +00:00
code = terrno;
2024-07-23 02:50:16 +00:00
QUERY_CHECK_CODE(code, lino, _end);
}
}
}
pCtx->resDataInfo.interBufSize = env.calcMemSize;
} else if (pExpr->pExpr->nodeType == QUERY_NODE_COLUMN || pExpr->pExpr->nodeType == QUERY_NODE_OPERATOR ||
pExpr->pExpr->nodeType == QUERY_NODE_VALUE) {
// for simple column, the result buffer needs to hold at least one element.
pCtx->resDataInfo.interBufSize = pFunct->resSchema.bytes;
}
pCtx->input.numOfInputCols = pFunct->numOfParams;
pCtx->input.pData = taosMemoryCalloc(pFunct->numOfParams, POINTER_BYTES);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pCtx->input.pData, code, lino, _end, terrno);
pCtx->input.pColumnDataAgg = taosMemoryCalloc(pFunct->numOfParams, POINTER_BYTES);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pCtx->input.pColumnDataAgg, code, lino, _end, terrno);
pCtx->pTsOutput = NULL;
pCtx->resDataInfo.bytes = pFunct->resSchema.bytes;
pCtx->resDataInfo.type = pFunct->resSchema.type;
pCtx->order = TSDB_ORDER_ASC;
pCtx->start.key = INT64_MIN;
pCtx->end.key = INT64_MIN;
pCtx->numOfParams = pExpr->base.numOfParams;
pCtx->param = pFunct->pParam;
pCtx->saveHandle.currentPage = -1;
pCtx->pStore = pStore;
pCtx->hasWindowOrGroup = false;
pCtx->needCleanup = false;
}
for (int32_t i = 1; i < numOfOutput; ++i) {
2022-06-25 12:36:38 +00:00
(*rowEntryInfoOffset)[i] = (int32_t)((*rowEntryInfoOffset)[i - 1] + sizeof(SResultRowEntryInfo) +
pFuncCtx[i - 1].resDataInfo.interBufSize);
}
2024-07-23 02:50:16 +00:00
code = setSelectValueColumnInfo(pFuncCtx, numOfOutput);
QUERY_CHECK_CODE(code, lino, _end);
_end:
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
for (int32_t i = 0; i < numOfOutput; ++i) {
taosMemoryFree(pFuncCtx[i].input.pData);
taosMemoryFree(pFuncCtx[i].input.pColumnDataAgg);
}
2024-08-02 08:50:01 +00:00
taosMemoryFreeClear(*rowEntryInfoOffset);
2024-07-23 02:50:16 +00:00
taosMemoryFreeClear(pFuncCtx);
2024-12-11 11:18:50 +00:00
terrno = code;
2024-07-23 02:50:16 +00:00
return NULL;
}
return pFuncCtx;
}
// NOTE: sources columns are more than the destination SSDatablock columns.
2022-06-20 08:39:19 +00:00
// doFilter in table scan needs every column even its output is false
2024-07-23 02:50:16 +00:00
int32_t relocateColumnData(SSDataBlock* pBlock, const SArray* pColMatchInfo, SArray* pCols, bool outputEveryColumn) {
int32_t code = TSDB_CODE_SUCCESS;
size_t numOfSrcCols = taosArrayGetSize(pCols);
int32_t i = 0, j = 0;
while (i < numOfSrcCols && j < taosArrayGetSize(pColMatchInfo)) {
SColumnInfoData* p = taosArrayGet(pCols, i);
2024-08-05 08:09:01 +00:00
if (!p) {
2024-08-05 10:18:13 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2024-08-05 08:09:01 +00:00
return terrno;
}
SColMatchItem* pmInfo = taosArrayGet(pColMatchInfo, j);
if (!pmInfo) {
return terrno;
}
if (p->info.colId == pmInfo->colId) {
2022-10-24 08:44:44 +00:00
SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, pmInfo->dstSlotId);
2024-08-05 08:09:01 +00:00
if (!pDst) {
return terrno;
}
2024-07-23 02:50:16 +00:00
code = colDataAssign(pDst, p, pBlock->info.rows, &pBlock->info);
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
return code;
}
i++;
j++;
} else if (p->info.colId < pmInfo->colId) {
i++;
} else {
2024-08-23 03:13:36 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
}
}
2024-07-23 02:50:16 +00:00
return code;
}
SInterval extractIntervalInfo(const STableScanPhysiNode* pTableScanNode) {
SInterval interval = {
.interval = pTableScanNode->interval,
.sliding = pTableScanNode->sliding,
.intervalUnit = pTableScanNode->intervalUnit,
.slidingUnit = pTableScanNode->slidingUnit,
.offset = pTableScanNode->offset,
.precision = pTableScanNode->scan.node.pOutputDataBlockDesc->precision,
.timeRange = pTableScanNode->scanRange,
};
calcIntervalAutoOffset(&interval);
return interval;
}
SColumn extractColumnFromColumnNode(SColumnNode* pColNode) {
SColumn c = {0};
2022-07-08 09:28:37 +00:00
c.slotId = pColNode->slotId;
c.colId = pColNode->colId;
c.type = pColNode->node.resType.type;
c.bytes = pColNode->node.resType.bytes;
c.scale = pColNode->node.resType.scale;
c.precision = pColNode->node.resType.precision;
return c;
}
2024-04-03 09:11:49 +00:00
int32_t initQueryTableDataCond(SQueryTableDataCond* pCond, const STableScanPhysiNode* pTableScanNode,
const SReadHandle* readHandle) {
pCond->order = pTableScanNode->scanSeq[0] > 0 ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
pCond->numOfCols = LIST_LENGTH(pTableScanNode->scan.pScanCols);
2022-12-01 09:24:26 +00:00
pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
2024-08-05 04:22:25 +00:00
if (!pCond->colList) {
return terrno;
}
2022-12-06 09:16:34 +00:00
pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
2024-09-26 06:51:57 +00:00
if (pCond->pSlotList == NULL) {
2022-12-01 09:24:26 +00:00
taosMemoryFreeClear(pCond->colList);
return terrno;
}
// TODO: get it from stable scan node
2022-07-10 02:15:27 +00:00
pCond->twindows = pTableScanNode->scanRange;
pCond->suid = pTableScanNode->scan.suid;
pCond->type = TIMEWINDOW_RANGE_CONTAINED;
2022-07-08 09:28:37 +00:00
pCond->startVersion = -1;
pCond->endVersion = -1;
2023-11-07 11:59:05 +00:00
pCond->skipRollup = readHandle->skipRollup;
if (readHandle->winRangeValid) {
pCond->twindows = readHandle->winRange;
}
2023-12-08 02:03:36 +00:00
// allowed read stt file optimization mode
pCond->notLoadData = (pTableScanNode->dataRequired == FUNC_DATA_REQUIRED_NOT_LOAD) &&
2024-04-03 09:11:49 +00:00
(pTableScanNode->scan.node.pConditions == NULL) && (pTableScanNode->interval == 0);
2023-12-08 02:03:36 +00:00
int32_t j = 0;
for (int32_t i = 0; i < pCond->numOfCols; ++i) {
STargetNode* pNode = (STargetNode*)nodesListGetNode(pTableScanNode->scan.pScanCols, i);
2024-08-05 08:09:01 +00:00
if (!pNode) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
return terrno;
}
SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
if (pColNode->colType == COLUMN_TYPE_TAG) {
continue;
}
pCond->colList[j].type = pColNode->node.resType.type;
pCond->colList[j].bytes = pColNode->node.resType.bytes;
pCond->colList[j].colId = pColNode->colId;
2024-03-21 01:37:46 +00:00
pCond->colList[j].pk = pColNode->isPk;
2022-12-01 09:24:26 +00:00
pCond->pSlotList[j] = pNode->slotId;
j += 1;
}
pCond->numOfCols = j;
return TSDB_CODE_SUCCESS;
}
int32_t initQueryTableDataCondWithColArray(SQueryTableDataCond* pCond, SQueryTableDataCond* pOrgCond,
const SReadHandle* readHandle, SArray* colArray) {
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
pCond->order = TSDB_ORDER_ASC;
pCond->numOfCols = (int32_t)taosArrayGetSize(colArray);
pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
QUERY_CHECK_NULL(pCond->colList, code, lino, _return, terrno);
pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
QUERY_CHECK_NULL(pCond->pSlotList, code, lino, _return, terrno);
pCond->twindows = pOrgCond->twindows;
pCond->type = pOrgCond->type;
pCond->startVersion = -1;
pCond->endVersion = -1;
pCond->skipRollup = true;
pCond->notLoadData = false;
for (int32_t i = 0; i < pCond->numOfCols; ++i) {
SColIdPair* pColPair = taosArrayGet(colArray, i);
QUERY_CHECK_NULL(pColPair, code, lino, _return, terrno);
bool find = false;
for (int32_t j = 0; j < pOrgCond->numOfCols; ++j) {
if (pOrgCond->colList[j].colId == pColPair->vtbColId) {
pCond->colList[i].type = pOrgCond->colList[j].type;
pCond->colList[i].bytes = pOrgCond->colList[j].bytes;
pCond->colList[i].colId = pColPair->orgColId;
pCond->colList[i].pk = pOrgCond->colList[j].pk;
pCond->pSlotList[i] = i;
find = true;
break;
}
}
QUERY_CHECK_CONDITION(find, code, lino, _return, TSDB_CODE_NOT_FOUND);
}
return code;
_return:
qError("%s failed at line %d since %s", __func__, lino, tstrerror(terrno));
taosMemoryFreeClear(pCond->colList);
taosMemoryFreeClear(pCond->pSlotList);
return code;
}
2022-12-01 12:01:09 +00:00
void cleanupQueryTableDataCond(SQueryTableDataCond* pCond) {
taosMemoryFreeClear(pCond->colList);
taosMemoryFreeClear(pCond->pSlotList);
}
int32_t convertFillType(int32_t mode) {
int32_t type = TSDB_FILL_NONE;
switch (mode) {
case FILL_MODE_PREV:
type = TSDB_FILL_PREV;
break;
case FILL_MODE_NONE:
type = TSDB_FILL_NONE;
break;
case FILL_MODE_NULL:
type = TSDB_FILL_NULL;
break;
2023-02-02 09:16:30 +00:00
case FILL_MODE_NULL_F:
type = TSDB_FILL_NULL_F;
break;
case FILL_MODE_NEXT:
type = TSDB_FILL_NEXT;
break;
case FILL_MODE_VALUE:
type = TSDB_FILL_SET_VALUE;
break;
2023-02-02 09:16:30 +00:00
case FILL_MODE_VALUE_F:
type = TSDB_FILL_SET_VALUE_F;
break;
case FILL_MODE_LINEAR:
type = TSDB_FILL_LINEAR;
break;
2024-11-28 10:29:20 +00:00
case FILL_MODE_NEAR:
type = TSDB_FILL_NEAR;
break;
default:
type = TSDB_FILL_NONE;
}
return type;
}
2022-07-10 02:15:27 +00:00
void getInitialStartTimeWindow(SInterval* pInterval, TSKEY ts, STimeWindow* w, bool ascQuery) {
2022-07-10 02:15:27 +00:00
if (ascQuery) {
*w = getAlignQueryTimeWindow(pInterval, ts);
2022-07-10 02:15:27 +00:00
} else {
// the start position of the first time window in the endpoint that spreads beyond the queried last timestamp
*w = getAlignQueryTimeWindow(pInterval, ts);
2022-07-10 02:15:27 +00:00
int64_t key = w->skey;
while (key < ts) { // moving towards end
key = getNextTimeWindowStart(pInterval, key, TSDB_ORDER_ASC);
2023-02-21 07:52:22 +00:00
if (key > ts) {
2022-07-10 02:15:27 +00:00
break;
}
w->skey = key;
}
2024-12-05 15:47:46 +00:00
w->ekey = taosTimeAdd(w->skey, pInterval->interval, pInterval->intervalUnit, pInterval->precision, NULL) - 1;
2022-07-10 02:15:27 +00:00
}
}
static STimeWindow doCalculateTimeWindow(int64_t ts, SInterval* pInterval) {
STimeWindow w = {0};
2022-07-10 02:15:27 +00:00
w.skey = taosTimeTruncate(ts, pInterval);
w.ekey = taosTimeGetIntervalEnd(w.skey, pInterval);
2022-07-10 02:15:27 +00:00
return w;
}
STimeWindow getFirstQualifiedTimeWindow(int64_t ts, STimeWindow* pWindow, SInterval* pInterval, int32_t order) {
2022-07-10 02:15:27 +00:00
STimeWindow win = *pWindow;
STimeWindow save = win;
while (win.skey <= ts && win.ekey >= ts) {
2022-07-10 02:15:27 +00:00
save = win;
// get previous time window
getNextTimeWindow(pInterval, &win, order == TSDB_ORDER_ASC ? TSDB_ORDER_DESC : TSDB_ORDER_ASC);
2022-07-10 02:15:27 +00:00
}
return save;
}
// get the correct time window according to the handled timestamp
// todo refactor
2022-07-10 02:15:27 +00:00
STimeWindow getActiveTimeWindow(SDiskbasedBuf* pBuf, SResultRowInfo* pResultRowInfo, int64_t ts, SInterval* pInterval,
int32_t order) {
STimeWindow w = {0};
if (pResultRowInfo->cur.pageId == -1) { // the first window, from the previous stored value
getInitialStartTimeWindow(pInterval, ts, &w, (order == TSDB_ORDER_ASC));
return w;
}
SResultRow* pRow = getResultRowByPos(pBuf, &pResultRowInfo->cur, false);
if (pRow) {
feat: support customized taos/taosd (#29736) * feat: support TDAcoreOS * chore: cmake options for TD_ACORE * chore: disable lemon for TD_ACORE * chore: add lzma2 and msvcregex * chore: cmake for lzma2 * chore: adapt for TD_ACORE * chore: adapt strcasecmp for TD_ACORE * chore: adapt for geos/threadName * chore: build adapt for TD_ACORE * chore: build adapt for TD_ACORE * chore: build adapt for TD_ACORE * chore: build adapt for TD_ACORE * chore: build adapt for TD_ACORE termio * chore: refact transComm.h for TD_ACORE * chore: refact transportInt.h for TD_ACORE * chore: refact trans.c for TD_ACORE * chore: refact trpc.h for TD_ACORE * chore: refact transCli.c/transComm.c/transSvr.c for TD_ACORE * chore: refact uv.h for TD_ACORE * chore: refact geosWrapper.h for TD_ACORE * chore: refact token/builtins/udf for TD_ACORE * chore: refact rocks for TD_ACORE * chore: refact tsdbCache.c for TD_ACORE, use LRU cache for last/last_row, not use rocksdb * chore: refact FAIL to _ERR to solve conflicts for TD_ACORE * chore: restore lemon.c/lempar.c * chore: support build lemon for TD_ACORE * chore: refact trpc and siginfo_t for TD_ACORE * chore: refact timezone for TD_ACORE * chore: refact lz4 for TD_ACORE * chore: refact TD_ACORE to make compile pass * chore: code optimization for TD_ASTRA * feat: support run taos with taosd integrated * feat: support invoke taos shell * feat: support invoke taos shell * feat: support invoke taos shell * chore: code optimization * chore: fix undefined reference problem os TD_ASTRA * chore: resolve compile problem for TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix undefined reference problem os TD_ASTRA * chore: fix getpid * chore: fix typo * chore: set stack size and ajust min pack size for TD_ASTRA * chore: fix pthread create parameters * chore: chmod adapt for TD_ASTRA * chore: fix trans compile problem * chore: adapt chmod for TD_ASTRA * chore: byte alignment for TD_ASTRA * chore: more code for adaption of TD_ASTRA * chore: more code for adaption of TD_ASTRA * chore: more code for adaption of TD_ASTRA * chore: byte alignment for TD_ASTRA * chore: conditional compile option * chore: adapt for TD_ASTRA * chore: adjust taosPId and msvcregex for TD_ASTRA * chore: log dir separator for wal build name * chore: fix type of pointer parameter * chore: fix compile problem of tsdbGetS3Size * enh: get last ver from wal log for TD_ASTRA * enh: refact wal meta ver * enh: refact wal meta ver * fix: typo of taosUcs4Compare * enh: process return value of CI * chore: more code for TD_ASTRA adaption * chore: return value of taosCloseFile in walMeta.c * chore: fix compile problem * chore: fix compile problem of TD_ASTRA * fix: update macro for tq and stream task * chore: code optimization for TD_ASTRA * chore: restore create log and init cfg interface * chore: restore strncasecmp and strcasecmp * fix: adjust the field position of SDataBlockInfo * fix: pragma pack min size * fix: pragma pack min size * chore: more code for TD_ASTRA adaption * fix: type of parameters * chore: adapt strncasecmp and strcasecmp for TD_ASTRA * chore: restore interface of init log * enh: pack push optimization * fix: taos init cfg * add astra support * fix: fetch the value of suid * chore: switch of build with udf * add temp code * chore: more code for TD_ASTRA adaption * chore: add macro ERRNO to replace errno * chore: bytes align for TD_ASTRA * fix: remove obsolete codes * enh: support USE_UDF macro * fix compile error * fix: resolve redefinition problem * fix: compile problem of log.cpp * fix: compile problem of osTimezone * fix: resolve compile problem of udf * fix: pragma definition on windows * fix: ucs4 and stpncpy for TD_ASTRA * fix: memory align problem for TD_ASTRA * enh: solve memory leak for TD_ASTRA_RPC * fix: compile problem of taosSetInt64Aligned * fix: restore mndSubscribe.c * fix: scalar for udf * chore: code adaption for TD_ASTRA * chore: code optimization for TD_ASTRA * fix: typo of add definition * fix: typo of macro in tudf.h * chore: remove void to make CI pass * enh: move macro from cmake.platform to cmake.options * enh: byte align for hash node and error code * chore: restore the size for lru cache * enh: restore some code about pack push * chore: restore the pack push in tmsg.h * fix: add macro of pack pop for windows --------- Co-authored-by: yihaoDeng <luomoxyz@126.com>
2025-03-14 05:32:13 +00:00
TAOS_SET_OBJ_ALIGNED(&w, pRow->win);
}
2022-07-10 02:15:27 +00:00
// in case of typical time window, we can calculate time window directly.
if (w.skey > ts || w.ekey < ts) {
w = doCalculateTimeWindow(ts, pInterval);
}
if (pInterval->interval != pInterval->sliding) {
// it is an sliding window query, in which sliding value is not equalled to
// interval value, and we need to find the first qualified time window.
w = getFirstQualifiedTimeWindow(ts, &w, pInterval, order);
}
return w;
}
TSKEY getNextTimeWindowStart(const SInterval* pInterval, TSKEY start, int32_t order) {
int32_t factor = GET_FORWARD_DIRECTION_FACTOR(order);
2024-12-05 15:47:46 +00:00
TSKEY nextStart = taosTimeAdd(start, -1 * pInterval->offset, pInterval->offsetUnit, pInterval->precision, NULL);
nextStart = taosTimeAdd(nextStart, factor * pInterval->sliding, pInterval->slidingUnit, pInterval->precision, NULL);
nextStart = taosTimeAdd(nextStart, pInterval->offset, pInterval->offsetUnit, pInterval->precision, NULL);
return nextStart;
}
void getNextTimeWindow(const SInterval* pInterval, STimeWindow* tw, int32_t order) {
tw->skey = getNextTimeWindowStart(pInterval, tw->skey, order);
2024-12-05 15:47:46 +00:00
tw->ekey = taosTimeAdd(tw->skey, pInterval->interval, pInterval->intervalUnit, pInterval->precision, NULL) - 1;
}
bool hasLimitOffsetInfo(SLimitInfo* pLimitInfo) {
return (pLimitInfo->limit.limit != -1 || pLimitInfo->limit.offset != -1 || pLimitInfo->slimit.limit != -1 ||
pLimitInfo->slimit.offset != -1);
}
2023-01-20 10:08:34 +00:00
bool hasSlimitOffsetInfo(SLimitInfo* pLimitInfo) {
return (pLimitInfo->slimit.limit != -1 || pLimitInfo->slimit.offset != -1);
}
void initLimitInfo(const SNode* pLimit, const SNode* pSLimit, SLimitInfo* pLimitInfo) {
SLimit limit = {.limit = getLimit(pLimit), .offset = getOffset(pLimit)};
SLimit slimit = {.limit = getLimit(pSLimit), .offset = getOffset(pSLimit)};
pLimitInfo->limit = limit;
pLimitInfo->slimit = slimit;
pLimitInfo->remainOffset = limit.offset;
pLimitInfo->remainGroupOffset = slimit.offset;
}
2022-10-29 19:01:40 +00:00
void resetLimitInfoForNextGroup(SLimitInfo* pLimitInfo) {
pLimitInfo->numOfOutputRows = 0;
pLimitInfo->remainOffset = pLimitInfo->limit.offset;
}
int32_t tableListGetSize(const STableListInfo* pTableList, int32_t* pRes) {
if (taosArrayGetSize(pTableList->pTableList) != taosHashGetSize(pTableList->map)) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
}
(*pRes) = taosArrayGetSize(pTableList->pTableList);
return TSDB_CODE_SUCCESS;
2022-10-29 19:01:40 +00:00
}
2023-05-23 03:46:02 +00:00
uint64_t tableListGetSuid(const STableListInfo* pTableList) { return pTableList->idInfo.suid; }
2022-10-30 14:13:49 +00:00
STableKeyInfo* tableListGetInfo(const STableListInfo* pTableList, int32_t index) {
if (taosArrayGetSize(pTableList->pTableList) == 0) {
return NULL;
}
return taosArrayGet(pTableList->pTableList, index);
}
int32_t tableListFind(const STableListInfo* pTableList, uint64_t uid, int32_t startIndex) {
int32_t numOfTables = taosArrayGetSize(pTableList->pTableList);
if (startIndex >= numOfTables) {
return -1;
}
for (int32_t i = startIndex; i < numOfTables; ++i) {
STableKeyInfo* p = taosArrayGet(pTableList->pTableList, i);
2024-08-05 08:09:01 +00:00
if (!p) {
2024-08-05 10:18:13 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2024-08-05 08:09:01 +00:00
return -1;
}
if (p->uid == uid) {
return i;
}
}
return -1;
}
void tableListGetSourceTableInfo(const STableListInfo* pTableList, uint64_t* psuid, uint64_t* uid, int32_t* type) {
*psuid = pTableList->idInfo.suid;
*uid = pTableList->idInfo.uid;
*type = pTableList->idInfo.tableType;
}
uint64_t tableListGetTableGroupId(const STableListInfo* pTableList, uint64_t tableUid) {
2022-10-29 19:01:40 +00:00
int32_t* slot = taosHashGet(pTableList->map, &tableUid, sizeof(tableUid));
2024-08-05 11:12:42 +00:00
if (slot == NULL) {
qDebug("table:%" PRIu64 " not found in table list", tableUid);
2024-08-05 11:12:42 +00:00
return -1;
}
2022-10-29 19:01:40 +00:00
STableKeyInfo* pKeyInfo = taosArrayGet(pTableList->pTableList, *slot);
return pKeyInfo->groupId;
}
2022-10-30 14:23:27 +00:00
// TODO handle the group offset info, fix it, the rule of group output will be broken by this function
2022-10-30 14:13:49 +00:00
int32_t tableListAddTableInfo(STableListInfo* pTableList, uint64_t uid, uint64_t gid) {
2024-07-23 02:50:16 +00:00
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
2022-10-29 19:01:40 +00:00
if (pTableList->map == NULL) {
pTableList->map = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(pTableList->map, code, lino, _end, terrno);
2022-10-29 19:01:40 +00:00
}
STableKeyInfo keyInfo = {.uid = uid, .groupId = gid};
void* p = taosHashGet(pTableList->map, &uid, sizeof(uid));
if (p != NULL) {
qInfo("table:%" PRId64 " already in tableIdList, ignore it", uid);
goto _end;
}
void* tmp = taosArrayPush(pTableList->pTableList, &keyInfo);
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2022-10-29 19:01:40 +00:00
int32_t slot = (int32_t)taosArrayGetSize(pTableList->pTableList) - 1;
2024-07-22 04:51:25 +00:00
code = taosHashPut(pTableList->map, &uid, sizeof(uid), &slot, sizeof(slot));
if (code != TSDB_CODE_SUCCESS) {
// we have checked the existence of uid in hash map above
QUERY_CHECK_CONDITION((code != TSDB_CODE_DUP_KEY), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
taosArrayPopTailBatch(pTableList->pTableList, 1); // let's pop the last element in the array list
2024-07-23 06:19:04 +00:00
}
2022-10-29 19:01:40 +00:00
2024-07-22 04:51:25 +00:00
_end:
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
} else {
qDebug("uid:%" PRIu64 ", groupId:%" PRIu64 " added into table list, slot:%d, total:%d", uid, gid, slot, slot + 1);
2024-07-22 04:51:25 +00:00
}
2024-07-22 04:51:25 +00:00
return code;
2022-10-29 19:01:40 +00:00
}
2022-10-30 14:13:49 +00:00
int32_t tableListGetGroupList(const STableListInfo* pTableList, int32_t ordinalGroupIndex, STableKeyInfo** pKeyInfo,
2022-11-01 11:13:07 +00:00
int32_t* size) {
int32_t totalGroups = tableListGetOutputGroups(pTableList);
int32_t numOfTables = 0;
int32_t code = tableListGetSize(pTableList, &numOfTables);
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
return code;
}
if (ordinalGroupIndex < 0 || ordinalGroupIndex >= totalGroups) {
2022-10-29 19:01:40 +00:00
return TSDB_CODE_INVALID_PARA;
}
// here handle two special cases:
// 1. only one group exists, and 2. one table exists for each group.
if (totalGroups == 1) {
*size = numOfTables;
2022-11-01 11:13:07 +00:00
*pKeyInfo = (*size == 0) ? NULL : taosArrayGet(pTableList->pTableList, 0);
2022-10-29 19:01:40 +00:00
return TSDB_CODE_SUCCESS;
} else if (totalGroups == numOfTables) {
2022-10-29 19:01:40 +00:00
*size = 1;
*pKeyInfo = taosArrayGet(pTableList->pTableList, ordinalGroupIndex);
return TSDB_CODE_SUCCESS;
}
int32_t offset = pTableList->groupOffset[ordinalGroupIndex];
if (ordinalGroupIndex < totalGroups - 1) {
*size = pTableList->groupOffset[ordinalGroupIndex + 1] - offset;
2022-10-29 19:01:40 +00:00
} else {
*size = numOfTables - offset;
2022-10-29 19:01:40 +00:00
}
*pKeyInfo = taosArrayGet(pTableList->pTableList, offset);
return TSDB_CODE_SUCCESS;
}
2022-10-30 14:13:49 +00:00
int32_t tableListGetOutputGroups(const STableListInfo* pTableList) { return pTableList->numOfOuputGroups; }
2022-10-29 19:01:40 +00:00
bool oneTableForEachGroup(const STableListInfo* pTableList) { return pTableList->oneTableForEachGroup; }
2022-10-30 14:13:49 +00:00
STableListInfo* tableListCreate() {
STableListInfo* pListInfo = taosMemoryCalloc(1, sizeof(STableListInfo));
if (pListInfo == NULL) {
return NULL;
}
2024-08-05 10:34:56 +00:00
pListInfo->remainGroups = NULL;
2022-10-30 14:13:49 +00:00
pListInfo->pTableList = taosArrayInit(4, sizeof(STableKeyInfo));
if (pListInfo->pTableList == NULL) {
goto _error;
}
pListInfo->map = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
2022-10-30 14:13:49 +00:00
if (pListInfo->map == NULL) {
goto _error;
}
pListInfo->numOfOuputGroups = 1;
return pListInfo;
_error:
tableListDestroy(pListInfo);
return NULL;
}
2024-07-23 02:50:16 +00:00
void tableListDestroy(STableListInfo* pTableListInfo) {
if (pTableListInfo == NULL) {
2024-07-23 02:50:16 +00:00
return;
}
2022-10-29 19:01:40 +00:00
2024-07-12 07:22:39 +00:00
taosArrayDestroy(pTableListInfo->pTableList);
2022-10-30 14:13:49 +00:00
taosMemoryFreeClear(pTableListInfo->groupOffset);
2022-10-29 19:01:40 +00:00
2022-10-30 14:13:49 +00:00
taosHashCleanup(pTableListInfo->map);
2024-01-02 07:45:03 +00:00
taosHashCleanup(pTableListInfo->remainGroups);
2022-10-30 14:13:49 +00:00
pTableListInfo->pTableList = NULL;
pTableListInfo->map = NULL;
taosMemoryFree(pTableListInfo);
}
void tableListClear(STableListInfo* pTableListInfo) {
2022-10-31 03:43:00 +00:00
if (pTableListInfo == NULL) {
return;
}
2022-10-30 14:13:49 +00:00
taosArrayClear(pTableListInfo->pTableList);
taosHashClear(pTableListInfo->map);
2024-01-02 07:45:03 +00:00
taosHashClear(pTableListInfo->remainGroups);
2022-10-30 14:13:49 +00:00
taosMemoryFree(pTableListInfo->groupOffset);
pTableListInfo->numOfOuputGroups = 1;
pTableListInfo->oneTableForEachGroup = false;
}
static int32_t orderbyGroupIdComparFn(const void* p1, const void* p2) {
2022-11-01 11:13:07 +00:00
STableKeyInfo* pInfo1 = (STableKeyInfo*)p1;
STableKeyInfo* pInfo2 = (STableKeyInfo*)p2;
2022-10-30 14:13:49 +00:00
if (pInfo1->groupId == pInfo2->groupId) {
return 0;
} else {
2022-11-01 11:13:07 +00:00
return pInfo1->groupId < pInfo2->groupId ? -1 : 1;
2022-10-30 14:13:49 +00:00
}
}
static int32_t sortTableGroup(STableListInfo* pTableListInfo) {
taosArraySort(pTableListInfo->pTableList, orderbyGroupIdComparFn);
int32_t size = taosArrayGetSize(pTableListInfo->pTableList);
SArray* pList = taosArrayInit(4, sizeof(int32_t));
2024-08-05 04:22:25 +00:00
if (!pList) {
return terrno;
}
2022-10-30 14:13:49 +00:00
STableKeyInfo* pInfo = taosArrayGet(pTableListInfo->pTableList, 0);
2024-08-05 08:09:01 +00:00
if (!pInfo) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
return terrno;
}
2025-03-28 10:10:57 +00:00
uint64_t gid = pInfo->groupId;
2022-10-30 14:13:49 +00:00
int32_t start = 0;
2024-07-23 02:50:16 +00:00
void* tmp = taosArrayPush(pList, &start);
if (!tmp) {
2024-08-05 08:09:01 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
return terrno;
2024-07-23 02:50:16 +00:00
}
2022-11-01 11:13:07 +00:00
for (int32_t i = 1; i < size; ++i) {
2022-10-30 14:13:49 +00:00
pInfo = taosArrayGet(pTableListInfo->pTableList, i);
2024-08-05 08:09:01 +00:00
if (!pInfo) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
return terrno;
}
2022-10-30 14:13:49 +00:00
if (pInfo->groupId != gid) {
2024-07-23 02:50:16 +00:00
tmp = taosArrayPush(pList, &i);
if (!tmp) {
2024-08-05 08:09:01 +00:00
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
return terrno;
2024-07-23 02:50:16 +00:00
}
2022-10-30 14:13:49 +00:00
gid = pInfo->groupId;
}
}
pTableListInfo->numOfOuputGroups = taosArrayGetSize(pList);
pTableListInfo->groupOffset = taosMemoryMalloc(sizeof(int32_t) * pTableListInfo->numOfOuputGroups);
2022-11-04 10:46:48 +00:00
if (pTableListInfo->groupOffset == NULL) {
taosArrayDestroy(pList);
2024-09-20 05:23:44 +00:00
return terrno;
2022-11-04 10:46:48 +00:00
}
2022-10-30 14:13:49 +00:00
memcpy(pTableListInfo->groupOffset, taosArrayGet(pList, 0), sizeof(int32_t) * pTableListInfo->numOfOuputGroups);
taosArrayDestroy(pList);
return TSDB_CODE_SUCCESS;
2022-10-30 14:13:49 +00:00
}
2023-06-26 10:43:00 +00:00
int32_t buildGroupIdMapForAllTables(STableListInfo* pTableListInfo, SReadHandle* pHandle, SScanPhysiNode* pScanNode,
SNodeList* group, bool groupSort, uint8_t* digest, SStorageAPI* pAPI, SHashObj* groupIdMap) {
2022-10-30 14:13:49 +00:00
int32_t code = TSDB_CODE_SUCCESS;
2022-11-01 11:13:07 +00:00
bool groupByTbname = groupbyTbname(group);
2022-10-30 14:13:49 +00:00
size_t numOfTables = taosArrayGetSize(pTableListInfo->pTableList);
2023-12-29 08:22:41 +00:00
if (!numOfTables) {
return code;
}
2025-06-20 05:39:25 +00:00
qDebug("numOfTables:%zu, groupByTbname:%d, group:%p", numOfTables, groupByTbname, group);
2022-10-30 14:13:49 +00:00
if (group == NULL || groupByTbname) {
2024-07-23 02:50:16 +00:00
if (tsCountAlwaysReturnValue && QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pScanNode) &&
((STableScanPhysiNode*)pScanNode)->needCountEmptyTable) {
pTableListInfo->remainGroups =
taosHashInit(numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
if (pTableListInfo->remainGroups == NULL) {
2024-09-20 05:23:44 +00:00
return terrno;
}
2024-07-23 02:50:16 +00:00
for (int i = 0; i < numOfTables; i++) {
STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
2024-08-05 08:09:01 +00:00
if (!info) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
return terrno;
}
2024-07-09 09:39:44 +00:00
info->groupId = groupByTbname ? info->uid : 0;
2024-07-23 10:32:03 +00:00
int32_t tempRes = taosHashPut(pTableListInfo->remainGroups, &(info->groupId), sizeof(info->groupId),
&(info->uid), sizeof(info->uid));
if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(tempRes));
return tempRes;
2024-07-23 02:50:16 +00:00
}
}
} else {
for (int32_t i = 0; i < numOfTables; i++) {
STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
2024-08-05 08:09:01 +00:00
if (!info) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
return terrno;
}
info->groupId = groupByTbname ? info->uid : 0;
2025-06-20 05:39:25 +00:00
}
2022-10-30 14:13:49 +00:00
}
2025-06-20 05:39:25 +00:00
if (groupIdMap && group != NULL){
getColInfoResultForGroupbyForStream(pHandle->vnode, group, pTableListInfo, pAPI, groupIdMap);
}
2022-10-30 14:13:49 +00:00
pTableListInfo->oneTableForEachGroup = groupByTbname;
2024-04-01 11:44:26 +00:00
if (numOfTables == 1 && pTableListInfo->idInfo.tableType == TSDB_CHILD_TABLE) {
pTableListInfo->oneTableForEachGroup = true;
}
2022-10-30 14:13:49 +00:00
if (groupSort && groupByTbname) {
taosArraySort(pTableListInfo->pTableList, orderbyGroupIdComparFn);
pTableListInfo->numOfOuputGroups = numOfTables;
2024-04-03 09:11:49 +00:00
} else if (groupByTbname && pScanNode->groupOrderScan) {
pTableListInfo->numOfOuputGroups = numOfTables;
2022-10-30 14:13:49 +00:00
} else {
pTableListInfo->numOfOuputGroups = 1;
}
} else {
2024-01-02 07:45:03 +00:00
bool initRemainGroups = false;
if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pScanNode)) {
STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pScanNode;
2024-04-03 09:11:49 +00:00
if (tsCountAlwaysReturnValue && pTableScanNode->needCountEmptyTable &&
!(groupSort || pScanNode->groupOrderScan)) {
2024-01-02 07:45:03 +00:00
initRemainGroups = true;
}
}
code = getColInfoResultForGroupby(pHandle->vnode, group, pTableListInfo, digest, pAPI, initRemainGroups, groupIdMap);
2022-10-30 14:13:49 +00:00
if (code != TSDB_CODE_SUCCESS) {
return code;
}
if (pScanNode->groupOrderScan) pTableListInfo->numOfOuputGroups = taosArrayGetSize(pTableListInfo->pTableList);
2023-08-03 10:05:52 +00:00
if (groupSort || pScanNode->groupOrderScan) {
2022-10-30 14:13:49 +00:00
code = sortTableGroup(pTableListInfo);
}
}
// add all table entry in the hash map
size_t size = taosArrayGetSize(pTableListInfo->pTableList);
2022-11-01 11:13:07 +00:00
for (int32_t i = 0; i < size; ++i) {
2022-10-30 14:13:49 +00:00
STableKeyInfo* p = taosArrayGet(pTableListInfo->pTableList, i);
2024-08-05 08:09:01 +00:00
if (!p) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
return terrno;
}
2025-03-28 10:10:57 +00:00
int32_t tempRes = taosHashPut(pTableListInfo->map, &p->uid, sizeof(uint64_t), &i, sizeof(int32_t));
2024-07-23 10:32:03 +00:00
if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(tempRes));
return tempRes;
2024-07-23 02:50:16 +00:00
}
2022-10-30 14:13:49 +00:00
}
return code;
}
int32_t createScanTableListInfo(SScanPhysiNode* pScanNode, SNodeList* pGroupTags, bool groupSort, SReadHandle* pHandle,
STableListInfo* pTableListInfo, SNode* pTagCond, SNode* pTagIndexCond,
SExecTaskInfo* pTaskInfo, SHashObj* groupIdMap) {
2022-11-01 11:13:07 +00:00
int64_t st = taosGetTimestampUs();
2022-11-01 07:00:02 +00:00
const char* idStr = GET_TASKID(pTaskInfo);
2022-10-30 14:13:49 +00:00
if (pHandle == NULL) {
qError("invalid handle, in creating operator tree, %s", idStr);
return TSDB_CODE_INVALID_PARA;
}
2025-06-05 09:26:09 +00:00
if (pHandle->uid != 0) {
pScanNode->uid = pHandle->uid;
pScanNode->tableType = TSDB_CHILD_TABLE;
}
2023-05-09 09:04:20 +00:00
uint8_t digest[17] = {0};
2023-06-26 10:43:00 +00:00
int32_t code = getTableList(pHandle->vnode, pScanNode, pTagCond, pTagIndexCond, pTableListInfo, digest, idStr,
&pTaskInfo->storageAPI);
2022-10-30 14:13:49 +00:00
if (code != TSDB_CODE_SUCCESS) {
2025-02-26 03:34:50 +00:00
qError("failed to getTableList, code:%s", tstrerror(code));
2022-10-30 14:13:49 +00:00
return code;
}
2022-11-16 06:40:39 +00:00
int32_t numOfTables = taosArrayGetSize(pTableListInfo->pTableList);
2022-10-30 14:13:49 +00:00
int64_t st1 = taosGetTimestampUs();
2022-11-01 07:00:02 +00:00
pTaskInfo->cost.extractListTime = (st1 - st) / 1000.0;
2022-11-16 06:40:39 +00:00
qDebug("extract queried table list completed, %d tables, elapsed time:%.2f ms %s", numOfTables,
pTaskInfo->cost.extractListTime, idStr);
2022-10-30 14:13:49 +00:00
2022-11-16 06:40:39 +00:00
if (numOfTables == 0) {
2022-10-30 14:13:49 +00:00
qDebug("no table qualified for query, %s" PRIx64, idStr);
return TSDB_CODE_SUCCESS;
}
2023-06-26 10:43:00 +00:00
code = buildGroupIdMapForAllTables(pTableListInfo, pHandle, pScanNode, pGroupTags, groupSort, digest,
&pTaskInfo->storageAPI, groupIdMap);
2022-10-30 14:13:49 +00:00
if (code != TSDB_CODE_SUCCESS) {
return code;
}
2022-11-01 11:13:07 +00:00
pTaskInfo->cost.groupIdMapTime = (taosGetTimestampUs() - st1) / 1000.0;
2022-11-01 07:00:02 +00:00
qDebug("generate group id map completed, elapsed time:%.2f ms %s", pTaskInfo->cost.groupIdMapTime, idStr);
2022-10-30 14:13:49 +00:00
return TSDB_CODE_SUCCESS;
2022-10-29 19:01:40 +00:00
}
2022-11-30 13:04:58 +00:00
2023-08-14 09:51:20 +00:00
char* getStreamOpName(uint16_t opType) {
switch (opType) {
2023-08-15 07:42:50 +00:00
case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
2023-08-14 09:51:20 +00:00
return "stream scan";
2023-08-15 07:42:50 +00:00
case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
return "project";
case QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW:
return "external window";
feat(stream): add new trigger continuous_window_close (#30125) * opt stream build twa result * opt force window close memory * feat(stream):optimize new interval and scan operator * adj log * opt code * opt code * fill history * fix issue for fill history * add ci * feat(stream): add new stream nonblock interval operator * adjust code * use new scan operator * use new scan operator * add log * fix issue * recover stream scan next function * fix issue * fix issue * fix issue * ignore disorder data * fix issue * fix issue for interval sliding * fix issue * fix ci issue * fix ci issue * fix ci issue * add semi && final nonblock interval operator * fix issue * fix issue * fix issue * fix issue * fix issue * fix issue * fix issue * fix issue * refactor(stream): track the msgId for each upstream tasks. * fix(stream): fix race condition. * fix(stream): update the task last msgId when putting into input queue succ. * fix issue * fix issue * put recalculate data to rocksdb * fix issue * fix issue * enh(query)[TD-33071]: add support for saving and restoring tsdbReader scan progress - Implement functionality to save scan progress during tsdbReader operations - Enable resuming scans from the last saved position * fix issue * fix issue * fix issue * fix issue * fix issue * add rec interval check * enh(stream):add recalculate tasks. * enh(stream): support the re-calculate the tasks. * fix issue && do refactor * do refactor * fix issue * fix issue * update backend opt * add new interface * add new session operator * support blob * add new session operator * fix issue * add rec state for rec task * fix invalid read * add new session window * enh(stream): update the stream tasks backend. * new session operator * add pull data * fix(stream): fix error in expand stream backend. * fix issue * fix issue * fix issue * merge code * fix issue * fix(stream): check for null ptr. * fix(stream): add more check. * fix issue * fix issue * fix issue * add debug code * fix issue * fix issue * fix issue * set rec end flag * fix(stream): opt re-calculate stream tasks. * fix issue * fix issue * add new operator * enh(stream): dispatch recalculate block to agg tasks. * fix issue * fix issue * fix(stream): adjust filter. * fix issue * refactor * refactor(stream): adjust the recalculate end block. * fix issue * fix(stream): set correct create trigger block. * fix issue * fix(stream): fix error in build recalculate end block. * fix(stream): check null ptr. * add stream client && fix issue * fix mem leak * fix(stream): free msg. * add stream client * fix(stream): fix error. * add stream client && fix issue * add stream client * refactor(stream): set the recalculate task complete. * add wend and group_id for session window dest table * feat(stream): refactor and set the recalcul agg task complete. * add cfg for adapter * fix issue * add state && event operator * feat(stream): support fill-history task. * add new fill operator * fix(stream): set correct backend when updating fill-history task to recalculate task. * add new fill operator * fix(stream): s2025-03-06 11:10:31.272 et ops always open in scan tsdb * fix(stream):set the correct taskType for sink task. * new fill operator * adj stream fill operator * fix issue * fix issue * fix issue * fix issue * fix issue * fix issue * fix issue * fix issue * adj test * fix issue * fix(stream): fix issue * fix(steam): fix issue * fix(steam): fix issue * fix(steam): fix issue * fix(steam): fix issue * fix(stream): fix issue * fix(stream): fix issue * fix(stream): fix issue * fix: ut com error * fix(stream): fix mem leak and adjust operator type check rule * fix(stream): fix mem leak and adjust test case * refactor code * fix(stream): free items. * fix(stream): free fix memory leak. * fix(stream): fix syntax error. * fix: ignore unexpect block * fix: adjust op type --------- Co-authored-by: Haojun Liao <hjliao@taosdata.com> Co-authored-by: Jinqing Kuang <kuangjinqingcn@gmail.com> Co-authored-by: yihaoDeng <luomoxyz@126.com>
2025-03-14 12:14:01 +00:00
}
return "error name";
2023-08-14 09:51:20 +00:00
}
2023-08-10 09:04:32 +00:00
void printDataBlock(SSDataBlock* pBlock, const char* flag, const char* taskIdStr) {
2024-08-09 09:36:19 +00:00
if (!pBlock) {
2025-05-13 03:35:52 +00:00
qDebug("%s===stream===%s: Block is Null", taskIdStr, flag);
2024-08-09 09:36:19 +00:00
return;
} else if (pBlock->info.rows == 0) {
2025-05-13 03:35:52 +00:00
qDebug("%s===stream===%s: Block is Empty. block type %d", taskIdStr, flag, pBlock->info.type);
2022-11-30 13:04:58 +00:00
return;
}
2025-03-28 10:10:57 +00:00
if (qDebugFlag & DEBUG_DEBUG) {
char* pBuf = NULL;
int32_t code = dumpBlockData(pBlock, flag, &pBuf, taskIdStr);
if (code == 0) {
qDebugL("%s", pBuf);
taosMemoryFree(pBuf);
}
2024-07-28 06:29:56 +00:00
}
2022-12-01 12:34:06 +00:00
}
2023-08-14 06:24:21 +00:00
2023-08-14 09:51:20 +00:00
void printSpecDataBlock(SSDataBlock* pBlock, const char* flag, const char* opStr, const char* taskIdStr) {
2023-12-28 07:34:13 +00:00
if (!pBlock) {
2024-01-05 08:20:55 +00:00
qDebug("%s===stream===%s %s: Block is Null", taskIdStr, flag, opStr);
2023-12-28 07:34:13 +00:00
return;
} else if (pBlock->info.rows == 0) {
2024-08-06 08:14:15 +00:00
qDebug("%s===stream===%s %s: Block is Empty. block type %d.skey:%" PRId64 ",ekey:%" PRId64 ",version%" PRId64,
taskIdStr, flag, opStr, pBlock->info.type, pBlock->info.window.skey, pBlock->info.window.ekey,
pBlock->info.version);
2023-08-14 09:51:20 +00:00
return;
}
2025-03-28 10:10:57 +00:00
if (qDebugFlag & DEBUG_DEBUG) {
2023-08-14 09:51:20 +00:00
char* pBuf = NULL;
2024-04-03 09:11:49 +00:00
char flagBuf[64];
2023-08-14 09:51:20 +00:00
snprintf(flagBuf, sizeof(flagBuf), "%s %s", flag, opStr);
2024-07-28 06:29:56 +00:00
int32_t code = dumpBlockData(pBlock, flagBuf, &pBuf, taskIdStr);
if (code == 0) {
2025-05-15 09:52:26 +00:00
qDebug("%s", pBuf);
2024-07-28 06:29:56 +00:00
taosMemoryFree(pBuf);
}
2023-08-14 09:51:20 +00:00
}
}
2023-08-14 06:24:21 +00:00
TSKEY getStartTsKey(STimeWindow* win, const TSKEY* tsCols) { return tsCols == NULL ? win->skey : tsCols[0]; }
2024-04-03 09:11:49 +00:00
void updateTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pWin, int64_t delta) {
2023-08-14 06:24:21 +00:00
int64_t* ts = (int64_t*)pColData->pData;
int64_t duration = pWin->ekey - pWin->skey + delta;
ts[2] = duration; // set the duration
ts[3] = pWin->skey; // window start key
ts[4] = pWin->ekey + delta; // window end key
}
2024-04-03 09:11:49 +00:00
int32_t compKeys(const SArray* pSortGroupCols, const char* oldkeyBuf, int32_t oldKeysLen, const SSDataBlock* pBlock,
int32_t rowIndex) {
SColumnDataAgg* pColAgg = NULL;
const char* isNull = oldkeyBuf;
2023-09-07 05:51:09 +00:00
const char* p = oldkeyBuf + sizeof(int8_t) * pSortGroupCols->size;
2023-09-07 05:51:09 +00:00
for (int32_t i = 0; i < pSortGroupCols->size; ++i) {
2024-04-03 09:11:49 +00:00
const SColumn* pCol = (SColumn*)TARRAY_GET_ELEM(pSortGroupCols, i);
2023-09-07 05:51:09 +00:00
const SColumnInfoData* pColInfoData = TARRAY_GET_ELEM(pBlock->pDataBlock, pCol->slotId);
2024-06-04 03:39:47 +00:00
if (pBlock->pBlockAgg) pColAgg = &pBlock->pBlockAgg[pCol->slotId];
if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
if (isNull[i] != 1) return 1;
} else {
if (isNull[i] != 0) return 1;
const char* val = colDataGetData(pColInfoData, rowIndex);
if (pCol->type == TSDB_DATA_TYPE_JSON) {
int32_t len = getJsonValueLen(val);
if (memcmp(p, val, len) != 0) return 1;
p += len;
} else if (IS_VAR_DATA_TYPE(pCol->type)) {
if (memcmp(p, val, varDataTLen(val)) != 0) return 1;
p += varDataTLen(val);
} else {
if (0 != memcmp(p, val, pCol->bytes)) return 1;
p += pCol->bytes;
}
}
}
if ((int32_t)(p - oldkeyBuf) != oldKeysLen) return 1;
return 0;
}
2024-04-03 09:11:49 +00:00
int32_t buildKeys(char* keyBuf, const SArray* pSortGroupCols, const SSDataBlock* pBlock, int32_t rowIndex) {
2023-09-07 05:51:09 +00:00
uint32_t colNum = pSortGroupCols->size;
SColumnDataAgg* pColAgg = NULL;
char* isNull = keyBuf;
char* p = keyBuf + sizeof(int8_t) * colNum;
for (int32_t i = 0; i < colNum; ++i) {
2023-09-07 05:51:09 +00:00
const SColumn* pCol = (SColumn*)TARRAY_GET_ELEM(pSortGroupCols, i);
const SColumnInfoData* pColInfoData = TARRAY_GET_ELEM(pBlock->pDataBlock, pCol->slotId);
if (pCol->slotId > pBlock->pDataBlock->size) continue;
2024-06-04 03:39:47 +00:00
if (pBlock->pBlockAgg) pColAgg = &pBlock->pBlockAgg[pCol->slotId];
if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
isNull[i] = 1;
} else {
isNull[i] = 0;
const char* val = colDataGetData(pColInfoData, rowIndex);
if (pCol->type == TSDB_DATA_TYPE_JSON) {
int32_t len = getJsonValueLen(val);
memcpy(p, val, len);
p += len;
} else if (IS_VAR_DATA_TYPE(pCol->type)) {
varDataCopy(p, val);
p += varDataTLen(val);
} else {
memcpy(p, val, pCol->bytes);
p += pCol->bytes;
}
}
}
return (int32_t)(p - keyBuf);
}
uint64_t calcGroupId(char* pData, int32_t len) {
T_MD5_CTX context;
tMD5Init(&context);
tMD5Update(&context, (uint8_t*)pData, len);
tMD5Final(&context);
// NOTE: only extract the initial 8 bytes of the final MD5 digest
uint64_t id = 0;
memcpy(&id, context.digest, sizeof(uint64_t));
if (0 == id) memcpy(&id, context.digest + 8, sizeof(uint64_t));
return id;
}
SNodeList* makeColsNodeArrFromSortKeys(SNodeList* pSortKeys) {
2024-04-03 09:11:49 +00:00
SNode* node;
SNodeList* ret = NULL;
FOREACH(node, pSortKeys) {
SOrderByExprNode* pSortKey = (SOrderByExprNode*)node;
2024-07-23 02:50:16 +00:00
int32_t code = nodesListMakeAppend(&ret, pSortKey->pExpr);
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
2024-12-11 11:18:50 +00:00
terrno = code;
2024-07-23 02:50:16 +00:00
return NULL;
}
}
return ret;
}
2024-08-05 08:09:01 +00:00
int32_t extractKeysLen(const SArray* keys, int32_t* pLen) {
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
int32_t len = 0;
int32_t keyNum = taosArrayGetSize(keys);
for (int32_t i = 0; i < keyNum; ++i) {
SColumn* pCol = (SColumn*)taosArrayGet(keys, i);
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL(pCol, code, lino, _end, terrno);
len += pCol->bytes;
}
2024-04-03 09:11:49 +00:00
len += sizeof(int8_t) * keyNum; // null flag
2024-08-05 08:09:01 +00:00
*pLen = len;
_end:
if (code != TSDB_CODE_SUCCESS) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
}
return code;
}