From 066a92e81923d9a469aac104e1e9363614aaca62 Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Tue, 8 Oct 2024 15:16:26 +0800 Subject: [PATCH 01/20] opti:case when else data type compatibility --- source/libs/parser/src/parTranslater.c | 75 ++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 3ae4583013..5e2d361dde 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3186,10 +3186,71 @@ static int32_t rewriteIsTrue(SNode* pSrc, SNode** pIsTrue) { return TSDB_CODE_SUCCESS; } +static bool selectCommonType(SDataType* commonType, const SDataType* newType) { + if (commonType->type == TSDB_DATA_TYPE_NULL) {//0 + *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; + } + + // Numeric types between 1 and 5 (BOOL to BIGINT) + if ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_BIGINT) && + (newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_BIGINT)) { + if (newType->type > commonType->type) { + *commonType = *newType; + } + return true; + } + + // Unsigned numeric types between 11 and 14 (UTINYINT to UBIGINT) + if ((commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type <= TSDB_DATA_TYPE_UBIGINT) && + (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT)) { + if (newType->type > commonType->type) { + *commonType = *newType; + } + return true; + } + bool commonIsNumeric = ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_DOUBLE) || + (commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type <= TSDB_DATA_TYPE_UBIGINT)); + bool newIsNumeric = ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_DOUBLE) || + (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT)); + + bool commonIsString = (commonType->type == TSDB_DATA_TYPE_VARCHAR || commonType->type == TSDB_DATA_TYPE_NCHAR); + bool newIsString = (newType->type == TSDB_DATA_TYPE_VARCHAR || newType->type == TSDB_DATA_TYPE_NCHAR); + + if ((commonIsNumeric && newIsString) || (commonIsString && newIsNumeric)) { + if (commonIsString) { + return true; + } else { + *commonType = *newType; + return true; + } + } + if ((commonType->type == TSDB_DATA_TYPE_VARCHAR && newType->type == TSDB_DATA_TYPE_NCHAR)) { + *commonType = *newType; + return true; + } + if ((commonType->type == TSDB_DATA_TYPE_NCHAR && newType->type == TSDB_DATA_TYPE_VARCHAR)) { + commonType->bytes = commonType->bytes < newType->bytes ? newType->bytes : commonType->bytes; + return true; + } + return false; +} + + static EDealRes translateCaseWhen(STranslateContext* pCxt, SCaseWhenNode* pCaseWhen) { - bool first = true; bool allNullThen = true; SNode* pNode = NULL; + SDataType commonType = {.bytes = 0, .precision = 0, .scale = 0, .type = 0}; FOREACH(pNode, pCaseWhen->pWhenThenList) { SWhenThenNode* pWhenThen = (SWhenThenNode*)pNode; if (NULL == pCaseWhen->pCase && !isCondition(pWhenThen->pWhen)) { @@ -3206,10 +3267,16 @@ static EDealRes translateCaseWhen(STranslateContext* pCxt, SCaseWhenNode* pCaseW continue; } allNullThen = false; - if (first || dataTypeComp(&pCaseWhen->node.resType, &pThenExpr->resType) < 0) { - pCaseWhen->node.resType = pThenExpr->resType; + if (!selectCommonType(&pCaseWhen->node.resType, &pThenExpr->resType)) { + pCxt->errCode = DEAL_RES_ERROR; + return DEAL_RES_ERROR; } - first = false; + } + + SExprNode* pElseExpr = (SExprNode*)pCaseWhen->pElse; + if (!selectCommonType(&pCaseWhen->node.resType, &pElseExpr->resType)) { + pCxt->errCode = DEAL_RES_ERROR; + return DEAL_RES_ERROR; } if (allNullThen) { From bab99c7897b462593356451182efbf76ec2cdebe Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Wed, 9 Oct 2024 14:16:42 +0800 Subject: [PATCH 02/20] opti:case when else data type compatibility --- source/libs/parser/src/parTranslater.c | 70 +++++++++++++++++++------- tests/script/tsim/scalar/caseWhen.sim | 16 +++--- tests/system-test/2-query/case_when.py | 10 ++-- 3 files changed, 64 insertions(+), 32 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 5e2d361dde..c07b99ef45 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3187,22 +3187,31 @@ static int32_t rewriteIsTrue(SNode* pSrc, SNode** pIsTrue) { } static bool selectCommonType(SDataType* commonType, const SDataType* newType) { - if (commonType->type == TSDB_DATA_TYPE_NULL) {//0 + if (commonType->type == TSDB_DATA_TYPE_NULL) { // 0 null type *commonType = *newType; return true; } - if (newType->type == TSDB_DATA_TYPE_NULL) { return true; } + // type equal if (commonType->type == newType->type) { - if(commonType->bytes <= newType->bytes) + if (commonType->bytes <= newType->bytes) { + *commonType = *newType; + } + return true; + } + // 15 ~ 21 these types are not compatible with other types? + if (commonType->type == TSDB_DATA_TYPE_TIMESTAMP || (commonType->type >= TSDB_DATA_TYPE_JSON && commonType->type <= TSDB_DATA_TYPE_MAX)) { + return true; + } + if (newType->type == TSDB_DATA_TYPE_TIMESTAMP || (newType->type >= TSDB_DATA_TYPE_JSON && newType->type <= TSDB_DATA_TYPE_MAX)) { *commonType = *newType; return true; } - // Numeric types between 1 and 5 (BOOL to BIGINT) + // type 1 ~ 5 if ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_BIGINT) && (newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_BIGINT)) { if (newType->type > commonType->type) { @@ -3211,7 +3220,28 @@ static bool selectCommonType(SDataType* commonType, const SDataType* newType) { return true; } - // Unsigned numeric types between 11 and 14 (UTINYINT to UBIGINT) + // type 6 + if ((commonType->type == TSDB_DATA_TYPE_FLOAT && + ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_INT))) || + (newType->type == TSDB_DATA_TYPE_FLOAT && + ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_INT)))) { + *commonType = (commonType->type == TSDB_DATA_TYPE_FLOAT) ? *commonType : *newType; + return true; + } + //type 7 + if ((commonType->type == TSDB_DATA_TYPE_DOUBLE && + ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_BIGINT) || + newType->type == TSDB_DATA_TYPE_FLOAT || + newType->type == TSDB_DATA_TYPE_DOUBLE)) || + (newType->type == TSDB_DATA_TYPE_DOUBLE && + ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_BIGINT) || + commonType->type == TSDB_DATA_TYPE_FLOAT || + commonType->type == TSDB_DATA_TYPE_DOUBLE))) { + *commonType = (commonType->type == TSDB_DATA_TYPE_DOUBLE) ? *commonType : *newType; + return true; + } + + // type 11 ~ 14 if ((commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type <= TSDB_DATA_TYPE_UBIGINT) && (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT)) { if (newType->type > commonType->type) { @@ -3219,14 +3249,15 @@ static bool selectCommonType(SDataType* commonType, const SDataType* newType) { } return true; } + bool commonIsNumeric = ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_DOUBLE) || (commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type <= TSDB_DATA_TYPE_UBIGINT)); bool newIsNumeric = ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_DOUBLE) || (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT)); - bool commonIsString = (commonType->type == TSDB_DATA_TYPE_VARCHAR || commonType->type == TSDB_DATA_TYPE_NCHAR); - bool newIsString = (newType->type == TSDB_DATA_TYPE_VARCHAR || newType->type == TSDB_DATA_TYPE_NCHAR); - + bool commonIsString = (commonType->type == TSDB_DATA_TYPE_VARCHAR || commonType->type == TSDB_DATA_TYPE_NCHAR || commonType->type == TSDB_DATA_TYPE_BINARY); + bool newIsString = (newType->type == TSDB_DATA_TYPE_VARCHAR || newType->type == TSDB_DATA_TYPE_NCHAR || commonType->type == TSDB_DATA_TYPE_BINARY); + // num and string if ((commonIsNumeric && newIsString) || (commonIsString && newIsNumeric)) { if (commonIsString) { return true; @@ -3235,22 +3266,23 @@ static bool selectCommonType(SDataType* commonType, const SDataType* newType) { return true; } } - if ((commonType->type == TSDB_DATA_TYPE_VARCHAR && newType->type == TSDB_DATA_TYPE_NCHAR)) { - *commonType = *newType; - return true; - } - if ((commonType->type == TSDB_DATA_TYPE_NCHAR && newType->type == TSDB_DATA_TYPE_VARCHAR)) { - commonType->bytes = commonType->bytes < newType->bytes ? newType->bytes : commonType->bytes; + //char and nchar + if (commonIsString && newIsString) { + if (commonType->type == TSDB_DATA_TYPE_NCHAR || newType->type == TSDB_DATA_TYPE_NCHAR) { + *commonType = (commonType->type == TSDB_DATA_TYPE_NCHAR) ? *commonType : *newType; // nchar first + } else { + if (commonType->bytes < newType->bytes) { + *commonType = *newType; + } + } return true; } return false; } - static EDealRes translateCaseWhen(STranslateContext* pCxt, SCaseWhenNode* pCaseWhen) { bool allNullThen = true; SNode* pNode = NULL; - SDataType commonType = {.bytes = 0, .precision = 0, .scale = 0, .type = 0}; FOREACH(pNode, pCaseWhen->pWhenThenList) { SWhenThenNode* pWhenThen = (SWhenThenNode*)pNode; if (NULL == pCaseWhen->pCase && !isCondition(pWhenThen->pWhen)) { @@ -3268,14 +3300,14 @@ static EDealRes translateCaseWhen(STranslateContext* pCxt, SCaseWhenNode* pCaseW } allNullThen = false; if (!selectCommonType(&pCaseWhen->node.resType, &pThenExpr->resType)) { - pCxt->errCode = DEAL_RES_ERROR; + pCxt->errCode = DEAL_RES_ERROR; return DEAL_RES_ERROR; } } SExprNode* pElseExpr = (SExprNode*)pCaseWhen->pElse; - if (!selectCommonType(&pCaseWhen->node.resType, &pElseExpr->resType)) { - pCxt->errCode = DEAL_RES_ERROR; + if (pElseExpr && !selectCommonType(&pCaseWhen->node.resType, &pElseExpr->resType)) { + pCxt->errCode = DEAL_RES_ERROR; return DEAL_RES_ERROR; } diff --git a/tests/script/tsim/scalar/caseWhen.sim b/tests/script/tsim/scalar/caseWhen.sim index c10413f23c..a658a9ad33 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 != 1664176501000 then +if $data00 != @2022-09-26 15:15:01.000@ then return -1 endi -if $data10 != 3 then +if $data10 != @1970-01-01 08:00:00.003@ then return -1 endi -if $data20 != 3 then +if $data20 != @1970-01-01 08:00:00.003@ then return -1 endi -if $data30 != 1664176504000 then +if $data30 != @2022-09-26 15:15:04.0@ 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 != a then +if $data00 != @1970-01-01 08:00:00.000@ then return -1 endi -if $data10 != 0 then +if $data10 != @1970-01-01 08:00:00.000@ then return -1 endi -if $data20 != 11 then +if $data20 != @1970-01-01 08:00:00.011@ then return -1 endi -if $data30 != 1664176504 then +if $data30 != @2022-09-26 15:15:04.000@ then return -1 endi diff --git a/tests/system-test/2-query/case_when.py b/tests/system-test/2-query/case_when.py index 1ccd2b5076..755917faff 100755 --- a/tests/system-test/2-query/case_when.py +++ b/tests/system-test/2-query/case_when.py @@ -280,11 +280,11 @@ class TDTestCase: for i in range(30): cs = self.state_window_list().split(',')[i] - sql1 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1 where tbname = 'stable_1_1' state_window(%s);" % (database,cs) - sql2 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1_1 state_window(%s) ;" % (database,cs) - self.constant_check(database,sql1,sql2,0) - self.constant_check(database,sql1,sql2,1) - self.constant_check(database,sql1,sql2,2) + # sql1 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1 where tbname = 'stable_1_1' state_window(%s);" % (database,cs) + # sql2 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1_1 state_window(%s) ;" % (database,cs) + # self.constant_check(database,sql1,sql2,0) + # self.constant_check(database,sql1,sql2,1) + # self.constant_check(database,sql1,sql2,2) From 521da82e14355ca3079b40122a713a865615aa82 Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Wed, 9 Oct 2024 17:23:58 +0800 Subject: [PATCH 03/20] opti:case when else data type compatibility --- source/libs/parser/src/parTranslater.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index c07b99ef45..fe4b462d80 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3220,22 +3220,26 @@ static bool selectCommonType(SDataType* commonType, const SDataType* newType) { return true; } - // type 6 + // type 6, float >= float, bool, tinyint, smallint, utinyint, usmallint if ((commonType->type == TSDB_DATA_TYPE_FLOAT && - ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_INT))) || + ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type < TSDB_DATA_TYPE_INT) || + newType->type == TSDB_DATA_TYPE_UTINYINT || newType->type == TSDB_DATA_TYPE_USMALLINT))|| (newType->type == TSDB_DATA_TYPE_FLOAT && - ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_INT)))) { + ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type < TSDB_DATA_TYPE_INT) || + commonType->type == TSDB_DATA_TYPE_UTINYINT || commonType->type == TSDB_DATA_TYPE_USMALLINT))) { *commonType = (commonType->type == TSDB_DATA_TYPE_FLOAT) ? *commonType : *newType; return true; } - //type 7 + //type 7, double >= double bool, tinyint, smallint, int, utinyint, usmallint, uint, if ((commonType->type == TSDB_DATA_TYPE_DOUBLE && - ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_BIGINT) || - newType->type == TSDB_DATA_TYPE_FLOAT || + ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type < TSDB_DATA_TYPE_BIGINT) || + newType->type == TSDB_DATA_TYPE_FLOAT || + (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type < TSDB_DATA_TYPE_UBIGINT) || newType->type == TSDB_DATA_TYPE_DOUBLE)) || (newType->type == TSDB_DATA_TYPE_DOUBLE && - ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_BIGINT) || + ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type < TSDB_DATA_TYPE_BIGINT) || commonType->type == TSDB_DATA_TYPE_FLOAT || + (commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type < TSDB_DATA_TYPE_UBIGINT) || commonType->type == TSDB_DATA_TYPE_DOUBLE))) { *commonType = (commonType->type == TSDB_DATA_TYPE_DOUBLE) ? *commonType : *newType; return true; From 945b4e44797c2406a95fdb97b662fc95fe438e1b Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Wed, 9 Oct 2024 17:33:34 +0800 Subject: [PATCH 04/20] opti:mmodify case --- tests/script/tsim/scalar/caseWhen.sim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/script/tsim/scalar/caseWhen.sim b/tests/script/tsim/scalar/caseWhen.sim index a658a9ad33..8ab8912081 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -397,16 +397,16 @@ 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.000000000 then return -1 endi -if $data10 != 1 then +if $data10 != 1.000000000 then return -1 endi -if $data20 != 5 then +if $data20 != 5.000000000 then return -1 endi -if $data30 != 1 then +if $data30 != 1.000000000 then return -1 endi From 1388ce32f668494001152756fb488f735bb81dbb Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Wed, 9 Oct 2024 19:14:45 +0800 Subject: [PATCH 05/20] opti:update case --- source/libs/parser/src/parTranslater.c | 16 ++++++++-------- tests/script/tsim/scalar/caseWhen.sim | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index fe4b462d80..effad91546 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3222,24 +3222,24 @@ static bool selectCommonType(SDataType* commonType, const SDataType* newType) { // type 6, float >= float, bool, tinyint, smallint, utinyint, usmallint if ((commonType->type == TSDB_DATA_TYPE_FLOAT && - ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type < TSDB_DATA_TYPE_INT) || - newType->type == TSDB_DATA_TYPE_UTINYINT || newType->type == TSDB_DATA_TYPE_USMALLINT))|| + ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_INT) || + (newType->type >= TSDB_DATA_TYPE_UTINYINT || newType->type <= TSDB_DATA_TYPE_UINT)))|| (newType->type == TSDB_DATA_TYPE_FLOAT && - ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type < TSDB_DATA_TYPE_INT) || - commonType->type == TSDB_DATA_TYPE_UTINYINT || commonType->type == TSDB_DATA_TYPE_USMALLINT))) { + ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_INT) || + (newType->type >= TSDB_DATA_TYPE_UTINYINT || newType->type <= TSDB_DATA_TYPE_UINT)))) { *commonType = (commonType->type == TSDB_DATA_TYPE_FLOAT) ? *commonType : *newType; return true; } //type 7, double >= double bool, tinyint, smallint, int, utinyint, usmallint, uint, if ((commonType->type == TSDB_DATA_TYPE_DOUBLE && - ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type < TSDB_DATA_TYPE_BIGINT) || + ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_BIGINT) || newType->type == TSDB_DATA_TYPE_FLOAT || - (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type < TSDB_DATA_TYPE_UBIGINT) || + (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT) || newType->type == TSDB_DATA_TYPE_DOUBLE)) || (newType->type == TSDB_DATA_TYPE_DOUBLE && - ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type < TSDB_DATA_TYPE_BIGINT) || + ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_BIGINT) || commonType->type == TSDB_DATA_TYPE_FLOAT || - (commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type < TSDB_DATA_TYPE_UBIGINT) || + (commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type <= TSDB_DATA_TYPE_UBIGINT) || commonType->type == TSDB_DATA_TYPE_DOUBLE))) { *commonType = (commonType->type == TSDB_DATA_TYPE_DOUBLE) ? *commonType : *newType; return true; diff --git a/tests/script/tsim/scalar/caseWhen.sim b/tests/script/tsim/scalar/caseWhen.sim index 8ab8912081..8a3707e021 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -423,7 +423,7 @@ endi if $data20 != @1970-01-01 08:00:00.003@ then return -1 endi -if $data30 != @2022-09-26 15:15:04.0@ then +if $data30 != @2022-09-26 15:15:04.000@ then return -1 endi From 3d72a5022532d0b0ba07a66a03570800d821ec91 Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Thu, 10 Oct 2024 11:25:32 +0800 Subject: [PATCH 06/20] modify case --- tests/script/tsim/scalar/caseWhen.sim | 32 +++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/script/tsim/scalar/caseWhen.sim b/tests/script/tsim/scalar/caseWhen.sim index 8a3707e021..77bb167286 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 != @2022-09-26 15:15:01.000@ then +if $data00 != @22-09-26 15:15:01.000@ then return -1 endi -if $data10 != @1970-01-01 08:00:00.003@ then +if $data10 != @70-01-01 08:00:00.003@ then return -1 endi -if $data20 != @1970-01-01 08:00:00.003@ then +if $data20 != @70-01-01 08:00:00.003@ then return -1 endi -if $data30 != @2022-09-26 15:15:04.000@ then +if $data30 != @22-09-26 15:15:04.000@ then return -1 endi @@ -657,16 +657,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 then +if $data00 != 0.000000000 then return -1 endi -if $data10 != 10 then +if $data10 != 10.000000000 then return -1 endi -if $data20 != 50 then +if $data20 != 50.000000000 then return -1 endi -if $data30 != -1 then +if $data30 != -1.000000000 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 != @1970-01-01 08:00:00.000@ then +if $data00 != @70-01-01 08:00:00.000@ then return -1 endi -if $data10 != @1970-01-01 08:00:00.000@ then +if $data10 != @70-01-01 08:00:00.000@ then return -1 endi -if $data20 != @1970-01-01 08:00:00.011@ then +if $data20 != @70-01-01 08:00:00.011@ then return -1 endi -if $data30 != @2022-09-26 15:15:04.000@ then +if $data30 != @22-09-26 15:15:04.000@ then return -1 endi @@ -1043,19 +1043,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 then +if $data01 != 0.000000000 then return -1 endi -if $data11 != 1 then +if $data11 != 1.000000000 then return -1 endi if $data21 != NULL then return -1 endi -if $data31 != 101 then +if $data31 != 101.000000000 then return -1 endi -if $data41 != 103 then +if $data41 != 103.000000000 then return -1 endi From 9a125698b893f9d03d3bf17f82e8090e20d76fbe Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Thu, 10 Oct 2024 13:46:11 +0800 Subject: [PATCH 07/20] opti:add bool type --- source/libs/parser/src/parTranslater.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index effad91546..cac2680b9d 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3246,8 +3246,8 @@ static bool selectCommonType(SDataType* commonType, const SDataType* newType) { } // type 11 ~ 14 - if ((commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type <= TSDB_DATA_TYPE_UBIGINT) && - (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT)) { + if ((commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type <= TSDB_DATA_TYPE_UBIGINT || commonType->type == TSDB_DATA_TYPE_BOOL) && + (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT || commonType->type == TSDB_DATA_TYPE_BOOL)) { if (newType->type > commonType->type) { *commonType = *newType; } From 87caa19c70de38d15d8b5514c341ec7567fd4f98 Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Thu, 10 Oct 2024 13:48:54 +0800 Subject: [PATCH 08/20] opti:add bool type --- source/libs/parser/src/parTranslater.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index cac2680b9d..d746130d22 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3247,7 +3247,7 @@ static bool selectCommonType(SDataType* commonType, const SDataType* newType) { // type 11 ~ 14 if ((commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type <= TSDB_DATA_TYPE_UBIGINT || commonType->type == TSDB_DATA_TYPE_BOOL) && - (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT || commonType->type == TSDB_DATA_TYPE_BOOL)) { + (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT || newType->type == TSDB_DATA_TYPE_BOOL)) { if (newType->type > commonType->type) { *commonType = *newType; } From f08e5054a33edcdf8923d264db91585bfc54d59e Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Thu, 10 Oct 2024 18:06:24 +0800 Subject: [PATCH 09/20] opti:modify type --- source/libs/parser/src/parTranslater.c | 114 ++++++------------------- tests/script/tsim/scalar/caseWhen.sim | 73 +++++++++++++++- tests/system-test/2-query/case_when.py | 19 +++-- 3 files changed, 106 insertions(+), 100 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index d746130d22..339590e2a3 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3186,102 +3186,42 @@ static int32_t rewriteIsTrue(SNode* pSrc, SNode** pIsTrue) { return TSDB_CODE_SUCCESS; } +extern int8_t gConvertTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX]; static bool selectCommonType(SDataType* commonType, const SDataType* newType) { - if (commonType->type == TSDB_DATA_TYPE_NULL) { // 0 null type - *commonType = *newType; - return true; + 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; + return true; } - - // type equal if (commonType->type == newType->type) { - if (commonType->bytes <= newType->bytes) { - *commonType = *newType; - } - return true; - } - // 15 ~ 21 these types are not compatible with other types? - if (commonType->type == TSDB_DATA_TYPE_TIMESTAMP || (commonType->type >= TSDB_DATA_TYPE_JSON && commonType->type <= TSDB_DATA_TYPE_MAX)) { - return true; - } - if (newType->type == TSDB_DATA_TYPE_TIMESTAMP || (newType->type >= TSDB_DATA_TYPE_JSON && newType->type <= TSDB_DATA_TYPE_MAX)) { - *commonType = *newType; - return true; - } - - // type 1 ~ 5 - if ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_BIGINT) && - (newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_BIGINT)) { - if (newType->type > commonType->type) { - *commonType = *newType; - } - return true; - } - - // type 6, float >= float, bool, tinyint, smallint, utinyint, usmallint - if ((commonType->type == TSDB_DATA_TYPE_FLOAT && - ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_INT) || - (newType->type >= TSDB_DATA_TYPE_UTINYINT || newType->type <= TSDB_DATA_TYPE_UINT)))|| - (newType->type == TSDB_DATA_TYPE_FLOAT && - ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_INT) || - (newType->type >= TSDB_DATA_TYPE_UTINYINT || newType->type <= TSDB_DATA_TYPE_UINT)))) { - *commonType = (commonType->type == TSDB_DATA_TYPE_FLOAT) ? *commonType : *newType; - return true; - } - //type 7, double >= double bool, tinyint, smallint, int, utinyint, usmallint, uint, - if ((commonType->type == TSDB_DATA_TYPE_DOUBLE && - ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_BIGINT) || - newType->type == TSDB_DATA_TYPE_FLOAT || - (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT) || - newType->type == TSDB_DATA_TYPE_DOUBLE)) || - (newType->type == TSDB_DATA_TYPE_DOUBLE && - ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_BIGINT) || - commonType->type == TSDB_DATA_TYPE_FLOAT || - (commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type <= TSDB_DATA_TYPE_UBIGINT) || - commonType->type == TSDB_DATA_TYPE_DOUBLE))) { - *commonType = (commonType->type == TSDB_DATA_TYPE_DOUBLE) ? *commonType : *newType; - return true; - } - - // type 11 ~ 14 - if ((commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type <= TSDB_DATA_TYPE_UBIGINT || commonType->type == TSDB_DATA_TYPE_BOOL) && - (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT || newType->type == TSDB_DATA_TYPE_BOOL)) { - if (newType->type > commonType->type) { - *commonType = *newType; - } - return true; - } - - bool commonIsNumeric = ((commonType->type >= TSDB_DATA_TYPE_BOOL && commonType->type <= TSDB_DATA_TYPE_DOUBLE) || - (commonType->type >= TSDB_DATA_TYPE_UTINYINT && commonType->type <= TSDB_DATA_TYPE_UBIGINT)); - bool newIsNumeric = ((newType->type >= TSDB_DATA_TYPE_BOOL && newType->type <= TSDB_DATA_TYPE_DOUBLE) || - (newType->type >= TSDB_DATA_TYPE_UTINYINT && newType->type <= TSDB_DATA_TYPE_UBIGINT)); - - bool commonIsString = (commonType->type == TSDB_DATA_TYPE_VARCHAR || commonType->type == TSDB_DATA_TYPE_NCHAR || commonType->type == TSDB_DATA_TYPE_BINARY); - bool newIsString = (newType->type == TSDB_DATA_TYPE_VARCHAR || newType->type == TSDB_DATA_TYPE_NCHAR || commonType->type == TSDB_DATA_TYPE_BINARY); - // num and string - if ((commonIsNumeric && newIsString) || (commonIsString && newIsNumeric)) { - if (commonIsString) { - return true; - } else { - *commonType = *newType; - return true; - } - } - //char and nchar - if (commonIsString && newIsString) { - if (commonType->type == TSDB_DATA_TYPE_NCHAR || newType->type == TSDB_DATA_TYPE_NCHAR) { - *commonType = (commonType->type == TSDB_DATA_TYPE_NCHAR) ? *commonType : *newType; // nchar first - } else { if (commonType->bytes < newType->bytes) { *commonType = *newType; } - } - return true; + return true; + } + int8_t type1 = commonType->type; + int8_t type2 = newType->type; + int8_t resultType; + if (type1 < type2) { + resultType = gConvertTypes[type1][type2]; + } else { + resultType = gConvertTypes[type2][type1]; + } + if (resultType == -1) { + return false; + } else if (resultType == 0) { + return false; + } else { + commonType->type = resultType; + commonType->bytes = (commonType->bytes >= newType->bytes) ? commonType->bytes : newType->bytes; + return true; } - return false; } static EDealRes translateCaseWhen(STranslateContext* pCxt, SCaseWhenNode* pCaseWhen) { diff --git a/tests/script/tsim/scalar/caseWhen.sim b/tests/script/tsim/scalar/caseWhen.sim index 77bb167286..777f976270 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -296,16 +296,16 @@ sql select case when '2' then 'b' when null then 0 end from tba1; if $rows != 4 then return -1 endi -if $data00 != b then +if $data00 != 0 then return -1 endi -if $data10 != b then +if $data10 != 0 then return -1 endi -if $data20 != b then +if $data20 != 0 then return -1 endi -if $data30 != b then +if $data30 != 0 then return -1 endi @@ -1061,4 +1061,69 @@ 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)) tags(tag_id INT); +sql create table t_test using test_stable tags(1); +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'); + +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.45000 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.900000000 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 +print $data00 +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 != @70-01-01 08:00:00.001@ 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 != 0.000000000 then + return -1 +endi 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 755917faff..cc170bb43b 100755 --- a/tests/system-test/2-query/case_when.py +++ b/tests/system-test/2-query/case_when.py @@ -122,8 +122,8 @@ class TDTestCase: self.constant_check(database,sql1,sql2,5) #TD-20260 - sql1 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1 where tbname = 'stable_1_1' and ts < now state_window(case when q_smallint <0 then 1 else 0 end);" %database - sql2 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1_1 where ts < now state_window(case when q_smallint <0 then 1 else 0 end);" %database + # sql1 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1 where tbname = 'stable_1_1' and ts < now state_window(case when q_smallint <0 then 1 else 0 end);" %database + # sql2 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1_1 where ts < now state_window(case when q_smallint <0 then 1 else 0 end);" %database self.constant_check(database,sql1,sql2,0) self.constant_check(database,sql1,sql2,1) self.constant_check(database,sql1,sql2,2) @@ -159,7 +159,7 @@ class TDTestCase: '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 cast(case q_int when q_int then q_int + (%d) else q_int is null end as double) last' %(a1), #'first cast(case q_int when q_int then q_int + 1 else q_int is null end as double) last' , 'first sum(case q_int when q_int then q_int + (%d) else q_int is null end + (%d)) last' %(a1,a2), #'first sum(case q_int when q_int then q_int + 1 else q_int is null end + 1) 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 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 %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' , @@ -232,7 +232,7 @@ class TDTestCase: '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 %d when ts then ts 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' , @@ -280,11 +280,12 @@ class TDTestCase: for i in range(30): cs = self.state_window_list().split(',')[i] - # sql1 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1 where tbname = 'stable_1_1' state_window(%s);" % (database,cs) - # sql2 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1_1 state_window(%s) ;" % (database,cs) - # self.constant_check(database,sql1,sql2,0) - # self.constant_check(database,sql1,sql2,1) - # self.constant_check(database,sql1,sql2,2) + print(cs) + sql1 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1 where tbname = 'stable_1_1' state_window(%s);" % (database,cs) + sql2 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1_1 state_window(%s) ;" % (database,cs) + self.constant_check(database,sql1,sql2,0) + self.constant_check(database,sql1,sql2,1) + self.constant_check(database,sql1,sql2,2) From d51731b32a89228a6e9733c11e8cb949ca25f9ac Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Fri, 11 Oct 2024 08:53:16 +0800 Subject: [PATCH 10/20] opti:modify case_when.py case --- tests/system-test/2-query/case_when.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system-test/2-query/case_when.py b/tests/system-test/2-query/case_when.py index cc170bb43b..5140f2e50f 100755 --- a/tests/system-test/2-query/case_when.py +++ b/tests/system-test/2-query/case_when.py @@ -236,7 +236,7 @@ class TDTestCase: '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 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 %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' , From fae8f5bfa58102da9c6994121012edd9c8c7263a Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Fri, 11 Oct 2024 13:41:17 +0800 Subject: [PATCH 11/20] opti:add case --- tests/script/tsim/scalar/caseWhen.sim | 21 +++++++++++++++++---- tests/system-test/2-query/case_when.py | 18 +++++++++--------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/tests/script/tsim/scalar/caseWhen.sim b/tests/script/tsim/scalar/caseWhen.sim index 777f976270..e7d2e4c456 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -1064,9 +1064,9 @@ 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)) tags(tag_id INT); +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 INT); sql create table t_test using test_stable tags(1); -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'); +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 @@ -1088,7 +1088,6 @@ sql select case when c_bool then c_bool else c_utinyint end as result from t_tes if $rows != 1 then return -1 endi -print $data00 if $data00 != 1 then return -1 endi @@ -1126,4 +1125,18 @@ sql select case when c_double > 100 then c_nchar else c_double end as result fro if $data00 != 0.000000000 then return -1 endi -system sh/exec.sh -n dnode1 -s stop -x SIGINT + +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_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; + + diff --git a/tests/system-test/2-query/case_when.py b/tests/system-test/2-query/case_when.py index 5140f2e50f..a05fa539bd 100755 --- a/tests/system-test/2-query/case_when.py +++ b/tests/system-test/2-query/case_when.py @@ -122,8 +122,8 @@ class TDTestCase: self.constant_check(database,sql1,sql2,5) #TD-20260 - # sql1 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1 where tbname = 'stable_1_1' and ts < now state_window(case when q_smallint <0 then 1 else 0 end);" %database - # sql2 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1_1 where ts < now state_window(case when q_smallint <0 then 1 else 0 end);" %database + sql1 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1 where tbname = 'stable_1_1' and ts < now state_window(case when q_smallint <0 then 1 else 0 end);" %database + sql2 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1_1 where ts < now state_window(case when q_smallint <0 then 1 else 0 end);" %database self.constant_check(database,sql1,sql2,0) self.constant_check(database,sql1,sql2,1) self.constant_check(database,sql1,sql2,2) @@ -159,7 +159,7 @@ class TDTestCase: '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 cast(case q_int when q_int then q_int + (%d) else q_int is null end as double) last' %(a1), #'first cast(case q_int when q_int then q_int + 1 else q_int is null end as double) last' , 'first sum(case q_int when q_int then q_int + (%d) else q_int is null end + (%d)) last' %(a1,a2), #'first sum(case q_int when q_int then q_int + 1 else q_int is null end + 1) 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 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 %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' , @@ -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' , From 9bbfff8bf8437e062d58431fd9b12019e4c43920 Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Fri, 11 Oct 2024 16:18:10 +0800 Subject: [PATCH 12/20] opti:modify type display between numbers and string --- source/libs/parser/src/parTranslater.c | 6 +++--- source/libs/scalar/src/sclvector.c | 24 ++++++++++++++++++++++++ tests/script/tsim/scalar/caseWhen.sim | 15 ++++++++++----- tests/system-test/2-query/case_when.py | 1 - 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 339590e2a3..a5e702976f 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3186,7 +3186,7 @@ static int32_t rewriteIsTrue(SNode* pSrc, SNode** pIsTrue) { return TSDB_CODE_SUCCESS; } -extern int8_t gConvertTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX]; +extern int8_t gDisplyTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX]; static bool 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) { @@ -3209,9 +3209,9 @@ static bool selectCommonType(SDataType* commonType, const SDataType* newType) { int8_t type2 = newType->type; int8_t resultType; if (type1 < type2) { - resultType = gConvertTypes[type1][type2]; + resultType = gDisplyTypes[type1][type2]; } else { - resultType = gConvertTypes[type2][type1]; + resultType = gDisplyTypes[type2][type1]; } if (resultType == -1) { return false; diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index 230454483d..667ffde032 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -1030,6 +1030,30 @@ 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, /*G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 0, 0, 0, 0, 0, 0, 12, 13, 14, 0, -1, 0, 0, 0, -1, + /*USMA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 14, 0, -1, 0, 0, 0, -1, + /*UINT*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, -1, 0, 0, 0, -1, + /*UBIG*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, + /*JSON*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, + /*VARB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1,-1, -1, + /*DECI*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, + /*BLOB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, + /*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}; + 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 e7d2e4c456..7b4c875675 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -296,16 +296,16 @@ sql select case when '2' then 'b' when null then 0 end from tba1; if $rows != 4 then return -1 endi -if $data00 != 0 then +if $data00 != b then return -1 endi -if $data10 != 0 then +if $data10 != b then return -1 endi -if $data20 != 0 then +if $data20 != b then return -1 endi -if $data30 != 0 then +if $data30 != b then return -1 endi @@ -1122,7 +1122,12 @@ if $data00 != @70-01-01 08:00:00.001@ then endi sql select case when c_double > 100 then c_nchar else c_double end as result from t_test; -if $data00 != 0.000000000 then +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 diff --git a/tests/system-test/2-query/case_when.py b/tests/system-test/2-query/case_when.py index a05fa539bd..85fe43b487 100755 --- a/tests/system-test/2-query/case_when.py +++ b/tests/system-test/2-query/case_when.py @@ -280,7 +280,6 @@ class TDTestCase: for i in range(30): cs = self.state_window_list().split(',')[i] - print(cs) sql1 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1 where tbname = 'stable_1_1' state_window(%s);" % (database,cs) sql2 = "select _wstart,avg(q_int),min(q_smallint) from %s.stable_1_1 state_window(%s) ;" % (database,cs) self.constant_check(database,sql1,sql2,0) From e7215c5a2462db5871df1e5348222e911b2d402f Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Fri, 11 Oct 2024 16:51:09 +0800 Subject: [PATCH 13/20] opti:modify common type between numbers and string --- source/libs/parser/src/parTranslater.c | 4 ++-- tests/script/tsim/scalar/caseWhen.sim | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index a5e702976f..bb98891b70 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3244,14 +3244,14 @@ static EDealRes translateCaseWhen(STranslateContext* pCxt, SCaseWhenNode* pCaseW } allNullThen = false; if (!selectCommonType(&pCaseWhen->node.resType, &pThenExpr->resType)) { - pCxt->errCode = DEAL_RES_ERROR; + pCxt->errCode = TSDB_CODE_SCALAR_CONVERT_ERROR; return DEAL_RES_ERROR; } } SExprNode* pElseExpr = (SExprNode*)pCaseWhen->pElse; if (pElseExpr && !selectCommonType(&pCaseWhen->node.resType, &pElseExpr->resType)) { - pCxt->errCode = DEAL_RES_ERROR; + pCxt->errCode = TSDB_CODE_SCALAR_CONVERT_ERROR; return DEAL_RES_ERROR; } diff --git a/tests/script/tsim/scalar/caseWhen.sim b/tests/script/tsim/scalar/caseWhen.sim index 7b4c875675..211e43a660 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -1144,4 +1144,4 @@ sql_error select case when c_double > 100 then c_varbinary else c_geometry end a 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; - +system sh/exec.sh -n dnode1 -s stop -x SIGINT From babd3f63604e2fd99fbdcf122b277d73d4c9d3bd Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Tue, 15 Oct 2024 17:32:04 +0800 Subject: [PATCH 14/20] opti:modify case when result type --- source/libs/parser/src/parTranslater.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index bb98891b70..e7a8d8e79c 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3213,15 +3213,20 @@ static bool selectCommonType(SDataType* commonType, const SDataType* newType) { } else { resultType = gDisplyTypes[type2][type1]; } - if (resultType == -1) { + if (resultType == -1 || resultType == 0) { return false; - } else if (resultType == 0) { - return false; - } else { - commonType->type = resultType; - commonType->bytes = (commonType->bytes >= newType->bytes) ? commonType->bytes : newType->bytes; - return true; + } + if (resultType == commonType->type){ + return true; } + if(resultType == newType->type) { + *commonType = *newType; + return true; + } + commonType->type = resultType; + commonType->bytes = TYPE_BYTES[resultType]; + return true; + } static EDealRes translateCaseWhen(STranslateContext* pCxt, SCaseWhenNode* pCaseWhen) { From 04c562a3a4055b04b115aa56b41039c56f66d091 Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Tue, 15 Oct 2024 17:42:33 +0800 Subject: [PATCH 15/20] opti:modify case when result type --- source/libs/scalar/src/sclvector.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index 667ffde032..ee65e4812b 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -1040,9 +1040,9 @@ int8_t gDisplyTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX] = { /*BIGI*/ 0, 0, 0, 0, 0, 0, 6, 7, 8, 9, 10, 5, 5, 5, 7, 0, -1, 0, 0, 0, -1, /*FLOA*/ 0, 0, 0, 0, 0, 0, 0, 7, 8, 6, 10, 6, 6, 6, 6, 0, -1, 0, 0, 0, -1, /*DOUB*/ 0, 0, 0, 0, 0, 0, 0, 0, 8, 7, 10, 7, 7, 7, 7, 0, -1, 0, 0, 0, -1, - /*VARC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 10, 7, 7, 7, 7, 0, 16, 0, 0, 0, 20, + /*VARC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 10, 8, 8, 8, 8, 0, 16, 0, 0, 0, 20, /*TIME*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 7, 0, -1, 0, 0, 0, -1, - /*NCHA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 16, 0, 0, 0, -1, + /*NCHA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 0, 16, 0, 0, 0, -1, /*UTIN*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 13, 14, 0, -1, 0, 0, 0, -1, /*USMA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 14, 0, -1, 0, 0, 0, -1, /*UINT*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, -1, 0, 0, 0, -1, From 23d119c65456cef8bc0a6600a32b27a688f87fb0 Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Wed, 16 Oct 2024 20:21:56 +0800 Subject: [PATCH 16/20] 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, 12, 13, 14, 0, -1,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 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 From 67f28df97b0f319c1491be94e29c5054cd4b627a Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Fri, 18 Oct 2024 10:31:35 +0800 Subject: [PATCH 17/20] opti:modify type in case when --- source/libs/scalar/src/sclvector.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index 5b785dcb39..31c23612e3 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -1033,7 +1033,7 @@ 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*/ -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, 8, 8, -1, 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, @@ -1042,7 +1042,7 @@ int8_t gDisplyTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX] = { /*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, 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, + /*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, 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, From a8ac0a2a738343ba1363321d220f242af16900de Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Fri, 18 Oct 2024 11:43:26 +0800 Subject: [PATCH 18/20] opti:modify case when type --- source/libs/scalar/src/sclvector.c | 9 ++++----- tests/script/tsim/scalar/caseWhen.sim | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index 31c23612e3..d9ccd64268 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -1041,19 +1041,18 @@ int8_t gDisplyTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX] = { /*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,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 20 -}; + /*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 f7f554cd56..1bc7352d25 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -1064,8 +1064,8 @@ 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 INT); -sql create table t_test using test_stable tags(1); +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; @@ -1141,6 +1141,21 @@ 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_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; From 324a4eab4ea5dea6f785d22b13819ee1aa4a76c1 Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Tue, 22 Oct 2024 10:21:42 +0800 Subject: [PATCH 19/20] 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,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, 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) { 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 From 60c555875ec70d9207fd9bceb96f88284dd7432d Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Tue, 22 Oct 2024 15:23:39 +0800 Subject: [PATCH 20/20] opti:add case --- tests/script/tsim/scalar/caseWhen.sim | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/script/tsim/scalar/caseWhen.sim b/tests/script/tsim/scalar/caseWhen.sim index 7c9e35321f..67c8ac3673 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -410,7 +410,22 @@ 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