Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor SValue #29251

Open
wants to merge 4 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions include/common/tdataformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ typedef struct {
uint32_t offset;
} SPrimaryKeyIndex;

#define DATUM_MAX_SIZE 16

struct SValue {
int8_t type;
union {
Expand All @@ -253,6 +255,26 @@ struct SValue {
};
};

// TODO wjm remove type parameter maybe
#define VALUE_GET_DATUM(pVal, type) \
IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL ? (pVal)->pData : (void*)&(pVal)->val

#define VALUE_GET_TRIVIAL_DATUM(pVal) ((pVal)->val)
#define VALUE_SET_TRIVIAL_DATUM(pVal, v) (pVal)->val = v

void valueSetDatum(SValue *pVal, int8_t type, const void *pDatum, uint32_t len);
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type);
void valueClearDatum(SValue *pVal, int8_t type);

//uint8_t* valueGetVarDatum(const SValue *pVal);
//uint32_t valueGetVarNDatum(const SValue *pVal);
//void valueSetVarDatum(SValue *pVal, uint8_t *pData, uint32_t nData);

//DecimalWord* valueGetDecimalDatum(const SValue *pVal);
//uint32_t valueGetDecimalWordNum(const SValue *pVal);
//void valueSetDecimalDatum(SValue *pVal, DecimalWord *words, int32_t wordNum);


