diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 752525fe7f..636be7c5cc 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3293,8 +3293,44 @@ static int32_t rewriteIsTrue(SNode* pSrc, SNode** pIsTrue) { return TSDB_CODE_SUCCESS; } +extern int8_t gDisplyTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX]; +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 TSDB_CODE_INVALID_PARA; + } + int8_t type1 = commonType->type; + int8_t type2 = newType->type; + int8_t resultType; + if (type1 < type2) { + resultType = gDisplyTypes[type1][type2]; + } else { + resultType = gDisplyTypes[type2][type1]; + } + if (resultType == -1) { + 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 TSDB_CODE_SUCCESS; + } + if(resultType == newType->type) { + *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; + return TSDB_CODE_SUCCESS; + +} + static EDealRes translateCaseWhen(STranslateContext* pCxt, SCaseWhenNode* pCaseWhen) { - bool first = true; bool allNullThen = true; SNode* pNode = NULL; FOREACH(pNode, pCaseWhen->pWhenThenList) { @@ -3313,10 +3349,18 @@ static EDealRes translateCaseWhen(STranslateContext* pCxt, SCaseWhenNode* pCaseW continue; } allNullThen = false; - if (first || dataTypeComp(&pCaseWhen->node.resType, &pThenExpr->resType) < 0) { - pCaseWhen->node.resType = pThenExpr->resType; + pCxt->errCode = selectCommonType(&pCaseWhen->node.resType, &pThenExpr->resType); + if(TSDB_CODE_SUCCESS != pCxt->errCode){ + return DEAL_RES_ERROR; + } + } + + SExprNode* pElseExpr = (SExprNode*)pCaseWhen->pElse; + if (NULL != pElseExpr) { + pCxt->errCode = selectCommonType(&pCaseWhen->node.resType, &pElseExpr->resType); + if(TSDB_CODE_SUCCESS != pCxt->errCode) { + return DEAL_RES_ERROR; } - first = false; } if (allNullThen) { diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index a086d1f367..54794b9044 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -1030,6 +1030,31 @@ int8_t gConvertTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX] = { /*MEDB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, /*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 BIGI FLOA DOUB VARC TIME NCHA UTINY USMA UINT UBIG JSON VARB DECI BLOB MEDB GEOM*/ + /*NULL*/ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, -1, -1, -1, 8, + /*BOOL*/ 0, 1, 2, 3, 4, 5, 6, 7, 8, 5, 10, 11, 12, 13, 14, 8, -1, -1, -1, -1, 8, + /*TINY*/ 0, 0, 2, 3, 4, 5, 8, 8, 8, 5, 10, 3, 4, 5, 8, 8, -1, -1, -1, -1, 8, + /*SMAL*/ 0, 0, 0, 3, 4, 5, 8, 8, 8, 5, 10, 3, 4, 5, 8, 8, -1, -1, -1, -1, 8, + /*INT */ 0, 0, 0, 0, 4, 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) { return 0; diff --git a/tests/script/tsim/scalar/caseWhen.sim b/tests/script/tsim/scalar/caseWhen.sim index c10413f23c..67c8ac3673 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -397,7 +397,7 @@ 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 then +if $data00 != 1.000000 then return -1 endi if $data10 != 1 then @@ -406,26 +406,26 @@ endi if $data20 != 5 then return -1 endi -if $data30 != 1 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 + if $rows != 4 then + return -1 + endi + if $data00 != 1664176501000 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 + 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 +601,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 @@ -660,10 +659,10 @@ endi if $data00 != 0 then return -1 endi -if $data10 != 10 then +if $data10 != 10.000000 then return -1 endi -if $data20 != 50 then +if $data20 != 50.000000 then return -1 endi if $data30 != -1 then @@ -890,19 +889,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 +1028,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 @@ -1052,13 +1051,117 @@ endi if $data21 != NULL then return -1 endi -if $data31 != 101 then +if $data31 != 101.000000 then return -1 endi -if $data41 != 103 then +if $data41 != 103.000000 then return -1 endi sql_error select case when sum(f1) then sum(f1)-abs(f1) end from tba1; +sql drop database if exists test_db; +sql create database test_db vgroups 5; +sql use test_db; +sql create stable test_stable (ts TIMESTAMP,c_int INT,c_uint INT UNSIGNED, c_bigint BIGINT, c_ubigint BIGINT UNSIGNED, c_float FLOAT, c_double DOUBLE, c_binary BINARY(20), c_smallint SMALLINT, c_usmallint SMALLINT UNSIGNED, c_tinyint TINYINT,c_utinyint TINYINT UNSIGNED,c_bool BOOL,c_nchar NCHAR(20), c_varchar VARCHAR(20), c_varbinary VARBINARY(20), c_geometry GEOMETRY(50)) tags(tag_id JSON); +sql create table t_test using test_stable tags('{\"tag1\":5}'); +sql insert into t_test values ('2022-09-30 15:15:01',123,456,1234567890,9876543210,123.45,678.90,'binary_val',32767,65535,127,255,true,'涛思数据','varchar_val', '1101', 'point(10 10)'); + +sql select case when c_int > 100 then c_float else c_int end as result from t_test; +if $rows != 1 then + return -1 +endi +if $data00 != 123.449997 then + return -1 +endi + +sql select case when c_bigint > 100000 then c_double else c_bigint end as result from t_test; +if $rows != 1 then + return -1 +endi +if $data00 != 678.900000 then + return -1 +endi + +sql select case when c_bool then c_bool else c_utinyint end as result from t_test; +if $rows != 1 then + return -1 +endi +if $data00 != 1 then + return -1 +endi + +sql select case when c_smallint > 30000 then c_usmallint else c_smallint end as result from t_test; +if $rows != 1 then + return -1 +endi +if $data00 != 65535 then + return -1 +endi + +sql select case when c_binary = 'binary_val' then c_nchar else c_binary end as result from t_test; +if $rows != 1 then + return -1 +endi +if $data00 != 涛思数据 then + return -1 +endi + +sql select case when c_bool then c_int else c_bool end as result from t_test; +if $rows != 1 then + return -1 +endi +if $data00 != 123 then + return -1 +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 != 1 then + return -1 +endi + +sql select case when c_double > 100 then c_nchar else c_double end as result from t_test; +if $data00 != 涛思数据 then + return -1 +endi + +sql select case when c_double > 100 then c_varchar else c_double 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_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 select case when 0 then tag_id else c_geometry end as result from t_test; +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 +endi + +sql select case when 0 then tag_id else c_float end as result from t_test; +if $data00 != 123.449997 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 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 diff --git a/tests/system-test/2-query/case_when.py b/tests/system-test/2-query/case_when.py index 1ccd2b5076..85fe43b487 100755 --- a/tests/system-test/2-query/case_when.py +++ b/tests/system-test/2-query/case_when.py @@ -231,12 +231,12 @@ class TDTestCase: 'first case when \'%d\' then \'b\' when null then %d end last' %(a1,a2) , #'first case when \'2\' then \'b\' when null then 0 end last' , 'first case when \'%d\' then \'b\' else null end last' %(a1), #'first case when \'0\' then \'b\' else null end last', 'first case when \'%d\' then \'b\' else %d end last' %(a1,a2), #'first case when \'0\' then \'b\' else 2 end last', - 'first case when q_int then q_int when q_int + (%d) then q_int + (%d) else q_int is null end last' %(a1,a2) , #'first case when q_int then q_int when q_int + 1 then q_int + 1 else q_int is null end last' , - 'first case when q_int then %d when ts then ts end last' %(a1), #'first case when q_int then 3 when ts then ts end last' , + 'first case when q_int then q_int when q_int + (%d) then cast(q_int + (%d) as int) else q_int is null end last' %(a1,a2) , #'first case when q_int then q_int when q_int + 1 then q_int + 1 else q_int is null end last' , + 'first case when q_int then %d when ts then cast(ts as int) end last' %(a1), #'first case when q_int then 3 when ts then ts end last' , 'first case when %d then q_int end last' %(a1), #'first case when 3 then q_int end last' , 'first case when q_int then %d when %d then %d end last' %(a1,a1,a3), #'first case when q_int then 3 when 1 then 2 end last' , 'first case when q_int < %d then %d when q_int >= %d then %d else %d end last' %(a1,a2,a1,a2,a3), #'first case when q_int < 3 then 1 when q_int >= 3 then 2 else 3 end last' , - 'first case when q_int is not null then case when q_int <= %d then q_int else q_int * (%d) end else -(%d) end last' %(a1,a1,a3), #'first case when q_int is not null then case when q_int <= 0 then q_int else q_int * 10 end else -1 end last' , + 'first case when q_int is not null then case when q_int <= %d then q_int else cast(q_int * (%d) as int) end else -(%d) end last' %(a1,a1,a3), #'first case when q_int is not null then case when q_int <= 0 then q_int else q_int * 10 end else -1 end last' , 'first case %d when %d then %d end last' %(a1,a2,a3), # 'first case 3 when 3 then 4 end last' , 'first case %d when %d then %d end last' %(a1,a2,a3), # 'first case 3 when 1 then 4 end last' , 'first case %d when %d then %d else %d end last' %(a1,a1,a2,a3), # 'first case 3 when 1 then 4 else 2 end last' , @@ -250,15 +250,15 @@ class TDTestCase: 'first case when \'a\' then \'b\' when null then %d end last' %(a1), # 'first case when \'a\' then \'b\' when null then 0 end last' , 'first case when \'%d\' then \'b\' when null then %d end last' %(a1,a2), # 'first case when \'2\' then \'b\' when null then 0 end last' , 'first case when %d then \'b\' else null end last' %(a1), # 'first case when 0 then \'b\' else null end last' , - 'first case when %d then \'b\' else %d+abs(%d) end last' %(a1,a2,a3), # 'first case when 0 then \'b\' else 2+abs(-2) end last' , + 'first case when %d then \'b\' else cast(%d+abs(%d) as int) end last' %(a1,a2,a3), # 'first case when 0 then \'b\' else 2+abs(-2) end last' , 'first case when %d then %d end last' %(a1,a2), # 'first case when 3 then 4 end last' , 'first case when %d then %d end last' %(a1,a2), # 'first case when 0 then 4 end last' , 'first case when null then %d end last' %(a1), # 'first case when null then 4 end last' , - #'first case when %d then %d+(%d) end last' %(a1,a2,a3), # 'first case when 1 then 4+1 end last' , + 'first case when %d then cast(%d+(%d) as int) end last' %(a1,a2,a3), # 'first case when 1 then 4+1 end last' , 'first case when %d-(%d) then %d end last' %(a1,a2,a3), # 'first case when 1-1 then 0 end last' , 'first case when %d+(%d) then %d end last' %(a1,a2,a3), # 'first case when 1+1 then 0 end last' , 'first case when abs(%d) then abs(%d) end last' %(a1,a2), # 'first case when abs(3) then abs(-1) end last' , - #'first case when abs(%d+(%d)) then abs(%d)+abs(%d) end last' %(a1,a2,a3,a1), # 'first case when abs(1+1) then abs(-1)+abs(3) end last' , + 'first case when abs(%d+(%d)) then cast(abs(%d)+abs(%d) as int) end last' %(a1,a2,a3,a1), # 'first case when abs(1+1) then abs(-1)+abs(3) end last' , 'first case when %d then %d else %d end last' %(a1,a2,a3), # 'first case when 0 then 1 else 3 end last' , 'first case when %d then %d when %d then %d else %d end last' %(a1,a2,a3,a1,a2), # 'first case when 0 then 1 when 1 then 0 else 3 end last' , 'first case when %d then %d when %d then %d when %d then %d end last' %(a1,a2,a3,a1,a2,a3), # 'first case when 0 then 1 when 1 then 0 when 2 then 3 end last' ,