From ef0d9262c98a85048398ca648a8e3b0cb9511fa4 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Mon, 14 Feb 2022 19:38:06 +0800 Subject: [PATCH] feature/qnode --- include/nodes/nodes.h | 1 + source/libs/function/inc/tscalar.h | 41 ++++++++++++ source/libs/function/src/taggfunction.c | 2 +- source/libs/function/src/tbinoperator.c | 12 ++-- source/libs/function/src/tscalar.c | 73 ++++++++++++++++++++++ source/libs/function/src/tscalarfunction.c | 3 + source/nodes/src/nodesUtilFuncs.c | 32 +++++++++- 7 files changed, 155 insertions(+), 9 deletions(-) create mode 100644 source/libs/function/inc/tscalar.h create mode 100644 source/libs/function/src/tscalar.c diff --git a/include/nodes/nodes.h b/include/nodes/nodes.h index b182063808..e692b58e54 100644 --- a/include/nodes/nodes.h +++ b/include/nodes/nodes.h @@ -337,6 +337,7 @@ SNodeList* nodesListAppend(SNodeList* pList, SNode* pNode); SListCell* nodesListErase(SNodeList* pList, SListCell* pCell); SNode* nodesListGetNode(SNodeList* pList, int32_t index); void nodesDestroyList(SNodeList* pList); +void *nodesGetValueFromNode(SValueNode *pNode); typedef enum EDealRes { DEAL_RES_CONTINUE = 1, diff --git a/source/libs/function/inc/tscalar.h b/source/libs/function/inc/tscalar.h new file mode 100644 index 0000000000..af0ca2dc4c --- /dev/null +++ b/source/libs/function/inc/tscalar.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef TDENGINE_SCALAR_H +#define TDENGINE_SCALAR_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#define sclFatal(...) qFatal(__VA_ARGS__) +#define sclError(...) qError(__VA_ARGS__) +#define sclWarn(...) qWarn(__VA_ARGS__) +#define sclInfo(...) qInfo(__VA_ARGS__) +#define sclDebug(...) qDebug(__VA_ARGS__) +#define sclTrace(...) qTrace(__VA_ARGS__) + +#define SCL_ERR_RET(c) do { int32_t _code = c; if (_code != TSDB_CODE_SUCCESS) { terrno = _code; return _code; } } while (0) +#define SCL_RET(c) do { int32_t _code = c; if (_code != TSDB_CODE_SUCCESS) { terrno = _code; } return _code; } while (0) +#define SCL_ERR_JRET(c) do { code = c; if (code != TSDB_CODE_SUCCESS) { terrno = code; goto _return; } } while (0) + + + + +#ifdef __cplusplus +} +#endif + +#endif // TDENGINE_SCALAR_H diff --git a/source/libs/function/src/taggfunction.c b/source/libs/function/src/taggfunction.c index d0d89611c4..881845f42b 100644 --- a/source/libs/function/src/taggfunction.c +++ b/source/libs/function/src/taggfunction.c @@ -3243,7 +3243,7 @@ static void arithmetic_function(SqlFunctionCtx *pCtx) { SScalarFuncParam output = {0}; output.data = pCtx->pOutput; - evaluateExprNodeTree(pSup->pExprInfo->pExpr, pCtx->size, &output, pSup, getArithColumnData); + //evaluateExprNodeTree(pSup->pExprInfo->pExpr, pCtx->size, &output, pSup, getArithColumnData); } #define LIST_MINMAX_N(ctx, minOutput, maxOutput, elemCnt, data, type, tsdbType, numOfNotNullElem) \ diff --git a/source/libs/function/src/tbinoperator.c b/source/libs/function/src/tbinoperator.c index 4de11cbe71..72144bf130 100644 --- a/source/libs/function/src/tbinoperator.c +++ b/source/libs/function/src/tbinoperator.c @@ -465,18 +465,16 @@ void vectorConcat(SScalarFuncParam* pLeft, SScalarFuncParam* pRight, void *out, _bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) { switch (binFunctionId) { - case TSDB_BINARY_OP_ADD: + case OP_TYPE_ADD: return vectorAdd; - case TSDB_BINARY_OP_SUBTRACT: + case OP_TYPE_SUB: return vectorSub; - case TSDB_BINARY_OP_MULTIPLY: + case OP_TYPE_MULTI: return vectorMultiply; - case TSDB_BINARY_OP_DIVIDE: + case OP_TYPE_DIV: return vectorDivide; - case TSDB_BINARY_OP_REMAINDER: + case OP_TYPE_MOD: return vectorRemainder; - case TSDB_BINARY_OP_CONCAT: - return vectorConcat; default: assert(0); return NULL; diff --git a/source/libs/function/src/tscalar.c b/source/libs/function/src/tscalar.c new file mode 100644 index 0000000000..6a4a8975af --- /dev/null +++ b/source/libs/function/src/tscalar.c @@ -0,0 +1,73 @@ +#include "nodes.h" +#include "tscalar.h" + +EDealRes sclCalculateConstants(SNode** pNode, void* pContext) { + if (QUERY_NODE_VALUE == nodeType(*pNode)) { + return DEAL_RES_CONTINUE; + } + + if (QUERY_NODE_OPERATOR != nodeType(*pNode)) { + sclError("invalid node type for calculating constants, type:%d", ); + *(int32_t *)pContext = TSDB_CODE_QRY_INVALID_INPUT; + return DEAL_RES_ERROR; + } + + SOperatorNode *oper = (SOperatorNode *)*pNode; + if (NULL == oper->pLeft || NULL == oper->pRight) { + sclError("invalid operation node, left:%p, right:%p", oper->pLeft, oper->pRight); + *(int32_t *)pContext = TSDB_CODE_QRY_INVALID_INPUT; + return DEAL_RES_ERROR; + } + + if (QUERY_NODE_VALUE != nodeType(oper->pLeft) || QUERY_NODE_VALUE != nodeType(oper->pRight)) { + sclError("invalid operation node, leftType:%d, rightType:%d", nodeType(oper->pLeft), nodeType(oper->pRight)); + *(int32_t *)pContext = TSDB_CODE_QRY_INVALID_INPUT; + return DEAL_RES_ERROR; + } + + SValueNode *res = nodesMakeNode(QUERY_NODE_VALUE); + if (NULL == res) { + sclError("make value node failed"); + *(int32_t *)pContext = TSDB_CODE_QRY_OUT_OF_MEMORY; + return DEAL_RES_ERROR; + } + + res->node.resType = oper->node.resType; + + SValueNode *leftValue = (SValueNode *)oper->pLeft; + SValueNode *rightValue = (SValueNode *)oper->pRight; + + SScalarFuncParam leftParam = {0}, rightParam = {0}; + _bin_scalar_fn_t OperatorFn = getBinScalarOperatorFn(oper->opType); + setScalarFuncParam(&leftParam, leftValue->node.resType, 0, nodesGetValueFromNode(leftValue), 1); + setScalarFuncParam(&rightParam, rightValue->node.resType, 0, nodesGetValueFromNode(rightValue), 1); + + OperatorFn(&leftParam, &rightParam, nodesGetValueFromNode(res), TSDB_ORDER_ASC); + + nodesDestroyNode(*pNode); + *pNode = (SNode*)res; + + return DEAL_RES_CONTINUE; +} + +int32_t scalarCalculateConstants(SNode *pNode, SNode **pRes) { + if (NULL == pNode) { + SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); + } + + int32_t code = 0; + + nodesRewriteNodePostOrder(&pNode, sclCalculateConstants, (void *)&code); + + if (code) { + nodesDestroyNode(pNode); + SCL_ERR_RET(code); + } + + *pRes = pNode; + + SCL_RET(code); +} + + + diff --git a/source/libs/function/src/tscalarfunction.c b/source/libs/function/src/tscalarfunction.c index 90d25b3e4a..50aaa07757 100644 --- a/source/libs/function/src/tscalarfunction.c +++ b/source/libs/function/src/tscalarfunction.c @@ -273,6 +273,7 @@ bool isStringOp(int32_t op) { return op == TSDB_BINARY_OP_CONCAT; } +#if 0 int32_t evaluateExprNodeTree(tExprNode* pExprs, int32_t numOfRows, SScalarFuncParam* pOutput, void* param, char* (*getSourceDataBlock)(void*, const char*, int32_t)) { if (pExprs == NULL) { @@ -361,6 +362,8 @@ int32_t evaluateExprNodeTree(tExprNode* pExprs, int32_t numOfRows, SScalarFuncPa return 0; } +#endif + SScalarFunctionInfo scalarFunc[8] = { {"ceil", FUNCTION_TYPE_SCALAR, FUNCTION_CEIL, tceil}, diff --git a/source/nodes/src/nodesUtilFuncs.c b/source/nodes/src/nodesUtilFuncs.c index e0e589157c..e13cf025cc 100644 --- a/source/nodes/src/nodesUtilFuncs.c +++ b/source/nodes/src/nodesUtilFuncs.c @@ -152,6 +152,36 @@ void nodesDestroyList(SNodeList* pList) { tfree(pList); } +void *nodesGetValueFromNode(SValueNode *pNode) { + switch (pNode->node.resType.type) { + case TSDB_DATA_TYPE_BOOL: + return (void *)&pNode->datum.b; + 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: + return (void *)&pNode->datum.i; + case TSDB_DATA_TYPE_UTINYINT: + case TSDB_DATA_TYPE_USMALLINT: + case TSDB_DATA_TYPE_UINT: + case TSDB_DATA_TYPE_UBIGINT: + return (void *)&pNode->datum.u; + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_DOUBLE: + return (void *)&pNode->datum.d; + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: + case TSDB_DATA_TYPE_VARCHAR: + case TSDB_DATA_TYPE_VARBINARY: + return (void *)pNode->datum.p; + default: + break; + } + + return NULL; +} + bool nodesIsExprNode(const SNode* pNode) { ENodeType type = nodeType(pNode); return (QUERY_NODE_COLUMN == type || QUERY_NODE_VALUE == type || QUERY_NODE_OPERATOR == type || QUERY_NODE_FUNCTION == type); @@ -209,4 +239,4 @@ bool nodesIsTimeorderQuery(const SNode* pQuery) { bool nodesIsTimelineQuery(const SNode* pQuery) { return false; -} \ No newline at end of file +}