#define TD_MAX_PK_COLS 2
struct SRowKey {
TSKEY ts;
Expand Down
6 changes: 3 additions & 3 deletions source/common/src/msg/tmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -11041,7 +11041,7 @@ int32_t tEncodeSTqOffsetVal(SEncoder *pEncoder, const STqOffsetVal *pOffsetVal)
if (IS_VAR_DATA_TYPE(pOffsetVal->primaryKey.type)) {
TAOS_CHECK_EXIT(tEncodeBinary(pEncoder, pOffsetVal->primaryKey.pData, pOffsetVal->primaryKey.nData));
} else {
TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pOffsetVal->primaryKey.val));
TAOS_CHECK_EXIT(tEncodeI64(pEncoder, VALUE_GET_TRIVIAL_DATUM(&pOffsetVal->primaryKey)));
}

} else if (pOffsetVal->type == TMQ_OFFSET__LOG) {
Expand Down Expand Up @@ -11072,7 +11072,7 @@ int32_t tDecodeSTqOffsetVal(SDecoder *pDecoder, STqOffsetVal *pOffsetVal) {
TAOS_CHECK_EXIT(
tDecodeBinaryAlloc32(pDecoder, (void **)&pOffsetVal->primaryKey.pData, &pOffsetVal->primaryKey.nData));
} else {
TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pOffsetVal->primaryKey.val));
TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &VALUE_GET_TRIVIAL_DATUM(&pOffsetVal->primaryKey)));
}
}
} else if (pOffsetVal->type == TMQ_OFFSET__LOG) {
Expand Down Expand Up @@ -11103,7 +11103,7 @@ void tFormatOffset(char *buf, int32_t maxLen, const STqOffsetVal *pVal) {
taosMemoryFree(tmp);
} else {
(void)snprintf(buf, maxLen, "tsdb:%" PRId64 "|%" PRId64 ",pk type:%d,val:%" PRId64, pVal->uid, pVal->ts,
pVal->primaryKey.type, pVal->primaryKey.val);
pVal->primaryKey.type, VALUE_GET_TRIVIAL_DATUM(&pVal->primaryKey));
}
}
}
Expand Down
27 changes: 18 additions & 9 deletions source/common/src/tdatablock.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,13 @@ int32_t blockDataUpdatePkRange(SSDataBlock* pDataBlock, int32_t pkColumnIndex, b
void* skey = colDataGetData(pColInfoData, 0);
void* ekey = colDataGetData(pColInfoData, (pInfo->rows - 1));

int64_t val = 0;
if (asc) {
if (IS_NUMERIC_TYPE(pColInfoData->info.type)) {
GET_TYPED_DATA(pInfo->pks[0].val, int64_t, pColInfoData->info.type, skey);
GET_TYPED_DATA(pInfo->pks[1].val, int64_t, pColInfoData->info.type, ekey);
GET_TYPED_DATA(val, int64_t, pColInfoData->info.type, skey);
VALUE_SET_TRIVIAL_DATUM(&pInfo->pks[0], val);
GET_TYPED_DATA(val, int64_t, pColInfoData->info.type, ekey);
VALUE_SET_TRIVIAL_DATUM(&pInfo->pks[1], val);
} else { // todo refactor
memcpy(pInfo->pks[0].pData, varDataVal(skey), varDataLen(skey));
pInfo->pks[0].nData = varDataLen(skey);
Expand All @@ -689,8 +692,10 @@ int32_t blockDataUpdatePkRange(SSDataBlock* pDataBlock, int32_t pkColumnIndex, b
}
} else {
if (IS_NUMERIC_TYPE(pColInfoData->info.type)) {
GET_TYPED_DATA(pInfo->pks[0].val, int64_t, pColInfoData->info.type, ekey);
GET_TYPED_DATA(pInfo->pks[1].val, int64_t, pColInfoData->info.type, skey);
GET_TYPED_DATA(val, int64_t, pColInfoData->info.type, ekey);
VALUE_SET_TRIVIAL_DATUM(&pInfo->pks[0], val);
GET_TYPED_DATA(val, int64_t, pColInfoData->info.type, skey);
VALUE_SET_TRIVIAL_DATUM(&pInfo->pks[1], val);
} else { // todo refactor
memcpy(pInfo->pks[0].pData, varDataVal(ekey), varDataLen(ekey));
pInfo->pks[0].nData = varDataLen(ekey);
Expand Down Expand Up @@ -2754,7 +2759,9 @@ int32_t buildSubmitReqFromDataBlock(SSubmitReq2** ppReq, const SSDataBlock* pDat
terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
return terrno;
}
SColVal cv = COL_VAL_VALUE(pCol->colId, ((SValue){.type = pCol->type, .val = *(TSKEY*)var}));
SValue val = {.type = pCol->type};
VALUE_SET_TRIVIAL_DATUM(&val, *(TSKEY*)var);
SColVal cv = COL_VAL_VALUE(pCol->colId, val);
void* px = taosArrayPush(pVals, &cv);
if (px == NULL) {
return terrno;
Expand All @@ -2767,7 +2774,9 @@ int32_t buildSubmitReqFromDataBlock(SSubmitReq2** ppReq, const SSDataBlock* pDat
return terrno;
}
} else {
SColVal cv = COL_VAL_VALUE(pCol->colId, ((SValue){.type = pCol->type, .val = *(int64_t*)var}));
SValue val = {.type = pCol->type};
VALUE_SET_TRIVIAL_DATUM(&val, *(int64_t*)var);
SColVal cv = COL_VAL_VALUE(pCol->colId, val);
void* px = taosArrayPush(pVals, &cv);
if (px == NULL) {
return terrno;
Expand Down Expand Up @@ -2820,13 +2829,13 @@ int32_t buildSubmitReqFromDataBlock(SSubmitReq2** ppReq, const SSDataBlock* pDat
} else {
SValue sv = {.type = pCol->type};
if (pCol->type == pColInfoData->info.type) {
memcpy(&sv.val, var, tDataTypes[pCol->type].bytes);
valueSetDatum(&sv, sv.type, var, tDataTypes[pCol->type].bytes);
} else {
/**
* 1. sum/avg would convert to int64_t/uint64_t/double during aggregation
* 2. below conversion may lead to overflow or loss, the app should select the right data type.
*/
char tv[8] = {0};
char tv[DATUM_MAX_SIZE] = {0};
if (pColInfoData->info.type == TSDB_DATA_TYPE_FLOAT) {
float v = 0;
GET_TYPED_DATA(v, float, pColInfoData->info.type, var);
Expand All @@ -2844,7 +2853,7 @@ int32_t buildSubmitReqFromDataBlock(SSubmitReq2** ppReq, const SSDataBlock* pDat
GET_TYPED_DATA(v, uint64_t, pColInfoData->info.type, var);
SET_TYPED_DATA(&tv, pCol->type, v);
}
memcpy(&sv.val, tv, tDataTypes[pCol->type].bytes);
valueSetDatum(&sv, sv.type, tv, tDataTypes[pCol->type].bytes);
}
SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
void* px = taosArrayPush(pVals, &cv);
Expand Down
Loading