提交 f8619dfb 编写于 作者: H Hongze Cheng

refact code for further refact

上级 1d76bd6d
...@@ -154,17 +154,7 @@ struct STSRowBuilder { ...@@ -154,17 +154,7 @@ struct STSRowBuilder {
struct SValue { struct SValue {
union { union {
int8_t i8; // TSDB_DATA_TYPE_BOOL||TSDB_DATA_TYPE_TINYINT int64_t val;
uint8_t u8; // TSDB_DATA_TYPE_UTINYINT
int16_t i16; // TSDB_DATA_TYPE_SMALLINT
uint16_t u16; // TSDB_DATA_TYPE_USMALLINT
int32_t i32; // TSDB_DATA_TYPE_INT
uint32_t u32; // TSDB_DATA_TYPE_UINT
int64_t i64; // TSDB_DATA_TYPE_BIGINT
uint64_t u64; // TSDB_DATA_TYPE_UBIGINT
TSKEY ts; // TSDB_DATA_TYPE_TIMESTAMP
float f; // TSDB_DATA_TYPE_FLOAT
double d; // TSDB_DATA_TYPE_DOUBLE
struct { struct {
uint32_t nData; uint32_t nData;
uint8_t *pData; uint8_t *pData;
......
...@@ -33,105 +33,21 @@ typedef struct { ...@@ -33,105 +33,21 @@ typedef struct {
// SValue // SValue
int32_t tPutValue(uint8_t *p, SValue *pValue, int8_t type) { int32_t tPutValue(uint8_t *p, SValue *pValue, int8_t type) {
int32_t n = 0;
if (IS_VAR_DATA_TYPE(type)) { if (IS_VAR_DATA_TYPE(type)) {
n += tPutBinary(p ? p + n : p, pValue->pData, pValue->nData); return tPutBinary(p, pValue->pData, pValue->nData);
} else { } else {
switch (type) { if (p) memcpy(p, &pValue->val, tDataTypes[type].bytes);
case TSDB_DATA_TYPE_BOOL: return tDataTypes[type].bytes;
n += tPutI8(p ? p + n : p, pValue->i8 ? 1 : 0);
break;
case TSDB_DATA_TYPE_TINYINT:
n += tPutI8(p ? p + n : p, pValue->i8);
break;
case TSDB_DATA_TYPE_SMALLINT:
n += tPutI16(p ? p + n : p, pValue->i16);
break;
case TSDB_DATA_TYPE_INT:
n += tPutI32(p ? p + n : p, pValue->i32);
break;
case TSDB_DATA_TYPE_BIGINT:
n += tPutI64(p ? p + n : p, pValue->i64);
break;
case TSDB_DATA_TYPE_FLOAT:
n += tPutFloat(p ? p + n : p, pValue->f);
break;
case TSDB_DATA_TYPE_DOUBLE:
n += tPutDouble(p ? p + n : p, pValue->d);
break;
case TSDB_DATA_TYPE_TIMESTAMP:
n += tPutI64(p ? p + n : p, pValue->ts);
break;
case TSDB_DATA_TYPE_UTINYINT:
n += tPutU8(p ? p + n : p, pValue->u8);
break;
case TSDB_DATA_TYPE_USMALLINT:
n += tPutU16(p ? p + n : p, pValue->u16);
break;
case TSDB_DATA_TYPE_UINT:
n += tPutU32(p ? p + n : p, pValue->u32);
break;
case TSDB_DATA_TYPE_UBIGINT:
n += tPutU64(p ? p + n : p, pValue->u64);
break;
default:
ASSERT(0);
}
} }
return n;
} }
int32_t tGetValue(uint8_t *p, SValue *pValue, int8_t type) { int32_t tGetValue(uint8_t *p, SValue *pValue, int8_t type) {
int32_t n = 0;
if (IS_VAR_DATA_TYPE(type)) { if (IS_VAR_DATA_TYPE(type)) {
n += tGetBinary(p, &pValue->pData, pValue ? &pValue->nData : NULL); return tGetBinary(p, &pValue->pData, pValue ? &pValue->nData : NULL);
} else { } else {
switch (type) { memcpy(&pValue->val, p, tDataTypes[type].bytes);
case TSDB_DATA_TYPE_BOOL: return tDataTypes[type].bytes;
n += tGetI8(p, &pValue->i8);
break;
case TSDB_DATA_TYPE_TINYINT:
n += tGetI8(p, &pValue->i8);
break;
case TSDB_DATA_TYPE_SMALLINT:
n += tGetI16(p, &pValue->i16);
break;
case TSDB_DATA_TYPE_INT:
n += tGetI32(p, &pValue->i32);
break;
case TSDB_DATA_TYPE_BIGINT:
n += tGetI64(p, &pValue->i64);
break;
case TSDB_DATA_TYPE_FLOAT:
n += tGetFloat(p, &pValue->f);
break;
case TSDB_DATA_TYPE_DOUBLE:
n += tGetDouble(p, &pValue->d);
break;
case TSDB_DATA_TYPE_TIMESTAMP:
n += tGetI64(p, &pValue->ts);
break;
case TSDB_DATA_TYPE_UTINYINT:
n += tGetU8(p, &pValue->u8);
break;
case TSDB_DATA_TYPE_USMALLINT:
n += tGetU16(p, &pValue->u16);
break;
case TSDB_DATA_TYPE_UINT:
n += tGetU32(p, &pValue->u32);
break;
case TSDB_DATA_TYPE_UBIGINT:
n += tGetU64(p, &pValue->u64);
break;
default:
ASSERT(0);
}
} }
return n;
} }
int tValueCmprFn(const SValue *pValue1, const SValue *pValue2, int8_t type) { int tValueCmprFn(const SValue *pValue1, const SValue *pValue2, int8_t type) {
......
...@@ -689,7 +689,7 @@ int32_t tdSTSRowNew(SArray *pArray, STSchema *pTSchema, STSRow **ppRow) { ...@@ -689,7 +689,7 @@ int32_t tdSTSRowNew(SArray *pArray, STSchema *pTSchema, STSRow **ppRow) {
memcpy(varDataVal(varBuf), pColVal->value.pData, pColVal->value.nData); memcpy(varDataVal(varBuf), pColVal->value.pData, pColVal->value.nData);
val = varBuf; val = varBuf;
} else { } else {
val = (const void *)&pColVal->value.i64; val = (const void *)&pColVal->value.val;
} }
} else { } else {
pColVal = NULL; pColVal = NULL;
......
...@@ -260,7 +260,7 @@ int32_t tsdbCacheInsertLast(SLRUCache *pCache, tb_uid_t uid, STSRow *row, STsdb ...@@ -260,7 +260,7 @@ int32_t tsdbCacheInsertLast(SLRUCache *pCache, tb_uid_t uid, STSRow *row, STsdb
SLastCol *tTsVal = (SLastCol *)taosArrayGet(pLast, iCol); SLastCol *tTsVal = (SLastCol *)taosArrayGet(pLast, iCol);
if (keyTs > tTsVal->ts) { if (keyTs > tTsVal->ts) {
STColumn *pTColumn = &pTSchema->columns[0]; STColumn *pTColumn = &pTSchema->columns[0];
SColVal tColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = keyTs}); SColVal tColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = keyTs});
taosArraySet(pLast, iCol, &(SLastCol){.ts = keyTs, .colVal = tColVal}); taosArraySet(pLast, iCol, &(SLastCol){.ts = keyTs, .colVal = tColVal});
} }
...@@ -1048,7 +1048,7 @@ static int32_t mergeLastRow(tb_uid_t uid, STsdb *pTsdb, bool *dup, STSRow **ppRo ...@@ -1048,7 +1048,7 @@ static int32_t mergeLastRow(tb_uid_t uid, STsdb *pTsdb, bool *dup, STSRow **ppRo
lastRowTs = TSDBROW_TS(pRow); lastRowTs = TSDBROW_TS(pRow);
STColumn *pTColumn = &pTSchema->columns[0]; STColumn *pTColumn = &pTSchema->columns[0];
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = lastRowTs}); *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = lastRowTs});
if (taosArrayPush(pColArray, pColVal) == NULL) { if (taosArrayPush(pColArray, pColVal) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY; code = TSDB_CODE_OUT_OF_MEMORY;
goto _err; goto _err;
...@@ -1147,7 +1147,7 @@ static int32_t mergeLast(tb_uid_t uid, STsdb *pTsdb, SArray **ppLastArray) { ...@@ -1147,7 +1147,7 @@ static int32_t mergeLast(tb_uid_t uid, STsdb *pTsdb, SArray **ppLastArray) {
lastRowTs = rowTs; lastRowTs = rowTs;
STColumn *pTColumn = &pTSchema->columns[0]; STColumn *pTColumn = &pTSchema->columns[0];
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = lastRowTs}); *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = lastRowTs});
if (taosArrayPush(pColArray, &(SLastCol){.ts = lastRowTs, .colVal = *pColVal}) == NULL) { if (taosArrayPush(pColArray, &(SLastCol){.ts = lastRowTs, .colVal = *pColVal}) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY; code = TSDB_CODE_OUT_OF_MEMORY;
goto _err; goto _err;
......
...@@ -649,7 +649,7 @@ int32_t tRowMergerInit2(SRowMerger *pMerger, STSchema *pResTSchema, TSDBROW *pRo ...@@ -649,7 +649,7 @@ int32_t tRowMergerInit2(SRowMerger *pMerger, STSchema *pResTSchema, TSDBROW *pRo
ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP); ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = key.ts}); *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = key.ts});
if (taosArrayPush(pMerger->pArray, pColVal) == NULL) { if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY; code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit; goto _exit;
...@@ -690,7 +690,7 @@ int32_t tRowMergerAdd(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) { ...@@ -690,7 +690,7 @@ int32_t tRowMergerAdd(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) {
STColumn *pTColumn; STColumn *pTColumn;
int32_t iCol, jCol = 1; int32_t iCol, jCol = 1;
ASSERT(((SColVal *)pMerger->pArray->pData)->value.ts == key.ts); ASSERT(((SColVal *)pMerger->pArray->pData)->value.val == key.ts);
for (iCol = 1; iCol < pMerger->pTSchema->numOfCols && jCol < pTSchema->numOfCols; ++iCol) { for (iCol = 1; iCol < pMerger->pTSchema->numOfCols && jCol < pTSchema->numOfCols; ++iCol) {
pTColumn = &pMerger->pTSchema->columns[iCol]; pTColumn = &pMerger->pTSchema->columns[iCol];
...@@ -744,7 +744,7 @@ int32_t tRowMergerInit(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) { ...@@ -744,7 +744,7 @@ int32_t tRowMergerInit(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) {
ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP); ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);
*pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = key.ts}); *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.val = key.ts});
if (taosArrayPush(pMerger->pArray, pColVal) == NULL) { if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY; code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit; goto _exit;
...@@ -770,7 +770,7 @@ int32_t tRowMerge(SRowMerger *pMerger, TSDBROW *pRow) { ...@@ -770,7 +770,7 @@ int32_t tRowMerge(SRowMerger *pMerger, TSDBROW *pRow) {
TSDBKEY key = TSDBROW_KEY(pRow); TSDBKEY key = TSDBROW_KEY(pRow);
SColVal *pColVal = &(SColVal){0}; SColVal *pColVal = &(SColVal){0};
ASSERT(((SColVal *)pMerger->pArray->pData)->value.ts == key.ts); ASSERT(((SColVal *)pMerger->pArray->pData)->value.val == key.ts);
for (int32_t iCol = 1; iCol < pMerger->pTSchema->numOfCols; iCol++) { for (int32_t iCol = 1; iCol < pMerger->pTSchema->numOfCols; iCol++) {
tsdbRowGetColVal(pRow, pMerger->pTSchema, iCol, pColVal); tsdbRowGetColVal(pRow, pMerger->pTSchema, iCol, pColVal);
...@@ -1513,73 +1513,79 @@ void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) { ...@@ -1513,73 +1513,79 @@ void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) {
case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_BOOL:
break; break;
case TSDB_DATA_TYPE_TINYINT: { case TSDB_DATA_TYPE_TINYINT: {
pColAgg->sum += colVal.value.i8; int8_t i8 = *(int8_t *)&colVal.value.val;
if (!minAssigned || pColAgg->min > colVal.value.i8) { pColAgg->sum += i8;
pColAgg->min = colVal.value.i8; if (!minAssigned || pColAgg->min > i8) {
pColAgg->min = i8;
minAssigned = true; minAssigned = true;
} }
if (!maxAssigned || pColAgg->max < colVal.value.i8) { if (!maxAssigned || pColAgg->max < i8) {
pColAgg->max = colVal.value.i8; pColAgg->max = i8;
maxAssigned = true; maxAssigned = true;
} }
break; break;
} }
case TSDB_DATA_TYPE_SMALLINT: { case TSDB_DATA_TYPE_SMALLINT: {
pColAgg->sum += colVal.value.i16; int16_t i16 = *(int16_t *)&colVal.value.val;
if (!minAssigned || pColAgg->min > colVal.value.i16) { pColAgg->sum += i16;
pColAgg->min = colVal.value.i16; if (!minAssigned || pColAgg->min > i16) {
pColAgg->min = i16;
minAssigned = true; minAssigned = true;
} }
if (!maxAssigned || pColAgg->max < colVal.value.i16) { if (!maxAssigned || pColAgg->max < i16) {
pColAgg->max = colVal.value.i16; pColAgg->max = i16;
maxAssigned = true; maxAssigned = true;
} }
break; break;
} }
case TSDB_DATA_TYPE_INT: { case TSDB_DATA_TYPE_INT: {
pColAgg->sum += colVal.value.i32; int32_t i32 = *(int32_t *)&colVal.value.val;
if (!minAssigned || pColAgg->min > colVal.value.i32) { pColAgg->sum += i32;
pColAgg->min = colVal.value.i32; if (!minAssigned || pColAgg->min > i32) {
pColAgg->min = i32;
minAssigned = true; minAssigned = true;
} }
if (!maxAssigned || pColAgg->max < colVal.value.i32) { if (!maxAssigned || pColAgg->max < i32) {
pColAgg->max = colVal.value.i32; pColAgg->max = i32;
maxAssigned = true; maxAssigned = true;
} }
break; break;
} }
case TSDB_DATA_TYPE_BIGINT: { case TSDB_DATA_TYPE_BIGINT: {
pColAgg->sum += colVal.value.i64; int64_t i64 = *(int64_t *)&colVal.value.val;
if (!minAssigned || pColAgg->min > colVal.value.i64) { pColAgg->sum += i64;
pColAgg->min = colVal.value.i64; if (!minAssigned || pColAgg->min > i64) {
pColAgg->min = i64;
minAssigned = true; minAssigned = true;
} }
if (!maxAssigned || pColAgg->max < colVal.value.i64) { if (!maxAssigned || pColAgg->max < i64) {
pColAgg->max = colVal.value.i64; pColAgg->max = i64;
maxAssigned = true; maxAssigned = true;
} }
break; break;
} }
case TSDB_DATA_TYPE_FLOAT: { case TSDB_DATA_TYPE_FLOAT: {
*(double *)(&pColAgg->sum) += colVal.value.f; float f = *(float *)&colVal.value.val;
if (!minAssigned || *(double *)(&pColAgg->min) > colVal.value.f) { *(double *)(&pColAgg->sum) += f;
*(double *)(&pColAgg->min) = colVal.value.f; if (!minAssigned || *(double *)(&pColAgg->min) > f) {
*(double *)(&pColAgg->min) = f;
minAssigned = true; minAssigned = true;
} }
if (!maxAssigned || *(double *)(&pColAgg->max) < colVal.value.f) { if (!maxAssigned || *(double *)(&pColAgg->max) < f) {
*(double *)(&pColAgg->max) = colVal.value.f; *(double *)(&pColAgg->max) = f;
maxAssigned = true; maxAssigned = true;
} }
break; break;
} }
case TSDB_DATA_TYPE_DOUBLE: { case TSDB_DATA_TYPE_DOUBLE: {
*(double *)(&pColAgg->sum) += colVal.value.d; double d = *(double *)&colVal.value.val;
if (!minAssigned || *(double *)(&pColAgg->min) > colVal.value.d) { *(double *)(&pColAgg->sum) += d;
*(double *)(&pColAgg->min) = colVal.value.d; if (!minAssigned || *(double *)(&pColAgg->min) > d) {
*(double *)(&pColAgg->min) = d;
minAssigned = true; minAssigned = true;
} }
if (!maxAssigned || *(double *)(&pColAgg->max) < colVal.value.d) { if (!maxAssigned || *(double *)(&pColAgg->max) < d) {
*(double *)(&pColAgg->max) = colVal.value.d; *(double *)(&pColAgg->max) = d;
maxAssigned = true; maxAssigned = true;
} }
break; break;
...@@ -1587,12 +1593,13 @@ void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) { ...@@ -1587,12 +1593,13 @@ void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) {
case TSDB_DATA_TYPE_VARCHAR: case TSDB_DATA_TYPE_VARCHAR:
break; break;
case TSDB_DATA_TYPE_TIMESTAMP: { case TSDB_DATA_TYPE_TIMESTAMP: {
if (!minAssigned || pColAgg->min > colVal.value.i64) { int64_t ts = *(int64_t *)&colVal.value.val;
pColAgg->min = colVal.value.i64; if (!minAssigned || pColAgg->min > ts) {
pColAgg->min = ts;
minAssigned = true; minAssigned = true;
} }
if (!maxAssigned || pColAgg->max < colVal.value.i64) { if (!maxAssigned || pColAgg->max < ts) {
pColAgg->max = colVal.value.i64; pColAgg->max = ts;
maxAssigned = true; maxAssigned = true;
} }
break; break;
...@@ -1600,49 +1607,53 @@ void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) { ...@@ -1600,49 +1607,53 @@ void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) {
case TSDB_DATA_TYPE_NCHAR: case TSDB_DATA_TYPE_NCHAR:
break; break;
case TSDB_DATA_TYPE_UTINYINT: { case TSDB_DATA_TYPE_UTINYINT: {
pColAgg->sum += colVal.value.u8; uint8_t u8 = *(uint8_t *)&colVal.value.val;
if (!minAssigned || pColAgg->min > colVal.value.u8) { pColAgg->sum += u8;
pColAgg->min = colVal.value.u8; if (!minAssigned || pColAgg->min > u8) {
pColAgg->min = u8;
minAssigned = true; minAssigned = true;
} }
if (!maxAssigned || pColAgg->max < colVal.value.u8) { if (!maxAssigned || pColAgg->max < u8) {
pColAgg->max = colVal.value.u8; pColAgg->max = u8;
maxAssigned = true; maxAssigned = true;
} }
break; break;
} }
case TSDB_DATA_TYPE_USMALLINT: { case TSDB_DATA_TYPE_USMALLINT: {
pColAgg->sum += colVal.value.u16; uint16_t u16 = *(uint16_t *)&colVal.value.val;
if (!minAssigned || pColAgg->min > colVal.value.u16) { pColAgg->sum += u16;
pColAgg->min = colVal.value.u16; if (!minAssigned || pColAgg->min > u16) {
pColAgg->min = u16;
minAssigned = true; minAssigned = true;
} }
if (!maxAssigned || pColAgg->max < colVal.value.u16) { if (!maxAssigned || pColAgg->max < u16) {
pColAgg->max = colVal.value.u16; pColAgg->max = u16;
maxAssigned = true; maxAssigned = true;
} }
break; break;
} }
case TSDB_DATA_TYPE_UINT: { case TSDB_DATA_TYPE_UINT: {
pColAgg->sum += colVal.value.u32; uint32_t u32 = *(uint32_t *)&colVal.value.val;
if (!minAssigned || pColAgg->min > colVal.value.u32) { pColAgg->sum += u32;
pColAgg->min = colVal.value.u32; if (!minAssigned || pColAgg->min > u32) {
pColAgg->min = u32;
minAssigned = true; minAssigned = true;
} }
if (!minAssigned || pColAgg->max < colVal.value.u32) { if (!minAssigned || pColAgg->max < u32) {
pColAgg->max = colVal.value.u32; pColAgg->max = u32;
maxAssigned = true; maxAssigned = true;
} }
break; break;
} }
case TSDB_DATA_TYPE_UBIGINT: { case TSDB_DATA_TYPE_UBIGINT: {
pColAgg->sum += colVal.value.u64; uint64_t u64 = *(uint64_t *)&colVal.value.val;
if (!minAssigned || pColAgg->min > colVal.value.u64) { pColAgg->sum += u64;
pColAgg->min = colVal.value.u64; if (!minAssigned || pColAgg->min > u64) {
pColAgg->min = u64;
minAssigned = true; minAssigned = true;
} }
if (!maxAssigned || pColAgg->max < colVal.value.u64) { if (!maxAssigned || pColAgg->max < u64) {
pColAgg->max = colVal.value.u64; pColAgg->max = u64;
maxAssigned = true; maxAssigned = true;
} }
break; break;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册