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

836 lines
26 KiB
C
Raw Normal View History

2022-07-06 00:57:10 +00:00
/*
* 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 "dataSinkInt.h"
#include "dataSinkMgt.h"
2023-04-28 03:42:34 +00:00
#include "executorInt.h"
2025-04-21 08:44:27 +00:00
#include "functionMgt.h"
2022-07-06 00:57:10 +00:00
#include "planner.h"
2025-04-21 08:44:27 +00:00
#include "query.h"
#include "querytask.h"
#include "storageapi.h"
2022-07-06 00:57:10 +00:00
#include "tcompression.h"
#include "tdatablock.h"
#include "tglobal.h"
#include "tqueue.h"
extern SDataSinkStat gDataSinkStat;
2022-07-06 08:29:51 +00:00
typedef struct SSubmitRes {
2023-01-04 03:43:20 +00:00
int64_t affectedRows;
int32_t code;
2022-12-15 16:43:19 +00:00
SSubmitRsp2* pRsp;
2022-07-06 08:29:51 +00:00
} SSubmitRes;
2022-07-06 00:57:10 +00:00
typedef struct SDataInserterHandle {
SDataSinkHandle sink;
SDataSinkManager* pManager;
2022-07-06 08:29:51 +00:00
STSchema* pSchema;
SQueryInserterNode* pNode;
SSubmitRes submitRes;
SInserterParam* pParam;
SArray* pDataBlocks;
2022-07-07 03:38:58 +00:00
SHashObj* pCols;
2022-07-06 00:57:10 +00:00
int32_t status;
bool queryEnd;
bool fullOrderColList;
2022-07-06 00:57:10 +00:00
uint64_t useconds;
uint64_t cachedSize;
2024-07-01 09:22:43 +00:00
uint64_t flags;
2022-07-06 00:57:10 +00:00
TdThreadMutex mutex;
2022-10-13 05:41:36 +00:00
tsem_t ready;
2023-02-07 10:35:42 +00:00
bool explain;
2025-04-21 08:44:27 +00:00
const char* dbFName;
SHashObj* dbVgInfoMap; // 存储数据库和vgroup信息的映射
SUseDbRsp* pRsp; // 用于存储数据库信息响应
// SExecTaskInfo* pTaskInfo; // 用于存储任务信息
2022-07-06 00:57:10 +00:00
} SDataInserterHandle;
2022-07-06 08:29:51 +00:00
typedef struct SSubmitRspParam {
SDataInserterHandle* pInserter;
} SSubmitRspParam;
2022-07-06 00:57:10 +00:00
2022-07-06 08:29:51 +00:00
int32_t inserterCallback(void* param, SDataBuf* pMsg, int32_t code) {
2022-10-13 05:41:36 +00:00
SSubmitRspParam* pParam = (SSubmitRspParam*)param;
2022-07-06 08:29:51 +00:00
SDataInserterHandle* pInserter = pParam->pInserter;
2024-09-12 06:17:14 +00:00
int32_t code2 = 0;
2022-07-06 00:57:10 +00:00
if (code) {
pInserter->submitRes.code = code;
}
2024-09-05 06:40:14 +00:00
2022-07-06 08:29:51 +00:00
if (code == TSDB_CODE_SUCCESS) {
pInserter->submitRes.pRsp = taosMemoryCalloc(1, sizeof(SSubmitRsp2));
if (NULL == pInserter->submitRes.pRsp) {
pInserter->submitRes.code = terrno;
goto _return;
}
2024-09-05 06:40:14 +00:00
2022-10-13 05:41:36 +00:00
SDecoder coder = {0};
2022-07-06 08:29:51 +00:00
tDecoderInit(&coder, pMsg->pData, pMsg->len);
2022-12-15 16:43:19 +00:00
code = tDecodeSSubmitRsp2(&coder, pInserter->submitRes.pRsp);
2022-07-06 08:29:51 +00:00
if (code) {
2022-12-16 11:07:09 +00:00
taosMemoryFree(pInserter->submitRes.pRsp);
2022-07-06 08:29:51 +00:00
pInserter->submitRes.code = code;
goto _return;
}
2022-10-13 05:41:36 +00:00
2022-12-15 16:43:19 +00:00
if (pInserter->submitRes.pRsp->affectedRows > 0) {
SArray* pCreateTbList = pInserter->submitRes.pRsp->aCreateTbRsp;
int32_t numOfTables = taosArrayGetSize(pCreateTbList);
for (int32_t i = 0; i < numOfTables; ++i) {
SVCreateTbRsp* pRsp = taosArrayGet(pCreateTbList, i);
if (NULL == pRsp) {
pInserter->submitRes.code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
goto _return;
}
2022-12-15 16:43:19 +00:00
if (TSDB_CODE_SUCCESS != pRsp->code) {
code = pRsp->code;
2022-12-16 11:07:09 +00:00
taosMemoryFree(pInserter->submitRes.pRsp);
2022-07-06 08:29:51 +00:00
pInserter->submitRes.code = code;
goto _return;
}
}
}
2022-10-13 05:41:36 +00:00
pInserter->submitRes.affectedRows += pInserter->submitRes.pRsp->affectedRows;
2023-01-04 03:43:20 +00:00
qDebug("submit rsp received, affectedRows:%d, total:%" PRId64, pInserter->submitRes.pRsp->affectedRows,
2022-10-13 05:41:36 +00:00
pInserter->submitRes.affectedRows);
2022-12-16 11:07:09 +00:00
tDecoderClear(&coder);
taosMemoryFree(pInserter->submitRes.pRsp);
2022-07-06 08:29:51 +00:00
}
2022-07-06 00:57:10 +00:00
2022-07-06 08:29:51 +00:00
_return:
2024-09-12 06:17:14 +00:00
code2 = tsem_post(&pInserter->ready);
if (code2 < 0) {
qError("tsem_post inserter ready failed, error:%s", tstrerror(code2));
if (TSDB_CODE_SUCCESS == code) {
pInserter->submitRes.code = code2;
}
}
2022-07-16 10:58:29 +00:00
taosMemoryFree(pMsg->pData);
2024-09-05 06:40:14 +00:00
2022-07-06 08:29:51 +00:00
return TSDB_CODE_SUCCESS;
2022-07-06 00:57:10 +00:00
}
2025-04-21 08:44:27 +00:00
void freeUseDbOutput_tmp(void* pOutput) {
SUseDbOutput* pOut = *(SUseDbOutput**)pOutput;
if (NULL == pOutput) {
return;
}
if (pOut->dbVgroup) {
freeVgInfo(pOut->dbVgroup);
}
taosMemFree(pOut);
}
// 处理数据库信息响应的回调函数
static int32_t processUseDbRspForInserter(void* param, SDataBuf* pMsg, int32_t code) {
int32_t lino = 0;
SDataInserterHandle* pInserter = (SDataInserterHandle*)param;
if (TSDB_CODE_SUCCESS != code) {
// pInserter->pTaskInfo->code = rpcCvtErrCode(code);
// if (pInserter->pTaskInfo->code != code) {
// qError("load db info rsp received, error:%s, cvted error:%s", tstrerror(code),
// tstrerror(pInserter->pTaskInfo->code));
// } else {
// qError("load db info rsp received, error:%s", tstrerror(code));
// }
goto _return;
}
// 分配响应结构内存
pInserter->pRsp = taosMemoryMalloc(sizeof(SUseDbRsp));
QUERY_CHECK_NULL(pInserter->pRsp, code, lino, _return, terrno);
// 反序列化响应数据
code = tDeserializeSUseDbRsp(pMsg->pData, (int32_t)pMsg->len, pInserter->pRsp);
QUERY_CHECK_CODE(code, lino, _return);
// 释放消息数据
taosMemoryFreeClear(pMsg->pData);
// 释放信号量,通知等待的线程响应已到达
code = tsem_post(&pInserter->ready);
QUERY_CHECK_CODE(code, lino, _return);
return code;
_return:
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
return code;
}
// 辅助函数为inserter构建数据库vgroup信息映射
static int32_t buildDbVgInfoMapForInserter(SDataInserterHandle* pInserter, SReadHandle* pHandle, const char* dbFName,
SUseDbOutput* output) {
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
char* buf1 = NULL;
SUseDbReq* pReq = NULL;
// SExecTaskInfo* pTaskInfo = pInserter->pTaskInfo;
// 分配并初始化请求结构
pReq = taosMemoryMalloc(sizeof(SUseDbReq));
QUERY_CHECK_NULL(pReq, code, lino, _return, terrno);
// 获取完整数据库名称
tstrncpy(pReq->db, dbFName, TSDB_DB_FNAME_LEN);
QUERY_CHECK_CODE(code, lino, _return);
// 计算序列化后的长度
int32_t contLen = tSerializeSUseDbReq(NULL, 0, pReq);
buf1 = taosMemoryCalloc(1, contLen);
QUERY_CHECK_NULL(buf1, code, lino, _return, terrno);
// 序列化请求数据
int32_t tempRes = tSerializeSUseDbReq(buf1, contLen, pReq);
if (tempRes < 0) {
QUERY_CHECK_CODE(terrno, lino, _return);
}
// 创建消息发送信息
SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
QUERY_CHECK_NULL(pMsgSendInfo, code, lino, _return, terrno);
// 设置消息参数
pMsgSendInfo->param = pInserter;
pMsgSendInfo->msgInfo.pData = buf1;
pMsgSendInfo->msgInfo.len = contLen;
pMsgSendInfo->msgType = TDMT_MND_GET_DB_INFO;
pMsgSendInfo->fp = processUseDbRspForInserter;
// pMsgSendInfo->requestId = pTaskInfo->id.queryId;
// 初始化信号量,用于等待响应
tsem_init(&pInserter->ready, 0, 0);
// 异步发送消息到服务器
code = asyncSendMsgToServer(pHandle->pMsgCb->clientRpc, &pInserter->pNode->epSet, NULL, pMsgSendInfo);
QUERY_CHECK_CODE(code, lino, _return);
// 等待响应
code = tsem_wait(&pInserter->ready);
QUERY_CHECK_CODE(code, lino, _return);
// 构建输出
code = queryBuildUseDbOutput(output, pInserter->pRsp);
QUERY_CHECK_CODE(code, lino, _return);
_return:
if (code) {
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
taosMemoryFree(buf1);
}
taosMemoryFree(pReq);
if (pInserter->pRsp) {
tFreeSUsedbRsp(pInserter->pRsp);
taosMemoryFreeClear(pInserter->pRsp);
}
return code;
}
int32_t inserterHashValueComp(void const* lp, void const* rp) {
uint32_t* key = (uint32_t*)lp;
SVgroupInfo* pVg = (SVgroupInfo*)rp;
if (*key < pVg->hashBegin) {
return -1;
} else if (*key > pVg->hashEnd) {
return 1;
}
return 0;
}
int inserterVgInfoComp(const void* lp, const void* rp) {
SVgroupInfo* pLeft = (SVgroupInfo*)lp;
SVgroupInfo* pRight = (SVgroupInfo*)rp;
if (pLeft->hashBegin < pRight->hashBegin) {
return -1;
} else if (pLeft->hashBegin > pRight->hashBegin) {
return 1;
}
return 0;
}
int32_t inserterGetVgId(SDBVgInfo* dbInfo, char* tbName, int32_t* vgId) {
if (NULL == dbInfo) {
return TSDB_CODE_CTG_INTERNAL_ERROR;
}
if (dbInfo->vgHash && NULL == dbInfo->vgArray) {
int32_t vgSize = taosHashGetSize(dbInfo->vgHash);
dbInfo->vgArray = taosArrayInit(vgSize, sizeof(SVgroupInfo));
if (NULL == dbInfo->vgArray) {
return terrno;
}
void* pIter = taosHashIterate(dbInfo->vgHash, NULL);
while (pIter) {
if (NULL == taosArrayPush(dbInfo->vgArray, pIter)) {
taosHashCancelIterate(dbInfo->vgHash, pIter);
return terrno;
}
pIter = taosHashIterate(dbInfo->vgHash, pIter);
}
taosArraySort(dbInfo->vgArray, inserterVgInfoComp);
}
uint32_t hashValue =
taosGetTbHashVal(tbName, (int32_t)strlen(tbName), dbInfo->hashMethod, dbInfo->hashPrefix, dbInfo->hashSuffix);
SVgroupInfo* vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, inserterHashValueComp, TD_EQ);
if (NULL == vgInfo) {
qError("no hash range found for hash value [%u], table:%s, numOfVgId:%d", hashValue, tbName,
(int32_t)taosArrayGetSize(dbInfo->vgArray));
return TSDB_CODE_CTG_INTERNAL_ERROR;
}
*vgId = vgInfo->vgId;
return TSDB_CODE_SUCCESS;
}
int32_t inserterGetDbVgInfo(SDataInserterHandle* pInserter, const char* dbFName, SDBVgInfo** dbVgInfo) {
int32_t code = TSDB_CODE_SUCCESS;
int32_t line = 0;
SUseDbOutput* output = NULL;
// QRY_PARAM_CHECK(dbVgInfo);
// QRY_PARAM_CHECK(pInserter);
// QRY_PARAM_CHECK(name);
// 如果dbVgInfoMap不存在创建它
if (pInserter->dbVgInfoMap == NULL) {
pInserter->dbVgInfoMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
if (pInserter->dbVgInfoMap == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
}
// 查找是否已经存在该数据库的信息
SUseDbOutput** find = (SUseDbOutput**)taosHashGet(pInserter->dbVgInfoMap, dbFName, strlen(dbFName));
if (find == NULL) {
// 如果不存在,创建新的输出
output = taosMemoryMalloc(sizeof(SUseDbOutput));
if (output == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
// 构建数据库vgroup信息映射
code = buildDbVgInfoMapForInserter(pInserter, pInserter->pParam->readHandle, dbFName, output);
QUERY_CHECK_CODE(code, line, _return);
// 将新创建的信息添加到哈希表中
code = taosHashPut(pInserter->dbVgInfoMap, dbFName, strlen(dbFName), &output, POINTER_BYTES);
QUERY_CHECK_CODE(code, line, _return);
} else {
output = *find;
}
*dbVgInfo = output->dbVgroup;
return code;
_return:
qError("%s failed at line %d since %s", __func__, line, tstrerror(code));
freeUseDbOutput_tmp(output);
return code;
}
2023-01-04 03:43:20 +00:00
static int32_t sendSubmitRequest(SDataInserterHandle* pInserter, void* pMsg, int32_t msgLen, void* pTransporter,
SEpSet* pEpset) {
2022-07-06 08:29:51 +00:00
// send the fetch remote task result reques
SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
if (NULL == pMsgSendInfo) {
taosMemoryFreeClear(pMsg);
return terrno;
2022-07-06 00:57:10 +00:00
}
2022-07-06 08:29:51 +00:00
SSubmitRspParam* pParam = taosMemoryCalloc(1, sizeof(SSubmitRspParam));
if (NULL == pParam) {
taosMemoryFreeClear(pMsg);
taosMemoryFreeClear(pMsgSendInfo);
return terrno;
}
2022-07-06 08:29:51 +00:00
pParam->pInserter = pInserter;
2022-07-06 00:57:10 +00:00
2022-07-06 08:29:51 +00:00
pMsgSendInfo->param = pParam;
2024-11-11 01:09:40 +00:00
pMsgSendInfo->paramFreeFp = taosAutoMemoryFree;
2022-07-06 08:29:51 +00:00
pMsgSendInfo->msgInfo.pData = pMsg;
pMsgSendInfo->msgInfo.len = msgLen;
2022-07-06 08:29:51 +00:00
pMsgSendInfo->msgType = TDMT_VND_SUBMIT;
pMsgSendInfo->fp = inserterCallback;
2022-07-06 00:57:10 +00:00
2024-09-05 06:40:14 +00:00
return asyncSendMsgToServer(pTransporter, pEpset, NULL, pMsgSendInfo);
2022-07-06 00:57:10 +00:00
}
static int32_t submitReqToMsg(int32_t vgId, SSubmitReq2* pReq, void** pData, int32_t* pLen) {
2023-01-04 03:43:20 +00:00
int32_t code = TSDB_CODE_SUCCESS;
int32_t len = 0;
2023-01-04 03:43:20 +00:00
void* pBuf = NULL;
2023-05-04 08:15:14 +00:00
tEncodeSize(tEncodeSubmitReq, pReq, len, code);
if (TSDB_CODE_SUCCESS == code) {
SEncoder encoder;
len += sizeof(SSubmitReq2Msg);
pBuf = taosMemoryMalloc(len);
if (NULL == pBuf) {
return terrno;
}
((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
2023-05-04 08:15:14 +00:00
code = tEncodeSubmitReq(&encoder, pReq);
tEncoderClear(&encoder);
}
2022-07-07 01:42:20 +00:00
if (TSDB_CODE_SUCCESS == code) {
*pData = pBuf;
*pLen = len;
} else {
taosMemoryFree(pBuf);
}
2024-09-05 06:40:14 +00:00
return code;
}
2022-07-06 13:00:31 +00:00
2023-01-04 03:43:20 +00:00
int32_t buildSubmitReqFromBlock(SDataInserterHandle* pInserter, SSubmitReq2** ppReq, const SSDataBlock* pDataBlock,
const STSchema* pTSchema, int64_t uid, int32_t vgId, tb_uid_t suid) {
SSubmitReq2* pReq = *ppReq;
SArray* pVals = NULL;
int32_t numOfBlks = 0;
terrno = TSDB_CODE_SUCCESS;
if (NULL == pReq) {
if (!(pReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
goto _end;
}
if (!(pReq->aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
goto _end;
}
2022-07-06 13:00:31 +00:00
}
int32_t colNum = taosArrayGetSize(pDataBlock->pDataBlock);
int32_t rows = pDataBlock->info.rows;
2022-07-06 13:00:31 +00:00
SSubmitTbData tbData = {0};
if (!(tbData.aRowP = taosArrayInit(rows, sizeof(SRow*)))) {
goto _end;
}
tbData.suid = suid;
tbData.uid = uid;
tbData.sver = pTSchema->version;
2022-07-06 13:00:31 +00:00
if (!pVals && !(pVals = taosArrayInit(colNum, sizeof(SColVal)))) {
taosArrayDestroy(tbData.aRowP);
goto _end;
}
2022-07-06 13:00:31 +00:00
int64_t lastTs = TSKEY_MIN;
2024-04-08 10:23:16 +00:00
bool needSortMerge = false;
2022-10-13 05:41:36 +00:00
for (int32_t j = 0; j < rows; ++j) { // iterate by row
taosArrayClear(pVals);
2022-07-07 03:38:58 +00:00
int32_t offset = 0;
2025-04-21 08:44:27 +00:00
SColumnInfoData* tbname = taosArrayGet(pDataBlock->pDataBlock, 0);
if (NULL == tbname) {
terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
qError("Insert into stable must have tbname column");
goto _end;
}
if (tbname->info.type != TSDB_DATA_TYPE_BINARY) {
terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
qError("tbname column must be binary");
goto _end;
}
if (colDataIsNull_s(tbname, j)) {
SColVal cv = COL_VAL_NULL(0, TSDB_DATA_TYPE_VARCHAR);
if (NULL == taosArrayPush(pVals, &cv)) {
goto _end;
}
} else {
void* data = colDataGetVarData(tbname, j);
SValue sv = (SValue){TSDB_DATA_TYPE_VARCHAR, .nData = varDataLen(data),
.pData = varDataVal(data)}; // address copy, no value
SColVal cv = COL_VAL_VALUE(0, sv);
SDBVgInfo* dbInfo = NULL;
int32_t code = inserterGetDbVgInfo(pInserter, pInserter->dbFName, &dbInfo);
if (code != TSDB_CODE_SUCCESS) {
goto _end;
}
char* tbFullName = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
sprintf(tbFullName, "%s.%s", pInserter->dbFName, sv.pData);
int32_t vgId = 0;
code = inserterGetVgId(dbInfo, tbFullName, &vgId);
if (code != TSDB_CODE_SUCCESS) {
goto _end;
}
// *vgId = vgInfo->vgId;
// if (NULL == taosArrayPush(pVals, &cv)) {
// goto _end;
// }
}
for (int32_t k = 0; k < pTSchema->numOfCols; ++k) { // iterate by column
2023-01-04 03:43:20 +00:00
int16_t colIdx = k;
const STColumn* pCol = &pTSchema->columns[k];
if (!pInserter->fullOrderColList) {
int16_t* slotId = taosHashGet(pInserter->pCols, &pCol->colId, sizeof(pCol->colId));
if (NULL == slotId) {
continue;
2022-07-08 02:27:17 +00:00
}
2022-10-13 05:41:36 +00:00
colIdx = *slotId;
}
2022-10-13 05:41:36 +00:00
SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, colIdx);
if (NULL == pColInfoData) {
terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
goto _end;
}
2024-09-05 06:40:14 +00:00
void* var = POINTER_SHIFT(pColInfoData->pData, j * pColInfoData->info.bytes);
switch (pColInfoData->info.type) {
case TSDB_DATA_TYPE_NCHAR:
2023-09-01 05:24:47 +00:00
case TSDB_DATA_TYPE_VARBINARY:
case TSDB_DATA_TYPE_VARCHAR: { // TSDB_DATA_TYPE_BINARY
2024-08-21 06:41:14 +00:00
if (pColInfoData->info.type != pCol->type) {
2024-09-05 06:40:14 +00:00
qError("column:%d type:%d in block dismatch with schema col:%d type:%d", colIdx, pColInfoData->info.type, k,
pCol->type);
2024-08-21 06:41:14 +00:00
terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
goto _end;
}
if (colDataIsNull_s(pColInfoData, j)) {
SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
if (NULL == taosArrayPush(pVals, &cv)) {
goto _end;
}
} else {
2024-02-21 06:29:10 +00:00
void* data = colDataGetVarData(pColInfoData, j);
SValue sv = (SValue){
.type = pCol->type, .nData = varDataLen(data), .pData = varDataVal(data)}; // address copy, no value
SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
if (NULL == taosArrayPush(pVals, &cv)) {
goto _end;
}
2022-07-08 06:26:53 +00:00
}
break;
}
case TSDB_DATA_TYPE_BLOB:
case TSDB_DATA_TYPE_JSON:
case TSDB_DATA_TYPE_MEDIUMBLOB:
qError("the column type %" PRIi16 " is defined but not implemented yet", pColInfoData->info.type);
terrno = TSDB_CODE_APP_ERROR;
goto _end;
break;
default:
if (pColInfoData->info.type < TSDB_DATA_TYPE_MAX && pColInfoData->info.type > TSDB_DATA_TYPE_NULL) {
if (colDataIsNull_s(pColInfoData, j)) {
if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId) {
qError("Primary timestamp column should not be null");
terrno = TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL;
goto _end;
}
2024-02-21 06:29:10 +00:00
SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type); // should use pCol->type
if (NULL == taosArrayPush(pVals, &cv)) {
goto _end;
}
2022-07-08 06:26:53 +00:00
} else {
2024-04-08 10:23:16 +00:00
if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId && !needSortMerge) {
if (*(int64_t*)var <= lastTs) {
needSortMerge = true;
} else {
lastTs = *(int64_t*)var;
}
}
2023-01-04 03:43:20 +00:00
2024-02-21 06:29:10 +00:00
SValue sv = {.type = pCol->type};
feat(decimal): support decimal data type (#30060) * decimal: create table * decimal: add test case decimal.py * decimal: add decimal.c * support input decimal * decimal test * refactor svalue * fix test cases * add decimal unit test * add decimal test cmake * support insert and query decimal type * define wide integer, support decimal128 * support decimal128 divide * set decimal type expr res types * scalar decimal * convert to decimal * fix decimal64/128 from str and to str * fix decimal from str and decimal to str * decimal simple conversion * unit test for decimal * decimal conversion and unit tests * decimal + - * / * decimal scalar ops and comparision * start to refactor GET_TYPED_DATA * support decimal max func, cast func * refactor GET_TYPED_DATA interface * decimal scalar comparision * start to implement sum for decimal * support sum and avg for decimal type * decimal tests * add decimal test * decimal add test cases * decimal use int256/int128 * decimal testing * fix decimal table meta and add tests for decimal col streams * fix create stream and create tsma * test insert decimal values * decimal from str * test decimal input * test parse decimal from string * add taos_fetch_field_e api * decimal insert tests * test decimal operators * decimal operator test * feat:support decimal in raw block * decimal operator tests * decimal test * feat:support decimal in raw block * feat:support decimal in raw block * feat:add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * decimal test operators * decimal operator test * test decimal operators * test decimal compare operators * decimal unary operator test * decimal col with decimal col oper test * test decimal col filtering * fix decimal float operator test * decimal test where filtering * fix decimal filtering * fix decimal order by * fix decimal op test * test decimal agg funcs * test decimal functions * remove assert * fix ci build for ret check * fix decimal windows build * fix ci ret check * skip decimal ret check * skip decimal ret check * fix decimal tests * fix decimal ci test * decimal test * fix(tmq): heap user after free * fix(tmq): double free * fix(tmq): double free * fix decimal tests * fix(decimal): decimal test ci build * fix(decimal): windows build * fix(decimal): decimal test build * fix(decimal): fix decimal build and tests * fix(decimal): fix decimal tests * fix(decimal): fix taos_fetch_fields_e api * fix(decimal): fix decimal taos_fetch_fields_e api * fix(decimal): rebase 3.0 * fix(decimal): fix decimal functions * fix(decimal): fix decimal test case memory leak * fix(decimal): fix decimal tests * fix(decimal): fix decimal test case * fix(decimal): fix decimal tests * feat(decimal): fix unit tests * feat(decimal): fix deicmal unit test --------- Co-authored-by: wangmm0220 <wangmm0220@gmail.com> Co-authored-by: yihaoDeng <yhdeng@taosdata.com>
2025-03-14 10:08:07 +00:00
valueSetDatum(&sv, sv.type, var, tDataTypes[pCol->type].bytes);
2024-02-21 06:29:10 +00:00
SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
if (NULL == taosArrayPush(pVals, &cv)) {
goto _end;
}
2022-07-08 06:26:53 +00:00
}
} else {
uError("the column type %" PRIi16 " is undefined\n", pColInfoData->info.type);
terrno = TSDB_CODE_APP_ERROR;
goto _end;
2022-07-08 06:26:53 +00:00
}
break;
2022-07-06 13:00:31 +00:00
}
}
2022-07-06 13:00:31 +00:00
SRow* pRow = NULL;
if ((terrno = tRowBuild(pVals, pTSchema, &pRow)) < 0) {
2023-05-04 08:15:14 +00:00
tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
goto _end;
}
if (NULL == taosArrayPush(tbData.aRowP, &pRow)) {
goto _end;
}
}
2022-07-06 13:00:31 +00:00
2024-04-08 10:23:16 +00:00
if (needSortMerge) {
2023-09-18 05:46:29 +00:00
if ((tRowSort(tbData.aRowP) != TSDB_CODE_SUCCESS) ||
2024-02-21 06:29:10 +00:00
(terrno = tRowMerge(tbData.aRowP, (STSchema*)pTSchema, 0)) != 0) {
goto _end;
}
2022-07-06 13:00:31 +00:00
}
if (NULL == taosArrayPush(pReq->aSubmitTbData, &tbData)) {
goto _end;
}
2022-07-06 13:00:31 +00:00
_end:
taosArrayDestroy(pVals);
if (terrno != 0) {
*ppReq = NULL;
if (pReq) {
2023-05-04 08:15:14 +00:00
tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
taosMemoryFree(pReq);
}
2024-09-05 06:40:14 +00:00
return terrno;
}
*ppReq = pReq;
2024-09-05 06:40:14 +00:00
2022-07-08 02:27:17 +00:00
return TSDB_CODE_SUCCESS;
2022-07-06 13:00:31 +00:00
}
int32_t dataBlocksToSubmitReq(SDataInserterHandle* pInserter, void** pMsg, int32_t* msgLen) {
const SArray* pBlocks = pInserter->pDataBlocks;
const STSchema* pTSchema = pInserter->pSchema;
int64_t uid = pInserter->pNode->tableId;
int64_t suid = pInserter->pNode->stableId;
int32_t vgId = pInserter->pNode->vgId;
int32_t sz = taosArrayGetSize(pBlocks);
int32_t code = 0;
2023-01-04 03:43:20 +00:00
SSubmitReq2* pReq = NULL;
for (int32_t i = 0; i < sz; i++) {
2025-04-14 11:02:02 +00:00
SSDataBlock* pDataBlock = taosArrayGetP(pBlocks, i); // pDataBlock select查询到的结果
if (NULL == pDataBlock) {
return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
}
code = buildSubmitReqFromBlock(pInserter, &pReq, pDataBlock, pTSchema, uid, vgId, suid);
if (code) {
if (pReq) {
2023-05-04 08:15:14 +00:00
tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
taosMemoryFree(pReq);
}
return code;
}
}
code = submitReqToMsg(vgId, pReq, pMsg, msgLen);
2023-05-04 08:15:14 +00:00
tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
taosMemoryFree(pReq);
2023-01-04 03:43:20 +00:00
return code;
}
2022-07-06 08:29:51 +00:00
static int32_t putDataBlock(SDataSinkHandle* pHandle, const SInputData* pInput, bool* pContinue) {
SDataInserterHandle* pInserter = (SDataInserterHandle*)pHandle;
2023-02-07 10:35:42 +00:00
if (!pInserter->explain) {
if (NULL == taosArrayPush(pInserter->pDataBlocks, &pInput->pData)) {
return terrno;
}
2023-02-07 10:35:42 +00:00
void* pMsg = NULL;
int32_t msgLen = 0;
int32_t code = dataBlocksToSubmitReq(pInserter, &pMsg, &msgLen);
if (code) {
return code;
}
2022-07-06 00:57:10 +00:00
2023-02-07 10:35:42 +00:00
taosArrayClear(pInserter->pDataBlocks);
2023-01-04 03:43:20 +00:00
2023-02-07 10:35:42 +00:00
code = sendSubmitRequest(pInserter, pMsg, msgLen, pInserter->pParam->readHandle->pMsgCb->clientRpc,
&pInserter->pNode->epSet);
if (code) {
return code;
}
2022-07-06 00:57:10 +00:00
QRY_ERR_RET(tsem_wait(&pInserter->ready));
2022-07-06 00:57:10 +00:00
2023-02-07 10:35:42 +00:00
if (pInserter->submitRes.code) {
return pInserter->submitRes.code;
}
2022-07-06 00:57:10 +00:00
}
2022-07-06 08:29:51 +00:00
*pContinue = true;
2022-10-13 05:41:36 +00:00
2022-07-06 00:57:10 +00:00
return TSDB_CODE_SUCCESS;
}
2022-07-06 08:29:51 +00:00
static void endPut(struct SDataSinkHandle* pHandle, uint64_t useconds) {
SDataInserterHandle* pInserter = (SDataInserterHandle*)pHandle;
(void)taosThreadMutexLock(&pInserter->mutex);
2022-07-06 08:29:51 +00:00
pInserter->queryEnd = true;
pInserter->useconds = useconds;
(void)taosThreadMutexUnlock(&pInserter->mutex);
2022-07-06 08:29:51 +00:00
}
static void getDataLength(SDataSinkHandle* pHandle, int64_t* pLen, int64_t* pRawLen, bool* pQueryEnd) {
2022-07-07 00:53:23 +00:00
SDataInserterHandle* pDispatcher = (SDataInserterHandle*)pHandle;
*pLen = pDispatcher->submitRes.affectedRows;
2022-10-13 05:41:36 +00:00
qDebug("got total affectedRows %" PRId64, *pLen);
2022-07-07 00:53:23 +00:00
}
2022-07-06 00:57:10 +00:00
static int32_t destroyDataSinker(SDataSinkHandle* pHandle) {
2022-07-06 08:29:51 +00:00
SDataInserterHandle* pInserter = (SDataInserterHandle*)pHandle;
(void)atomic_sub_fetch_64(&gDataSinkStat.cachedSize, pInserter->cachedSize);
2022-07-06 08:29:51 +00:00
taosArrayDestroy(pInserter->pDataBlocks);
2024-11-11 01:09:40 +00:00
taosMemoryFree(pInserter->pSchema);
2022-07-16 10:58:29 +00:00
taosMemoryFree(pInserter->pParam);
taosHashCleanup(pInserter->pCols);
2024-11-15 02:16:15 +00:00
nodesDestroyNode((SNode *)pInserter->pNode);
pInserter->pNode = NULL;
(void)taosThreadMutexDestroy(&pInserter->mutex);
2024-02-21 06:29:10 +00:00
2023-08-22 10:29:25 +00:00
taosMemoryFree(pInserter->pManager);
2025-04-21 08:44:27 +00:00
if (pInserter->dbVgInfoMap) {
taosHashCleanup(pInserter->dbVgInfoMap);
}
2022-07-06 00:57:10 +00:00
return TSDB_CODE_SUCCESS;
}
static int32_t getCacheSize(struct SDataSinkHandle* pHandle, uint64_t* size) {
2022-07-06 01:09:34 +00:00
SDataInserterHandle* pDispatcher = (SDataInserterHandle*)pHandle;
2022-07-06 00:57:10 +00:00
*size = atomic_load_64(&pDispatcher->cachedSize);
return TSDB_CODE_SUCCESS;
}
2024-07-01 09:22:43 +00:00
static int32_t getSinkFlags(struct SDataSinkHandle* pHandle, uint64_t* pFlags) {
SDataInserterHandle* pDispatcher = (SDataInserterHandle*)pHandle;
*pFlags = atomic_load_64(&pDispatcher->flags);
return TSDB_CODE_SUCCESS;
}
2024-11-15 02:16:15 +00:00
int32_t createDataInserter(SDataSinkManager* pManager, SDataSinkNode** ppDataSink, DataSinkHandle* pHandle,
2022-10-13 05:41:36 +00:00
void* pParam) {
2024-11-15 02:16:15 +00:00
SDataSinkNode* pDataSink = *ppDataSink;
2022-07-06 00:57:10 +00:00
SDataInserterHandle* inserter = taosMemoryCalloc(1, sizeof(SDataInserterHandle));
if (NULL == inserter) {
2023-04-18 02:40:53 +00:00
taosMemoryFree(pParam);
2023-08-22 10:40:42 +00:00
goto _return;
2022-07-06 00:57:10 +00:00
}
2022-10-13 05:41:36 +00:00
SQueryInserterNode* pInserterNode = (SQueryInserterNode*)pDataSink;
2022-07-06 00:57:10 +00:00
inserter->sink.fPut = putDataBlock;
inserter->sink.fEndPut = endPut;
2022-07-07 00:53:23 +00:00
inserter->sink.fGetLen = getDataLength;
2022-07-06 08:29:51 +00:00
inserter->sink.fGetData = NULL;
2022-07-06 00:57:10 +00:00
inserter->sink.fDestroy = destroyDataSinker;
inserter->sink.fGetCacheSize = getCacheSize;
2024-07-01 09:22:43 +00:00
inserter->sink.fGetFlags = getSinkFlags;
2022-07-06 00:57:10 +00:00
inserter->pManager = pManager;
2022-07-06 08:29:51 +00:00
inserter->pNode = pInserterNode;
2022-07-06 00:57:10 +00:00
inserter->pParam = pParam;
inserter->status = DS_BUF_EMPTY;
inserter->queryEnd = false;
2023-02-07 10:35:42 +00:00
inserter->explain = pInserterNode->explain;
2024-11-15 02:16:15 +00:00
*ppDataSink = NULL;
2022-07-06 08:29:51 +00:00
int64_t suid = 0;
2024-09-05 06:40:14 +00:00
int32_t code = pManager->pAPI->metaFn.getTableSchema(inserter->pParam->readHandle->vnode, pInserterNode->tableId,
&inserter->pSchema, &suid);
2022-07-06 08:29:51 +00:00
if (code) {
2023-08-22 10:40:42 +00:00
terrno = code;
goto _return;
2022-07-06 08:29:51 +00:00
}
2025-04-21 08:44:27 +00:00
pManager->pAPI->metaFn.getBasicInfo(inserter->pParam->readHandle->vnode, &inserter->dbFName, NULL, NULL, NULL);
2022-07-07 01:42:20 +00:00
if (pInserterNode->stableId != suid) {
2022-07-06 08:29:51 +00:00
terrno = TSDB_CODE_TDB_INVALID_TABLE_ID;
2023-08-22 10:40:42 +00:00
goto _return;
2022-07-06 08:29:51 +00:00
}
inserter->pDataBlocks = taosArrayInit(1, POINTER_BYTES);
2022-07-06 00:57:10 +00:00
if (NULL == inserter->pDataBlocks) {
2023-09-06 02:57:49 +00:00
goto _return;
2022-07-06 00:57:10 +00:00
}
QRY_ERR_JRET(taosThreadMutexInit(&inserter->mutex, NULL));
2022-07-06 08:29:51 +00:00
inserter->fullOrderColList = pInserterNode->pCols->length == inserter->pSchema->numOfCols;
2022-10-13 05:41:36 +00:00
inserter->pCols = taosHashInit(pInserterNode->pCols->length, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT),
false, HASH_NO_LOCK);
if (NULL == inserter->pCols) {
2024-09-05 06:40:14 +00:00
goto _return;
}
2024-09-05 06:40:14 +00:00
2023-01-04 03:43:20 +00:00
SNode* pNode = NULL;
int32_t i = 0;
2022-07-07 03:38:58 +00:00
FOREACH(pNode, pInserterNode->pCols) {
2025-04-14 11:02:02 +00:00
// 忽略tbname
2025-04-21 08:44:27 +00:00
if (pNode->type == QUERY_NODE_FUNCTION && ((SFunctionNode*)pNode)->funcType == FUNCTION_TYPE_TBNAME) {
int16_t colId = 0;
int16_t slotId = 0;
QRY_ERR_JRET(taosHashPut(inserter->pCols, &colId, sizeof(colId), &slotId, sizeof(slotId)));
2025-04-14 11:02:02 +00:00
continue;
}
2022-07-07 03:38:58 +00:00
SColumnNode* pCol = (SColumnNode*)pNode;
QRY_ERR_JRET(taosHashPut(inserter->pCols, &pCol->colId, sizeof(pCol->colId), &pCol->slotId, sizeof(pCol->slotId)));
if (inserter->fullOrderColList && pCol->colId != inserter->pSchema->columns[i].colId) {
inserter->fullOrderColList = false;
}
2022-12-30 09:47:06 +00:00
++i;
2022-07-07 03:38:58 +00:00
}
2022-10-13 05:41:36 +00:00
QRY_ERR_JRET(tsem_init(&inserter->ready, 0, 0));
2022-10-13 05:41:36 +00:00
2025-04-21 08:44:27 +00:00
inserter->dbVgInfoMap = NULL;
2022-07-06 00:57:10 +00:00
*pHandle = inserter;
return TSDB_CODE_SUCCESS;
2023-08-22 10:40:42 +00:00
_return:
if (inserter) {
(void)destroyDataSinker((SDataSinkHandle*)inserter);
2023-08-22 10:40:42 +00:00
taosMemoryFree(inserter);
} else {
taosMemoryFree(pManager);
}
2024-02-21 06:29:10 +00:00
2024-11-15 02:16:15 +00:00
nodesDestroyNode((SNode *)*ppDataSink);
*ppDataSink = NULL;
2024-02-21 06:29:10 +00:00
return terrno;
2022-07-06 00:57:10 +00:00
}