From 398a41d09bfdd62af9ba9e98fa7dec4e2927d463 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Wed, 6 Apr 2022 15:20:38 +0800 Subject: [PATCH] feature/qnode --- include/common/ttypes.h | 1 + include/common/tvariant.h | 1 + include/libs/nodes/querynodes.h | 2 ++ source/common/src/tvariant.c | 26 +++++++++++++- source/libs/executor/src/executorimpl.c | 22 +++++++++++- source/libs/nodes/src/nodesUtilFuncs.c | 41 +++++++++++++++++++++ source/libs/parser/src/parTranslater.c | 48 ++++--------------------- source/libs/scalar/inc/sclvector.h | 2 ++ source/libs/scalar/src/scalar.c | 3 +- source/libs/scalar/src/sclvector.c | 30 +++++++++++++++- 10 files changed, 130 insertions(+), 46 deletions(-) diff --git a/include/common/ttypes.h b/include/common/ttypes.h index 1cb398fb85..37d688a0ef 100644 --- a/include/common/ttypes.h +++ b/include/common/ttypes.h @@ -146,6 +146,7 @@ typedef struct { #define IS_FLOAT_TYPE(_t) ((_t) == TSDB_DATA_TYPE_FLOAT || (_t) == TSDB_DATA_TYPE_DOUBLE) #define IS_NUMERIC_TYPE(_t) ((IS_SIGNED_NUMERIC_TYPE(_t)) || (IS_UNSIGNED_NUMERIC_TYPE(_t)) || (IS_FLOAT_TYPE(_t))) +#define IS_MATHABLE_TYPE(_t) (IS_NUMERIC_TYPE(_t) || (_t) == (TSDB_DATA_TYPE_BOOL) || (_t) == (TSDB_DATA_TYPE_TIMESTAMP)) #define IS_VALID_TINYINT(_t) ((_t) > INT8_MIN && (_t) <= INT8_MAX) #define IS_VALID_SMALLINT(_t) ((_t) > INT16_MIN && (_t) <= INT16_MAX) diff --git a/include/common/tvariant.h b/include/common/tvariant.h index 63f305ab2d..83dccd0092 100644 --- a/include/common/tvariant.h +++ b/include/common/tvariant.h @@ -59,6 +59,7 @@ int32_t taosVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool #endif int32_t taosVariantTypeSetType(SVariant *pVariant, char type); +char * taosVariantGet(SVariant *pVar, int32_t type); #ifdef __cplusplus } diff --git a/include/libs/nodes/querynodes.h b/include/libs/nodes/querynodes.h index d035f7de8f..87aadfd244 100644 --- a/include/libs/nodes/querynodes.h +++ b/include/libs/nodes/querynodes.h @@ -22,6 +22,7 @@ extern "C" { #include "nodes.h" #include "tmsg.h" +#include "tvariant.h" #define TABLE_TOTAL_COL_NUM(pMeta) ((pMeta)->tableInfo.numOfColumns + (pMeta)->tableInfo.numOfTags) #define TABLE_META_SIZE(pMeta) (NULL == (pMeta) ? 0 : (sizeof(STableMeta) + TABLE_TOTAL_COL_NUM((pMeta)) * sizeof(SSchema))) @@ -317,6 +318,7 @@ bool nodesIsTimelineQuery(const SNode* pQuery); void* nodesGetValueFromNode(SValueNode *pNode); char* nodesGetStrValueFromNode(SValueNode *pNode); char *getFillModeString(EFillMode mode); +void valueNodeToVariant(const SValueNode* pNode, SVariant* pVal); #ifdef __cplusplus } diff --git a/source/common/src/tvariant.c b/source/common/src/tvariant.c index 3995db89b6..77feb04f6c 100644 --- a/source/common/src/tvariant.c +++ b/source/common/src/tvariant.c @@ -1014,4 +1014,28 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) { } return 0; -} \ No newline at end of file +} + +char * taosVariantGet(SVariant *pVar, int32_t type) { + switch (type) { + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_SMALLINT: + case TSDB_DATA_TYPE_BIGINT: + case TSDB_DATA_TYPE_INT: + case TSDB_DATA_TYPE_TIMESTAMP: + return (char *)&pVar->i; + case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_FLOAT: + return (char *)&pVar->d; + case TSDB_DATA_TYPE_BINARY: + return (char *)pVar->pz; + case TSDB_DATA_TYPE_NCHAR: + return (char *)pVar->ucs4; + default: + return NULL; + } + + return NULL; +} + diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index a1642fc2c8..bb2432c07a 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -1289,6 +1289,12 @@ static void projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSData taosArrayDestroy(pBlockList); + } else if (pExpr[k].pExpr->nodeType == QUERY_NODE_VALUE) { + SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, k); + for (int32_t i = 0; i < pSrcBlock->info.rows; ++i) { + colDataAppend(pColInfoData, i, taosVariantGet(&pExpr[k].base.pParam[0].param, pExpr[k].base.pParam[0].type), TSDB_DATA_TYPE_NULL == pExpr[k].base.pParam[0].param.nType); + } + pResult->info.rows = pSrcBlock->info.rows; } else { ASSERT(0); } @@ -2093,7 +2099,7 @@ static SqlFunctionCtx* createSqlFunctionCtx_rv(SExprInfo* pExprInfo, int32_t num } } pCtx->resDataInfo.interBufSize = env.calcMemSize; - } else if (pExpr->pExpr->nodeType == QUERY_NODE_COLUMN || pExpr->pExpr->nodeType == QUERY_NODE_OPERATOR) { + } else if (pExpr->pExpr->nodeType == QUERY_NODE_COLUMN || pExpr->pExpr->nodeType == QUERY_NODE_OPERATOR || pExpr->pExpr->nodeType == QUERY_NODE_VALUE) { pCtx->resDataInfo.interBufSize = pFunct->resSchema.bytes; // for simple column, the intermediate buffer needs to hold one element. } @@ -2864,6 +2870,9 @@ int32_t loadDataBlock(SExecTaskInfo* pTaskInfo, STableScanInfo* pTableScanInfo, if (pTableScanInfo->pFilterNode != NULL) { SFilterInfo* filter = NULL; int32_t code = filterInitFromNode((SNode*)pTableScanInfo->pFilterNode, &filter, 0); + if (code) { + return code; + } SFilterColumnParam param1 = {.numOfCols = pBlock->info.numOfCols, .pDataBlock = pBlock->pDataBlock}; code = filterSetDataFromSlotId(filter, ¶m1); @@ -8309,6 +8318,17 @@ SExprInfo* createExprInfo(SNodeList* pNodeList, SNodeList* pGroupKeys, int32_t* // pExp->base.pParam[0].type = FUNC_PARAM_TYPE_COLUMN; // pExp->base.pParam[0].pCol = createColumn(pTargetNode->dataBlockId, pTargetNode->slotId, pType); + } else if (nodeType(pTargetNode->pExpr) == QUERY_NODE_VALUE) { + pExp->pExpr->nodeType = QUERY_NODE_VALUE; + SValueNode* pValNode = (SValueNode*)pTargetNode->pExpr; + + pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam)); + pExp->base.numOfParams = 1; + + SDataType* pType = &pValNode->node.resType; + pExp->base.resSchema = createResSchema(pType->type, pType->bytes, pTargetNode->slotId, pType->scale, pType->precision, pValNode->node.aliasName); + pExp->base.pParam[0].type = FUNC_PARAM_TYPE_VALUE; + valueNodeToVariant(pValNode, &pExp->base.pParam[0].param); } else { ASSERT(0); } diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index cfee083f46..6bf2c99d0d 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -1092,4 +1092,45 @@ int32_t nodesGetOutputNumFromSlotList(SNodeList* pSlots) { } +void valueNodeToVariant(const SValueNode* pNode, SVariant* pVal) { + pVal->nType = pNode->node.resType.type; + pVal->nLen = pNode->node.resType.bytes; + switch (pNode->node.resType.type) { + case TSDB_DATA_TYPE_NULL: + break; + case TSDB_DATA_TYPE_BOOL: + pVal->i = pNode->datum.b; + break; + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_SMALLINT: + case TSDB_DATA_TYPE_INT: + case TSDB_DATA_TYPE_BIGINT: + case TSDB_DATA_TYPE_TIMESTAMP: + pVal->i = pNode->datum.i; + break; + case TSDB_DATA_TYPE_UTINYINT: + case TSDB_DATA_TYPE_USMALLINT: + case TSDB_DATA_TYPE_UINT: + case TSDB_DATA_TYPE_UBIGINT: + pVal->u = pNode->datum.u; + break; + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_DOUBLE: + pVal->d = pNode->datum.d; + break; + case TSDB_DATA_TYPE_NCHAR: + case TSDB_DATA_TYPE_VARCHAR: + case TSDB_DATA_TYPE_VARBINARY: + pVal->pz = pNode->datum.p; + break; + case TSDB_DATA_TYPE_JSON: + case TSDB_DATA_TYPE_DECIMAL: + case TSDB_DATA_TYPE_BLOB: + // todo + default: + break; + } +} + + diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index bf909a0eaf..97fce407b6 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -438,8 +438,12 @@ static EDealRes translateValue(STranslateContext* pCxt, SValueNode* pVal) { static EDealRes translateOperator(STranslateContext* pCxt, SOperatorNode* pOp) { if (nodesIsUnaryOp(pOp)) { - if (OP_TYPE_MINUS == pOp->opType && !IS_NUMERIC_TYPE(((SExprNode*)(pOp->pLeft))->resType.type)) { - return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pLeft))->aliasName); + if (OP_TYPE_MINUS == pOp->opType) { + if (!IS_MATHABLE_TYPE(((SExprNode*)(pOp->pLeft))->resType.type)) { + return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pLeft))->aliasName); + } + pOp->node.resType.type = TSDB_DATA_TYPE_DOUBLE; + pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes; } return DEAL_RES_CONTINUE; } @@ -2338,46 +2342,6 @@ static void addCreateTbReqIntoVgroup(int32_t acctId, SHashObj* pVgroupHashmap, } } -static void valueNodeToVariant(const SValueNode* pNode, SVariant* pVal) { - pVal->nType = pNode->node.resType.type; - pVal->nLen = pNode->node.resType.bytes; - switch (pNode->node.resType.type) { - case TSDB_DATA_TYPE_NULL: - break; - case TSDB_DATA_TYPE_BOOL: - pVal->i = pNode->datum.b; - break; - case TSDB_DATA_TYPE_TINYINT: - case TSDB_DATA_TYPE_SMALLINT: - case TSDB_DATA_TYPE_INT: - case TSDB_DATA_TYPE_BIGINT: - case TSDB_DATA_TYPE_TIMESTAMP: - pVal->i = pNode->datum.i; - break; - case TSDB_DATA_TYPE_UTINYINT: - case TSDB_DATA_TYPE_USMALLINT: - case TSDB_DATA_TYPE_UINT: - case TSDB_DATA_TYPE_UBIGINT: - pVal->u = pNode->datum.u; - break; - case TSDB_DATA_TYPE_FLOAT: - case TSDB_DATA_TYPE_DOUBLE: - pVal->d = pNode->datum.d; - break; - case TSDB_DATA_TYPE_NCHAR: - case TSDB_DATA_TYPE_VARCHAR: - case TSDB_DATA_TYPE_VARBINARY: - pVal->pz = pNode->datum.p; - break; - case TSDB_DATA_TYPE_JSON: - case TSDB_DATA_TYPE_DECIMAL: - case TSDB_DATA_TYPE_BLOB: - // todo - default: - break; - } -} - static int32_t addValToKVRow(STranslateContext* pCxt, SValueNode* pVal, const SSchema* pSchema, SKVRowBuilder* pBuilder) { if (DEAL_RES_ERROR == translateValue(pCxt, pVal)) { return pCxt->errCode; diff --git a/source/libs/scalar/inc/sclvector.h b/source/libs/scalar/inc/sclvector.h index 3f41ad875c..e51115e9c0 100644 --- a/source/libs/scalar/inc/sclvector.h +++ b/source/libs/scalar/inc/sclvector.h @@ -75,6 +75,8 @@ static FORCE_INLINE _getDoubleValue_fn_t getVectorDoubleValueFn(int32_t srcType) p = getVectorDoubleValue_FLOAT; } else if (srcType == TSDB_DATA_TYPE_DOUBLE) { p = getVectorDoubleValue_DOUBLE; + } else if (srcType == TSDB_DATA_TYPE_TIMESTAMP) { + p = getVectorDoubleValue_BIGINT; } else { assert(0); } diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index d77ada3b4e..07358f905f 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -10,7 +10,8 @@ int32_t scalarGetOperatorParamNum(EOperatorType type) { if (OP_TYPE_IS_NULL == type || OP_TYPE_IS_NOT_NULL == type || OP_TYPE_IS_TRUE == type || OP_TYPE_IS_NOT_TRUE == type - || OP_TYPE_IS_FALSE == type || OP_TYPE_IS_NOT_FALSE == type || OP_TYPE_IS_UNKNOWN == type || OP_TYPE_IS_NOT_UNKNOWN == type) { + || OP_TYPE_IS_FALSE == type || OP_TYPE_IS_NOT_FALSE == type || OP_TYPE_IS_UNKNOWN == type || OP_TYPE_IS_NOT_UNKNOWN == type + || OP_TYPE_MINUS == type) { return 1; } diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index ef3be80893..c5f18fb1a9 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -773,6 +773,32 @@ void vectorMathRemainder(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam doReleaseVec(pRightCol, rightConvert); } +void vectorMathMinus(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) { + SColumnInfoData *pOutputCol = pOut->columnData; + + pOut->numOfRows = pLeft->numOfRows; + + int32_t i = ((_ord) == TSDB_ORDER_ASC)? 0 : (pLeft->numOfRows - 1); + int32_t step = ((_ord) == TSDB_ORDER_ASC)? 1 : -1; + + int32_t leftConvert = 0; + SColumnInfoData *pLeftCol = doVectorConvert(pLeft, &leftConvert); + + _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type); + + double *output = (double *)pOutputCol->pData; + for (; i < pLeft->numOfRows && i >= 0; i += step, output += 1) { + *output = - getVectorDoubleValueFnLeft(pLeftCol->pData, i); + } + + pOutputCol->hasNull = pLeftCol->hasNull; + if (pOutputCol->hasNull) { + memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(pLeft->numOfRows)); + } + + doReleaseVec(pLeftCol, leftConvert); +} + void vectorConcat(SScalarParam* pLeft, SScalarParam* pRight, void *out, int32_t _ord) { #if 0 int32_t len = pLeft->bytes + pRight->bytes; @@ -1102,6 +1128,8 @@ _bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) { return vectorMathDivide; case OP_TYPE_MOD: return vectorMathRemainder; + case OP_TYPE_MINUS: + return vectorMathMinus; case OP_TYPE_GREATER_THAN: return vectorGreater; case OP_TYPE_GREATER_EQUAL: @@ -1140,4 +1168,4 @@ _bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) { assert(0); return NULL; } -} \ No newline at end of file +}