From 1cde4253790474ea6fb91dfc8160e3623f402008 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Thu, 10 Jun 2021 18:19:18 +0800 Subject: [PATCH] [td-4673]: fix bug in derivative. --- src/client/src/tscSQLParser.c | 12 ++-- src/client/src/tscUtil.c | 7 ++- src/query/inc/qTableMeta.h | 2 +- src/query/src/qExecutor.c | 2 +- tests/script/general/parser/function.sim | 70 +++++++++++++++++++++++- 5 files changed, 81 insertions(+), 12 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 3328ef63a4..1503638a03 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -6510,13 +6510,13 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, char* size_t size = tscNumOfExprs(pQueryInfo); for (int32_t i = 0; i < size; ++i) { SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - int32_t functId = pExpr->base.functionId; + int32_t f = pExpr->base.functionId; /* * group by normal columns. * Check if the column projection is identical to the group by column or not */ - if (functId == TSDB_FUNC_PRJ && pExpr->base.colInfo.colId != PRIMARYKEY_TIMESTAMP_COL_INDEX) { + if (f == TSDB_FUNC_PRJ && pExpr->base.colInfo.colId != PRIMARYKEY_TIMESTAMP_COL_INDEX) { bool qualified = false; for (int32_t j = 0; j < pQueryInfo->groupbyExpr.numOfGroupCols; ++j) { SColIndex* pColIndex = taosArrayGet(pQueryInfo->groupbyExpr.columnInfo, j); @@ -6531,12 +6531,12 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, char* } } - if (IS_MULTIOUTPUT(aAggs[functId].status) && functId != TSDB_FUNC_TOP && functId != TSDB_FUNC_BOTTOM && - functId != TSDB_FUNC_DIFF && functId != TSDB_FUNC_TAGPRJ && functId != TSDB_FUNC_PRJ) { + if (IS_MULTIOUTPUT(aAggs[f].status) && f != TSDB_FUNC_TOP && f != TSDB_FUNC_BOTTOM && + f != TSDB_FUNC_DIFF && f != TSDB_FUNC_DERIVATIVE && f != TSDB_FUNC_TAGPRJ && f != TSDB_FUNC_PRJ) { return invalidOperationMsg(msg, msg1); } - if (functId == TSDB_FUNC_COUNT && pExpr->base.colInfo.colIndex == TSDB_TBNAME_COLUMN_INDEX) { + if (f == TSDB_FUNC_COUNT && pExpr->base.colInfo.colIndex == TSDB_TBNAME_COLUMN_INDEX) { return invalidOperationMsg(msg, msg1); } } @@ -7989,7 +7989,7 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf pQueryInfo->arithmeticOnAgg = tsIsArithmeticQueryOnAggResult(pQueryInfo); pQueryInfo->orderProjectQuery = tscOrderedProjectionQueryOnSTable(pQueryInfo, 0); - pQueryInfo->diffQuery = tscIsDiffQuery(pQueryInfo); +// pQueryInfo->diffQuery = tscIsDiffQuery(pQueryInfo); SExprInfo** p = NULL; int32_t numOfExpr = 0; diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index c17cd21c42..e3dba3fcbc 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -223,6 +223,7 @@ bool tscIsProjectionQueryOnSTable(SQueryInfo* pQueryInfo, int32_t tableIndex) { functionId != TSDB_FUNC_ARITHM && functionId != TSDB_FUNC_TS_COMP && functionId != TSDB_FUNC_DIFF && + functionId != TSDB_FUNC_DERIVATIVE && functionId != TSDB_FUNC_TS_DUMMY && functionId != TSDB_FUNC_TID_TAG) { return false; @@ -463,11 +464,13 @@ bool tscIsDiffQuery(SQueryInfo* pQueryInfo) { size_t num = tscNumOfExprs(pQueryInfo); for(int32_t i = 0; i < num; ++i) { SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - if (pExpr == NULL || pExpr->base.functionId == TSDB_FUNC_TS_DUMMY) { + + int32_t f = pExpr->base.functionId; + if (pExpr == NULL || f == TSDB_FUNC_TS_DUMMY) { continue; } - if (pExpr->base.functionId == TSDB_FUNC_DIFF) { + if (f == TSDB_FUNC_DIFF || f == TSDB_FUNC_DERIVATIVE) { return true; } } diff --git a/src/query/inc/qTableMeta.h b/src/query/inc/qTableMeta.h index 1fd78ed324..ec389de9e6 100644 --- a/src/query/inc/qTableMeta.h +++ b/src/query/inc/qTableMeta.h @@ -138,7 +138,7 @@ typedef struct SQueryInfo { bool hasFilter; bool onlyTagQuery; bool orderProjectQuery; - bool diffQuery; +// bool diffQuery; bool stateWindow; } SQueryInfo; diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 7c79a4c81e..2d6b1c0bf6 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -3156,7 +3156,7 @@ void updateOutputBuf(SOptrBasicInfo* pBInfo, int32_t *bufCapacity, int32_t numOf // re-estabilish output buffer pointer. int32_t functionId = pBInfo->pCtx[i].functionId; - if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF) { + if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF || functionId == TSDB_FUNC_DERIVATIVE) { pBInfo->pCtx[i].ptsOutputBuf = pBInfo->pCtx[0].pOutput; } } diff --git a/tests/script/general/parser/function.sim b/tests/script/general/parser/function.sim index bec4267b8b..6a3e5596c1 100644 --- a/tests/script/general/parser/function.sim +++ b/tests/script/general/parser/function.sim @@ -935,9 +935,10 @@ endi print ===========================> derivative sql drop table t1 sql drop table tx; -sql drop table m1; +sql drop table if exists m1; sql drop table if exists tm0; sql drop table if exists tm1; + sql create table tm0(ts timestamp, k double) sql insert into tm0 values('2015-08-18T00:00:00Z', 2.064) ('2015-08-18T00:06:00Z', 2.116) ('2015-08-18T00:12:00Z', 2.028) sql insert into tm0 values('2015-08-18T00:18:00Z', 2.126) ('2015-08-18T00:24:00Z', 2.041) ('2015-08-18T00:30:00Z', 2.051) @@ -1053,4 +1054,69 @@ endi sql_error select derivative(k, 6m, 1) from tm0 interval(1s); sql_error select derivative(k, 6m, 1) from tm0 session(ts, 1s); sql_error select derivative(k, 6m, 1) from tm0 group by k; -sql_error select derivative(k, 6m, 1) from \ No newline at end of file + +sql drop table if exists tm0 +sql drop table if exists m1 + +sql create table m1 (ts timestamp, k double ) tags(a int); +sql create table if not exists t0 using m1 tags(1); +sql create table if not exists t1 using m1 tags(2); + +sql insert into t0 values('2020-1-1 1:1:1', 1); +sql insert into t0 values('2020-1-1 1:1:3', 3); +sql insert into t0 values('2020-1-1 1:2:4', 4); +sql insert into t0 values('2020-1-1 1:2:5', 5); +sql insert into t0 values('2020-1-1 1:2:6', 6); +sql insert into t0 values('2020-1-1 1:3:7', 7); +sql insert into t0 values('2020-1-1 1:3:8', 8); +sql insert into t0 values('2020-1-1 1:3:9', 9); +sql insert into t0 values('2020-1-1 1:4:10', 10); + +sql insert into t1 values('2020-1-1 1:1:2', 2); +sql insert into t1 values('2020-1-1 1:1:4', 20); +sql insert into t1 values('2020-1-1 1:1:6', 200); +sql insert into t1 values('2020-1-1 1:1:8', 2000); +sql insert into t1 values('2020-1-1 1:1:10', 20000); + +sql_error select derivative(k, 1s, 0) from m1; +sql_error select derivative(k, 1s, 0) from m1 group by a; +sql select derivative(k, 1s, 0) from m1 group by tbname +if $rows != 12 then + return -1 +endi + +if $data00 != @20-01-01 01:01:03.000@ then + return -1 +endi + +if $data01 != 1.000000000 then + return -1 +endi + +if $data02 != @t0@ then + return -1 +endi + +if $data10 != @20-01-01 01:02:04.000@ then + return -1 +endi + +if $data11 != 0.016393443 then + return -1 +endi + +if $data12 != t0 then + return -1 +endi + +if $data90 != @20-01-01 01:01:06.000@ then + return -1 +endi + +if $data91 != 90.000000000 then + return -1 +endi + +if $data92 != t1 then + return -1 +endi \ No newline at end of file -- GitLab