未验证 提交 655b7ec5 编写于 作者: X xiao-yu-wang 提交者: GitHub

Merge pull request #10214 from taosdata/feature/3.0_wxy

TD-13338 SELECT statement translate code
......@@ -22,6 +22,81 @@ extern "C" {
#include "nodes.h"
typedef enum EFunctionType {
// aggregate function
FUNCTION_TYPE_APERCENTILE = 1,
FUNCTION_TYPE_AVG,
FUNCTION_TYPE_COUNT,
FUNCTION_TYPE_ELAPSED,
FUNCTION_TYPE_IRATE,
FUNCTION_TYPE_LAST_ROW,
FUNCTION_TYPE_LEASTSQUARES,
FUNCTION_TYPE_MAX,
FUNCTION_TYPE_MIN,
FUNCTION_TYPE_MODE,
FUNCTION_TYPE_PERCENTILE,
FUNCTION_TYPE_SPREAD,
FUNCTION_TYPE_STDDEV,
FUNCTION_TYPE_SUM,
FUNCTION_TYPE_TWA,
// nonstandard SQL function
FUNCTION_TYPE_BOTTOM = 500,
FUNCTION_TYPE_CSUM,
FUNCTION_TYPE_DERIVATIVE,
FUNCTION_TYPE_DIFF,
FUNCTION_TYPE_FIRST,
FUNCTION_TYPE_INTERP,
FUNCTION_TYPE_LAST,
FUNCTION_TYPE_MAVG,
FUNCTION_TYPE_SAMPLE,
FUNCTION_TYPE_TAIL,
FUNCTION_TYPE_TOP,
FUNCTION_TYPE_UNIQUE,
// math function
FUNCTION_TYPE_ABS = 1000,
FUNCTION_TYPE_ACOS,
FUNCTION_TYPE_ASION,
FUNCTION_TYPE_ATAN,
FUNCTION_TYPE_CEIL,
FUNCTION_TYPE_COS,
FUNCTION_TYPE_FLOOR,
FUNCTION_TYPE_LOG,
FUNCTION_TYPE_POW,
FUNCTION_TYPE_ROUND,
FUNCTION_TYPE_SIN,
FUNCTION_TYPE_SQRT,
FUNCTION_TYPE_TAN,
// string function
FUNCTION_TYPE_CHAR_LENGTH = 1500,
FUNCTION_TYPE_CONCAT,
FUNCTION_TYPE_CONCAT_WS,
FUNCTION_TYPE_LENGTH,
// conversion function
FUNCTION_TYPE_CAST = 2000,
FUNCTION_TYPE_TO_ISO8601,
FUNCTION_TYPE_TO_JSON,
FUNCTION_TYPE_UNIXTIMESTAMP,
// date and time function
FUNCTION_TYPE_NOW = 2500,
FUNCTION_TYPE_TIMEDIFF,
FUNCTION_TYPE_TIMETRUNCATE,
FUNCTION_TYPE_TIMEZONE,
FUNCTION_TYPE_TODAY,
// system function
FUNCTION_TYPE_DATABASE = 3000,
FUNCTION_TYPE_CLIENT_VERSION,
FUNCTION_TYPE_SERVER_SERSION,
FUNCTION_TYPE_SERVER_STATUS,
FUNCTION_TYPE_CURRENT_USER,
FUNCTION_TYPE_USER
} EFunctionType;
struct SqlFunctionCtx;
struct SResultRowEntryInfo;
struct STimeWindow;
......@@ -47,14 +122,18 @@ int32_t fmFuncMgtInit();
int32_t fmGetHandle(FuncMgtHandle* pHandle);
int32_t fmGetFuncId(FuncMgtHandle handle, const char* name);
int32_t fmGetFuncResultType(FuncMgtHandle handle, SFunctionNode* pFunc);
int32_t fmGetFuncInfo(FuncMgtHandle handle, const char* pFuncName, int32_t* pFuncId, int32_t* pFuncType);
int32_t fmGetFuncResultType(SFunctionNode* pFunc);
bool fmIsAggFunc(int32_t funcId);
bool fmIsScalarFunc(int32_t funcId);
bool fmIsNonstandardSQLFunc(int32_t funcId);
bool fmIsStringFunc(int32_t funcId);
bool fmIsTimestampFunc(int32_t funcId);
bool fmIsDatetimeFunc(int32_t funcId);
bool fmIsTimelineFunc(int32_t funcId);
bool fmIsTimeorderFunc(int32_t funcId);
bool fmIsNonstandardSQLFunc(int32_t funcId);
int32_t fmFuncScanType(int32_t funcId);
int32_t fmGetFuncExecFuncs(FuncMgtHandle handle, int32_t funcId, SFuncExecFuncs* pFpSet);
......
......@@ -30,6 +30,20 @@ extern "C" {
#define FOREACH(node, list) \
for (SListCell* cell = (NULL != (list) ? (list)->pHead : NULL); (NULL != cell ? (node = cell->pNode, true) : (node = NULL, false)); cell = cell->pNext)
// only be use in FOREACH
#define ERASE_NODE(list) \
if (NULL == cell->pPrev) { \
(list)->pHead = cell->pNext; \
} else { \
cell->pPrev->pNext = cell->pNext; \
cell->pNext->pPrev = cell->pPrev; \
} \
SListCell* tmp = cell; \
cell = cell->pNext; \
tfree(tmp);
#define REPLACE_NODE(newNode) cell->pNode = (SNode*)(newNode)
#define FORBOTH(node1, list1, node2, list2) \
for (SListCell* cell1 = (NULL != (list1) ? (list1)->pHead : NULL), *cell2 = (NULL != (list2) ? (list2)->pHead : NULL); \
(NULL == cell1 ? (node1 = NULL, false) : (node1 = cell1->pNode, true)), (NULL == cell2 ? (node2 = NULL, false) : (node2 = cell2->pNode, true)), (node1 != NULL && node2 != NULL); \
......@@ -71,8 +85,9 @@ typedef struct SNode {
} SNode;
typedef struct SListCell {
SNode* pNode;
struct SListCell* pPrev;
struct SListCell* pNext;
SNode* pNode;
} SListCell;
typedef struct SNodeList {
......@@ -192,6 +207,7 @@ typedef struct SFunctionNode {
SExprNode node; // QUERY_NODE_FUNCTION
char functionName[TSDB_FUNC_NAME_LEN];
int32_t funcId;
int32_t funcType;
SNodeList* pParameterList;
} SFunctionNode;
......@@ -327,6 +343,7 @@ void nodesDestroyNode(SNode* pNode);
SNodeList* nodesMakeList();
SNodeList* nodesListAppend(SNodeList* pList, SNode* pNode);
SNode* nodesListGetNode(SNodeList* pList, int32_t index);
void nodesDestroyList(SNodeList* pList);
typedef bool (*FQueryNodeWalker)(SNode* pNode, void* pContext);
......
......@@ -444,13 +444,15 @@ int32_t* taosGetErrno();
#define TSDB_CODE_SCH_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x2502) //scheduler internal error
//parser
#define TSDB_CODE_PAR_INVALID_COLUMN TAOS_DEF_ERROR_CODE(0, 0x2601) //invalid column name
#define TSDB_CODE_PAR_TABLE_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x2602) //table not exist
#define TSDB_CODE_PAR_AMBIGUOUS_COLUMN TAOS_DEF_ERROR_CODE(0, 0x2603) //ambiguous column
#define TSDB_CODE_PAR_WRONG_VALUE_TYPE TAOS_DEF_ERROR_CODE(0, 0x2604) //wrong value type
#define TSDB_CODE_PAR_FUNTION_PARA_NUM TAOS_DEF_ERROR_CODE(0, 0x2605) //invalid number of arguments
#define TSDB_CODE_PAR_FUNTION_PARA_TYPE TAOS_DEF_ERROR_CODE(0, 0x2606) //inconsistent datatypes
#define TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION TAOS_DEF_ERROR_CODE(0, 0x2607) //there mustn't be aggregation
#define TSDB_CODE_PAR_INVALID_COLUMN TAOS_DEF_ERROR_CODE(0, 0x2601) //Invalid column name
#define TSDB_CODE_PAR_TABLE_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x2602) //Table does not exist
#define TSDB_CODE_PAR_AMBIGUOUS_COLUMN TAOS_DEF_ERROR_CODE(0, 0x2603) //Column ambiguously defined
#define TSDB_CODE_PAR_WRONG_VALUE_TYPE TAOS_DEF_ERROR_CODE(0, 0x2604) //Invalid value type
#define TSDB_CODE_PAR_INVALID_FUNTION TAOS_DEF_ERROR_CODE(0, 0x2605) //Invalid function name
#define TSDB_CODE_PAR_FUNTION_PARA_NUM TAOS_DEF_ERROR_CODE(0, 0x2606) //Invalid number of arguments
#define TSDB_CODE_PAR_FUNTION_PARA_TYPE TAOS_DEF_ERROR_CODE(0, 0x2607) //Inconsistent datatypes
#define TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION TAOS_DEF_ERROR_CODE(0, 0x2608) //There mustn't be aggregation
#define TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT TAOS_DEF_ERROR_CODE(0, 0x2609) //ORDER BY item must be the number of a SELECT-list expression
#ifdef __cplusplus
}
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef _TD_BUILDINS_H_
#define _TD_BUILDINS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "functionMgt.h"
#define FUNCTION_NAME_MAX_LENGTH 16
#define FUNC_MGT_FUNC_CLASSIFICATION_MASK(n) (1 << n)
#define FUNC_MGT_AGG_FUNC FUNC_MGT_FUNC_CLASSIFICATION_MASK(0)
#define FUNC_MGT_SCALAR_FUNC FUNC_MGT_FUNC_CLASSIFICATION_MASK(1)
#define FUNC_MGT_NONSTANDARD_SQL_FUNC FUNC_MGT_FUNC_CLASSIFICATION_MASK(2)
#define FUNC_MGT_STRING_FUNC FUNC_MGT_FUNC_CLASSIFICATION_MASK(3)
#define FUNC_MGT_DATETIME_FUNC FUNC_MGT_FUNC_CLASSIFICATION_MASK(4)
#define FUNC_MGT_TIMELINE_FUNC FUNC_MGT_FUNC_CLASSIFICATION_MASK(5)
#define FUNC_MGT_TIMEORDER_FUNC FUNC_MGT_FUNC_CLASSIFICATION_MASK(6)
#define FUNC_MGT_TEST_MASK(val, mask) (((val) & (mask)) != 0)
typedef int32_t (*FCheckAndGetResultType)(SFunctionNode* pFunc);
typedef struct SBuiltinFuncDefinition {
char name[FUNCTION_NAME_MAX_LENGTH];
EFunctionType type;
uint64_t classification;
FCheckAndGetResultType checkFunc;
FExecGetEnv getEnvFunc;
FExecInit initFunc;
FExecProcess processFunc;
FExecFinalize finalizeFunc;
} SBuiltinFuncDefinition;
extern const SBuiltinFuncDefinition funcMgtBuiltins[];
extern const int funcMgtBuiltinsNum;
#ifdef __cplusplus
}
#endif
#endif /*_TD_BUILDINS_H_*/
......@@ -21,53 +21,9 @@ extern "C" {
#endif
#include "functionMgt.h"
// #include "builtins.h"
#define FUNC_MGT_DATA_TYPE_MASK(n) (1 << n)
#define FUNC_MGT_DATA_TYPE_NULL 0
#define FUNC_MGT_DATA_TYPE_BOOL FUNC_MGT_DATA_TYPE_MASK(0)
#define FUNC_MGT_DATA_TYPE_TINYINT FUNC_MGT_DATA_TYPE_MASK(1)
#define FUNC_MGT_DATA_TYPE_SMALLINT FUNC_MGT_DATA_TYPE_MASK(2)
#define FUNC_MGT_DATA_TYPE_INT FUNC_MGT_DATA_TYPE_MASK(3)
#define FUNC_MGT_DATA_TYPE_BIGINT FUNC_MGT_DATA_TYPE_MASK(4)
#define FUNC_MGT_DATA_TYPE_FLOAT FUNC_MGT_DATA_TYPE_MASK(5)
#define FUNC_MGT_DATA_TYPE_DOUBLE FUNC_MGT_DATA_TYPE_MASK(6)
#define FUNC_MGT_DATA_TYPE_BINARY FUNC_MGT_DATA_TYPE_MASK(7)
#define FUNC_MGT_DATA_TYPE_TIMESTAMP FUNC_MGT_DATA_TYPE_MASK(8)
#define FUNC_MGT_DATA_TYPE_NCHAR FUNC_MGT_DATA_TYPE_MASK(9)
#define FUNC_MGT_DATA_TYPE_UTINYINT FUNC_MGT_DATA_TYPE_MASK(10)
#define FUNC_MGT_DATA_TYPE_USMALLINT FUNC_MGT_DATA_TYPE_MASK(11)
#define FUNC_MGT_DATA_TYPE_UINT FUNC_MGT_DATA_TYPE_MASK(12)
#define FUNC_MGT_DATA_TYPE_UBIGINT FUNC_MGT_DATA_TYPE_MASK(13)
#define FUNC_MGT_DATA_TYPE_VARCHAR FUNC_MGT_DATA_TYPE_MASK(14)
#define FUNC_MGT_DATA_TYPE_VARBINARY FUNC_MGT_DATA_TYPE_MASK(15)
#define FUNC_MGT_DATA_TYPE_JSON FUNC_MGT_DATA_TYPE_MASK(16)
#define FUNC_MGT_DATA_TYPE_DECIMAL FUNC_MGT_DATA_TYPE_MASK(17)
#define FUNC_MGT_DATA_TYPE_BLOB FUNC_MGT_DATA_TYPE_MASK(18)
#define FUNC_MGT_EXACT_NUMERIC_DATA_TYPE \
(FUNC_MGT_DATA_TYPE_TINYINT | FUNC_MGT_DATA_TYPE_SMALLINT | FUNC_MGT_DATA_TYPE_INT | FUNC_MGT_DATA_TYPE_BIGINT \
| FUNC_MGT_DATA_TYPE_UTINYINT | FUNC_MGT_DATA_TYPE_USMALLINT | FUNC_MGT_DATA_TYPE_UINT | FUNC_MGT_DATA_TYPE_UBIGINT)
#define FUNC_MGT_APPRO_NUMERIC_DATA_TYPE (FUNC_MGT_DATA_TYPE_FLOAT | FUNC_MGT_DATA_TYPE_DOUBLE)
#define FUNC_MGT_NUMERIC_DATA_TYPE (FUNC_MGT_EXACT_NUMERIC_DATA_TYPE | FUNC_MGT_APPRO_NUMERIC_DATA_TYPE)
typedef void* FuncDef;
typedef struct SFuncElement {
FuncDef (*defineFunc)();
} SFuncElement;
extern const SFuncElement gBuiltinFuncs[];
FuncDef createFuncDef(const char* name, int32_t maxNumOfParams);
FuncDef setOneParamSignature(FuncDef def, int64_t resDataType, int64_t paramDataType);
FuncDef setTwoParamsSignature(FuncDef def, int64_t resDataType, int64_t p1DataType, int64_t p2DataType);
FuncDef setFollowParamSignature(FuncDef def, int64_t paramDataType);
FuncDef setFollowParamsSignature(FuncDef def, int64_t p1DataType, int64_t p2DataType, int32_t followNo);
FuncDef setExecFuncs(FuncDef def, FExecGetEnv getEnv, FExecInit init, FExecProcess process, FExecFinalize finalize);
#ifdef __cplusplus
}
......
......@@ -89,10 +89,6 @@ static FORCE_INLINE void initResultRowEntry(SResultRowEntryInfo *pResInfo, int32
memset(GET_ROWCELL_INTERBUF(pResInfo), 0, bufLen);
}
#include "functionMgtInt.h"
FuncDef defineCount();
#ifdef __cplusplus
}
#endif
......
......@@ -13,9 +13,36 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "functionMgtInt.h"
#include "taggfunction.h"
#include "builtins.h"
#include "taoserror.h"
const SFuncElement gBuiltinFuncs[] = {
{.defineFunc = defineCount}
int32_t stubCheckAndGetResultType(SFunctionNode* pFunc);
const SBuiltinFuncDefinition funcMgtBuiltins[] = {
{
.name = "count",
.type = FUNCTION_TYPE_COUNT,
.classification = FUNC_MGT_AGG_FUNC,
.checkFunc = stubCheckAndGetResultType,
.getEnvFunc = NULL,
.initFunc = NULL,
.processFunc = NULL,
.finalizeFunc = NULL
},
{
.name = "concat",
.type = FUNCTION_TYPE_CONCAT,
.classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
.checkFunc = stubCheckAndGetResultType,
.getEnvFunc = NULL,
.initFunc = NULL,
.processFunc = NULL,
.finalizeFunc = NULL
}
};
const int funcMgtBuiltinsNum = (sizeof(funcMgtBuiltins) / sizeof(SBuiltinFuncDefinition));
int32_t stubCheckAndGetResultType(SFunctionNode* pFunc) {
return TSDB_CODE_SUCCESS;
}
......@@ -19,6 +19,7 @@
#include "taos.h"
#include "taoserror.h"
#include "thash.h"
#include "builtins.h"
typedef struct SFuncMgtService {
SHashObj* pFuncNameHashTable;
......@@ -27,62 +28,46 @@ typedef struct SFuncMgtService {
static SFuncMgtService gFunMgtService;
int32_t fmFuncMgtInit() {
gFunMgtService.pFuncNameHashTable = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
gFunMgtService.pFuncNameHashTable = taosHashInit(funcMgtBuiltinsNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
if (NULL == gFunMgtService.pFuncNameHashTable) {
return TSDB_CODE_FAILED;
}
return TSDB_CODE_SUCCESS;
}
typedef struct SFuncDef {
char name[TSDB_FUNC_NAME_LEN];
int32_t maxNumOfParams;
SFuncExecFuncs execFuncs;
} SFuncDef ;
FuncDef createFuncDef(const char* name, int32_t maxNumOfParams) {
SFuncDef* pDef = calloc(1, sizeof(SFuncDef));
if (NULL == pDef) {
return NULL;
for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name, strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
return TSDB_CODE_FAILED;
}
}
strcpy(pDef->name, name);
pDef->maxNumOfParams = maxNumOfParams;
return pDef;
}
FuncDef setOneParamSignature(FuncDef def, int64_t resDataType, int64_t paramDataType) {
// todo
}
FuncDef setTwoParamsSignature(FuncDef def, int64_t resDataType, int64_t p1DataType, int64_t p2DataType) {
// todo
}
FuncDef setFollowParamSignature(FuncDef def, int64_t paramDataType) {
// todo
}
FuncDef setFollowParamsSignature(FuncDef def, int64_t p1DataType, int64_t p2DataType, int32_t followNo) {
// todo
return TSDB_CODE_SUCCESS;
}
FuncDef setExecFuncs(FuncDef def, FExecGetEnv getEnv, FExecInit init, FExecProcess process, FExecFinalize finalize) {
SFuncDef* pDef = (SFuncDef*)def;
pDef->execFuncs.getEnv = getEnv;
pDef->execFuncs.init = init;
pDef->execFuncs.process = process;
pDef->execFuncs.finalize = finalize;
return def;
int32_t fmGetHandle(FuncMgtHandle* pHandle) {
*pHandle = &gFunMgtService;
return TSDB_CODE_SUCCESS;
}
int32_t registerFunc(FuncDef func) {
int32_t fmGetFuncInfo(FuncMgtHandle handle, const char* pFuncName, int32_t* pFuncId, int32_t* pFuncType) {
SFuncMgtService* pService = (SFuncMgtService*)handle;
pFuncId = taosHashGet(pService->pFuncNameHashTable, pFuncName, strlen(pFuncName));
if (NULL == pFuncId) {
return TSDB_CODE_FAILED;
}
if (*pFuncId < 0 || *pFuncId >= funcMgtBuiltinsNum) {
return TSDB_CODE_FAILED;
}
*pFuncType = funcMgtBuiltins[*pFuncId].type;
return TSDB_CODE_SUCCESS;
}
int32_t fmGetFuncResultType(FuncMgtHandle handle, SFunctionNode* pFunc) {
return TSDB_CODE_SUCCESS;
int32_t fmGetFuncResultType(SFunctionNode* pFunc) {
if (pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
return TSDB_CODE_FAILED;
}
return funcMgtBuiltins[pFunc->funcId].checkFunc(pFunc);
}
bool fmIsAggFunc(int32_t funcId) {
return false;
if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
return false;
}
return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, FUNC_MGT_AGG_FUNC);
}
......@@ -4853,9 +4853,3 @@ SAggFunctionInfo aggFunc[35] = {{
statisRequired,
}
};
FuncDef defineCount() {
FuncDef def = createFuncDef("count", 1);
// todo define signature
return setExecFuncs(def, NULL, function_setup, count_function, doFinalizer);
}
......@@ -112,6 +112,7 @@ expression(A) ::= literal(B).
//expression(A) ::= pseudo_column(B). { PARSER_TRACE; A = B; }
expression(A) ::= column_reference(B). { PARSER_TRACE; A = B; }
expression(A) ::= function_name(B) NK_LP expression_list(C) NK_RP(D). { PARSER_TRACE; A = createRawExprNodeExt(pCxt, &B, &D, createFunctionNode(pCxt, &B, C)); }
expression(A) ::= function_name(B) NK_LP NK_STAR(C) NK_RP(D). { PARSER_TRACE; A = createRawExprNodeExt(pCxt, &B, &D, createFunctionNode(pCxt, &B, createNodeList(pCxt, createColumnNode(pCxt, NULL, &C)))); }
//expression(A) ::= cast_expression(B). { PARSER_TRACE; A = B; }
//expression(A) ::= case_expression(B). { PARSER_TRACE; A = B; }
expression(A) ::= subquery(B). { PARSER_TRACE; A = B; }
......
此差异已折叠。
......@@ -238,9 +238,19 @@ abort_parse:
typedef enum ESqlClause {
SQL_CLAUSE_FROM = 1,
SQL_CLAUSE_WHERE
SQL_CLAUSE_WHERE,
SQL_CLAUSE_PARTITION_BY,
SQL_CLAUSE_WINDOW,
SQL_CLAUSE_GROUP_BY,
SQL_CLAUSE_HAVING,
SQL_CLAUSE_SELECT,
SQL_CLAUSE_ORDER_BY
} ESqlClause;
static bool afterGroupBy(ESqlClause clause) {
return clause < SQL_CLAUSE_HAVING;
}
typedef struct STranslateContext {
SParseContext* pParseCxt;
FuncMgtHandle fmgt;
......@@ -249,6 +259,7 @@ typedef struct STranslateContext {
SArray* pNsLevel; // element is SArray*, the element of this subarray is STableNode*
int32_t currLevel;
ESqlClause currClause;
void* pExt;
} STranslateContext;
static int32_t translateSubquery(STranslateContext* pCxt, SNode* pNode);
......@@ -263,12 +274,16 @@ static char* getSyntaxErrFormat(int32_t errCode) {
return "Column ambiguously defined : %s";
case TSDB_CODE_PAR_WRONG_VALUE_TYPE:
return "Invalid value type : %s";
case TSDB_CODE_PAR_INVALID_FUNTION:
return "Invalid function name : %s";
case TSDB_CODE_PAR_FUNTION_PARA_NUM:
return "Invalid number of arguments : %s";
case TSDB_CODE_PAR_FUNTION_PARA_TYPE:
return "Inconsistent datatypes : %s";
case TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION:
return "There mustn't be aggregation";
case TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT:
return "ORDER BY item must be the number of a SELECT-list expression";
default:
return "Unknown error";
}
......@@ -346,7 +361,9 @@ static void setColumnInfoBySchema(const STableNode* pTable, const SSchema* pColS
static void setColumnInfoByExpr(const STableNode* pTable, SExprNode* pExpr, SColumnNode* pCol) {
pCol->pProjectRef = (SNode*)pExpr;
pExpr->pAssociationList = nodesListAppend(pExpr->pAssociationList, (SNode*)pCol);
strcpy(pCol->tableAlias, pTable->tableAlias);
if (NULL != pTable) {
strcpy(pCol->tableAlias, pTable->tableAlias);
}
strcpy(pCol->colName, pExpr->aliasName);
pCol->node.resType = pExpr->resType;
}
......@@ -435,11 +452,32 @@ static bool translateColumnWithoutPrefix(STranslateContext* pCxt, SColumnNode* p
return true;
}
static bool translateColumnUseAlias(STranslateContext* pCxt, SColumnNode* pCol) {
SNodeList* pProjectionList = pCxt->pExt;
SNode* pNode;
FOREACH(pNode, pProjectionList) {
SExprNode* pExpr = (SExprNode*)pNode;
if (0 == strcmp(pCol->colName, pExpr->aliasName)) {
setColumnInfoByExpr(NULL, pExpr, pCol);
return true;
}
}
return false;
}
static bool translateColumn(STranslateContext* pCxt, SColumnNode* pCol) {
// count(*)/first(*)/last(*)
if (0 == strcmp(pCol->colName, "*")) {
return true;
}
if ('\0' != pCol->tableAlias[0]) {
return translateColumnWithPrefix(pCxt, pCol);
}
return translateColumnWithoutPrefix(pCxt, pCol);
bool found = false;
if (SQL_CLAUSE_ORDER_BY == pCxt->currClause) {
found = translateColumnUseAlias(pCxt, pCol);
}
return found ? true : translateColumnWithoutPrefix(pCxt, pCol);
}
static int32_t trimStringCopy(const char* src, int32_t len, char* dst) {
......@@ -476,17 +514,32 @@ static bool translateValue(STranslateContext* pCxt, SValueNode* pVal) {
case TSDB_DATA_TYPE_BOOL:
pVal->datum.b = (0 == strcasecmp(pVal->literal, "true"));
break;
case TSDB_DATA_TYPE_TINYINT:
case TSDB_DATA_TYPE_SMALLINT:
case TSDB_DATA_TYPE_INT:
case TSDB_DATA_TYPE_BIGINT: {
char* endPtr = NULL;
pVal->datum.i = strtoull(pVal->literal, &endPtr, 10);
break;
}
case TSDB_DATA_TYPE_UTINYINT:
case TSDB_DATA_TYPE_USMALLINT:
case TSDB_DATA_TYPE_UINT:
case TSDB_DATA_TYPE_UBIGINT:{
char* endPtr = NULL;
pVal->datum.u = strtoull(pVal->literal, &endPtr, 10);
break;
}
case TSDB_DATA_TYPE_FLOAT:
case TSDB_DATA_TYPE_DOUBLE: {
char* endPtr = NULL;
pVal->datum.d = strtold(pVal->literal, &endPtr);
break;
}
case TSDB_DATA_TYPE_BINARY: {
case TSDB_DATA_TYPE_BINARY:
case TSDB_DATA_TYPE_NCHAR:
case TSDB_DATA_TYPE_VARCHAR:
case TSDB_DATA_TYPE_VARBINARY: {
int32_t n = strlen(pVal->literal);
pVal->datum.p = calloc(1, n);
trimStringCopy(pVal->literal, n, pVal->datum.p);
......@@ -504,6 +557,10 @@ static bool translateValue(STranslateContext* pCxt, SValueNode* pVal) {
tfree(tmp);
break;
}
case TSDB_DATA_TYPE_JSON:
case TSDB_DATA_TYPE_DECIMAL:
case TSDB_DATA_TYPE_BLOB:
// todo
default:
break;
}
......@@ -540,12 +597,16 @@ static bool translateOperator(STranslateContext* pCxt, SOperatorNode* pOp) {
}
static bool translateFunction(STranslateContext* pCxt, SFunctionNode* pFunc) {
int32_t code = fmGetFuncResultType(pCxt->fmgt, pFunc);
if (TSDB_CODE_SUCCESS != fmGetFuncInfo(pCxt->fmgt, pFunc->functionName, &pFunc->funcId, &pFunc->funcType)) {
generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_INVALID_FUNTION, pFunc->functionName);
return false;
}
int32_t code = fmGetFuncResultType(pFunc);
if (TSDB_CODE_SUCCESS != code) {
generateSyntaxErrMsg(pCxt, code, pFunc->functionName);
return false;
}
if (fmIsAggFunc(pFunc->funcId) && (SQL_CLAUSE_FROM == pCxt->currClause || SQL_CLAUSE_WHERE == pCxt->currClause)) {
if (fmIsAggFunc(pFunc->funcId) && afterGroupBy(pCxt->currClause)) {
generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION);
return false;
}
......@@ -620,11 +681,6 @@ static int32_t translateTable(STranslateContext* pCxt, SNode* pTable) {
return code;
}
static int32_t translateFrom(STranslateContext* pCxt, SNode* pTable) {
pCxt->currClause = SQL_CLAUSE_FROM;
return translateTable(pCxt, pTable);
}
static int32_t translateStar(STranslateContext* pCxt, SSelectStmt* pSelect, bool* pIsSelectStar) {
if (NULL == pSelect->pProjectionList) { // select * ...
SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel);
......@@ -641,21 +697,149 @@ static int32_t translateStar(STranslateContext* pCxt, SSelectStmt* pSelect, bool
return TSDB_CODE_SUCCESS;
}
static int32_t getPositionValue(const SValueNode* pVal) {
switch (pVal->node.resType.type) {
case TSDB_DATA_TYPE_NULL:
case TSDB_DATA_TYPE_BINARY:
case TSDB_DATA_TYPE_TIMESTAMP:
case TSDB_DATA_TYPE_NCHAR:
case TSDB_DATA_TYPE_VARCHAR:
case TSDB_DATA_TYPE_VARBINARY:
case TSDB_DATA_TYPE_JSON:
return -1;
case TSDB_DATA_TYPE_BOOL:
return (pVal->datum.b ? 1 : 0);
case TSDB_DATA_TYPE_TINYINT:
case TSDB_DATA_TYPE_SMALLINT:
case TSDB_DATA_TYPE_INT:
case TSDB_DATA_TYPE_BIGINT:
return pVal->datum.i;
case TSDB_DATA_TYPE_FLOAT:
case TSDB_DATA_TYPE_DOUBLE:
return pVal->datum.d;
case TSDB_DATA_TYPE_UTINYINT:
case TSDB_DATA_TYPE_USMALLINT:
case TSDB_DATA_TYPE_UINT:
case TSDB_DATA_TYPE_UBIGINT:
return pVal->datum.u;
default:
break;
}
return -1;
}
static bool translateOrderByPosition(STranslateContext* pCxt, SNodeList* pProjectionList, SNodeList* pOrderByList, bool* pOther) {
*pOther = false;
SNode* pNode;
FOREACH(pNode, pOrderByList) {
if (QUERY_NODE_VALUE == nodeType(pNode)) {
SValueNode* pVal = (SValueNode*)pNode;
if (translateValue(pCxt, pVal)) {
return false;
}
int32_t pos = getPositionValue((SValueNode*)pNode);
if (pos < 0) {
ERASE_NODE(pOrderByList);
nodesDestroyNode(pNode);
continue;
} else if (0 == pos || pos > LIST_LENGTH(pProjectionList)) {
generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT);
return false;
} else {
SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
setColumnInfoByExpr(NULL, (SExprNode*)nodesListGetNode(pProjectionList, pos), pCol);
REPLACE_NODE(pCol);
nodesDestroyNode(pNode);
}
} else {
*pOther = true;
}
}
return true;
}
static int32_t translateOrderBy(STranslateContext* pCxt, SNodeList* pProjectionList, SNodeList* pOrderByList) {
bool other;
if (!translateOrderByPosition(pCxt, pProjectionList, pOrderByList, &other)) {
return pCxt->errCode;
}
if (!other) {
return TSDB_CODE_SUCCESS;
}
pCxt->currClause = SQL_CLAUSE_ORDER_BY;
pCxt->pExt = pProjectionList;
return translateExprList(pCxt, pOrderByList);
}
static int32_t translateSelectList(STranslateContext* pCxt, SSelectStmt* pSelect) {
bool isSelectStar = false;
int32_t code = translateStar(pCxt, pSelect, &isSelectStar);
if (TSDB_CODE_SUCCESS == code && !isSelectStar) {
pCxt->currClause = SQL_CLAUSE_SELECT;
code = translateExprList(pCxt, pSelect->pProjectionList);
}
return code;
}
static int32_t translateHaving(STranslateContext* pCxt, SNode* pHaving) {
pCxt->currClause = SQL_CLAUSE_HAVING;
return translateExpr(pCxt, pHaving);
}
static int32_t translateGroupBy(STranslateContext* pCxt, SNodeList* pGroupByList) {
pCxt->currClause = SQL_CLAUSE_GROUP_BY;
return translateExprList(pCxt, pGroupByList);
}
static int32_t translateWindow(STranslateContext* pCxt, SNode* pWindow) {
pCxt->currClause = SQL_CLAUSE_WINDOW;
return translateExpr(pCxt, pWindow);
}
static int32_t translatePartitionBy(STranslateContext* pCxt, SNodeList* pPartitionByList) {
pCxt->currClause = SQL_CLAUSE_PARTITION_BY;
return translateExprList(pCxt, pPartitionByList);
}
static int32_t translateWhere(STranslateContext* pCxt, SNode* pWhere) {
pCxt->currClause = SQL_CLAUSE_WHERE;
return translateExpr(pCxt, pWhere);
}
static int32_t translateFrom(STranslateContext* pCxt, SNode* pTable) {
pCxt->currClause = SQL_CLAUSE_FROM;
return translateTable(pCxt, pTable);
}
// typedef struct SSelectStmt {
// bool isDistinct;
// SNode* pLimit;
// SNode* pSlimit;
// } SSelectStmt;
static int32_t translateSelect(STranslateContext* pCxt, SSelectStmt* pSelect) {
int32_t code = TSDB_CODE_SUCCESS;
code = translateFrom(pCxt, pSelect->pFromTable);
if (TSDB_CODE_SUCCESS == code) {
code = translateExpr(pCxt, pSelect->pWhere);
code = translateWhere(pCxt, pSelect->pWhere);
}
if (TSDB_CODE_SUCCESS == code) {
code = translateExprList(pCxt, pSelect->pGroupByList);
code = translatePartitionBy(pCxt, pSelect->pPartitionByList);
}
bool isSelectStar = false;
if (TSDB_CODE_SUCCESS == code) {
code = translateStar(pCxt, pSelect, &isSelectStar);
code = translateWindow(pCxt, pSelect->pWindow);
}
if (TSDB_CODE_SUCCESS == code && !isSelectStar) {
code = translateExprList(pCxt, pSelect->pProjectionList);
if (TSDB_CODE_SUCCESS == code) {
code = translateGroupBy(pCxt, pSelect->pGroupByList);
}
if (TSDB_CODE_SUCCESS == code) {
code = translateHaving(pCxt, pSelect->pHaving);
}
if (TSDB_CODE_SUCCESS == code) {
code = translateSelectList(pCxt, pSelect);
}
if (TSDB_CODE_SUCCESS == code) {
code = translateOrderBy(pCxt, pSelect->pProjectionList, pSelect->pOrderByList);
}
// printf("%s:%d code = %d\n", __FUNCTION__, __LINE__, code);
return code;
......@@ -676,9 +860,11 @@ static int32_t translateQuery(STranslateContext* pCxt, SNode* pNode) {
static int32_t translateSubquery(STranslateContext* pCxt, SNode* pNode) {
++(pCxt->currLevel);
ESqlClause currClause = pCxt->currClause;
void* pExt = pCxt->pExt;
int32_t code = translateQuery(pCxt, pNode);
--(pCxt->currLevel);
pCxt->currClause = currClause;
pCxt->pExt = pExt;
return code;
}
......@@ -691,5 +877,13 @@ int32_t doTranslate(SParseContext* pParseCxt, SQuery* pQuery) {
.currLevel = 0,
.currClause = 0
};
int32_t code = fmFuncMgtInit();
if (TSDB_CODE_SUCCESS != code) {
return code;
}
code = fmGetHandle(&cxt.fmgt);
if (TSDB_CODE_SUCCESS != code) {
return code;
}
return translateQuery(&cxt, pQuery->pRoot);
}
......@@ -13,6 +13,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <string>
#include <gtest/gtest.h>
#include "parserImpl.h"
......@@ -30,12 +33,11 @@ protected:
void bind(const char* sql) {
reset();
cxt_.acctId = atoi(acctId_.c_str());
cxt_.db = (char*) db_.c_str();
strcpy(sqlBuf_, sql);
cxt_.db = db_.c_str();
sqlBuf_ = string(sql);
transform(sqlBuf_.begin(), sqlBuf_.end(), sqlBuf_.begin(), ::tolower);
cxt_.sqlLen = strlen(sql);
sqlBuf_[cxt_.sqlLen] = '\0';
cxt_.pSql = sqlBuf_;
cxt_.pSql = sqlBuf_.c_str();
}
bool run(int32_t parseCode = TSDB_CODE_SUCCESS, int32_t translateCode = TSDB_CODE_SUCCESS) {
......@@ -68,7 +70,6 @@ protected:
private:
static const int max_err_len = 1024;
static const int max_sql_len = 1024 * 1024;
string dataTypeToStr(const SDataType& dt) {
switch (dt.type) {
......@@ -409,7 +410,7 @@ private:
string acctId_;
string db_;
char errMagBuf_[max_err_len];
char sqlBuf_[max_sql_len];
string sqlBuf_;
SParseContext cxt_;
SQuery query_;
};
......@@ -450,6 +451,16 @@ TEST_F(NewParserTest, selectExpression) {
ASSERT_TRUE(run());
}
TEST_F(NewParserTest, selectClause) {
setDatabase("root", "test");
bind("SELECT count(*) cnt FROM t1 WHERE c1 > 0 GROUP BY c2 ORDER BY cnt");
ASSERT_TRUE(run());
bind("SELECT count(*) cnt FROM t1 WHERE c1 > 0 GROUP BY c2 ORDER BY 1");
ASSERT_TRUE(run());
}
TEST_F(NewParserTest, selectSyntaxError) {
setDatabase("root", "test");
......@@ -477,4 +488,7 @@ TEST_F(NewParserTest, selectSemanticError) {
bind("SELECT c2 FROM t1 tt1, t1 tt2 WHERE tt1.c1 = tt2.c1");
ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_FAILED));
bind("SELECT c2 FROM t1 where count(*) > 0");
ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_FAILED));
}
......@@ -119,6 +119,16 @@ SNodeList* nodesListAppend(SNodeList* pList, SNode* pNode) {
return pList;
}
SNode* nodesListGetNode(SNodeList* pList, int32_t index) {
SNode* node;
FOREACH(node, pList) {
if (0 == index--) {
return node;
}
}
return NULL;
}
void nodesDestroyList(SNodeList* pList) {
SNode* node;
FOREACH(node, pList) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册