From 091d1d05932d2566a88f0c4d3cb4aa7f21eefbf3 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Fri, 14 Jan 2022 14:34:40 +0800 Subject: [PATCH] [TD-12945](query): taos shell crash when constant comparison cause crash --- src/query/src/qSqlParser.c | 90 +++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/src/query/src/qSqlParser.c b/src/query/src/qSqlParser.c index 3074894071..9f5f036e6c 100644 --- a/src/query/src/qSqlParser.c +++ b/src/query/src/qSqlParser.c @@ -18,6 +18,7 @@ #include "taosdef.h" #include "taosmsg.h" #include "tcmdtype.h" +#include "tcompare.h" #include "tstrbuild.h" #include "ttoken.h" #include "ttokendef.h" @@ -318,7 +319,8 @@ tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType) { } if ((pLeft != NULL && pRight != NULL) && - (optrType == TK_PLUS || optrType == TK_MINUS || optrType == TK_STAR || optrType == TK_DIVIDE || optrType == TK_REM)) { + (optrType == TK_PLUS || optrType == TK_MINUS || optrType == TK_STAR || optrType == TK_DIVIDE || optrType == TK_REM || + optrType == TK_EQ || optrType == TK_NE || optrType == TK_LT || optrType == TK_GT || optrType == TK_LE || optrType == TK_GE)) { /* * if a exprToken is noted as the TK_TIMESTAMP, the time precision is microsecond * Otherwise, the time precision is adaptive, determined by the time precision from databases. @@ -360,6 +362,30 @@ tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType) { pExpr->value.i64 = pLeft->value.i64 % pRight->value.i64; break; } + case TK_EQ: { + pExpr->value.i64 = (pLeft->value.i64 == pRight->value.i64) ? 1 : 0; + break; + } + case TK_NE: { + pExpr->value.i64 = (pLeft->value.i64 != pRight->value.i64) ? 1 : 0; + break; + } + case TK_LT: { + pExpr->value.i64 = (pLeft->value.i64 < pRight->value.i64) ? 1 : 0; + break; + } + case TK_GT: { + pExpr->value.i64 = (pLeft->value.i64 > pRight->value.i64) ? 1 : 0; + break; + } + case TK_LE: { + pExpr->value.i64 = (pLeft->value.i64 <= pRight->value.i64) ? 1 : 0; + break; + } + case TK_GE: { + pExpr->value.i64 = (pLeft->value.i64 >= pRight->value.i64) ? 1 : 0; + break; + } } tSqlExprDestroy(pLeft); @@ -395,6 +421,68 @@ tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType) { pExpr->value.dKey = left - ((int64_t)(left / right)) * right; break; } + case TK_EQ: { + pExpr->tokenId = TK_INTEGER; + pExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + if (compareDoubleVal(&left, &right) == 0) { + pExpr->value.i64 = 1; + } else { + pExpr->value.i64 = 0; + } + break; + } + case TK_NE: { + pExpr->tokenId = TK_INTEGER; + pExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + if (compareDoubleVal(&left, &right) != 0) { + pExpr->value.i64 = 1; + } else { + pExpr->value.i64 = 0; + } + break; + } + case TK_LT: { + pExpr->tokenId = TK_INTEGER; + pExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + if (compareDoubleVal(&left, &right) == -1) { + pExpr->value.i64 = 1; + } else { + pExpr->value.i64 = 0; + } + break; + } + case TK_GT: { + pExpr->tokenId = TK_INTEGER; + pExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + if (compareDoubleVal(&left, &right) == 1) { + pExpr->value.i64 = 1; + } else { + pExpr->value.i64 = 0; + } + break; + } + case TK_LE: { + int32_t res = compareDoubleVal(&left, &right); + pExpr->tokenId = TK_INTEGER; + pExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + if (res == 0 || res == -1) { + pExpr->value.i64 = 1; + } else { + pExpr->value.i64 = 0; + } + break; + } + case TK_GE: { + int32_t res = compareDoubleVal(&left, &right); + pExpr->tokenId = TK_INTEGER; + pExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + if (res == 0 || res == 1) { + pExpr->value.i64 = 1; + } else { + pExpr->value.i64 = 0; + } + break; + } } tSqlExprDestroy(pLeft); -- GitLab