From 327c2150ee64c443aa0960d2d820e3e8ae61bad5 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Tue, 8 Mar 2022 14:22:21 +0800 Subject: [PATCH] [TD-11216]: Time window related keywords --- src/client/src/tscSQLParser.c | 2 +- src/client/src/tscUtil.c | 5 ++--- src/inc/taosdef.h | 2 ++ src/query/src/qAggMain.c | 14 +++++++++++--- src/query/src/qExecutor.c | 13 ++++++------- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index df46bd8786..3f6a656826 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -2478,7 +2478,7 @@ int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, t } //for tbname and other pseudo columns - if (index.columnIndex <= TSDB_TBNAME_COLUMN_INDEX && index.columnIndex >= TSDB_MIN_VALID_COLUMN_INDEX) { + if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX || TSDB_COL_IS_TSWIN_COL(index.columnIndex)) { if (outerQuery) { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); int32_t numOfCols = tscGetNumOfColumns(pTableMetaInfo->pTableMeta); diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 256861c71b..ce3487cf5b 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -2551,8 +2551,7 @@ SExprInfo* tscExprCreate(STableMetaInfo* pTableMetaInfo, int16_t functionId, SCo p->colInfo.colId = TSDB_TBNAME_COLUMN_INDEX; p->colBytes = s->bytes; p->colType = s->type; - } else if (pColIndex->columnIndex < TSDB_TBNAME_COLUMN_INDEX && - pColIndex->columnIndex >= TSDB_MIN_VALID_COLUMN_INDEX) { + } else if (TSDB_COL_IS_TSWIN_COL(pColIndex->columnIndex)) { SSchema* s = tGetTimeWindowColumnSchema(pColIndex->columnIndex); p->colInfo.colId = s->colId; p->colBytes = s->bytes; @@ -3079,7 +3078,7 @@ bool tscValidateColumnId(STableMetaInfo* pTableMetaInfo, int32_t colId) { return false; } - if ((colId <= TSDB_TBNAME_COLUMN_INDEX && colId >= TSDB_MIN_VALID_COLUMN_INDEX) || + if (colId == TSDB_TBNAME_COLUMN_INDEX || TSDB_COL_IS_TSWIN_COL(colId) || colId <= TSDB_UD_COLUMN_INDEX) { return true; } diff --git a/src/inc/taosdef.h b/src/inc/taosdef.h index 4e127927e4..ed0e23b7d7 100644 --- a/src/inc/taosdef.h +++ b/src/inc/taosdef.h @@ -282,6 +282,8 @@ do { \ #define TSDB_TSWIN_DURATION_COLUMN_INDEX (-4) #define TSDB_MIN_VALID_COLUMN_INDEX (-4) +#define TSDB_COL_IS_TSWIN_COL(_i) ((_i) <= TSDB_TSWIN_START_COLUMN_INDEX && (_i) >= TSDB_TSWIN_DURATION_COLUMN_INDEX) + #define TSDB_UD_COLUMN_INDEX (-1000) #define TSDB_RES_COL_ID (-5000) diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index 1ebee07036..a3dbb32868 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -5662,6 +5662,14 @@ static void wduration_function(SQLFunctionCtx *pCtx) { *(int64_t *)(pCtx->pOutput) = duration; } +static void tswin_function_finalizer(SQLFunctionCtx *pCtx) { + SET_VAL(pCtx, pCtx->size, 1); + if (pCtx->stableQuery) { + *(int64_t *)(pCtx->pOutput) = *(int64_t *)pCtx->pInput; + } + doFinalizer(pCtx); +} + ///////////////////////////////////////////////////////////////////////////////////////////// /* * function compatible list. @@ -6201,7 +6209,7 @@ SAggFunctionInfo aAggs[TSDB_FUNC_MAX_NUM] = {{ TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, function_setup, wstart_function, - noop1, + tswin_function_finalizer, noop1, dataBlockRequired, }, @@ -6213,7 +6221,7 @@ SAggFunctionInfo aAggs[TSDB_FUNC_MAX_NUM] = {{ TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, function_setup, wstop_function, - noop1, + tswin_function_finalizer, noop1, dataBlockRequired, }, @@ -6225,7 +6233,7 @@ SAggFunctionInfo aAggs[TSDB_FUNC_MAX_NUM] = {{ TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, function_setup, wduration_function, - noop1, + tswin_function_finalizer, noop1, dataBlockRequired, } diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index c0a6b9e802..25bf7e62a1 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -462,7 +462,7 @@ static bool isProjQuery(SQueryAttr *pQueryAttr) { static bool hasNull(SColIndex* pColIndex, SDataStatis *pStatis) { if (TSDB_COL_IS_TAG(pColIndex->flag) || TSDB_COL_IS_UD_COL(pColIndex->flag) || - pColIndex->colId == PRIMARYKEY_TIMESTAMP_COL_INDEX || pColIndex->colId < TSDB_TBNAME_COLUMN_INDEX) { + TSDB_COL_IS_TSWIN_COL(pColIndex->colId) || pColIndex->colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) { return false; } @@ -1192,7 +1192,7 @@ static void doSetInputDataBlock(SOperatorInfo* pOperator, SQLFunctionCtx* pCtx, setArithParams((SScalarExprSupport*)pCtx[i].param[1].pz, &pOperator->pExpr[i], pBlock); } else { SColIndex* pCol = &pOperator->pExpr[i].base.colInfo; - if ((TSDB_COL_IS_NORMAL_COL(pCol->flag) && pCol->colId >= 0) || (pCtx[i].functionId == TSDB_FUNC_BLKINFO) || + if ((TSDB_COL_IS_NORMAL_COL(pCol->flag) && !TSDB_COL_IS_TSWIN_COL(pCol->colId)) || (pCtx[i].functionId == TSDB_FUNC_BLKINFO) || (TSDB_COL_IS_TAG(pCol->flag) && pOperator->pRuntimeEnv->scanFlag == MERGE_STAGE)) { SColIndex* pColIndex = &pOperator->pExpr[i].base.colInfo; SColumnInfoData* p = taosArrayGet(pBlock->pDataBlock, pColIndex->colIndex); @@ -1844,7 +1844,7 @@ static bool functionNeedToExecute(SQueryRuntimeEnv *pRuntimeEnv, SQLFunctionCtx void setBlockStatisInfo(SQLFunctionCtx *pCtx, SSDataBlock* pSDataBlock, SColIndex* pColIndex) { SDataStatis *pStatis = NULL; - if (pSDataBlock->pBlockStatis != NULL && TSDB_COL_IS_NORMAL_COL(pColIndex->flag) && pColIndex->colId >= 0) { + if (pSDataBlock->pBlockStatis != NULL && TSDB_COL_IS_NORMAL_COL(pColIndex->flag) && !TSDB_COL_IS_TSWIN_COL(pColIndex->colId)) { pStatis = &pSDataBlock->pBlockStatis[pColIndex->colIndex]; pCtx->preAggVals.statis = *pStatis; @@ -8391,8 +8391,8 @@ static int32_t getColumnIndexInSource(SQueriedTableInfo *pTableInfo, SSqlExpr *p int32_t j = 0; if (TSDB_COL_IS_TAG(pExpr->colInfo.flag)) { - if (pExpr->colInfo.colId <= TSDB_TBNAME_COLUMN_INDEX && - pExpr->colInfo.colId >= TSDB_MIN_VALID_COLUMN_INDEX) { + if (pExpr->colInfo.colId == TSDB_TBNAME_COLUMN_INDEX || + TSDB_COL_IS_TSWIN_COL(pExpr->colInfo.colId)) { return pExpr->colInfo.colId; } @@ -9176,8 +9176,7 @@ int32_t createQueryFunc(SQueriedTableInfo* pTableInfo, int32_t numOfOutput, SExp SSchema* s = tGetTbnameColumnSchema(); type = s->type; bytes = s->bytes; - } else if (pExprs[i].base.colInfo.colId < TSDB_TBNAME_COLUMN_INDEX && - pExprs[i].base.colInfo.colId >= TSDB_MIN_VALID_COLUMN_INDEX && + } else if (TSDB_COL_IS_TSWIN_COL(pExprs[i].base.colInfo.colId) && (pExprs[i].base.functionId >= TSDB_FUNC_WSTART || pExprs[i].base.functionId <= TSDB_FUNC_WDURATION)) { SSchema* s = tGetTimeWindowColumnSchema(pExprs[i].base.colInfo.colId); type = s->type; -- GitLab