From 1cac65d8c0092261de98e5371282371eacf83051 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 22 Sep 2022 19:08:33 +0800 Subject: [PATCH] refact more code --- source/dnode/vnode/src/tsdb/tsdbDiskData.c | 88 +------ source/dnode/vnode/src/tsdb/tsdbUtil.c | 268 ++++++++------------- 2 files changed, 97 insertions(+), 259 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbDiskData.c b/source/dnode/vnode/src/tsdb/tsdbDiskData.c index 5a235c5c82..8d25873036 100644 --- a/source/dnode/vnode/src/tsdb/tsdbDiskData.c +++ b/source/dnode/vnode/src/tsdb/tsdbDiskData.c @@ -143,96 +143,12 @@ static int32_t tGnrtDiskCol(SDiskColBuilder *pBuilder, SDiskCol *pDiskCol) { return code; } -static FORCE_INLINE void tDiskColUpdateSmaBool(SDiskColBuilder *pBuilder, SColVal *pColVal) { - // TODO -} -static FORCE_INLINE void tDiskColUpdateSmaTinyint(SDiskColBuilder *pBuilder, SColVal *pColVal) { - int8_t val = *(int8_t *)&pColVal->value.val; - pBuilder->sma.sum += val; - if (!pBuilder->minSet || pBuilder->sma.min > val) pBuilder->sma.min = val; - if (!pBuilder->maxSet || pBuilder->sma.max < val) pBuilder->sma.max = val; -} -static FORCE_INLINE void tDiskColUpdateSmaSmallint(SDiskColBuilder *pBuilder, SColVal *pColVal) { - int16_t val = *(int16_t *)&pColVal->value.val; - pBuilder->sma.sum += val; - if (!pBuilder->minSet || pBuilder->sma.min > val) pBuilder->sma.min = val; - if (!pBuilder->maxSet || pBuilder->sma.max < val) pBuilder->sma.max = val; -} -static FORCE_INLINE void tDiskColUpdateSmaInt(SDiskColBuilder *pBuilder, SColVal *pColVal) { - int32_t val = *(int32_t *)&pColVal->value.val; - pBuilder->sma.sum += val; - if (!pBuilder->minSet || pBuilder->sma.min > val) pBuilder->sma.min = val; - if (!pBuilder->maxSet || pBuilder->sma.max < val) pBuilder->sma.max = val; -} -static FORCE_INLINE void tDiskColUpdateBigint(SDiskColBuilder *pBuilder, SColVal *pColVal) { - int64_t val = *(int64_t *)&pColVal->value.val; - pBuilder->sma.sum += val; - if (!pBuilder->minSet || pBuilder->sma.min > val) pBuilder->sma.min = val; - if (!pBuilder->maxSet || pBuilder->sma.max < val) pBuilder->sma.max = val; -} -static FORCE_INLINE void tDiskColUpdateFloat(SDiskColBuilder *pBuilder, SColVal *pColVal) { - float val = *(float *)&pColVal->value.val; - *(double *)&pBuilder->sma.sum += val; - if (!pBuilder->minSet || *(double *)&pBuilder->sma.min > val) *(double *)&pBuilder->sma.min = val; - if (!pBuilder->maxSet || *(double *)&pBuilder->sma.max < val) *(double *)&pBuilder->sma.max = val; -} -static FORCE_INLINE void tDiskColUpdateDouble(SDiskColBuilder *pBuilder, SColVal *pColVal) { - double val = *(double *)&pColVal->value.val; - *(double *)&pBuilder->sma.sum += val; - if (!pBuilder->minSet || *(double *)&pBuilder->sma.min > val) *(double *)&pBuilder->sma.min = val; - if (!pBuilder->maxSet || *(double *)&pBuilder->sma.max < val) *(double *)&pBuilder->sma.max = val; -} -static FORCE_INLINE void tDiskColUpdateUTinyint(SDiskColBuilder *pBuilder, SColVal *pColVal) { - uint8_t val = *(uint8_t *)&pColVal->value.val; - pBuilder->sma.sum += val; - if (!pBuilder->minSet || pBuilder->sma.min > val) pBuilder->sma.min = val; - if (!pBuilder->maxSet || pBuilder->sma.max < val) pBuilder->sma.max = val; -} -static FORCE_INLINE void tDiskColUpdateUSmallint(SDiskColBuilder *pBuilder, SColVal *pColVal) { - uint16_t val = *(uint16_t *)&pColVal->value.val; - pBuilder->sma.sum += val; - if (!pBuilder->minSet || pBuilder->sma.min > val) pBuilder->sma.min = val; - if (!pBuilder->maxSet || pBuilder->sma.max < val) pBuilder->sma.max = val; -} -static FORCE_INLINE void tDiskColUpdateUInt(SDiskColBuilder *pBuilder, SColVal *pColVal) { - uint32_t val = *(uint32_t *)&pColVal->value.val; - pBuilder->sma.sum += val; - if (!pBuilder->minSet || pBuilder->sma.min > val) pBuilder->sma.min = val; - if (!pBuilder->maxSet || pBuilder->sma.max < val) pBuilder->sma.max = val; -} -static FORCE_INLINE void tDiskColUpdateUBigint(SDiskColBuilder *pBuilder, SColVal *pColVal) { - uint64_t val = *(uint64_t *)&pColVal->value.val; - pBuilder->sma.sum += val; - if (!pBuilder->minSet || pBuilder->sma.min > val) pBuilder->sma.min = val; - if (!pBuilder->maxSet || pBuilder->sma.max < val) pBuilder->sma.max = val; -} -static void (*tDiskColUpdateSmaImpl[])(SDiskColBuilder *pBuilder, SColVal *pColVal) = { - NULL, // TSDB_DATA_TYPE_NULL - tDiskColUpdateSmaBool, // TSDB_DATA_TYPE_BOOL - tDiskColUpdateSmaTinyint, // TSDB_DATA_TYPE_TINYINT - tDiskColUpdateSmaSmallint, // TSDB_DATA_TYPE_SMALLINT - tDiskColUpdateSmaInt, // TSDB_DATA_TYPE_INT - tDiskColUpdateBigint, // TSDB_DATA_TYPE_BIGINT - tDiskColUpdateFloat, // TSDB_DATA_TYPE_FLOAT - tDiskColUpdateDouble, // TSDB_DATA_TYPE_DOUBLE - NULL, // TSDB_DATA_TYPE_VARCHAR - tDiskColUpdateBigint, // TSDB_DATA_TYPE_TIMESTAMP - NULL, // TSDB_DATA_TYPE_NCHAR - tDiskColUpdateUTinyint, // TSDB_DATA_TYPE_UTINYINT - tDiskColUpdateUSmallint, // TSDB_DATA_TYPE_USMALLINT - tDiskColUpdateUInt, // TSDB_DATA_TYPE_UINT - tDiskColUpdateUBigint, // TSDB_DATA_TYPE_UBIGINT - NULL, // TSDB_DATA_TYPE_JSON - NULL, // TSDB_DATA_TYPE_VARBINARY - NULL, // TSDB_DATA_TYPE_DECIMAL - NULL, // TSDB_DATA_TYPE_BLOB - NULL, // TSDB_DATA_TYPE_MEDIUMBLOB -}; +extern void (*tSmaUpdateImpl[])(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet); static FORCE_INLINE void tDiskColUpdateSma(SDiskColBuilder *pBuilder, SColVal *pColVal) { if (pColVal->isNone || pColVal->isNull) { pBuilder->sma.numOfNull++; } else { - tDiskColUpdateSmaImpl[pBuilder->type](pBuilder, pColVal); + tSmaUpdateImpl[pBuilder->type](&pBuilder->sma, pColVal, &pBuilder->minSet, &pBuilder->maxSet); } } diff --git a/source/dnode/vnode/src/tsdb/tsdbUtil.c b/source/dnode/vnode/src/tsdb/tsdbUtil.c index c77af951af..0575b30526 100644 --- a/source/dnode/vnode/src/tsdb/tsdbUtil.c +++ b/source/dnode/vnode/src/tsdb/tsdbUtil.c @@ -1492,185 +1492,107 @@ int32_t tGetColumnDataAgg(uint8_t *p, SColumnDataAgg *pColAgg) { return n; } +#define SMA_UPDATE(SUM_V, MIN_V, MAX_V, VAL, MINSET, MAXSET) \ + do { \ + (SUM_V) += (VAL); \ + if (!(MINSET)) { \ + (MIN_V) = (VAL); \ + (MINSET) = 1; \ + } else if ((MIN_V) > (VAL)) { \ + (MIN_V) = (VAL); \ + } \ + if (!(MAXSET)) { \ + (MAX_V) = (VAL); \ + (MAXSET) = 1; \ + } else if ((MAX_V) < (VAL)) { \ + (MAX_V) = (VAL); \ + } \ + } while (0) + +static FORCE_INLINE void tSmaUpdateBool(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) { + int8_t val = *(int8_t *)&pColVal->value.val ? 1 : 0; + SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet); +} +static FORCE_INLINE void tSmaUpdateTinyint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, + uint8_t *maxSet) { + int8_t val = *(int8_t *)&pColVal->value.val; + SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet); +} +static FORCE_INLINE void tSmaUpdateSmallint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, + uint8_t *maxSet) { + int16_t val = *(int16_t *)&pColVal->value.val; + SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet); +} +static FORCE_INLINE void tSmaUpdateInt(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) { + int32_t val = *(int32_t *)&pColVal->value.val; + SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet); +} +static FORCE_INLINE void tSmaUpdateBigint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) { + int64_t val = *(int64_t *)&pColVal->value.val; + SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet); +} +static FORCE_INLINE void tSmaUpdateFloat(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) { + float val = *(float *)&pColVal->value.val; + SMA_UPDATE(*(double *)&pColAgg->sum, *(double *)&pColAgg->min, *(double *)&pColAgg->max, val, *minSet, *maxSet); +} +static FORCE_INLINE void tSmaUpdateDouble(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) { + double val = *(double *)&pColVal->value.val; + SMA_UPDATE(*(double *)&pColAgg->sum, *(double *)&pColAgg->min, *(double *)&pColAgg->max, val, *minSet, *maxSet); +} +static FORCE_INLINE void tSmaUpdateUTinyint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, + uint8_t *maxSet) { + uint8_t val = *(uint8_t *)&pColVal->value.val; + SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet); +} +static FORCE_INLINE void tSmaUpdateUSmallint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, + uint8_t *maxSet) { + uint16_t val = *(uint16_t *)&pColVal->value.val; + SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet); +} +static FORCE_INLINE void tSmaUpdateUInt(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) { + uint32_t val = *(uint32_t *)&pColVal->value.val; + SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet); +} +static FORCE_INLINE void tSmaUpdateUBigint(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, + uint8_t *maxSet) { + uint64_t val = *(uint64_t *)&pColVal->value.val; + SMA_UPDATE(pColAgg->sum, pColAgg->min, pColAgg->max, val, *minSet, *maxSet); +} +void (*tSmaUpdateImpl[])(SColumnDataAgg *pColAgg, SColVal *pColVal, uint8_t *minSet, uint8_t *maxSet) = { + NULL, + tSmaUpdateBool, // TSDB_DATA_TYPE_BOOL + tSmaUpdateTinyint, // TSDB_DATA_TYPE_TINYINT + tSmaUpdateSmallint, // TSDB_DATA_TYPE_SMALLINT + tSmaUpdateInt, // TSDB_DATA_TYPE_INT + tSmaUpdateBigint, // TSDB_DATA_TYPE_BIGINT + tSmaUpdateFloat, // TSDB_DATA_TYPE_FLOAT + tSmaUpdateDouble, // TSDB_DATA_TYPE_DOUBLE + NULL, // TSDB_DATA_TYPE_VARCHAR + tSmaUpdateBigint, // TSDB_DATA_TYPE_TIMESTAMP + NULL, // TSDB_DATA_TYPE_NCHAR + tSmaUpdateUTinyint, // TSDB_DATA_TYPE_UTINYINT + tSmaUpdateUSmallint, // TSDB_DATA_TYPE_USMALLINT + tSmaUpdateUInt, // TSDB_DATA_TYPE_UINT + tSmaUpdateUBigint, // TSDB_DATA_TYPE_UBIGINT + NULL, // TSDB_DATA_TYPE_JSON + NULL, // TSDB_DATA_TYPE_VARBINARY + NULL, // TSDB_DATA_TYPE_DECIMAL + NULL, // TSDB_DATA_TYPE_BLOB + NULL, // TSDB_DATA_TYPE_MEDIUMBLOB +}; void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) { - SColVal colVal; - SColVal *pColVal = &colVal; - - memset(pColAgg, 0, sizeof(*pColAgg)); - bool minAssigned = false; - bool maxAssigned = false; - *pColAgg = (SColumnDataAgg){.colId = pColData->cid}; + uint8_t minSet = 0; + uint8_t maxSet = 0; + + SColVal cv; for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) { - tColDataGetValue(pColData, iVal, pColVal); + tColDataGetValue(pColData, iVal, &cv); - if (pColVal->isNone || pColVal->isNull) { + if (cv.isNone || cv.isNull) { pColAgg->numOfNull++; } else { - switch (pColData->type) { - case TSDB_DATA_TYPE_NULL: - break; - case TSDB_DATA_TYPE_BOOL: - break; - case TSDB_DATA_TYPE_TINYINT: { - int8_t i8 = *(int8_t *)&colVal.value.val; - pColAgg->sum += i8; - if (!minAssigned || pColAgg->min > i8) { - pColAgg->min = i8; - minAssigned = true; - } - if (!maxAssigned || pColAgg->max < i8) { - pColAgg->max = i8; - maxAssigned = true; - } - break; - } - case TSDB_DATA_TYPE_SMALLINT: { - int16_t i16 = *(int16_t *)&colVal.value.val; - pColAgg->sum += i16; - if (!minAssigned || pColAgg->min > i16) { - pColAgg->min = i16; - minAssigned = true; - } - if (!maxAssigned || pColAgg->max < i16) { - pColAgg->max = i16; - maxAssigned = true; - } - break; - } - case TSDB_DATA_TYPE_INT: { - int32_t i32 = *(int32_t *)&colVal.value.val; - pColAgg->sum += i32; - if (!minAssigned || pColAgg->min > i32) { - pColAgg->min = i32; - minAssigned = true; - } - if (!maxAssigned || pColAgg->max < i32) { - pColAgg->max = i32; - maxAssigned = true; - } - break; - } - case TSDB_DATA_TYPE_BIGINT: { - int64_t i64 = *(int64_t *)&colVal.value.val; - pColAgg->sum += i64; - if (!minAssigned || pColAgg->min > i64) { - pColAgg->min = i64; - minAssigned = true; - } - if (!maxAssigned || pColAgg->max < i64) { - pColAgg->max = i64; - maxAssigned = true; - } - break; - } - case TSDB_DATA_TYPE_FLOAT: { - float f = *(float *)&colVal.value.val; - *(double *)(&pColAgg->sum) += f; - if (!minAssigned || *(double *)(&pColAgg->min) > f) { - *(double *)(&pColAgg->min) = f; - minAssigned = true; - } - if (!maxAssigned || *(double *)(&pColAgg->max) < f) { - *(double *)(&pColAgg->max) = f; - maxAssigned = true; - } - break; - } - case TSDB_DATA_TYPE_DOUBLE: { - double d = *(double *)&colVal.value.val; - *(double *)(&pColAgg->sum) += d; - if (!minAssigned || *(double *)(&pColAgg->min) > d) { - *(double *)(&pColAgg->min) = d; - minAssigned = true; - } - if (!maxAssigned || *(double *)(&pColAgg->max) < d) { - *(double *)(&pColAgg->max) = d; - maxAssigned = true; - } - break; - } - case TSDB_DATA_TYPE_VARCHAR: - break; - case TSDB_DATA_TYPE_TIMESTAMP: { - int64_t ts = *(int64_t *)&colVal.value.val; - if (!minAssigned || pColAgg->min > ts) { - pColAgg->min = ts; - minAssigned = true; - } - if (!maxAssigned || pColAgg->max < ts) { - pColAgg->max = ts; - maxAssigned = true; - } - break; - } - case TSDB_DATA_TYPE_NCHAR: - break; - case TSDB_DATA_TYPE_UTINYINT: { - uint8_t u8 = *(uint8_t *)&colVal.value.val; - pColAgg->sum += u8; - if (!minAssigned || pColAgg->min > u8) { - pColAgg->min = u8; - minAssigned = true; - } - if (!maxAssigned || pColAgg->max < u8) { - pColAgg->max = u8; - maxAssigned = true; - } - break; - } - case TSDB_DATA_TYPE_USMALLINT: { - uint16_t u16 = *(uint16_t *)&colVal.value.val; - pColAgg->sum += u16; - if (!minAssigned || pColAgg->min > u16) { - pColAgg->min = u16; - minAssigned = true; - } - if (!maxAssigned || pColAgg->max < u16) { - pColAgg->max = u16; - maxAssigned = true; - } - break; - } - case TSDB_DATA_TYPE_UINT: { - uint32_t u32 = *(uint32_t *)&colVal.value.val; - pColAgg->sum += u32; - if (!minAssigned || pColAgg->min > u32) { - pColAgg->min = u32; - minAssigned = true; - } - if (!minAssigned || pColAgg->max < u32) { - pColAgg->max = u32; - maxAssigned = true; - } - break; - } - case TSDB_DATA_TYPE_UBIGINT: { - uint64_t u64 = *(uint64_t *)&colVal.value.val; - pColAgg->sum += u64; - if (!minAssigned || pColAgg->min > u64) { - pColAgg->min = u64; - minAssigned = true; - } - if (!maxAssigned || pColAgg->max < u64) { - pColAgg->max = u64; - maxAssigned = true; - } - break; - } - case TSDB_DATA_TYPE_JSON: - break; - case TSDB_DATA_TYPE_VARBINARY: - break; - case TSDB_DATA_TYPE_DECIMAL: - break; - case TSDB_DATA_TYPE_BLOB: - break; - case TSDB_DATA_TYPE_MEDIUMBLOB: - break; - default: - ASSERT(0); - } + tSmaUpdateImpl[pColData->type](pColAgg, &cv, &minSet, &maxSet); } } } -- GitLab