From f71dfa40e581ca9f5e13c1f3ccd027473a62bd06 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 18 Dec 2024 11:01:23 +0800 Subject: [PATCH] fix:[TS-5761] error for in operator --- source/libs/scalar/src/filter.c | 22 +++++++++++-------- source/libs/scalar/src/scalar.c | 20 ++++++++++++----- source/libs/scalar/src/sclvector.c | 14 +++++++++--- source/util/src/thashutil.c | 8 +++---- tests/script/tsim/scalar/in.sim | 4 ++-- .../system-test/2-query/ts-5761-scalemode.py | 21 +++++++++++++++++- tests/system-test/2-query/ts-5761.py | 21 +++++++++++++++++- 7 files changed, 85 insertions(+), 25 deletions(-) diff --git a/source/libs/scalar/src/filter.c b/source/libs/scalar/src/filter.c index e482a1d241..2f3ca504e4 100644 --- a/source/libs/scalar/src/filter.c +++ b/source/libs/scalar/src/filter.c @@ -4848,7 +4848,6 @@ EDealRes fltReviseRewriter(SNode **pNode, void *pContext) { } SColumnNode *refNode = (SColumnNode *)node->pLeft; - int32_t type = refNode->node.resType.type; SExprNode *exprNode = NULL; if (OP_TYPE_IN != node->opType && OP_TYPE_NOT_IN != node->opType) { SValueNode *valueNode = (SValueNode *)node->pRight; @@ -4857,29 +4856,34 @@ EDealRes fltReviseRewriter(SNode **pNode, void *pContext) { valueNode->node.resType.type = TSDB_DATA_TYPE_BIGINT; } exprNode = &valueNode->node; - type = vectorGetConvertType(refNode->node.resType.type, exprNode->resType.type); + int32_t type = vectorGetConvertType(refNode->node.resType.type, exprNode->resType.type); + if (0 != type && type != refNode->node.resType.type) { + stat->scalarMode = true; + } + return DEAL_RES_CONTINUE; } else { SNodeListNode *listNode = (SNodeListNode *)node->pRight; - if (LIST_LENGTH(listNode->pNodeList) > 10) { + if (LIST_LENGTH(listNode->pNodeList) > 10 || OP_TYPE_NOT_IN == node->opType) { stat->scalarMode = true; - return DEAL_RES_CONTINUE; } + int32_t type = -1; exprNode = &listNode->node; SListCell *cell = listNode->pNodeList->pHead; for (int32_t i = 0; i < listNode->pNodeList->length; ++i) { SValueNode *valueNode = (SValueNode *)cell->pNode; cell = cell->pNext; - int32_t tmp = vectorGetConvertType(type, valueNode->node.resType.type); + int32_t tmp = vectorGetConvertType(refNode->node.resType.type, valueNode->node.resType.type); if (tmp != 0){ - type = tmp; + stat->scalarMode = true; + if (IS_NUMERIC_TYPE(tmp) && tmp > type){ + type = tmp; + } } + } if (IS_NUMERIC_TYPE(type)){ exprNode->resType.type = type; } - } - if ((0 != type && type != refNode->node.resType.type) || OP_TYPE_NOT_IN == node->opType) { - stat->scalarMode = true; return DEAL_RES_CONTINUE; } } diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index ed50ccd37b..56f0d5a60f 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -201,9 +201,6 @@ int32_t scalarGenerateSetFromList(void **data, void *pNode, uint32_t type, int8_ cell = cell->pNext; } - if (taosHashGetSize(pObj) == 0) { - goto _return; - } *data = pObj; colDataDestroy(out.columnData); @@ -386,8 +383,21 @@ int32_t sclInitParam(SNode *node, SScalarParam *param, SScalarCtx *ctx, int32_t SCL_RET(TSDB_CODE_QRY_INVALID_INPUT); } - int32_t type = ctx->type.peerType; - if (IS_VAR_DATA_TYPE(ctx->type.selfType) && IS_NUMERIC_TYPE(ctx->type.peerType)){ + int32_t type = -1; + SListCell *cell = nodeList->pNodeList->pHead; + for (int32_t i = 0; i < nodeList->pNodeList->length; ++i) { + SValueNode *valueNode = (SValueNode *)cell->pNode; + cell = cell->pNext; + int32_t tmp = vectorGetConvertType(ctx->type.selfType, valueNode->node.resType.type); + if (tmp != 0 && IS_NUMERIC_TYPE(tmp) && tmp > type){ + type = tmp; + } + } + if (IS_NUMERIC_TYPE(type)){ + ctx->type.peerType = type; + } + type = ctx->type.peerType; + if (IS_VAR_DATA_TYPE(ctx->type.selfType) && IS_NUMERIC_TYPE(type)){ SCL_ERR_RET(scalarGenerateSetFromList((void **)¶m->pHashFilter, node, type, 1)); SCL_ERR_RET(scalarGenerateSetFromList((void **)¶m->pHashFilterVar, node, ctx->type.selfType, 2)); } else { diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index bc0f12f7b2..4c53658c32 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -2013,9 +2013,17 @@ int32_t doVectorCompare(SScalarParam *pLeft, SScalarParam *pLeftVar, SScalarPara char *pLeftData = colDataGetData(pLeft->columnData, i); bool res = filterDoCompare(fp, optr, pLeftData, pRight->pHashFilter); - if (pLeftVar != NULL && !res){ - pLeftData = colDataGetData(pLeftVar->columnData, i); - res = res || filterDoCompare(fpVar, optr, pLeftData, pRight->pHashFilterVar); + if (pLeftVar != NULL && taosHashGetSize(pRight->pHashFilterVar) > 0){ + do{ + if (optr == OP_TYPE_IN && res){ + break; + } + if (optr == OP_TYPE_NOT_IN && !res){ + break; + } + pLeftData = colDataGetData(pLeftVar->columnData, i); + res = filterDoCompare(fpVar, optr, pLeftData, pRight->pHashFilterVar); + }while(0); } colDataSetInt8(pOut->columnData, i, (int8_t *)&res); if (res) { diff --git a/source/util/src/thashutil.c b/source/util/src/thashutil.c index b466e1b351..67441f8bb0 100644 --- a/source/util/src/thashutil.c +++ b/source/util/src/thashutil.c @@ -152,8 +152,8 @@ uint32_t taosFloatHash(const char *key, uint32_t UNUSED_PARAM(len)) { if (FLT_EQUAL(f, 0.0)) { return 0; } - if (fabs(f) < FLT_MAX / BASE - DLT) { - int32_t t = (int32_t)(round(BASE * (f + DLT))); + if (fabs(f) < INT32_MAX) { + int32_t t = (int32_t)(floor(f)); return (uint32_t)t; } else { return 0x7fc00000; @@ -168,8 +168,8 @@ uint32_t taosDoubleHash(const char *key, uint32_t UNUSED_PARAM(len)) { if (FLT_EQUAL(f, 0.0)) { return 0; } - if (fabs(f) < DBL_MAX / BASE - DLT) { - int32_t t = (int32_t)(round(BASE * (f + DLT))); + if (fabs(f) < INT32_MAX) { + int32_t t = (int32_t)(floor(f)); return (uint32_t)t; } else { return 0x7fc00000; diff --git a/tests/script/tsim/scalar/in.sim b/tests/script/tsim/scalar/in.sim index a2164675f0..0ffe6f5100 100644 --- a/tests/script/tsim/scalar/in.sim +++ b/tests/script/tsim/scalar/in.sim @@ -44,7 +44,7 @@ if $data20 != @ Time Range: [-9223372036854775808, 9223372036854775807]@ th endi sql select * from tb1 where fbool in (0, 3); -if $rows != 5 then +if $rows != 3 then return -1 endi @@ -69,7 +69,7 @@ if $rows != 10 then endi sql select * from st1 where tbool in (0, 3); -if $rows != 15 then +if $rows != 5 then return -1 endi diff --git a/tests/system-test/2-query/ts-5761-scalemode.py b/tests/system-test/2-query/ts-5761-scalemode.py index c6fea20072..3fbc023eef 100644 --- a/tests/system-test/2-query/ts-5761-scalemode.py +++ b/tests/system-test/2-query/ts-5761-scalemode.py @@ -21,12 +21,17 @@ class TDTestCase: tdSql.execute(f"use db") # super tableUNSIGNED - tdSql.execute("CREATE TABLE t1( time TIMESTAMP, c1 BIGINT, c2 smallint, c3 double, c4 int UNSIGNED, c5 bool, c6 binary(32), c7 nchar(32));") + tdSql.execute("CREATE TABLE st( time TIMESTAMP, c1 BIGINT, c2 smallint, c3 double, c4 int UNSIGNED, c5 bool, c6 binary(32), c7 nchar(32)) tags(t1 binary(32), t2 nchar(32))") + tdSql.execute("create table t1 using st tags('1', '1.7')") + tdSql.execute("create table t2 using st tags('0', '')") + tdSql.execute("create table t3 using st tags('1', 'er')") # create index for all tags tdSql.execute("INSERT INTO t1 VALUES (1641024000000, 1, 1, 1, 1, 1, '1', '1.7')") tdSql.execute("INSERT INTO t1 VALUES (1641024000001, 0, 0, 1.7, 0, 0, '0', '')") tdSql.execute("INSERT INTO t1 VALUES (1641024000002, 1, 1, 1, 1, 1, '1', 'er')") + tdSql.execute("INSERT INTO t2 VALUES (1641024000002, 1, 1, 1, 1, 1, '1', 'er')") + tdSql.execute("INSERT INTO t3 VALUES (1641024000002, 1, 1, 1, 1, 1, '1', 'er')") def check(self): tdSql.query(f"SELECT * FROM t1 WHERE c1 = 1.7") @@ -84,6 +89,8 @@ class TDTestCase: tdSql.checkRows(1) tdSql.query(f"SELECT * FROM t1 WHERE c6 not in (0, 2)") tdSql.checkRows(2) + tdSql.query(f"SELECT * FROM t1 WHERE c6 not in (0, 2, 'sef')") + tdSql.checkRows(2) tdSql.query(f"SELECT * FROM t1 WHERE c7 = 1.7") tdSql.checkRows(1) @@ -106,6 +113,18 @@ class TDTestCase: tdSql.query(f"SELECT * FROM t1 WHERE c7 not in ('', 2)") tdSql.checkRows(2) + tdSql.query(f"SELECT * FROM st WHERE t2 in ('', 2)") + tdSql.checkRows(1) + tdSql.query(f"SELECT * FROM st WHERE t2 not in ('', 2)") + tdSql.checkRows(4) + + tdSql.query(f"SELECT * FROM st WHERE t1 in ('d343', 0, 2)") + tdSql.checkRows(1) + tdSql.query(f"SELECT * FROM st WHERE t1 in (0, 2)") + tdSql.checkRows(1) + tdSql.query(f"SELECT * FROM st WHERE t1 not in (0, 2)") + tdSql.checkRows(4) + def run(self): self.prepareData() self.check() diff --git a/tests/system-test/2-query/ts-5761.py b/tests/system-test/2-query/ts-5761.py index d734a269dd..de80835a00 100644 --- a/tests/system-test/2-query/ts-5761.py +++ b/tests/system-test/2-query/ts-5761.py @@ -20,12 +20,17 @@ class TDTestCase: tdSql.execute(f"use db") # super tableUNSIGNED - tdSql.execute("CREATE TABLE t1( time TIMESTAMP, c1 BIGINT, c2 smallint, c3 double, c4 int UNSIGNED, c5 bool, c6 binary(32), c7 nchar(32));") + tdSql.execute("CREATE TABLE st( time TIMESTAMP, c1 BIGINT, c2 smallint, c3 double, c4 int UNSIGNED, c5 bool, c6 binary(32), c7 nchar(32)) tags(t1 binary(32), t2 nchar(32))") + tdSql.execute("create table t1 using st tags('1', '1.7')") + tdSql.execute("create table t2 using st tags('0', '')") + tdSql.execute("create table t3 using st tags('1', 'er')") # create index for all tags tdSql.execute("INSERT INTO t1 VALUES (1641024000000, 1, 1, 1, 1, 1, '1', '1.7')") tdSql.execute("INSERT INTO t1 VALUES (1641024000001, 0, 0, 1.7, 0, 0, '0', '')") tdSql.execute("INSERT INTO t1 VALUES (1641024000002, 1, 1, 1, 1, 1, '1', 'er')") + tdSql.execute("INSERT INTO t2 VALUES (1641024000002, 1, 1, 1, 1, 1, '1', 'er')") + tdSql.execute("INSERT INTO t3 VALUES (1641024000002, 1, 1, 1, 1, 1, '1', 'er')") def check(self): tdSql.query(f"SELECT * FROM t1 WHERE c1 = 1.7") @@ -83,6 +88,8 @@ class TDTestCase: tdSql.checkRows(1) tdSql.query(f"SELECT * FROM t1 WHERE c6 not in (0, 2)") tdSql.checkRows(2) + tdSql.query(f"SELECT * FROM t1 WHERE c6 not in (0, 2, 'sef')") + tdSql.checkRows(2) tdSql.query(f"SELECT * FROM t1 WHERE c7 = 1.7") tdSql.checkRows(1) @@ -105,6 +112,18 @@ class TDTestCase: tdSql.query(f"SELECT * FROM t1 WHERE c7 not in ('', 2)") tdSql.checkRows(2) + tdSql.query(f"SELECT * FROM st WHERE t2 in ('', 2)") + tdSql.checkRows(1) + tdSql.query(f"SELECT * FROM st WHERE t2 not in ('', 2)") + tdSql.checkRows(4) + + tdSql.query(f"SELECT * FROM st WHERE t1 in ('d343', 0, 2)") + tdSql.checkRows(1) + tdSql.query(f"SELECT * FROM st WHERE t1 in (0, 2)") + tdSql.checkRows(1) + tdSql.query(f"SELECT * FROM st WHERE t1 not in (0, 2)") + tdSql.checkRows(4) + def run(self): self.prepareData() self.check()