提交 55bb9cce 编写于 作者: X Xiaoyu Wang

TD-13197 SELECT statement syntax definition

上级 9e1e32d3
...@@ -209,48 +209,76 @@ ...@@ -209,48 +209,76 @@
#define NEW_TK_OR 1 #define NEW_TK_OR 1
#define NEW_TK_AND 2 #define NEW_TK_AND 2
#define NEW_TK_NOT 3 #define NEW_TK_UNION 3
#define NEW_TK_UNION 4 #define NEW_TK_ALL 4
#define NEW_TK_ALL 5 #define NEW_TK_MINUS 5
#define NEW_TK_MINUS 6 #define NEW_TK_EXCEPT 6
#define NEW_TK_EXCEPT 7 #define NEW_TK_INTERSECT 7
#define NEW_TK_INTERSECT 8 #define NEW_TK_NK_PLUS 8
#define NEW_TK_NK_PLUS 9 #define NEW_TK_NK_MINUS 9
#define NEW_TK_NK_MINUS 10 #define NEW_TK_NK_STAR 10
#define NEW_TK_NK_STAR 11 #define NEW_TK_NK_SLASH 11
#define NEW_TK_NK_SLASH 12 #define NEW_TK_NK_REM 12
#define NEW_TK_SHOW 13 #define NEW_TK_SHOW 13
#define NEW_TK_DATABASES 14 #define NEW_TK_DATABASES 14
#define NEW_TK_NK_INTEGER 15 #define NEW_TK_NK_INTEGER 15
#define NEW_TK_NK_FLOAT 16 #define NEW_TK_NK_FLOAT 16
#define NEW_TK_NK_STRING 17 #define NEW_TK_NK_STRING 17
#define NEW_TK_NK_BOOL 18 #define NEW_TK_NK_BOOL 18
#define NEW_TK_NK_NOW 19 #define NEW_TK_TIMESTAMP 19
#define NEW_TK_NK_ID 20 #define NEW_TK_NK_VARIABLE 20
#define NEW_TK_NK_QUESTION 21 #define NEW_TK_NK_COMMA 21
#define NEW_TK_NK_LP 22 #define NEW_TK_NK_ID 22
#define NEW_TK_NK_RP 23 #define NEW_TK_NK_LP 23
#define NEW_TK_NK_DOT 24 #define NEW_TK_NK_RP 24
#define NEW_TK_FROM 25 #define NEW_TK_NK_DOT 25
#define NEW_TK_NK_COMMA 26 #define NEW_TK_BETWEEN 26
#define NEW_TK_AS 27 #define NEW_TK_NOT 27
#define NEW_TK_JOIN 28 #define NEW_TK_IS 28
#define NEW_TK_ON 29 #define NEW_TK_NULL 29
#define NEW_TK_INNER 30 #define NEW_TK_NK_LT 30
#define NEW_TK_SELECT 31 #define NEW_TK_NK_GT 31
#define NEW_TK_DISTINCT 32 #define NEW_TK_NK_LE 32
#define NEW_TK_ORDER 33 #define NEW_TK_NK_GE 33
#define NEW_TK_BY 34 #define NEW_TK_NK_NE 34
#define NEW_TK_SLIMIT 35 #define NEW_TK_NK_EQ 35
#define NEW_TK_SOFFSET 36 #define NEW_TK_LIKE 36
#define NEW_TK_LIMIT 37 #define NEW_TK_MATCH 37
#define NEW_TK_OFFSET 38 #define NEW_TK_NMATCH 38
#define NEW_TK_NK_LR 39 #define NEW_TK_IN 39
#define NEW_TK_ASC 40 #define NEW_TK_FROM 40
#define NEW_TK_DESC 41 #define NEW_TK_AS 41
#define NEW_TK_NULLS 42 #define NEW_TK_JOIN 42
#define NEW_TK_FIRST 43 #define NEW_TK_ON 43
#define NEW_TK_LAST 44 #define NEW_TK_INNER 44
#define NEW_TK_SELECT 45
#define NEW_TK_DISTINCT 46
#define NEW_TK_WHERE 47
#define NEW_TK_PARTITION 48
#define NEW_TK_BY 49
#define NEW_TK_SESSION 50
#define NEW_TK_STATE_WINDOW 51
#define NEW_TK_INTERVAL 52
#define NEW_TK_SLIDING 53
#define NEW_TK_FILL 54
#define NEW_TK_VALUE 55
#define NEW_TK_NONE 56
#define NEW_TK_PREV 57
#define NEW_TK_LINEAR 58
#define NEW_TK_NEXT 59
#define NEW_TK_GROUP 60
#define NEW_TK_HAVING 61
#define NEW_TK_ORDER 62
#define NEW_TK_SLIMIT 63
#define NEW_TK_SOFFSET 64
#define NEW_TK_LIMIT 65
#define NEW_TK_OFFSET 66
#define NEW_TK_NK_LR 67
#define NEW_TK_ASC 68
#define NEW_TK_DESC 69
#define NEW_TK_NULLS 70
#define NEW_TK_FIRST 71
#define NEW_TK_LAST 72
#define TK_SPACE 300 #define TK_SPACE 300
#define TK_COMMENT 301 #define TK_COMMENT 301
......
...@@ -51,6 +51,8 @@ typedef enum ENodeType { ...@@ -51,6 +51,8 @@ typedef enum ENodeType {
QUERY_NODE_STATE_WINDOW, QUERY_NODE_STATE_WINDOW,
QUERY_NODE_SESSION_WINDOW, QUERY_NODE_SESSION_WINDOW,
QUERY_NODE_INTERVAL_WINDOW, QUERY_NODE_INTERVAL_WINDOW,
QUERY_NODE_NODE_LIST,
QUERY_NODE_FILL,
QUERY_NODE_SET_OPERATOR, QUERY_NODE_SET_OPERATOR,
QUERY_NODE_SELECT_STMT, QUERY_NODE_SELECT_STMT,
...@@ -157,14 +159,19 @@ typedef struct SLogicConditionNode { ...@@ -157,14 +159,19 @@ typedef struct SLogicConditionNode {
typedef struct SIsNullCondNode { typedef struct SIsNullCondNode {
ENodeType type; // QUERY_NODE_IS_NULL_CONDITION ENodeType type; // QUERY_NODE_IS_NULL_CONDITION
SNode* pExpr; SNode* pExpr;
bool isNot; bool isNull;
} SIsNullCondNode; } SIsNullCondNode;
typedef struct SNodeListNode {
ENodeType type; // QUERY_NODE_NODE_LIST
SNodeList* pNodeList;
} SNodeListNode;
typedef struct SFunctionNode { typedef struct SFunctionNode {
SExprNode type; // QUERY_NODE_FUNCTION SExprNode type; // QUERY_NODE_FUNCTION
char functionName[TSDB_FUNC_NAME_LEN]; char functionName[TSDB_FUNC_NAME_LEN];
int32_t funcId; int32_t funcId;
SNodeList* pParameterList; // SNode SNodeList* pParameterList;
} SFunctionNode; } SFunctionNode;
typedef struct STableNode { typedef struct STableNode {
...@@ -242,11 +249,27 @@ typedef struct SSessionWindowNode { ...@@ -242,11 +249,27 @@ typedef struct SSessionWindowNode {
typedef struct SIntervalWindowNode { typedef struct SIntervalWindowNode {
ENodeType type; // QUERY_NODE_INTERVAL_WINDOW ENodeType type; // QUERY_NODE_INTERVAL_WINDOW
int64_t interval; SNode* pInterval; // SValueNode
int64_t sliding; SNode* pOffset; // SValueNode
int64_t offset; SNode* pSliding; // SValueNode
SNode* pFill;
} SIntervalWindowNode; } SIntervalWindowNode;
typedef enum EFillMode {
FILL_MODE_NONE = 1,
FILL_MODE_VALUE,
FILL_MODE_PREV,
FILL_MODE_NULL,
FILL_MODE_LINEAR,
FILL_MODE_NEXT
} EFillMode;
typedef struct SFillNode {
ENodeType type; // QUERY_NODE_FILL
EFillMode mode;
SNode* pValues; // SNodeListNode
} SFillNode;
typedef struct SSelectStmt { typedef struct SSelectStmt {
ENodeType type; // QUERY_NODE_SELECT_STMT ENodeType type; // QUERY_NODE_SELECT_STMT
bool isDistinct; bool isDistinct;
...@@ -259,8 +282,8 @@ typedef struct SSelectStmt { ...@@ -259,8 +282,8 @@ typedef struct SSelectStmt {
SNodeList* pGroupByList; // SGroupingSetNode SNodeList* pGroupByList; // SGroupingSetNode
SNode* pHaving; SNode* pHaving;
SNodeList* pOrderByList; // SOrderByExprNode SNodeList* pOrderByList; // SOrderByExprNode
SLimitNode* pLimit; SNode* pLimit;
SLimitNode* pSlimit; SNode* pSlimit;
} SSelectStmt; } SSelectStmt;
typedef enum ESetOperatorType { typedef enum ESetOperatorType {
......
...@@ -23,11 +23,6 @@ extern "C" { ...@@ -23,11 +23,6 @@ extern "C" {
#include "nodes.h" #include "nodes.h"
#include "parser.h" #include "parser.h"
typedef enum EResourceType {
AST_CXT_RESOURCE_NODE = 1,
AST_CXT_RESOURCE_NODE_LIST
} EResourceType;
typedef struct SAstCreateContext { typedef struct SAstCreateContext {
SParseContext* pQueryCxt; SParseContext* pQueryCxt;
bool notSupport; bool notSupport;
...@@ -39,9 +34,6 @@ typedef struct SAstCreateContext { ...@@ -39,9 +34,6 @@ typedef struct SAstCreateContext {
int32_t createAstCreateContext(SParseContext* pQueryCxt, SAstCreateContext* pCxt); int32_t createAstCreateContext(SParseContext* pQueryCxt, SAstCreateContext* pCxt);
int32_t destroyAstCreateContext(SAstCreateContext* pCxt); int32_t destroyAstCreateContext(SAstCreateContext* pCxt);
void* acquireRaii(SAstCreateContext* pCxt, EResourceType type, void* p);
void* releaseRaii(SAstCreateContext* pCxt, void* p);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -32,14 +32,25 @@ SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode ...@@ -32,14 +32,25 @@ SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode
SNode* createColumnNode(SAstCreateContext* pCxt, const SToken* pTableName, const SToken* pColumnName); SNode* createColumnNode(SAstCreateContext* pCxt, const SToken* pTableName, const SToken* pColumnName);
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral); SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral);
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral);
SNode* addMinusSign(SAstCreateContext* pCxt, SNode* pNode); SNode* addMinusSign(SAstCreateContext* pCxt, SNode* pNode);
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* pAlias); SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* pAlias);
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2); SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2);
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight);
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight);
SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight);
SNode* createIsNullCondNode(SAstCreateContext* pCxt, SNode* pExpr, bool isNull);
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList);
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList);
SNode* createRealTableNode(SAstCreateContext* pCxt, const SToken* pDbName, const SToken* pTableName, const SToken* pTableAlias); SNode* createRealTableNode(SAstCreateContext* pCxt, const SToken* pDbName, const SToken* pTableName, const SToken* pTableAlias);
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias); SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias);
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond); SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond);
SNode* createLimitNode(SAstCreateContext* pCxt, SNode* pLimit, SNode* pOffset); SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset);
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder); SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder);
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, const SToken* pVal);
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pCol);
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding, SNode* pFill);
SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues);
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere); SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere);
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList); SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList);
......
此差异已折叠。
...@@ -16,53 +16,14 @@ ...@@ -16,53 +16,14 @@
#include "ttoken.h" #include "ttoken.h"
#include "astCreateContext.h" #include "astCreateContext.h"
typedef struct SResourceEntry {
EResourceType type;
void* res;
} SResourceEntry;
int32_t createAstCreateContext(SParseContext* pQueryCxt, SAstCreateContext* pCxt) { int32_t createAstCreateContext(SParseContext* pQueryCxt, SAstCreateContext* pCxt) {
pCxt->pQueryCxt = pQueryCxt; pCxt->pQueryCxt = pQueryCxt;
pCxt->notSupport = false; pCxt->notSupport = false;
pCxt->valid = true; pCxt->valid = true;
pCxt->pRootNode = NULL; pCxt->pRootNode = NULL;
pCxt->pResourceHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, false);
if (NULL == pCxt->pResourceHash) {
return TSDB_CODE_TSC_OUT_OF_MEMORY;
}
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
int32_t destroyAstCreateContext(SAstCreateContext* pCxt) { int32_t destroyAstCreateContext(SAstCreateContext* pCxt) {
SResourceEntry* item = taosHashIterate(pCxt->pResourceHash, NULL); return TSDB_CODE_SUCCESS;
while (item) {
switch (item->type) {
case AST_CXT_RESOURCE_NODE:
nodesDestroyNode(item->res);
break;
case AST_CXT_RESOURCE_NODE_LIST:
nodesDestroyList(item->res);
break;
default:
tfree(item->res);
}
item = taosHashIterate(pCxt->pResourceHash, item);
}
}
void* acquireRaii(SAstCreateContext* pCxt, EResourceType type, void* p) {
if (NULL == p) {
return NULL;
}
SResourceEntry entry = { .type = type, .res = p };
taosHashPut(pCxt->pResourceHash, &p, POINTER_BYTES, &entry, sizeof(SResourceEntry));
return p;
}
void* releaseRaii(SAstCreateContext* pCxt, void* p) {
if (NULL == p) {
return NULL;
}
taosHashRemove(pCxt->pResourceHash, &p, POINTER_BYTES);
return p;
} }
...@@ -53,11 +53,11 @@ static bool checkColumnName(SAstCreateContext* pCxt, const SToken* pColumnName) ...@@ -53,11 +53,11 @@ static bool checkColumnName(SAstCreateContext* pCxt, const SToken* pColumnName)
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) { SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
SNodeList* list = nodesMakeList(); SNodeList* list = nodesMakeList();
CHECK_OUT_OF_MEM(list); CHECK_OUT_OF_MEM(list);
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE_LIST, nodesListAppend(list, releaseRaii(pCxt, pNode))); return nodesListAppend(list, pNode);
} }
SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) { SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
return nodesListAppend(pList, releaseRaii(pCxt, pNode)); return nodesListAppend(pList, pNode);
} }
SNode* createColumnNode(SAstCreateContext* pCxt, const SToken* pTableName, const SToken* pColumnName) { SNode* createColumnNode(SAstCreateContext* pCxt, const SToken* pTableName, const SToken* pColumnName) {
...@@ -70,14 +70,21 @@ SNode* createColumnNode(SAstCreateContext* pCxt, const SToken* pTableName, const ...@@ -70,14 +70,21 @@ SNode* createColumnNode(SAstCreateContext* pCxt, const SToken* pTableName, const
strncpy(col->tableName, pTableName->z, pTableName->n); strncpy(col->tableName, pTableName->z, pTableName->n);
} }
strncpy(col->colName, pColumnName->z, pColumnName->n); strncpy(col->colName, pColumnName->z, pColumnName->n);
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE, col); return (SNode*)col;
} }
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) { SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE); SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
CHECK_OUT_OF_MEM(val); CHECK_OUT_OF_MEM(val);
// strncpy(col->colName, pColumnName->z, pColumnName->n); // todo
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE, val); return (SNode*)val;
}
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
CHECK_OUT_OF_MEM(val);
// todo
return (SNode*)val;
} }
SNode* addMinusSign(SAstCreateContext* pCxt, SNode* pNode) { SNode* addMinusSign(SAstCreateContext* pCxt, SNode* pNode) {
...@@ -89,9 +96,51 @@ SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType typ ...@@ -89,9 +96,51 @@ SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType typ
CHECK_OUT_OF_MEM(cond); CHECK_OUT_OF_MEM(cond);
cond->condType = type; cond->condType = type;
cond->pParameterList = nodesMakeList(); cond->pParameterList = nodesMakeList();
nodesListAppend(cond->pParameterList, releaseRaii(pCxt, pParam1)); nodesListAppend(cond->pParameterList, pParam1);
nodesListAppend(cond->pParameterList, releaseRaii(pCxt, pParam2)); nodesListAppend(cond->pParameterList, pParam2);
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE, cond); return (SNode*)cond;
}
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
SOperatorNode* op = (SOperatorNode*)nodesMakeNode(QUERY_NODE_OPERATOR);
CHECK_OUT_OF_MEM(op);
op->opType = type;
op->pLeft = pLeft;
op->pRight = pRight;
return (SNode*)op;
}
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND,
createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft), createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, pExpr, pRight));
}
SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR,
createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft), createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, pExpr, pRight));
}
SNode* createIsNullCondNode(SAstCreateContext* pCxt, SNode* pExpr, bool isNull) {
SIsNullCondNode* cond = (SIsNullCondNode*)nodesMakeNode(QUERY_NODE_IS_NULL_CONDITION);
CHECK_OUT_OF_MEM(cond);
cond->pExpr = pExpr;
cond->isNull = isNull;
return (SNode*)cond;
}
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
CHECK_OUT_OF_MEM(func);
strncpy(func->functionName, pFuncName->z, pFuncName->n);
func->pParameterList = pParameterList;
return (SNode*)func;
}
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
CHECK_OUT_OF_MEM(list);
list->pNodeList = pList;
return (SNode*)list;
} }
SNode* createRealTableNode(SAstCreateContext* pCxt, const SToken* pDbName, const SToken* pTableName, const SToken* pTableAlias) { SNode* createRealTableNode(SAstCreateContext* pCxt, const SToken* pDbName, const SToken* pTableName, const SToken* pTableAlias) {
...@@ -104,41 +153,74 @@ SNode* createRealTableNode(SAstCreateContext* pCxt, const SToken* pDbName, const ...@@ -104,41 +153,74 @@ SNode* createRealTableNode(SAstCreateContext* pCxt, const SToken* pDbName, const
strncpy(realTable->dbName, pDbName->z, pDbName->n); strncpy(realTable->dbName, pDbName->z, pDbName->n);
} }
strncpy(realTable->table.tableName, pTableName->z, pTableName->n); strncpy(realTable->table.tableName, pTableName->z, pTableName->n);
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE, realTable); return (SNode*)realTable;
} }
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias) { SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias) {
STempTableNode* tempTable = (STempTableNode*)nodesMakeNode(QUERY_NODE_TEMP_TABLE); STempTableNode* tempTable = (STempTableNode*)nodesMakeNode(QUERY_NODE_TEMP_TABLE);
CHECK_OUT_OF_MEM(tempTable); CHECK_OUT_OF_MEM(tempTable);
tempTable->pSubquery = pSubquery; tempTable->pSubquery = pSubquery;
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE, tempTable); return (SNode*)tempTable;
} }
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) { SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) {
SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE); SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE);
CHECK_OUT_OF_MEM(joinTable); CHECK_OUT_OF_MEM(joinTable);
joinTable->joinType = type; joinTable->joinType = type;
joinTable->pLeft = releaseRaii(pCxt, pLeft); joinTable->pLeft = pLeft;
joinTable->pRight = releaseRaii(pCxt, pRight); joinTable->pRight = pRight;
joinTable->pOnCond = pJoinCond; joinTable->pOnCond = pJoinCond;
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE, joinTable); return (SNode*)joinTable;
} }
SNode* createLimitNode(SAstCreateContext* pCxt, SNode* pLimit, SNode* pOffset) { SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset) {
SLimitNode* limitNode = (SLimitNode*)nodesMakeNode(QUERY_NODE_LIMIT); SLimitNode* limitNode = (SLimitNode*)nodesMakeNode(QUERY_NODE_LIMIT);
CHECK_OUT_OF_MEM(limitNode); CHECK_OUT_OF_MEM(limitNode);
// limitNode->limit = limit; // limitNode->limit = limit;
// limitNode->offset = offset; // limitNode->offset = offset;
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE, limitNode); return (SNode*)limitNode;
} }
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) { SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR); SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR);
CHECK_OUT_OF_MEM(orderByExpr); CHECK_OUT_OF_MEM(orderByExpr);
orderByExpr->pExpr = releaseRaii(pCxt, pExpr); orderByExpr->pExpr = pExpr;
orderByExpr->order = order; orderByExpr->order = order;
orderByExpr->nullOrder = nullOrder; orderByExpr->nullOrder = nullOrder;
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE, orderByExpr); return (SNode*)orderByExpr;
}
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, const SToken* pVal) {
SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW);
CHECK_OUT_OF_MEM(session);
session->pCol = pCol;
// session->gap = getInteger(pVal);
return (SNode*)session;
}
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pCol) {
SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
CHECK_OUT_OF_MEM(state);
state->pCol = pCol;
return (SNode*)state;
}
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding, SNode* pFill) {
SIntervalWindowNode* interval = (SIntervalWindowNode*)nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW);
CHECK_OUT_OF_MEM(interval);
interval->pInterval = pInterval;
interval->pOffset = pOffset;
interval->pSliding = pSliding;
interval->pFill = pFill;
return (SNode*)interval;
}
SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
SFillNode* fill = (SFillNode*)nodesMakeNode(QUERY_NODE_FILL);
CHECK_OUT_OF_MEM(fill);
fill->mode = mode;
fill->pValues = pValues;
return (SNode*)fill;
} }
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* pAlias) { SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* pAlias) {
...@@ -148,56 +230,56 @@ SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* p ...@@ -148,56 +230,56 @@ SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* p
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) { SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) { if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
((SSelectStmt*)pStmt)->pWhere = releaseRaii(pCxt, pWhere); ((SSelectStmt*)pStmt)->pWhere = pWhere;
} }
return pStmt; return pStmt;
} }
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) { SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) { if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
((SSelectStmt*)pStmt)->pPartitionByList = releaseRaii(pCxt, pPartitionByList); ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
} }
return pStmt; return pStmt;
} }
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) { SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) { if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
((SSelectStmt*)pStmt)->pWindow = releaseRaii(pCxt, pWindow); ((SSelectStmt*)pStmt)->pWindow = pWindow;
} }
return pStmt; return pStmt;
} }
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) { SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) { if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
((SSelectStmt*)pStmt)->pGroupByList = releaseRaii(pCxt, pGroupByList); ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
} }
return pStmt; return pStmt;
} }
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) { SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) { if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
((SSelectStmt*)pStmt)->pHaving = releaseRaii(pCxt, pHaving); ((SSelectStmt*)pStmt)->pHaving = pHaving;
} }
return pStmt; return pStmt;
} }
SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) { SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) { if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
((SSelectStmt*)pStmt)->pOrderByList = releaseRaii(pCxt, pOrderByList); ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
} }
return pStmt; return pStmt;
} }
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) { SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) { if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
((SSelectStmt*)pStmt)->pSlimit = releaseRaii(pCxt, pSlimit); ((SSelectStmt*)pStmt)->pSlimit = pSlimit;
} }
return pStmt; return pStmt;
} }
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) { SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) { if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
((SSelectStmt*)pStmt)->pLimit = releaseRaii(pCxt, pLimit); ((SSelectStmt*)pStmt)->pLimit = pLimit;
} }
return pStmt; return pStmt;
} }
...@@ -209,23 +291,23 @@ SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pPr ...@@ -209,23 +291,23 @@ SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pPr
if (NULL == pProjectionList) { if (NULL == pProjectionList) {
select->isStar = true; select->isStar = true;
} }
select->pProjectionList = releaseRaii(pCxt, pProjectionList); select->pProjectionList = pProjectionList;
select->pFromTable = releaseRaii(pCxt, pTable); select->pFromTable = pTable;
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE, select); return (SNode*)select;
} }
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) { SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR); SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
CHECK_OUT_OF_MEM(setOp); CHECK_OUT_OF_MEM(setOp);
setOp->opType = type; setOp->opType = type;
setOp->pLeft = releaseRaii(pCxt, pLeft); setOp->pLeft = pLeft;
setOp->pRight = releaseRaii(pCxt, pRight); setOp->pRight = pRight;
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE, setOp); return (SNode*)setOp;
} }
SNode* createShowStmt(SAstCreateContext* pCxt, EShowStmtType type) { SNode* createShowStmt(SAstCreateContext* pCxt, EShowStmtType type) {
SShowStmt* show = (SShowStmt*)nodesMakeNode(QUERY_NODE_SHOW_STMT); SShowStmt* show = (SShowStmt*)nodesMakeNode(QUERY_NODE_SHOW_STMT);
CHECK_OUT_OF_MEM(show); CHECK_OUT_OF_MEM(show);
show->showType = type; show->showType = type;
return acquireRaii(pCxt, AST_CXT_RESOURCE_NODE, show); return (SNode*)show;
} }
此差异已折叠。
...@@ -86,15 +86,15 @@ int32_t doParse(SParseContext* pParseCxt, SQuery* pQuery) { ...@@ -86,15 +86,15 @@ int32_t doParse(SParseContext* pParseCxt, SQuery* pQuery) {
int32_t i = 0; int32_t i = 0;
while (1) { while (1) {
SToken t0 = {0}; SToken t0 = {0};
printf("===========================\n"); // printf("===========================\n");
if (cxt.pQueryCxt->pSql[i] == 0) { if (cxt.pQueryCxt->pSql[i] == 0) {
NewParse(pParser, 0, t0, &cxt); NewParse(pParser, 0, t0, &cxt);
goto abort_parse; goto abort_parse;
} }
printf("input: [%s]\n", cxt.pQueryCxt->pSql + i); // printf("input: [%s]\n", cxt.pQueryCxt->pSql + i);
t0.n = getToken((char *)&cxt.pQueryCxt->pSql[i], &t0.type); t0.n = getToken((char *)&cxt.pQueryCxt->pSql[i], &t0.type);
t0.z = (char *)(cxt.pQueryCxt->pSql + i); t0.z = (char *)(cxt.pQueryCxt->pSql + i);
printf("token : %d %d [%s]\n", t0.type, t0.n, t0.z); // printf("token : %d %d [%s]\n", t0.type, t0.n, t0.z);
i += t0.n; i += t0.n;
switch (t0.type) { switch (t0.type) {
...@@ -131,7 +131,7 @@ int32_t doParse(SParseContext* pParseCxt, SQuery* pQuery) { ...@@ -131,7 +131,7 @@ int32_t doParse(SParseContext* pParseCxt, SQuery* pQuery) {
} }
abort_parse: abort_parse:
printf("doParse completed.\n"); // printf("doParse completed.\n");
NewParseFree(pParser, free); NewParseFree(pParser, free);
destroyAstCreateContext(&cxt); destroyAstCreateContext(&cxt);
pQuery->pRoot = cxt.pRootNode; pQuery->pRoot = cxt.pRootNode;
......
...@@ -41,19 +41,10 @@ protected: ...@@ -41,19 +41,10 @@ protected:
bool run(int32_t expectCode = TSDB_CODE_SUCCESS) { bool run(int32_t expectCode = TSDB_CODE_SUCCESS) {
int32_t code = doParse(&cxt_, &query_); int32_t code = doParse(&cxt_, &query_);
if (code != TSDB_CODE_SUCCESS) { if (code != TSDB_CODE_SUCCESS) {
cout << "code:" << tstrerror(code) << ", msg:" << errMagBuf_ << endl; cout << "sql:[" << cxt_.pSql << "] code:" << tstrerror(code) << ", msg:" << errMagBuf_ << endl;
return (code == expectCode); return (code == expectCode);
} }
cout << nodeType(query_.pRoot) << endl;
if (NULL != query_.pRoot && QUERY_NODE_SELECT_STMT == nodeType(query_.pRoot)) { if (NULL != query_.pRoot && QUERY_NODE_SELECT_STMT == nodeType(query_.pRoot)) {
// SNode* pWhereCond;
// SNodeList* pPartitionByList; // SNode
// SNode* pWindowClause;
// SNodeList* pGroupByList; // SGroupingSetNode
// SNodeList* pOrderByList; // SOrderByExprNode
// SLimitNode limit;
// SLimitNode slimit;
SSelectStmt* select = (SSelectStmt*)query_.pRoot; SSelectStmt* select = (SSelectStmt*)query_.pRoot;
string sql("SELECT "); string sql("SELECT ");
if (select->isDistinct) { if (select->isDistinct) {
...@@ -68,14 +59,6 @@ protected: ...@@ -68,14 +59,6 @@ protected:
tableToSql(select->pFromTable, sql); tableToSql(select->pFromTable, sql);
cout << sql << endl; cout << sql << endl;
} }
// char* pStr = NULL;
// int32_t len = 0;
// code = nodesNodeToString(query_.pRoot, &pStr, &len);
// if (code != TSDB_CODE_SUCCESS) {
// cout << "code:" << tstrerror(code) << ", msg:" << errMagBuf_ << endl;
// return code;
// }
// cout << "node tree:\n" << pStr << endl;
return (code == expectCode); return (code == expectCode);
} }
...@@ -85,7 +68,6 @@ private: ...@@ -85,7 +68,6 @@ private:
void tableToSql(const SNode* node, string& sql) { void tableToSql(const SNode* node, string& sql) {
const STableNode* table = (const STableNode*)node; const STableNode* table = (const STableNode*)node;
cout << "node : " << nodeType(node) << endl;
switch (nodeType(node)) { switch (nodeType(node)) {
case QUERY_NODE_REAL_TABLE: { case QUERY_NODE_REAL_TABLE: {
SRealTableNode* realTable = (SRealTableNode*)table; SRealTableNode* realTable = (SRealTableNode*)table;
...@@ -108,12 +90,14 @@ private: ...@@ -108,12 +90,14 @@ private:
if (!firstNode) { if (!firstNode) {
sql.append(", "); sql.append(", ");
} }
firstNode = false;
switch (nodeType(node)) { switch (nodeType(node)) {
case QUERY_NODE_COLUMN: case QUERY_NODE_COLUMN:
sql.append(((SColumnNode*)node)->colName); sql.append(((SColumnNode*)node)->colName);
break; break;
} }
} }
sql.append(" ");
} }
void reset() { void reset() {
...@@ -140,6 +124,12 @@ TEST_F(NewParserTest, selectStar) { ...@@ -140,6 +124,12 @@ TEST_F(NewParserTest, selectStar) {
bind("SELECT * FROM test.t1"); bind("SELECT * FROM test.t1");
ASSERT_TRUE(run()); ASSERT_TRUE(run());
bind("SELECT ts FROM t1");
ASSERT_TRUE(run());
bind("SELECT ts, tag1, c1 FROM t1");
ASSERT_TRUE(run());
} }
TEST_F(NewParserTest, syntaxError) { TEST_F(NewParserTest, syntaxError) {
......
...@@ -91,7 +91,7 @@ static bool logicConditionNodeEqual(const SLogicConditionNode* a, const SLogicCo ...@@ -91,7 +91,7 @@ static bool logicConditionNodeEqual(const SLogicConditionNode* a, const SLogicCo
static bool isNullConditionNodeEqual(const SIsNullCondNode* a, const SIsNullCondNode* b) { static bool isNullConditionNodeEqual(const SIsNullCondNode* a, const SIsNullCondNode* b) {
COMPARE_NODE_FIELD(pExpr); COMPARE_NODE_FIELD(pExpr);
COMPARE_SCALAR_FIELD(isNot); COMPARE_SCALAR_FIELD(isNull);
return true; return true;
} }
......
...@@ -92,7 +92,12 @@ SNodeList* nodesListAppend(SNodeList* pList, SNode* pNode) { ...@@ -92,7 +92,12 @@ SNodeList* nodesListAppend(SNodeList* pList, SNode* pNode) {
return pList; return pList;
} }
p->pNode = pNode; p->pNode = pNode;
pList->pTail->pNext = p; if (NULL == pList->pHead) {
pList->pHead = p;
}
if (NULL != pList->pTail) {
pList->pTail->pNext = p;
}
pList->pTail = p; pList->pTail = p;
return pList; return pList;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册