From 7a1ffd92accee32904c667858ab66b08fc1fb708 Mon Sep 17 00:00:00 2001 From: wangjiaming0909 Date: Tue, 4 Mar 2025 22:28:26 +0800 Subject: [PATCH] fix decimal op test --- include/libs/decimal/decimal.h | 1 + source/libs/decimal/src/decimal.c | 84 ++++++++++++++++++++++------ source/libs/executor/CMakeLists.txt | 2 +- source/libs/executor/src/operator.c | 3 - source/util/src/tcompare.c | 4 +- tests/system-test/2-query/decimal.py | 9 ++- 6 files changed, 74 insertions(+), 29 deletions(-) diff --git a/include/libs/decimal/decimal.h b/include/libs/decimal/decimal.h index 3319a76ab7..b4df324ee7 100644 --- a/include/libs/decimal/decimal.h +++ b/include/libs/decimal/decimal.h @@ -90,6 +90,7 @@ int32_t decimal128ToDataVal(Decimal128* dec, SValue* pVal); int32_t decimalToStr(const DecimalType* pDec, int8_t type, int8_t precision, int8_t scale, char* pBuf, int32_t bufLen); int32_t decimalGetRetType(const SDataType* pLeftT, const SDataType* pRightT, EOperatorType opType, SDataType* pOutType); +bool decimal64Compare(EOperatorType op, const SDecimalCompareCtx* pLeft, const SDecimalCompareCtx* pRight); bool decimalCompare(EOperatorType op, const SDecimalCompareCtx* pLeft, const SDecimalCompareCtx* pRight); int32_t decimalOp(EOperatorType op, const SDataType* pLeftT, const SDataType* pRightT, const SDataType* pOutT, const void* pLeftData, const void* pRightData, void* pOutputData); diff --git a/source/libs/decimal/src/decimal.c b/source/libs/decimal/src/decimal.c index acea6e5bf0..6856cd5a85 100644 --- a/source/libs/decimal/src/decimal.c +++ b/source/libs/decimal/src/decimal.c @@ -1125,6 +1125,71 @@ int32_t decimalOp(EOperatorType op, const SDataType* pLeftT, const SDataType* pR return code; } +bool doCompareDecimal128(EOperatorType op, const Decimal128* pLeftDec, const Decimal128* pRightDec) { + switch (op) { + case OP_TYPE_GREATER_THAN: + return decimal128Gt(pLeftDec, pRightDec, WORD_NUM(Decimal)); + case OP_TYPE_GREATER_EQUAL: + return !decimal128Lt(pLeftDec, pRightDec, WORD_NUM(Decimal)); + case OP_TYPE_LOWER_THAN: + return decimal128Lt(pLeftDec, pRightDec, WORD_NUM(Decimal)); + case OP_TYPE_LOWER_EQUAL: + return !decimal128Gt(pLeftDec, pRightDec, WORD_NUM(Decimal)); + case OP_TYPE_EQUAL: + return decimal128Eq(pLeftDec, pRightDec, WORD_NUM(Decimal)); + case OP_TYPE_NOT_EQUAL: + return !decimal128Eq(pLeftDec, pRightDec, WORD_NUM(Decimal)); + default: + break; + } + return false; +} + +bool decimal64Compare(EOperatorType op, const SDecimalCompareCtx* pLeft, const SDecimalCompareCtx* pRight) { + bool ret = false; + uint8_t leftPrec = 0, leftScale = 0, rightPrec = 0, rightScale = 0; + decimalFromTypeMod(pLeft->typeMod, &leftPrec, &leftScale); + decimalFromTypeMod(pRight->typeMod, &rightPrec, &rightScale); + int32_t deltaScale = leftScale - rightScale; + + Decimal64 leftDec = *(Decimal64*)pLeft->pData, rightDec = *(Decimal64*)pRight->pData; + + if (deltaScale != 0) { + bool needInt128 = (deltaScale < 0 && leftPrec - deltaScale > TSDB_DECIMAL64_MAX_PRECISION) || + (rightPrec + deltaScale > TSDB_DECIMAL64_MAX_PRECISION); + if (needInt128) { + Decimal128 dec128L = {0}, dec128R = {0}; + makeDecimal128FromDecimal64(&dec128L, leftDec); + makeDecimal128FromDecimal64(&dec128R, rightDec); + return doCompareDecimal128(op, &dec128L, &dec128R); + } else { + if (deltaScale < 0) { + decimal64ScaleUp(&leftDec, -deltaScale); + } else { + decimal64ScaleUp(&rightDec, deltaScale); + } + } + } + + switch (op) { + case OP_TYPE_GREATER_THAN: + return decimal64Gt(&leftDec, &rightDec, WORD_NUM(Decimal64)); + case OP_TYPE_GREATER_EQUAL: + return !decimal64Lt(&leftDec, &rightDec, WORD_NUM(Decimal64)); + case OP_TYPE_LOWER_THAN: + return decimal64Lt(&leftDec, &rightDec, WORD_NUM(Decimal64)); + case OP_TYPE_LOWER_EQUAL: + return !decimal64Gt(&leftDec, &rightDec, WORD_NUM(Decimal64)); + case OP_TYPE_EQUAL: + return decimal64Eq(&leftDec, &rightDec, WORD_NUM(Decimal64)); + case OP_TYPE_NOT_EQUAL: + return !decimal64Eq(&leftDec, &rightDec, WORD_NUM(Decimal64)); + default: + break; + } + return ret; +} + // There is no need to do type conversions, we assume that pLeftT and pRightT are all decimal128 types. bool decimalCompare(EOperatorType op, const SDecimalCompareCtx* pLeft, const SDecimalCompareCtx* pRight) { bool ret = false; @@ -1171,24 +1236,7 @@ bool decimalCompare(EOperatorType op, const SDecimalCompareCtx* pLeft, const SDe } } } - - switch (op) { - case OP_TYPE_GREATER_THAN: - return decimal128Gt(&pLeftDec, &pRightDec, WORD_NUM(Decimal)); - case OP_TYPE_GREATER_EQUAL: - return !decimal128Lt(&pLeftDec, &pRightDec, WORD_NUM(Decimal)); - case OP_TYPE_LOWER_THAN: - return decimal128Lt(&pLeftDec, &pRightDec, WORD_NUM(Decimal)); - case OP_TYPE_LOWER_EQUAL: - return !decimal128Gt(&pLeftDec, &pRightDec, WORD_NUM(Decimal)); - case OP_TYPE_EQUAL: - return decimal128Eq(&pLeftDec, &pRightDec, WORD_NUM(Decimal)); - case OP_TYPE_NOT_EQUAL: - return !decimal128Eq(&pLeftDec, &pRightDec, WORD_NUM(Decimal)); - default: - break; - } - return ret; + return doCompareDecimal128(op, &pLeftDec, &pRightDec); } #define ABS_INT64(v) (v) == INT64_MIN ? (uint64_t)INT64_MAX + 1 : (uint64_t)llabs(v) diff --git a/source/libs/executor/CMakeLists.txt b/source/libs/executor/CMakeLists.txt index 9a6b52bfb5..9a49076b6b 100644 --- a/source/libs/executor/CMakeLists.txt +++ b/source/libs/executor/CMakeLists.txt @@ -11,7 +11,7 @@ if(${BUILD_WITH_ANALYSIS}) endif() target_link_libraries(executor - PRIVATE os util common function parser planner qcom scalar nodes index wal tdb geometry profiler + PRIVATE os util common function parser planner qcom scalar nodes index wal tdb geometry ) target_include_directories( diff --git a/source/libs/executor/src/operator.c b/source/libs/executor/src/operator.c index 96595a6a7f..057deed038 100644 --- a/source/libs/executor/src/operator.c +++ b/source/libs/executor/src/operator.c @@ -28,7 +28,6 @@ #include "storageapi.h" #include "tdatablock.h" -#include "gperftools/profiler.h" SOperatorFpSet createOperatorFpSet(__optr_open_fn_t openFn, __optr_fn_t nextFn, __optr_fn_t cleanup, __optr_close_fn_t closeFn, __optr_reqBuf_fn_t reqBufFn, __optr_explain_fn_t explain, @@ -283,7 +282,6 @@ int32_t stopTableScanOperator(SOperatorInfo* pOperator, const char* pIdStr, SSto int32_t createOperator(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo, SReadHandle* pHandle, SNode* pTagCond, SNode* pTagIndexCond, const char* pUser, const char* dbname, SOperatorInfo** pOptrInfo) { QRY_PARAM_CHECK(pOptrInfo); - ProfilerStart("/tmp/createOperator.prof"); int32_t code = 0; int32_t type = nodeType(pPhyNode); @@ -656,7 +654,6 @@ int32_t createOperator(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo, SReadHand } void destroyOperator(SOperatorInfo* pOperator) { - ProfilerFlush(); if (pOperator == NULL) { return; } diff --git a/source/util/src/tcompare.c b/source/util/src/tcompare.c index 8434c875db..a89637c2fa 100644 --- a/source/util/src/tcompare.c +++ b/source/util/src/tcompare.c @@ -1045,8 +1045,8 @@ int32_t compareUint64Uint32(const void *pLeft, const void *pRight) { } int32_t compareDecimal64(const void* pleft, const void* pright) { - if (decimalCompare(OP_TYPE_GREATER_THAN, pleft, pright)) return 1; - if (decimalCompare(OP_TYPE_LOWER_THAN, pleft, pright)) return -1; + if (decimal64Compare(OP_TYPE_GREATER_THAN, pleft, pright)) return 1; + if (decimal64Compare(OP_TYPE_LOWER_THAN, pleft, pright)) return -1; return 0; } diff --git a/tests/system-test/2-query/decimal.py b/tests/system-test/2-query/decimal.py index aa74754668..272140d002 100644 --- a/tests/system-test/2-query/decimal.py +++ b/tests/system-test/2-query/decimal.py @@ -43,11 +43,11 @@ scalar_convert_err = -2147470768 decimal_insert_validator_test = False -operator_test_round = 1 +operator_test_round = 10 tb_insert_rows = 1000 -binary_op_with_const_test = False -binary_op_with_col_test = False -unary_op_test = False +binary_op_with_const_test = True +binary_op_with_col_test = True +unary_op_test = True binary_op_in_where_test = True class DecimalTypeGeneratorConfig: @@ -1516,7 +1516,6 @@ class TDTestCase: get_constant_cols_func, get_exprs_func, ): - constant_cols = get_constant_cols_func() exprs: List[DecimalColumnExpr] = get_exprs_func() if not binary_op_with_const_test: return