From 23d119c65456cef8bc0a6600a32b27a688f87fb0 Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Wed, 16 Oct 2024 20:21:56 +0800 Subject: [PATCH] opti:modify result type of case when --- source/libs/parser/src/parTranslater.c | 45 +++++++++++--------------- source/libs/scalar/src/sclvector.c | 43 ++++++++++++------------ tests/script/tsim/scalar/caseWhen.sim | 31 +++++++++--------- 3 files changed, 57 insertions(+), 62 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index e7a8d8e79c..26dd98e0d0 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3187,23 +3187,10 @@ static int32_t rewriteIsTrue(SNode* pSrc, SNode** pIsTrue) { } extern int8_t gDisplyTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX]; -static bool selectCommonType(SDataType* commonType, const SDataType* newType) { +static int32_t selectCommonType(SDataType* commonType, const SDataType* newType) { if (commonType->type < TSDB_DATA_TYPE_NULL || commonType->type >= TSDB_DATA_TYPE_MAX || newType->type < TSDB_DATA_TYPE_NULL || newType->type >= TSDB_DATA_TYPE_MAX) { - return false; - } - if (commonType->type == TSDB_DATA_TYPE_NULL) { - *commonType = *newType; - return true; - } - if (newType->type == TSDB_DATA_TYPE_NULL) { - return true; - } - if (commonType->type == newType->type) { - if (commonType->bytes < newType->bytes) { - *commonType = *newType; - } - return true; + return TSDB_CODE_INVALID_PARA; } int8_t type1 = commonType->type; int8_t type2 = newType->type; @@ -3214,18 +3201,22 @@ static bool selectCommonType(SDataType* commonType, const SDataType* newType) { resultType = gDisplyTypes[type2][type1]; } if (resultType == -1 || resultType == 0) { - return false; - } + return TSDB_CODE_SCALAR_CONVERT_ERROR; + } + if (commonType->type == newType->type) { + commonType->bytes = TMAX(commonType->bytes, newType->bytes); + return TSDB_CODE_SUCCESS; + } if (resultType == commonType->type){ - return true; + return TSDB_CODE_SUCCESS; } if(resultType == newType->type) { *commonType = *newType; - return true; + return TSDB_CODE_SUCCESS; } commonType->type = resultType; - commonType->bytes = TYPE_BYTES[resultType]; - return true; + commonType->bytes = TMAX(TMAX(commonType->bytes, newType->bytes),TYPE_BYTES[resultType]); + return TSDB_CODE_SUCCESS; } @@ -3248,16 +3239,18 @@ static EDealRes translateCaseWhen(STranslateContext* pCxt, SCaseWhenNode* pCaseW continue; } allNullThen = false; - if (!selectCommonType(&pCaseWhen->node.resType, &pThenExpr->resType)) { - pCxt->errCode = TSDB_CODE_SCALAR_CONVERT_ERROR; + pCxt->errCode = selectCommonType(&pCaseWhen->node.resType, &pThenExpr->resType); + if(TSDB_CODE_SUCCESS != pCxt->errCode){ return DEAL_RES_ERROR; } } SExprNode* pElseExpr = (SExprNode*)pCaseWhen->pElse; - if (pElseExpr && !selectCommonType(&pCaseWhen->node.resType, &pElseExpr->resType)) { - pCxt->errCode = TSDB_CODE_SCALAR_CONVERT_ERROR; - return DEAL_RES_ERROR; + if (NULL != pElseExpr) { + pCxt->errCode = selectCommonType(&pCaseWhen->node.resType, &pElseExpr->resType); + if(TSDB_CODE_SUCCESS != pCxt->errCode) { + return DEAL_RES_ERROR; + } } if (allNullThen) { diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index ee65e4812b..5b785dcb39 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -1032,27 +1032,28 @@ int8_t gConvertTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX] = { int8_t gDisplyTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX] = { /*NULL BOOL TINY SMAL INT BIG FLOA DOUB VARC TIME NCHA UTIN USMA UINT UBIG JSON VARB DECI BLOB MEDB GEOM*/ - /*NULL*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - /*BOOL*/ 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,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, 0, 8, + /*TIME*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 10, 5, 5, 14, 8, 8, -1, 0, 0, 0, 8, + /*NCHA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 10, -1, 0, 0, 0, 8, + /*UTIN*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 12, 13, 14, 8, -1, 0, 0, 0, 8, + /*USMA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 13, 14, 8, -1, 0, 0, 0, 8, + /*UINT*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 14, 8, -1, 0, 0, 0, 8, + /*UBIG*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 8, -1, 0, 0, 0, 8, + /*JSON*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, -1, 0, 0, 0, -1, + /*VARB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, + /*DECI*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /*BLOB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /*MEDB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, + /*GEOM*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20 +}; int32_t vectorGetConvertType(int32_t type1, int32_t type2) { if (type1 == type2) { diff --git a/tests/script/tsim/scalar/caseWhen.sim b/tests/script/tsim/scalar/caseWhen.sim index 211e43a660..f7f554cd56 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -414,16 +414,16 @@ sql select case when f1 then 3 when ts then ts end from tba1; if $rows != 4 then return -1 endi -if $data00 != @22-09-26 15:15:01.000@ then +if $data00 != 1664176501000 then return -1 endi -if $data10 != @70-01-01 08:00:00.003@ then +if $data10 != 3 then return -1 endi -if $data20 != @70-01-01 08:00:00.003@ then +if $data20 != 3 then return -1 endi -if $data30 != @22-09-26 15:15:04.000@ then +if $data30 != 1664176504000 then return -1 endi @@ -830,16 +830,16 @@ sql select case cast(f2 as int) when 0 then f2 when f1 then 11 else ts end from if $rows != 4 then return -1 endi -if $data00 != @70-01-01 08:00:00.000@ then +if $data00 != a then return -1 endi -if $data10 != @70-01-01 08:00:00.000@ then +if $data10 != 0 then return -1 endi -if $data20 != @70-01-01 08:00:00.011@ then +if $data20 != 11 then return -1 endi -if $data30 != @22-09-26 15:15:04.000@ then +if $data30 != 1664176504 then return -1 endi @@ -1117,7 +1117,7 @@ if $data00 != 123 then endi sql select case when ts > '2022-01-01 00:00:00' then c_bool else ts end as result from t_test; -if $data00 != @70-01-01 08:00:00.001@ then +if $data00 != 1 then return -1 endi @@ -1131,17 +1131,18 @@ if $data00 != varchar_val then return -1 endi -sql select case when c_bool then c_varbinary else c_varchar end as result from t_test; -if $data00 != null then - return -1; -endi sql select case when ts > '2022-01-01 00:00:00' then c_varchar else c_geometry end as result from t_test; - if $data00 != varchar_val then return -1 endi + +sql select case when ts > '2022-01-01 00:00:00' then c_bool else c_geometry end as result from t_test; +if $data00 != true then + return -1 +endi + sql_error select case when c_double > 100 then c_varbinary else c_geometry end as result from t_test; -sql_error select case when ts > '2022-01-01 00:00:00' then c_bool else c_geometry end as result from t_test; sql_error select case when c_bool then c_double else c_varbinary end as result from t_test; +sql_error select case when c_bool then c_varbinary else c_varchar end as result from t_test; system sh/exec.sh -n dnode1 -s stop -x SIGINT