diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index c8e80790ef8dba1ae8fa2d6c8ee08149cb4f8a3f..b28dd9cf1c06ee1f862ef2c585bc888259dab29e 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -3385,10 +3385,26 @@ static int32_t validateSQLExpr(SSqlCmd* pCmd, tSQLExpr* pExpr, SQueryInfo* pQuer tSQLExprItem item = {.pNode = pExpr, .aliasName = NULL}; - // sql function in selection clause, append sql function info in pSqlCmd structure sequentially + // sql function list in selection clause. + // Append the sqlExpr into exprList of pQueryInfo structure sequentially if (addExprAndResultField(pCmd, pQueryInfo, outputIndex, &item, false) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } + + // It is invalid in case of more than one sqlExpr, such as first(ts, k) - last(ts, k) + int32_t inc = (int32_t) tscSqlExprNumOfExprs(pQueryInfo) - outputIndex; + if (inc > 1) { + return TSDB_CODE_TSC_INVALID_SQL; + } + + // Not supported data type in arithmetic expression + for(int32_t i = 0; i < inc; ++i) { + SSqlExpr* p1 = tscSqlExprGet(pQueryInfo, i + outputIndex); + int16_t t = p1->resType; + if (t == TSDB_DATA_TYPE_BINARY || t == TSDB_DATA_TYPE_NCHAR || t == TSDB_DATA_TYPE_BOOL || t == TSDB_DATA_TYPE_TIMESTAMP) { + return TSDB_CODE_TSC_INVALID_SQL; + } + } } return TSDB_CODE_SUCCESS; diff --git a/src/query/src/qSyntaxtreefunction.c b/src/query/src/qSyntaxtreefunction.c index 2104edfd910bba1a1701800387545c4f58dfb625..7f7fca2c1e0ca6ce85f0df1c1b6e6dce82d8c000 100644 --- a/src/query/src/qSyntaxtreefunction.c +++ b/src/query/src/qSyntaxtreefunction.c @@ -1247,7 +1247,10 @@ _bi_consumer_fn_t tGetBiConsumerFn(int32_t leftType, int32_t rightType, int32_t case TSDB_BINARY_OP_REMAINDER: return rem_function_arraylist[leftType][rightType]; default: + assert(0); return NULL; } + + assert(0); return NULL; } diff --git a/tests/script/general/parser/col_arithmetic_operation.sim b/tests/script/general/parser/col_arithmetic_operation.sim index e1557227d4ca79ff6772311bcfafd49146ddb947..4977e906d72e845a668a3c17d6d093bface66826 100644 --- a/tests/script/general/parser/col_arithmetic_operation.sim +++ b/tests/script/general/parser/col_arithmetic_operation.sim @@ -109,4 +109,39 @@ if $data02 != 225000 then return -1 endi +sql select first(speed) - last(speed), first(speed) as b, last(speed) as b, min(speed) - max(speed), spread(speed) from stb interval(1y)", NULL); +if $rows != 1 then + return -1 +endi + +if $data00 != @20-01-01 00:00:00.000@ then + return -1 +endi + +if $data01 != -20.000000000 then + return -1 +endi + +if $data02 != 100.000000000 then + return -1 +endi + +if $data03 != 120.000000000 then + return -1 +endi + +if $data04 != -20.000000000 then + return -1 +endi + +if $data05 != 20.000000000 then + return -1 +endi + +sql_error select first(speed, mileage) - last(speed, mileage) from stb interval(1y) +sql_error select first(ts) - last(ts) from stb interval(1y) +sql_error select top(speed, 2) - last(speed) from stb; +sql_error select stddev(speed) - last(speed) from stb; +sql_error select diff(speed) - last(speed) from stb; + system sh/exec.sh -n dnode1 -s stop -x SIGINT \ No newline at end of file