From 324a4eab4ea5dea6f785d22b13819ee1aa4a76c1 Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Tue, 22 Oct 2024 10:21:42 +0800 Subject: [PATCH] opti:modify result type for case when --- source/libs/parser/src/parTranslater.c | 7 ++- source/libs/scalar/src/sclvector.c | 45 +++++++++--------- tests/script/tsim/scalar/caseWhen.sim | 63 +++++++++++--------------- 3 files changed, 54 insertions(+), 61 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 26dd98e0d0..126d9e0122 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3200,7 +3200,7 @@ static int32_t selectCommonType(SDataType* commonType, const SDataType* newType) } else { resultType = gDisplyTypes[type2][type1]; } - if (resultType == -1 || resultType == 0) { + if (resultType == -1) { return TSDB_CODE_SCALAR_CONVERT_ERROR; } if (commonType->type == newType->type) { @@ -3214,8 +3214,11 @@ static int32_t selectCommonType(SDataType* commonType, const SDataType* newType) *commonType = *newType; return TSDB_CODE_SUCCESS; } + commonType->bytes = TMAX(TMAX(commonType->bytes, newType->bytes), TYPE_BYTES[resultType]); + if(resultType == TSDB_DATA_TYPE_VARCHAR && (IS_FLOAT_TYPE(commonType->type) || IS_FLOAT_TYPE(newType->type))) { + commonType->bytes += TYPE_BYTES[TSDB_DATA_TYPE_DOUBLE]; + } commonType->type = resultType; - commonType->bytes = TMAX(TMAX(commonType->bytes, newType->bytes),TYPE_BYTES[resultType]); return TSDB_CODE_SUCCESS; } diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index d9ccd64268..82d79a9564 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -1031,28 +1031,29 @@ int8_t gConvertTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX] = { /*GEOM*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0}; 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*/ -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, 0, 0, 8, - /*BOOL*/ 0, 1, 2, 3, 4, 5, 6, 7, 8, 5, 10, 11, 12, 13, 14, 8, -1, 0, 0, 0, 8, - /*TINY*/ 0, 0, 2, 3, 4, 5, 6, 7, 8, 5, 10, 3, 4, 5, 8, 8, -1, 0, 0, 0, 8, - /*SMAL*/ 0, 0, 0, 3, 4, 5, 6, 7, 8, 5, 10, 3, 4, 5, 8, 8, -1, 0, 0, 0, 8, - /*INT */ 0, 0, 0, 0, 4, 5, 6, 7, 8, 5, 10, 4, 4, 5, 8, 8, -1, 0, 0, 0, 8, - /*BIGI*/ 0, 0, 0, 0, 0, 5, 6, 7, 8, 5, 10, 5, 5, 5, 8, 8, -1, 0, 0, 0, 8, - /*FLOA*/ 0, 0, 0, 0, 0, 0, 6, 7, 8, 8, 10, 6, 6, 6, 8, 8, -1, 0, 0, 0, 8, - /*DOUB*/ 0, 0, 0, 0, 0, 0, 0, 7, 8, 8, 10, 7, 7, 7, 8, 8, -1, 0, 0, 0, 8, - /*VARC*/ 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 10, 8, 8, 8, 8, 8, -1, 0, 0, 0, 8, - /*TIME*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 10, 5, 5, 5, 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, 10, - /*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, 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, 5, 8, 8, 8, 5, 10, 4, 4, 5, 8, 8, -1, -1, -1, -1, 8, + /*BIGI*/ 0, 0, 0, 0, 0, 5, 8, 8, 8, 5, 10, 5, 5, 5, 8, 8, -1, -1, -1, -1, 8, + /*FLOA*/ 0, 0, 0, 0, 0, 0, 6, 7, 8, 8, 10, 8, 8, 8, 8, 8, -1, -1, -1, -1, 8, + /*DOUB*/ 0, 0, 0, 0, 0, 0, 0, 7, 8, 8, 10, 8, 8, 8, 8, 8, -1, -1, -1, -1, 8, + /*VARC*/ 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 10, 8, 8, 8, 8, 8, -1, -1, -1, -1, 8, + /*TIME*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 10, 5, 5, 5, 8, 8, -1, -1, -1, -1, 8, + /*NCHA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 10, -1, -1, -1, -1, 10, + /*UTINY*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 12, 13, 14, 8, -1, -1, -1, -1, 8, + /*USMA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 13, 14, 8, -1, -1, -1, -1, 8, + /*UINT*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 14, 8, -1, -1, -1, -1, 8, + /*UBIG*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 8, -1, -1, -1, -1, 8, + /*JSON*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, -1, -1, -1, -1, 8, + /*VARB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, -1, -1, -1, -1, + /*DECI*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, + /*BLOB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, + /*MEDB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -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 1bc7352d25..7c9e35321f 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -397,35 +397,20 @@ sql select case when f1 then f1 when f1 + 1 then f1 + 1 else f1 is null end from if $rows != 4 then return -1 endi -if $data00 != 1.000000000 then +if $data00 != 1.000000 then return -1 endi -if $data10 != 1.000000000 then +if $data10 != 1 then return -1 endi -if $data20 != 5.000000000 then +if $data20 != 5 then return -1 endi -if $data30 != 1.000000000 then +if $data30 != true then return -1 endi -sql select case when f1 then 3 when ts then ts end from tba1; -if $rows != 4 then - return -1 -endi -if $data00 != 1664176501000 then - return -1 -endi -if $data10 != 3 then - return -1 -endi -if $data20 != 3 then - return -1 -endi -if $data30 != 1664176504000 then - return -1 -endi + sql select case when 3 then f1 end from tba1; if $rows != 4 then @@ -601,7 +586,6 @@ endi if $data30 != 1.000000000 then return -1 endi - sql select sum(case f1 when f1 then f1 + 1 else f1 is null end + 1) from tba1; if $rows != 1 then return -1 @@ -657,16 +641,16 @@ sql select case when f1 is not null then case when f1 <= 0 then f1 else f1 * 10 if $rows != 4 then return -1 endi -if $data00 != 0.000000000 then +if $data00 != 0 then return -1 endi -if $data10 != 10.000000000 then +if $data10 != 10.000000 then return -1 endi -if $data20 != 50.000000000 then +if $data20 != 50.000000 then return -1 endi -if $data30 != -1.000000000 then +if $data30 != -1 then return -1 endi @@ -890,19 +874,19 @@ endi if $data10 != 0 then return -1 endi -if $data11 != -99.000000000 then +if $data11 != -99.000000 then return -1 endi if $data20 != 1 then return -1 endi -if $data21 != 100.000000000 then +if $data21 != 100.000000 then return -1 endi if $data30 != 5 then return -1 endi -if $data31 != -94.000000000 then +if $data31 != -94.000000 then return -1 endi @@ -1029,13 +1013,13 @@ endi if $data00 != NULL then return -1 endi -if $data10 != -99.000000000 then +if $data10 != -99.000000 then return -1 endi -if $data20 != 1.000000000 then +if $data20 != 1 then return -1 endi -if $data30 != 5.000000000 then +if $data30 != 5 then return -1 endi @@ -1043,19 +1027,19 @@ sql select distinct tbname, case t1 when t2 then t1 else t1 + 100 end from sta o if $rows != 5 then return -1 endi -if $data01 != 0.000000000 then +if $data01 != 0 then return -1 endi -if $data11 != 1.000000000 then +if $data11 != 1 then return -1 endi if $data21 != NULL then return -1 endi -if $data31 != 101.000000000 then +if $data31 != 101.000000 then return -1 endi -if $data41 != 103.000000000 then +if $data41 != 103.000000 then return -1 endi @@ -1072,7 +1056,7 @@ sql select case when c_int > 100 then c_float else c_int end as result from t_te if $rows != 1 then return -1 endi -if $data00 != 123.45000 then +if $data00 != 123.449997 then return -1 endi @@ -1080,7 +1064,7 @@ sql select case when c_bigint > 100000 then c_double else c_bigint end as result if $rows != 1 then return -1 endi -if $data00 != 678.900000000 then +if $data00 != 678.900000 then return -1 endi @@ -1146,6 +1130,11 @@ if $data00 != 16842773 then return -1 endi +sql select case when 0 then tag_id else c_nchar end as result from t_test; +if $data00 != 涛思数据 then + return -1 +endi + sql select case when 0 then tag_id else c_int end as result from t_test; if $data00 != 123 then return -1