From 768e05939cd6df96c610b07e54c3c3143105dcde Mon Sep 17 00:00:00 2001 From: Xiaoyu Wang Date: Mon, 28 Feb 2022 04:02:43 -0500 Subject: [PATCH] TD-13747 New SQL model integration --- include/libs/executor/dataSinkMgt.h | 3 +- include/libs/nodes/nodes.h | 9 +- include/libs/nodes/plannodes.h | 31 + include/libs/nodes/querynodes.h | 23 + include/libs/parser/newParser.h | 43 - include/libs/parser/parsenodes.h | 163 - include/libs/parser/parser.h | 33 + include/libs/planner/planner.h | 61 + include/libs/planner/plannerOp.h | 51 - include/libs/scheduler/scheduler.h | 6 +- source/client/CMakeLists.txt | 2 +- source/client/inc/clientInt.h | 4 +- source/client/src/clientEnv.c | 2 +- source/client/src/clientImpl.c | 95 +- source/client/src/tmq.c | 6 +- source/dnode/mnode/impl/src/mndSubscribe.c | 6 +- source/libs/executor/inc/dataSinkInt.h | 3 +- source/libs/executor/src/dataDispatcher.c | 18 +- source/libs/executor/src/dataSinkMgt.c | 4 +- source/libs/executor/src/executor.c | 2 +- source/libs/executor/src/executorimpl.c | 99 +- source/libs/parser/inc/astGenerator.h | 369 -- source/libs/parser/inc/astToMsg.h | 32 - source/libs/parser/inc/insertParser.h | 2 +- source/libs/parser/inc/parserImpl.h | 6 +- source/libs/parser/inc/parserInt.h | 113 - source/libs/parser/inc/parserUtil.h | 59 +- source/libs/parser/inc/queryInfoUtil.h | 50 - source/libs/parser/src/astGenerator.c | 1158 ----- source/libs/parser/src/astToMsg.c | 520 --- source/libs/parser/src/astValidate.c | 4002 ----------------- source/libs/parser/src/dCDAstProcess.c | 901 ---- source/libs/parser/src/dataBlockMgt.c | 3 +- source/libs/parser/src/insertParser.c | 301 +- source/libs/parser/src/parser.c | 34 + source/libs/parser/src/parserImpl.c | 29 +- source/libs/parser/src/parserUtil.c | 1910 +------- source/libs/parser/src/queryInfoUtil.c | 377 -- source/libs/parser/src/sql.c | 3698 --------------- source/libs/parser/test/insertParserTest.cpp | 29 +- source/libs/parser/test/newParserTest.cpp | 8 +- source/libs/parser/test/parserTests.cpp | 787 ---- source/libs/parser/test/plannerTest.cpp | 209 - source/libs/parser/test/tokenizerTest.cpp | 6 +- source/libs/planner/inc/plannerInt.h | 137 - source/libs/planner/inc/plannerUtil.h | 29 - source/libs/planner/src/logicPlan.c | 4 + source/libs/planner/src/physicalPlan.c | 4 + source/libs/planner/src/physicalPlanJson.c | 4 + source/libs/planner/src/planner.c | 80 +- source/libs/planner/src/plannerImpl.c | 34 +- source/libs/planner/src/plannerUtil.c | 6 - source/libs/planner/test/newPlannerTest.cpp | 10 +- source/libs/planner/test/phyPlanTests.cpp | 3 +- source/libs/planner/test/plannerTests.cpp | 2 +- source/libs/qworker/CMakeLists.txt | 2 +- source/libs/qworker/test/CMakeLists.txt | 2 +- source/libs/scheduler/CMakeLists.txt | 2 +- source/libs/scheduler/src/scheduler.c | 10 +- source/libs/scheduler/test/schedulerTests.cpp | 20 +- 60 files changed, 821 insertions(+), 14795 deletions(-) delete mode 100644 include/libs/parser/newParser.h delete mode 100644 include/libs/parser/parsenodes.h delete mode 100644 include/libs/planner/plannerOp.h delete mode 100644 source/libs/parser/inc/astGenerator.h delete mode 100644 source/libs/parser/inc/astToMsg.h delete mode 100644 source/libs/parser/inc/parserInt.h delete mode 100644 source/libs/parser/inc/queryInfoUtil.h delete mode 100644 source/libs/parser/src/astGenerator.c delete mode 100644 source/libs/parser/src/astToMsg.c delete mode 100644 source/libs/parser/src/astValidate.c delete mode 100644 source/libs/parser/src/dCDAstProcess.c delete mode 100644 source/libs/parser/src/queryInfoUtil.c delete mode 100644 source/libs/parser/src/sql.c delete mode 100644 source/libs/parser/test/parserTests.cpp delete mode 100644 source/libs/parser/test/plannerTest.cpp delete mode 100644 source/libs/planner/inc/plannerInt.h delete mode 100644 source/libs/planner/inc/plannerUtil.h delete mode 100644 source/libs/planner/src/plannerUtil.c diff --git a/include/libs/executor/dataSinkMgt.h b/include/libs/executor/dataSinkMgt.h index 19438b5dd4..ec15291700 100644 --- a/include/libs/executor/dataSinkMgt.h +++ b/include/libs/executor/dataSinkMgt.h @@ -23,6 +23,7 @@ extern "C" { #include "os.h" #include "thash.h" #include "executor.h" +#include "plannodes.h" #define DS_BUF_LOW 1 #define DS_BUF_FULL 2 @@ -59,7 +60,7 @@ typedef struct SOutputData { * @param pHandle output * @return error code */ -int32_t dsCreateDataSinker(const struct SDataSink *pDataSink, DataSinkHandle* pHandle); +int32_t dsCreateDataSinker(const SDataSinkNode* pDataSink, DataSinkHandle* pHandle); /** * Put the result set returned by the executor into datasinker. diff --git a/include/libs/nodes/nodes.h b/include/libs/nodes/nodes.h index 96815ac29f..1cbf5ab8e7 100644 --- a/include/libs/nodes/nodes.h +++ b/include/libs/nodes/nodes.h @@ -70,6 +70,7 @@ typedef enum ENodeType { QUERY_NODE_SET_OPERATOR, QUERY_NODE_SELECT_STMT, QUERY_NODE_SHOW_STMT, + QUERY_NODE_VNODE_MODIF_STMT, // logic plan node QUERY_NODE_LOGIC_PLAN_SCAN, @@ -80,9 +81,15 @@ typedef enum ENodeType { // physical plan node QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, + QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN, + QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, QUERY_NODE_PHYSICAL_PLAN_PROJECT, QUERY_NODE_PHYSICAL_PLAN_JOIN, - QUERY_NODE_PHYSICAL_PLAN_AGG + QUERY_NODE_PHYSICAL_PLAN_AGG, + QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, + QUERY_NODE_PHYSICAL_PLAN_SORT, + + QUERY_NODE_DSINK_DISPATCH } ENodeType; /** diff --git a/include/libs/nodes/plannodes.h b/include/libs/nodes/plannodes.h index 608146e3df..5d00e373cc 100644 --- a/include/libs/nodes/plannodes.h +++ b/include/libs/nodes/plannodes.h @@ -21,6 +21,7 @@ extern "C" { #endif #include "querynodes.h" +#include "query.h" #include "tmsg.h" typedef struct SLogicNode { @@ -78,6 +79,8 @@ typedef struct SDataBlockDescNode { ENodeType type; int16_t dataBlockId; SNodeList* pSlots; + int32_t resultRowSize; + int16_t precision; } SDataBlockDescNode; typedef struct SPhysiNode { @@ -129,6 +132,34 @@ typedef struct SAggPhysiNode { SNodeList* pAggFuncs; } SAggPhysiNode; +typedef struct SDownstreamSource { + SQueryNodeAddr addr; + uint64_t taskId; + uint64_t schedId; +} SDownstreamSource; + +typedef struct SExchangePhysiNode { + SPhysiNode node; + uint64_t srcTemplateId; // template id of datasource suplans + SArray* pSrcEndPoints; // SArray, scheduler fill by calling qSetSuplanExecutionNode +} SExchangePhysiNode; + +typedef struct SDataSinkNode { + ENodeType type;; + SDataBlockDescNode inputDataBlockDesc; +} SDataSinkNode; + +typedef struct SDataDispatcherNode { + SDataSinkNode sink; +} SDataDispatcherNode; + +typedef struct SDataInserterNode { + SDataSinkNode sink; + int32_t numOfTables; + uint32_t size; + char *pData; +} SDataInserterNode; + #ifdef __cplusplus } #endif diff --git a/include/libs/nodes/querynodes.h b/include/libs/nodes/querynodes.h index 42f9310ef1..4cc39325f7 100644 --- a/include/libs/nodes/querynodes.h +++ b/include/libs/nodes/querynodes.h @@ -21,6 +21,7 @@ extern "C" { #endif #include "nodes.h" +#include "tmsg.h" typedef struct SRawExprNode { ENodeType nodeType; @@ -248,6 +249,28 @@ typedef enum ESqlClause { SQL_CLAUSE_ORDER_BY } ESqlClause; + +typedef enum { + PAYLOAD_TYPE_KV = 0, + PAYLOAD_TYPE_RAW = 1, +} EPayloadType; + +typedef struct SVgDataBlocks { + SVgroupInfo vg; + int32_t numOfTables; // number of tables in current submit block + uint32_t size; + char *pData; // SMsgDesc + SSubmitReq + SSubmitBlk + ... +} SVgDataBlocks; + +typedef struct SVnodeModifOpStmt { + ENodeType nodeType; + SArray* pDataBlocks; // data block for each vgroup, SArray. + int8_t schemaAttache; // denote if submit block is built with table schema or not + uint8_t payloadType; // EPayloadType. 0: K-V payload for non-prepare insert, 1: rawPayload for prepare insert + uint32_t insertType; // insert data from [file|sql statement| bound statement] + const char* sql; // current sql statement position +} SVnodeModifOpStmt; + void nodesWalkSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeWalker walker, void* pContext); void nodesRewriteSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeRewriter rewriter, void* pContext); diff --git a/include/libs/parser/newParser.h b/include/libs/parser/newParser.h deleted file mode 100644 index fd631087db..0000000000 --- a/include/libs/parser/newParser.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 _TD_NEW_PARSER_H_ -#define _TD_NEW_PARSER_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include "parser.h" - -typedef enum EStmtType { - STMT_TYPE_CMD = 1, - STMT_TYPE_QUERY -} EStmtType; - -typedef struct SQuery { - EStmtType stmtType; - SNode* pRoot; - int32_t numOfResCols; - SSchema* pResSchema; -} SQuery; - -int32_t parser(SParseContext* pParseCxt, SQuery* pQuery); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_NEW_PARSER_H_*/ diff --git a/include/libs/parser/parsenodes.h b/include/libs/parser/parsenodes.h deleted file mode 100644 index 6cd5feca24..0000000000 --- a/include/libs/parser/parsenodes.h +++ /dev/null @@ -1,163 +0,0 @@ -/* - * 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 _TD_PARSENODES_H_ -#define _TD_PARSENODES_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include "catalog.h" -#include "common.h" -#include "function.h" -#include "tmsgtype.h" -#include "tname.h" -#include "tvariant.h" - -/** - * The first field of a node of any type is guaranteed to be the int16_t. - * Hence the type of any node can be gotten by casting it to SQueryNode. - */ -typedef struct SQueryNode { - int16_t type; -} SQueryNode; - -#define queryNodeType(nodeptr) (((const SQueryNode*)(nodeptr))->type) - -typedef struct SFieldInfo { - int16_t numOfOutput; // number of column in result - SField *final; - SArray *internalField; // SArray -} SFieldInfo; - -typedef struct SCond { - uint64_t uid; - int32_t len; // length of tag query condition data - char * cond; -} SCond; - -typedef struct SJoinNode { - uint64_t uid; - int16_t tagColId; - SArray* tsJoin; - SArray* tagJoin; -} SJoinNode; - -typedef struct SJoinInfo { - bool hasJoin; - SJoinNode *joinTables[TSDB_MAX_JOIN_TABLE_NUM]; -} SJoinInfo; - -typedef struct STagCond { - int16_t relType; // relation between tbname list and query condition, including : TK_AND or TK_OR - SCond tbnameCond; // tbname query condition, only support tbname query condition on one table - SJoinInfo joinInfo; // join condition, only support two tables join currently - SArray *pCond; // for different table, the query condition must be seperated -} STagCond; - -typedef struct STableMetaInfo { - STableMeta *pTableMeta; // table meta, cached in client side and acquired by name - SVgroupsInfo *vgroupList; - SName name; - char aliasName[TSDB_TABLE_NAME_LEN]; // alias name of table specified in query sql - SArray *tagColList; // SArray, involved tag columns -} STableMetaInfo; - -typedef struct SColumnIndex { - int16_t tableIndex; - int16_t columnIndex; - int16_t type; // normal column/tag/ user input constant column -} SColumnIndex; - -// select statement -typedef struct SQueryStmtInfo { - int16_t command; // the command may be different for each subclause, so keep it seperately. - uint32_t type; // query/insert type - STimeWindow window; // the whole query time window - SInterval interval; // tumble time window - SSessionWindow sessionWindow; // session time window - SStateWindow stateWindow; // state window query - SGroupbyExpr groupbyExpr; // groupby tags info - SArray * colList; // SArray - SFieldInfo fieldsInfo; - SArray** exprList; // SArray - SLimit limit; - SLimit slimit; - STagCond tagCond; - SArray * colCond; - SArray * order; - int16_t numOfTables; - int16_t curTableIdx; - STableMetaInfo **pTableMetaInfo; - struct STSBuf *tsBuf; - - int16_t fillType; // final result fill type - int64_t * fillVal; // default value for fill - int32_t numOfFillVal; // fill value size - - char * msg; // pointer to the pCmd->payload to keep error message temporarily - int64_t clauseLimit; // limit for current sub clause - - int64_t prjOffset; // offset value in the original sql expression, only applied at client side - int64_t vgroupLimit; // table limit in case of super table projection query + global order + limit - - int32_t udColumnId; // current user-defined constant output field column id, monotonically decreases from TSDB_UD_COLUMN_INDEX - int32_t bufLen; - char* buf; - SArray *pUdfInfo; - - struct SQueryStmtInfo *sibling; // sibling - SMultiFunctionsDesc info; - SArray *pDownstream; // SArray - int32_t havingFieldNum; - int32_t exprListLevelIndex; -} SQueryStmtInfo; - -typedef enum { - PAYLOAD_TYPE_KV = 0, - PAYLOAD_TYPE_RAW = 1, -} EPayloadType; - -typedef struct SVgDataBlocks { - SVgroupInfo vg; - int32_t numOfTables; // number of tables in current submit block - uint32_t size; - char *pData; // SMsgDesc + SSubmitReq + SSubmitBlk + ... -} SVgDataBlocks; - -typedef struct SVnodeModifOpStmtInfo { - int16_t nodeType; - SArray* pDataBlocks; // data block for each vgroup, SArray. - int8_t schemaAttache; // denote if submit block is built with table schema or not - uint8_t payloadType; // EPayloadType. 0: K-V payload for non-prepare insert, 1: rawPayload for prepare insert - uint32_t insertType; // insert data from [file|sql statement| bound statement] - const char* sql; // current sql statement position -} SVnodeModifOpStmtInfo; - -typedef struct SDclStmtInfo { - int16_t nodeType; - int16_t msgType; - SEpSet epSet; - char* pMsg; - int32_t msgLen; - void* pExtension; // todo remove it soon -} SDclStmtInfo; - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_PARSENODES_H_*/ diff --git a/include/libs/parser/parser.h b/include/libs/parser/parser.h index 1d193888c9..d48c059ef1 100644 --- a/include/libs/parser/parser.h +++ b/include/libs/parser/parser.h @@ -20,6 +20,8 @@ extern "C" { #endif +#if 0 + #include "parsenodes.h" typedef struct SParseContext { @@ -94,6 +96,37 @@ int32_t getNewResColId(); void addIntoSourceParam(SSourceParam* pSourceParam, tExprNode* pNode, SColumn* pColumn); SExprInfo* createBinaryExprInfo(struct tExprNode* pNode, SSchema* pResSchema); +#else + +#include "querynodes.h" +#include "tmsg.h" + +typedef struct SParseContext { + uint64_t requestId; + int32_t acctId; + const char *db; + void *pTransporter; + SEpSet mgmtEpSet; + const char *pSql; // sql string + size_t sqlLen; // length of the sql string + char *pMsg; // extended error message if exists to help identifying the problem in sql statement. + int32_t msgLen; // max length of the msg + struct SCatalog *pCatalog; +} SParseContext; + +typedef struct SQuery { + bool isCmd; + SNode* pRoot; + int32_t numOfResCols; + SSchema* pResSchema; +} SQuery; + +int32_t qParseQuerySql(SParseContext* pCxt, SQuery** pQuery); + +void qDestroyQuery(SQuery* pQueryNode); + +#endif + #ifdef __cplusplus } #endif diff --git a/include/libs/planner/planner.h b/include/libs/planner/planner.h index 126cee390c..ced96ba46c 100644 --- a/include/libs/planner/planner.h +++ b/include/libs/planner/planner.h @@ -20,6 +20,8 @@ extern "C" { #endif +#if 0 + #include "query.h" #include "tmsg.h" #include "tarray.h" @@ -207,6 +209,65 @@ void qDestroyQueryDag(SQueryDag* pDag); char* qDagToString(const SQueryDag* pDag); SQueryDag* qStringToDag(const char* pStr); +#else + +#include "plannodes.h" +#include "query.h" + +#define QUERY_TYPE_MERGE 1 +#define QUERY_TYPE_PARTIAL 2 +#define QUERY_TYPE_SCAN 3 +#define QUERY_TYPE_MODIFY 4 + +typedef struct SSubplanId { + uint64_t queryId; + uint64_t templateId; + uint64_t subplanId; +} SSubplanId; + +typedef struct SSubplan { + SSubplanId id; // unique id of the subplan + int32_t type; // QUERY_TYPE_MERGE|QUERY_TYPE_PARTIAL|QUERY_TYPE_SCAN|QUERY_TYPE_MODIFY + int32_t msgType; // message type for subplan, used to denote the send message type to vnode. + int32_t level; // the execution level of current subplan, starting from 0 in a top-down manner. + SQueryNodeAddr execNode; // for the scan/modify subplan, the optional execution node + SArray* pChildren; // the datasource subplan,from which to fetch the result + SArray* pParents; // the data destination subplan, get data from current subplan + SPhysiNode* pNode; // physical plan of current subplan + SDataSinkNode* pDataSink; // data of the subplan flow into the datasink +} SSubplan; + +typedef struct SQueryPlan { + uint64_t queryId; + int32_t numOfSubplans; + SArray* pSubplans; // SArray*>. The execution level of subplan, starting from 0. +} SQueryPlan; + +typedef struct SPlanContext { + uint64_t queryId; + SNode* pAstRoot; +} SPlanContext; + +// Create the physical plan for the query, according to the AST. +int32_t qCreateQueryPlan(SPlanContext* pCxt, SQueryPlan** pPlan); + +// Set datasource of this subplan, multiple calls may be made to a subplan. +// @subplan subplan to be schedule +// @templateId templateId of a group of datasource subplans of this @subplan +// @ep one execution location of this group of datasource subplans +void qSetSubplanExecutionNode(SSubplan* subplan, uint64_t templateId, SDownstreamSource* pSource); + +// Convert to subplan to string for the scheduler to send to the executor +int32_t qSubPlanToString(const SSubplan* subplan, char** str, int32_t* len); +int32_t qStringToSubplan(const char* str, SSubplan** subplan); + +char* qQueryPlanToString(const SQueryPlan* pPlan); +SQueryPlan* qStringToQueryPlan(const char* pStr); + +void qDestroyQueryPlan(SQueryPlan* pPlan); + +#endif + #ifdef __cplusplus } #endif diff --git a/include/libs/planner/plannerOp.h b/include/libs/planner/plannerOp.h deleted file mode 100644 index 9030ffc946..0000000000 --- a/include/libs/planner/plannerOp.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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 . - */ - -#if defined(INCLUDE_AS_ENUM) // enum define mode - #undef OP_ENUM_MACRO - #define OP_ENUM_MACRO(op) OP_##op, -#elif defined(INCLUDE_AS_NAME) // comment define mode - #undef OP_ENUM_MACRO - #define OP_ENUM_MACRO(op) #op, -#else - #error To use this include file, first define either INCLUDE_AS_ENUM or INCLUDE_AS_NAME -#endif - -OP_ENUM_MACRO(StreamScan) -OP_ENUM_MACRO(TableScan) -OP_ENUM_MACRO(TableSeqScan) -OP_ENUM_MACRO(TagScan) -OP_ENUM_MACRO(SystemTableScan) -OP_ENUM_MACRO(StreamBlockScan) -OP_ENUM_MACRO(Aggregate) -OP_ENUM_MACRO(Project) -// OP_ENUM_MACRO(Groupby) -OP_ENUM_MACRO(Limit) -OP_ENUM_MACRO(SLimit) -OP_ENUM_MACRO(TimeWindow) -OP_ENUM_MACRO(SessionWindow) -OP_ENUM_MACRO(StateWindow) -OP_ENUM_MACRO(Fill) -OP_ENUM_MACRO(MultiTableAggregate) -OP_ENUM_MACRO(MultiTableTimeInterval) -OP_ENUM_MACRO(Filter) -OP_ENUM_MACRO(Distinct) -OP_ENUM_MACRO(Join) -OP_ENUM_MACRO(AllTimeWindow) -OP_ENUM_MACRO(AllMultiTableTimeInterval) -OP_ENUM_MACRO(Order) -OP_ENUM_MACRO(Exchange) - -//OP_ENUM_MACRO(TableScan) diff --git a/include/libs/scheduler/scheduler.h b/include/libs/scheduler/scheduler.h index e856adaf31..a916a667b3 100644 --- a/include/libs/scheduler/scheduler.h +++ b/include/libs/scheduler/scheduler.h @@ -72,7 +72,7 @@ int32_t schedulerInit(SSchedulerCfg *cfg); * @param nodeList Qnode/Vnode address list, element is SQueryNodeAddr * @return */ -int32_t schedulerExecJob(void *transport, SArray *nodeList, SQueryDag* pDag, struct SSchJob** pJob, const char* sql, SQueryResult *pRes); +int32_t schedulerExecJob(void *transport, SArray *nodeList, SQueryPlan* pDag, struct SSchJob** pJob, const char* sql, SQueryResult *pRes); /** * Process the query job, generated according to the query physical plan. @@ -80,7 +80,7 @@ int32_t schedulerExecJob(void *transport, SArray *nodeList, SQueryDag* pDag, str * @param pNodeList Qnode/Vnode address list, element is SQueryNodeAddr * @return */ -int32_t schedulerAsyncExecJob(void *transport, SArray *pNodeList, SQueryDag* pDag, const char* sql, struct SSchJob** pJob); +int32_t schedulerAsyncExecJob(void *transport, SArray *pNodeList, SQueryPlan* pDag, const char* sql, struct SSchJob** pJob); /** * Fetch query result from the remote query executor @@ -112,7 +112,7 @@ void schedulerDestroy(void); * @param pTasks SArray** * @return */ -int32_t schedulerConvertDagToTaskList(SQueryDag* pDag, SArray **pTasks); +int32_t schedulerConvertDagToTaskList(SQueryPlan* pDag, SArray **pTasks); /** * make one task info's multiple copies diff --git a/source/client/CMakeLists.txt b/source/client/CMakeLists.txt index 3210c0c6de..a632337d4a 100644 --- a/source/client/CMakeLists.txt +++ b/source/client/CMakeLists.txt @@ -8,7 +8,7 @@ target_include_directories( target_link_libraries( taos INTERFACE api - PRIVATE os util common transport parser planner catalog scheduler function qcom + PRIVATE os util common transport nodes parser planner catalog scheduler function qcom ) if(${BUILD_TEST}) diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index 523921634b..dee80b59e0 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -172,7 +172,7 @@ typedef struct SRequestSendRecvBody { SShowReqInfo showInfo; // todo this attribute will be removed after the query framework being completed. SDataBuf requestMsg; struct SSchJob* pQueryJob; // query job, created according to sql query DAG. - struct SQueryDag* pDag; // the query dag, generated according to the sql statement. + struct SQueryPlan* pDag; // the query dag, generated according to the sql statement. SReqResultInfo resInfo; } SRequestSendRecvBody; @@ -232,7 +232,7 @@ void setResultDataPtr(SReqResultInfo* pResultInfo, TAOS_FIELD* pFields, int32_t int32_t buildRequest(STscObj* pTscObj, const char* sql, int sqlLen, SRequestObj** pRequest); -int32_t parseSql(SRequestObj* pRequest, SQueryNode** pQuery); +int32_t parseSql(SRequestObj* pRequest, SQuery** pQuery); // --- heartbeat // global, called by mgmt diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 08285c9d26..0f63a1f98c 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -184,7 +184,7 @@ static void doDestroyRequest(void *p) { tfree(pRequest->pInfo); doFreeReqResultInfo(&pRequest->body.resInfo); - qDestroyQueryDag(pRequest->body.pDag); + qDestroyQueryPlan(pRequest->body.pDag); if (pRequest->body.showInfo.pArray != NULL) { taosArrayDestroy(pRequest->body.showInfo.pArray); diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index f256feb251..85e7344248 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -139,7 +139,7 @@ int32_t buildRequest(STscObj* pTscObj, const char* sql, int sqlLen, SRequestObj* return TSDB_CODE_SUCCESS; } -int32_t parseSql(SRequestObj* pRequest, SQueryNode** pQuery) { +int32_t parseSql(SRequestObj* pRequest, SQuery** pQuery) { STscObj* pTscObj = pRequest->pTscObj; SParseContext cxt = { @@ -161,54 +161,57 @@ int32_t parseSql(SRequestObj* pRequest, SQueryNode** pQuery) { } code = qParseQuerySql(&cxt, pQuery); + if (TSDB_CODE_SUCCESS == code) { + setResSchemaInfo(&pRequest->body.resInfo, (*pQuery)->pResSchema, (*pQuery)->numOfResCols); + } tfree(cxt.db); return code; } -int32_t execDdlQuery(SRequestObj* pRequest, SQueryNode* pQuery) { - SDclStmtInfo* pDcl = (SDclStmtInfo*)pQuery; - pRequest->type = pDcl->msgType; - pRequest->body.requestMsg = (SDataBuf){.pData = pDcl->pMsg, .len = pDcl->msgLen, .handle = NULL}; - - STscObj* pTscObj = pRequest->pTscObj; - SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest); - - int64_t transporterId = 0; - if (pDcl->msgType == TDMT_VND_CREATE_TABLE || pDcl->msgType == TDMT_VND_SHOW_TABLES) { - if (pDcl->msgType == TDMT_VND_SHOW_TABLES) { - SShowReqInfo* pShowReqInfo = &pRequest->body.showInfo; - if (pShowReqInfo->pArray == NULL) { - pShowReqInfo->currentIndex = 0; // set the first vnode/ then iterate the next vnode - pShowReqInfo->pArray = pDcl->pExtension; - } - } - asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &pDcl->epSet, &transporterId, pSendMsg); - } else { - asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &pDcl->epSet, &transporterId, pSendMsg); - } - - tsem_wait(&pRequest->body.rspSem); +int32_t execDdlQuery(SRequestObj* pRequest, SQuery* pQuery) { + // SDclStmtInfo* pDcl = (SDclStmtInfo*)pQuery; + // pRequest->type = pDcl->msgType; + // pRequest->body.requestMsg = (SDataBuf){.pData = pDcl->pMsg, .len = pDcl->msgLen, .handle = NULL}; + + // STscObj* pTscObj = pRequest->pTscObj; + // SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest); + + // int64_t transporterId = 0; + // if (pDcl->msgType == TDMT_VND_CREATE_TABLE || pDcl->msgType == TDMT_VND_SHOW_TABLES) { + // if (pDcl->msgType == TDMT_VND_SHOW_TABLES) { + // SShowReqInfo* pShowReqInfo = &pRequest->body.showInfo; + // if (pShowReqInfo->pArray == NULL) { + // pShowReqInfo->currentIndex = 0; // set the first vnode/ then iterate the next vnode + // pShowReqInfo->pArray = pDcl->pExtension; + // } + // } + // asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &pDcl->epSet, &transporterId, pSendMsg); + // } else { + // asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &pDcl->epSet, &transporterId, pSendMsg); + // } + + // tsem_wait(&pRequest->body.rspSem); return TSDB_CODE_SUCCESS; } -int32_t getPlan(SRequestObj* pRequest, SQueryNode* pQueryNode, SQueryDag** pDag, SArray* pNodeList) { - pRequest->type = pQueryNode->type; +int32_t getPlan(SRequestObj* pRequest, SQuery* pQuery, SQueryPlan** pDag, SArray* pNodeList) { + // pRequest->type = pQuery->type; - SSchema* pSchema = NULL; - int32_t numOfCols = 0; - int32_t code = qCreateQueryDag(pQueryNode, pDag, &pSchema, &numOfCols, pNodeList, pRequest->requestId); - if (code != 0) { - return code; - } + // SSchema* pSchema = NULL; + // int32_t numOfCols = 0; + // int32_t code = qCreateQueryDag(pQuery, pDag, &pSchema, &numOfCols, pNodeList, pRequest->requestId); + // if (code != 0) { + // return code; + // } - if (pQueryNode->type == TSDB_SQL_SELECT) { - setResSchemaInfo(&pRequest->body.resInfo, pSchema, numOfCols); - pRequest->type = TDMT_VND_QUERY; - } + // if (pQuery->type == TSDB_SQL_SELECT) { + // setResSchemaInfo(&pRequest->body.resInfo, pSchema, numOfCols); + // pRequest->type = TDMT_VND_QUERY; + // } - tfree(pSchema); - return code; + // tfree(pSchema); + // return code; } void setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols) { @@ -224,7 +227,7 @@ void setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t } } -int32_t scheduleQuery(SRequestObj* pRequest, SQueryDag* pDag, SArray* pNodeList) { +int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList) { void* pTransporter = pRequest->pTscObj->pAppInfo->pTransporter; if (TSDB_SQL_INSERT == pRequest->type || TSDB_SQL_CREATE_TABLE == pRequest->type) { SQueryResult res = {.code = 0, .numOfRows = 0, .msgSize = ERROR_MSG_BUF_DEFAULT_SIZE, .msg = pRequest->msgBuf}; @@ -254,24 +257,24 @@ TAOS_RES* taos_query_l(TAOS* taos, const char* sql, int sqlLen) { } SRequestObj* pRequest = NULL; - SQueryNode* pQueryNode = NULL; - SArray* pNodeList = taosArrayInit(4, sizeof(struct SQueryNodeAddr)); + SQuery* pQuery; + SArray* pNodeList = taosArrayInit(4, sizeof(struct SQueryNodeAddr)); terrno = TSDB_CODE_SUCCESS; CHECK_CODE_GOTO(buildRequest(pTscObj, sql, sqlLen, &pRequest), _return); - CHECK_CODE_GOTO(parseSql(pRequest, &pQueryNode), _return); + CHECK_CODE_GOTO(parseSql(pRequest, &pQuery), _return); - if (qIsDdlQuery(pQueryNode)) { - CHECK_CODE_GOTO(execDdlQuery(pRequest, pQueryNode), _return); + if (pQuery->isCmd) { + CHECK_CODE_GOTO(execDdlQuery(pRequest, pQuery), _return); } else { - CHECK_CODE_GOTO(getPlan(pRequest, pQueryNode, &pRequest->body.pDag, pNodeList), _return); + CHECK_CODE_GOTO(getPlan(pRequest, pQuery, &pRequest->body.pDag, pNodeList), _return); CHECK_CODE_GOTO(scheduleQuery(pRequest, pRequest->body.pDag, pNodeList), _return); pRequest->code = terrno; } _return: taosArrayDestroy(pNodeList); - qDestroyQuery(pQueryNode); + qDestroyQuery(pQuery); if (NULL != pRequest && TSDB_CODE_SUCCESS != terrno) { pRequest->code = terrno; } diff --git a/source/client/src/tmq.c b/source/client/src/tmq.c index d3d9fce210..6271b28546 100644 --- a/source/client/src/tmq.c +++ b/source/client/src/tmq.c @@ -424,7 +424,7 @@ SArray* tmqGetConnInfo(SClientHbKey connKey, void* param) { TAOS_RES* tmq_create_topic(TAOS* taos, const char* topicName, const char* sql, int sqlLen) { STscObj* pTscObj = (STscObj*)taos; SRequestObj* pRequest = NULL; - SQueryNode* pQueryNode = NULL; + SQuery* pQueryNode = NULL; char* pStr = NULL; terrno = TSDB_CODE_SUCCESS; @@ -447,7 +447,7 @@ TAOS_RES* tmq_create_topic(TAOS* taos, const char* topicName, const char* sql, i } tscDebug("start to create topic, %s", topicName); - +#if 0 CHECK_CODE_GOTO(buildRequest(pTscObj, sql, sqlLen, &pRequest), _return); CHECK_CODE_GOTO(parseSql(pRequest, &pQueryNode), _return); @@ -503,7 +503,7 @@ TAOS_RES* tmq_create_topic(TAOS* taos, const char* topicName, const char* sql, i asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &epSet, &transporterId, sendInfo); tsem_wait(&pRequest->body.rspSem); - +#endif _return: qDestroyQuery(pQueryNode); /*if (sendInfo != NULL) {*/ diff --git a/source/dnode/mnode/impl/src/mndSubscribe.c b/source/dnode/mnode/impl/src/mndSubscribe.c index 2ea157fea4..7c6093ebf1 100644 --- a/source/dnode/mnode/impl/src/mndSubscribe.c +++ b/source/dnode/mnode/impl/src/mndSubscribe.c @@ -760,9 +760,9 @@ static int32_t mndProcessDoRebalanceMsg(SMnodeMsg *pMsg) { static int32_t mndInitUnassignedVg(SMnode *pMnode, const SMqTopicObj *pTopic, SMqSubscribeObj *pSub) { SSdb *pSdb = pMnode->pSdb; SVgObj *pVgroup = NULL; - SQueryDag *pDag = qStringToDag(pTopic->physicalPlan); + SQueryPlan *pPlan = qStringToQueryPlan(pTopic->physicalPlan); SArray *pArray = NULL; - SArray *inner = taosArrayGet(pDag->pSubplans, 0); + SArray *inner = taosArrayGet(pPlan->pSubplans, 0); SSubplan *plan = taosArrayGetP(inner, 0); SArray *unassignedVg = pSub->unassignedVg; @@ -779,7 +779,7 @@ static int32_t mndInitUnassignedVg(SMnode *pMnode, const SMqTopicObj *pTopic, SM plan->execNode.nodeId = pVgroup->vgId; plan->execNode.epset = mndGetVgroupEpset(pMnode, pVgroup); - if (schedulerConvertDagToTaskList(pDag, &pArray) < 0) { + if (schedulerConvertDagToTaskList(pPlan, &pArray) < 0) { terrno = TSDB_CODE_MND_UNSUPPORTED_TOPIC; mError("unsupport topic: %s, sql: %s", pTopic->name, pTopic->sql); return -1; diff --git a/source/libs/executor/inc/dataSinkInt.h b/source/libs/executor/inc/dataSinkInt.h index 7003564365..7d9209714b 100644 --- a/source/libs/executor/inc/dataSinkInt.h +++ b/source/libs/executor/inc/dataSinkInt.h @@ -22,6 +22,7 @@ extern "C" { #include "common.h" #include "dataSinkMgt.h" +#include "plannodes.h" struct SDataSink; struct SDataSinkHandle; @@ -45,7 +46,7 @@ typedef struct SDataSinkHandle { FDestroyDataSinker fDestroy; } SDataSinkHandle; -int32_t createDataDispatcher(SDataSinkManager* pManager, const struct SDataSink* pDataSink, DataSinkHandle* pHandle); +int32_t createDataDispatcher(SDataSinkManager* pManager, const SDataSinkNode* pDataSink, DataSinkHandle* pHandle); #ifdef __cplusplus } diff --git a/source/libs/executor/src/dataDispatcher.c b/source/libs/executor/src/dataDispatcher.c index 3623f5947d..4f1b83ad5d 100644 --- a/source/libs/executor/src/dataDispatcher.c +++ b/source/libs/executor/src/dataDispatcher.c @@ -39,7 +39,7 @@ typedef struct SDataCacheEntry { typedef struct SDataDispatchHandle { SDataSinkHandle sink; SDataSinkManager* pManager; - SDataBlockSchema schema; + SDataBlockDescNode schema; STaosQueue* pDataBlocks; SDataDispatchBuf nextOutput; int32_t status; @@ -48,12 +48,13 @@ typedef struct SDataDispatchHandle { pthread_mutex_t mutex; } SDataDispatchHandle; -static bool needCompress(const SSDataBlock* pData, const SDataBlockSchema* pSchema) { +static bool needCompress(const SSDataBlock* pData, const SDataBlockDescNode* pSchema) { if (tsCompressColData < 0 || 0 == pData->info.rows) { return false; } - for (int32_t col = 0; col < pSchema->numOfCols; ++col) { + int32_t numOfCols = LIST_LENGTH(pSchema->pSlots); + for (int32_t col = 0; col < numOfCols; ++col) { SColumnInfoData* pColRes = taosArrayGet(pData->pDataBlock, col); int32_t colSize = pColRes->info.bytes * pData->info.rows; if (NEEDTO_COMPRESS_QUERY(colSize)) { @@ -70,13 +71,14 @@ static int32_t compressColData(SColumnInfoData *pColRes, int32_t numOfRows, char pColRes->pData, colSize, numOfRows, data, colSize + COMP_OVERFLOW_BYTES, compressed, NULL, 0); } -static void copyData(const SInputData* pInput, const SDataBlockSchema* pSchema, char* data, int8_t compressed, int32_t *compLen) { +static void copyData(const SInputData* pInput, const SDataBlockDescNode* pSchema, char* data, int8_t compressed, int32_t *compLen) { + int32_t numOfCols = LIST_LENGTH(pSchema->pSlots); int32_t *compSizes = (int32_t*)data; if (compressed) { - data += pSchema->numOfCols * sizeof(int32_t); + data += numOfCols * sizeof(int32_t); } - for (int32_t col = 0; col < pSchema->numOfCols; ++col) { + for (int32_t col = 0; col < numOfCols; ++col) { SColumnInfoData* pColRes = taosArrayGet(pInput->pData->pDataBlock, col); if (compressed) { compSizes[col] = compressColData(pColRes, pInput->pData->info.rows, data, compressed); @@ -224,7 +226,7 @@ static int32_t destroyDataSinker(SDataSinkHandle* pHandle) { pthread_mutex_destroy(&pDispatcher->mutex); } -int32_t createDataDispatcher(SDataSinkManager* pManager, const SDataSink* pDataSink, DataSinkHandle* pHandle) { +int32_t createDataDispatcher(SDataSinkManager* pManager, const SDataSinkNode* pDataSink, DataSinkHandle* pHandle) { SDataDispatchHandle* dispatcher = calloc(1, sizeof(SDataDispatchHandle)); if (NULL == dispatcher) { terrno = TSDB_CODE_QRY_OUT_OF_MEMORY; @@ -236,7 +238,7 @@ int32_t createDataDispatcher(SDataSinkManager* pManager, const SDataSink* pDataS dispatcher->sink.fGetData = getDataBlock; dispatcher->sink.fDestroy = destroyDataSinker; dispatcher->pManager = pManager; - dispatcher->schema = pDataSink->schema; + dispatcher->schema = pDataSink->inputDataBlockDesc; dispatcher->status = DS_BUF_EMPTY; dispatcher->queryEnd = false; dispatcher->pDataBlocks = taosOpenQueue(); diff --git a/source/libs/executor/src/dataSinkMgt.c b/source/libs/executor/src/dataSinkMgt.c index eb1f75f359..501b476f8a 100644 --- a/source/libs/executor/src/dataSinkMgt.c +++ b/source/libs/executor/src/dataSinkMgt.c @@ -25,8 +25,8 @@ int32_t dsDataSinkMgtInit(SDataSinkMgtCfg *cfg) { pthread_mutex_init(&gDataSinkManager.mutex, NULL); } -int32_t dsCreateDataSinker(const struct SDataSink *pDataSink, DataSinkHandle* pHandle) { - if (DSINK_Dispatch == pDataSink->info.type) { +int32_t dsCreateDataSinker(const SDataSinkNode *pDataSink, DataSinkHandle* pHandle) { + if (QUERY_NODE_DSINK_DISPATCH == nodeType(pDataSink)) { return createDataDispatcher(&gDataSinkManager, pDataSink, pHandle); } return TSDB_CODE_FAILED; diff --git a/source/libs/executor/src/executor.c b/source/libs/executor/src/executor.c index 4078ee9291..ff2447b595 100644 --- a/source/libs/executor/src/executor.c +++ b/source/libs/executor/src/executor.c @@ -20,7 +20,7 @@ static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, char* id) { ASSERT(pOperator != NULL); - if (pOperator->operatorType != OP_StreamScan) { + if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) { if (pOperator->numOfDownstream == 0) { qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id); return TSDB_CODE_QRY_APP_ERROR; diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 5508b5ecd7..6d983700e8 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -5285,7 +5285,7 @@ SOperatorInfo* createExchangeOperatorInfo(const SArray* pSources, const SArray* tsem_init(&pInfo->ready, 0, 0); pOperator->name = "ExchangeOperator"; - pOperator->operatorType = OP_Exchange; + pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_EXCHANGE; pOperator->blockingOptr = false; pOperator->status = OP_IN_EXECUTING; pOperator->info = pInfo; @@ -5365,7 +5365,7 @@ SOperatorInfo* createTableScanOperatorInfo(void* pTsdbReadHandle, int32_t order, pInfo->scanFlag = MAIN_SCAN; pOperator->name = "TableScanOperator"; - pOperator->operatorType = OP_TableScan; + pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN; pOperator->blockingOptr = false; pOperator->status = OP_IN_EXECUTING; pOperator->info = pInfo; @@ -5389,7 +5389,7 @@ SOperatorInfo* createTableSeqScanOperatorInfo(void* pTsdbReadHandle, STaskRuntim SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); pOperator->name = "TableSeqScanOperator"; - pOperator->operatorType = OP_TableSeqScan; + pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN; pOperator->blockingOptr = false; pOperator->status = OP_IN_EXECUTING; pOperator->info = pInfo; @@ -5452,7 +5452,7 @@ SOperatorInfo* createStreamScanOperatorInfo(void *streamReadHandle, SArray* pExp pInfo->readerHandle = streamReadHandle; pOperator->name = "StreamBlockScanOperator"; - pOperator->operatorType = OP_StreamScan; + pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN; pOperator->blockingOptr = false; pOperator->status = OP_IN_EXECUTING; pOperator->info = pInfo; @@ -6236,7 +6236,7 @@ SOperatorInfo *createOrderOperatorInfo(SOperatorInfo* downstream, SArray* pExprI } pOperator->name = "Order"; - pOperator->operatorType = OP_Order; + pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_SORT; pOperator->blockingOptr = true; pOperator->status = OP_IN_EXECUTING; pOperator->info = pInfo; @@ -7193,6 +7193,21 @@ static int32_t initAggInfo(SAggOperatorInfo* pInfo, SArray* pExprInfo, int32_t n return TSDB_CODE_SUCCESS; } +static void assignExprInfo(SExprInfo* dst, const SExprInfo* src) { + assert(dst != NULL && src != NULL); + + *dst = *src; + + dst->pExpr = exprdup(src->pExpr); + dst->base.pColumns = calloc(src->base.numOfCols, sizeof(SColumn)); + memcpy(dst->base.pColumns, src->base.pColumns, sizeof(SColumn) * src->base.numOfCols); + + memset(dst->base.param, 0, sizeof(SVariant) * tListLen(dst->base.param)); + for (int32_t j = 0; j < src->base.numOfParams; ++j) { + taosVariantAssign(&dst->base.param[j], &src->base.param[j]); + } +} + static SExprInfo* exprArrayDup(SArray* pExprInfo) { size_t numOfOutput = taosArrayGetSize(pExprInfo); SExprInfo* p = calloc(numOfOutput, sizeof(SExprInfo)); @@ -7215,7 +7230,7 @@ SOperatorInfo* createAggregateOperatorInfo(SOperatorInfo* downstream, SArray* pE SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); pOperator->name = "TableAggregate"; - pOperator->operatorType = OP_Aggregate; + pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_AGG; pOperator->blockingOptr = true; pOperator->status = OP_IN_EXECUTING; pOperator->info = pInfo; @@ -7316,7 +7331,7 @@ SOperatorInfo* createMultiTableAggOperatorInfo(SOperatorInfo* downstream, SArray SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); pOperator->name = "MultiTableAggregate"; - pOperator->operatorType = OP_MultiTableAggregate; + // pOperator->operatorType = OP_MultiTableAggregate; pOperator->blockingOptr = true; pOperator->status = OP_IN_EXECUTING; pOperator->info = pInfo; @@ -7690,7 +7705,7 @@ SOperatorInfo* createSLimitOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorI SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); pOperator->name = "SLimitOperator"; - pOperator->operatorType = OP_SLimit; + // pOperator->operatorType = OP_SLimit; pOperator->blockingOptr = false; pOperator->status = OP_IN_EXECUTING; // pOperator->exec = doSLimit; @@ -7846,7 +7861,7 @@ SOperatorInfo* createTagScanOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SExprInfo SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); pOperator->name = "SeqTableTagScan"; - pOperator->operatorType = OP_TagScan; + pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN; pOperator->blockingOptr = false; pOperator->status = OP_IN_EXECUTING; pOperator->info = pInfo; @@ -8164,25 +8179,25 @@ static SExecTaskInfo* createExecTaskInfo(uint64_t queryId, uint64_t taskId) { return pTaskInfo; } -static tsdbReaderT doCreateDataReader(STableScanPhyNode* pTableScanNode, SReadHandle* pHandle, uint64_t queryId, uint64_t taskId); +static tsdbReaderT doCreateDataReader(STableScanPhysiNode* pTableScanNode, SReadHandle* pHandle, uint64_t queryId, uint64_t taskId); static int32_t doCreateTableGroup(void* metaHandle, int32_t tableType, uint64_t tableUid, STableGroupInfo* pGroupInfo, uint64_t queryId, uint64_t taskId); -SOperatorInfo* doCreateOperatorTreeNode(SPhyNode* pPhyNode, SExecTaskInfo* pTaskInfo, SReadHandle* pHandle, uint64_t queryId, uint64_t taskId, STableGroupInfo* pTableGroupInfo) { - if (pPhyNode->pChildren == NULL || taosArrayGetSize(pPhyNode->pChildren) == 0) { - if (pPhyNode->info.type == OP_TableScan) { - SScanPhyNode* pScanPhyNode = (SScanPhyNode*)pPhyNode; +SOperatorInfo* doCreateOperatorTreeNode(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo, SReadHandle* pHandle, uint64_t queryId, uint64_t taskId, STableGroupInfo* pTableGroupInfo) { + if (pPhyNode->pChildren == NULL || LIST_LENGTH(pPhyNode->pChildren) == 0) { + if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pPhyNode)) { + SScanPhysiNode* pScanPhyNode = (SScanPhysiNode*)pPhyNode; - size_t numOfCols = taosArrayGetSize(pPhyNode->pTargets); - tsdbReaderT pDataReader = doCreateDataReader((STableScanPhyNode*) pPhyNode, pHandle, (uint64_t) queryId, taskId); + size_t numOfCols = LIST_LENGTH(pScanPhyNode->pScanCols); + tsdbReaderT pDataReader = doCreateDataReader((STableScanPhysiNode*) pPhyNode, pHandle, (uint64_t) queryId, taskId); int32_t code = doCreateTableGroup(pHandle->meta, pScanPhyNode->tableType, pScanPhyNode->uid, pTableGroupInfo, queryId, taskId); return createTableScanOperatorInfo(pDataReader, pScanPhyNode->order, numOfCols, pScanPhyNode->count, pScanPhyNode->reverse, pTaskInfo); - } else if (pPhyNode->info.type == OP_Exchange) { - SExchangePhyNode* pEx = (SExchangePhyNode*) pPhyNode; - return createExchangeOperatorInfo(pEx->pSrcEndPoints, pEx->node.pTargets, pTaskInfo); - } else if (pPhyNode->info.type == OP_StreamScan) { - SScanPhyNode* pScanPhyNode = (SScanPhyNode*)pPhyNode; // simple child table. + } else if (QUERY_NODE_PHYSICAL_PLAN_EXCHANGE == nodeType(pPhyNode)) { + // SExchangePhysiNode* pEx = (SExchangePhysiNode*) pPhyNode; + // return createExchangeOperatorInfo(pEx->pSrcEndPoints, pEx->node.pTargets, pTaskInfo); + } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN == nodeType(pPhyNode)) { + SScanPhysiNode* pScanPhyNode = (SScanPhysiNode*)pPhyNode; // simple child table. STableGroupInfo groupInfo = {0}; int32_t code = doCreateTableGroup(pHandle->meta, pScanPhyNode->tableType, pScanPhyNode->uid, &groupInfo, queryId, taskId); @@ -8198,58 +8213,58 @@ SOperatorInfo* doCreateOperatorTreeNode(SPhyNode* pPhyNode, SExecTaskInfo* pTask taosArrayPush(idList, &pkeyInfo->uid); } - SOperatorInfo* pOperator = createStreamScanOperatorInfo(pHandle->reader, pPhyNode->pTargets, idList, pTaskInfo); - taosArrayDestroy(idList); + // SOperatorInfo* pOperator = createStreamScanOperatorInfo(pHandle->reader, pScanPhyNode->pScanCols, idList, pTaskInfo); + // taosArrayDestroy(idList); - //TODO destroy groupInfo - return pOperator; + // //TODO destroy groupInfo + // return pOperator; } } - if (pPhyNode->info.type == OP_Aggregate) { - size_t size = taosArrayGetSize(pPhyNode->pChildren); + if (QUERY_NODE_PHYSICAL_PLAN_AGG == nodeType(pPhyNode)) { + size_t size = LIST_LENGTH(pPhyNode->pChildren); assert(size == 1); // TODO single table agg for (int32_t i = 0; i < size; ++i) { - SPhyNode* pChildNode = taosArrayGetP(pPhyNode->pChildren, i); + SPhysiNode* pChildNode = (SPhysiNode*)nodesListGetNode(pPhyNode->pChildren, i); SOperatorInfo* op = doCreateOperatorTreeNode(pChildNode, pTaskInfo, pHandle, queryId, taskId, pTableGroupInfo); - return createAggregateOperatorInfo(op, pPhyNode->pTargets, pTaskInfo, pTableGroupInfo); + // return createAggregateOperatorInfo(op, pPhyNode->pTargets, pTaskInfo, pTableGroupInfo); } - } else if (pPhyNode->info.type == OP_MultiTableAggregate) { + } /*else if (pPhyNode->info.type == OP_MultiTableAggregate) { size_t size = taosArrayGetSize(pPhyNode->pChildren); assert(size == 1); for (int32_t i = 0; i < size; ++i) { - SPhyNode* pChildNode = taosArrayGetP(pPhyNode->pChildren, i); + SPhysiNode* pChildNode = taosArrayGetP(pPhyNode->pChildren, i); SOperatorInfo* op = doCreateOperatorTreeNode(pChildNode, pTaskInfo, pHandle, queryId, taskId, pTableGroupInfo); return createMultiTableAggOperatorInfo(op, pPhyNode->pTargets, pTaskInfo, pTableGroupInfo); } - } + }*/ } -static tsdbReaderT createDataReaderImpl(STableScanPhyNode* pTableScanNode, STableGroupInfo* pGroupInfo, void* readHandle, uint64_t queryId, uint64_t taskId) { +static tsdbReaderT createDataReaderImpl(STableScanPhysiNode* pTableScanNode, STableGroupInfo* pGroupInfo, void* readHandle, uint64_t queryId, uint64_t taskId) { STsdbQueryCond cond = {.loadExternalRows = false}; cond.order = pTableScanNode->scan.order; - cond.numOfCols = taosArrayGetSize(pTableScanNode->scan.node.pTargets); + cond.numOfCols = LIST_LENGTH(pTableScanNode->scan.pScanCols); cond.colList = calloc(cond.numOfCols, sizeof(SColumnInfo)); if (cond.colList == NULL) { terrno = TSDB_CODE_QRY_OUT_OF_MEMORY; return NULL; } - cond.twindow = pTableScanNode->window; + cond.twindow = pTableScanNode->scanRange; cond.type = BLOCK_LOAD_OFFSET_SEQ_ORDER; for (int32_t i = 0; i < cond.numOfCols; ++i) { - SExprInfo* pExprInfo = taosArrayGetP(pTableScanNode->scan.node.pTargets, i); - assert(pExprInfo->pExpr->nodeType == TEXPR_COL_NODE); + // SExprInfo* pExprInfo = taosArrayGetP(pTableScanNode->scan.node.pTargets, i); + // assert(pExprInfo->pExpr->nodeType == TEXPR_COL_NODE); - SSchema* pSchema = pExprInfo->pExpr->pSchema; - cond.colList[i].type = pSchema->type; - cond.colList[i].bytes = pSchema->bytes; - cond.colList[i].colId = pSchema->colId; + // SSchema* pSchema = pExprInfo->pExpr->pSchema; + // cond.colList[i].type = pSchema->type; + // cond.colList[i].bytes = pSchema->bytes; + // cond.colList[i].colId = pSchema->colId; } return tsdbQueryTables(readHandle, &cond, pGroupInfo, queryId, taskId); @@ -8266,7 +8281,7 @@ static int32_t doCreateTableGroup(void* metaHandle, int32_t tableType, uint64_t return code; } -static tsdbReaderT doCreateDataReader(STableScanPhyNode* pTableScanNode, SReadHandle* pHandle, uint64_t queryId, uint64_t taskId) { +static tsdbReaderT doCreateDataReader(STableScanPhysiNode* pTableScanNode, SReadHandle* pHandle, uint64_t queryId, uint64_t taskId) { STableGroupInfo groupInfo = {0}; uint64_t uid = pTableScanNode->scan.uid; diff --git a/source/libs/parser/inc/astGenerator.h b/source/libs/parser/inc/astGenerator.h deleted file mode 100644 index 1327259a51..0000000000 --- a/source/libs/parser/inc/astGenerator.h +++ /dev/null @@ -1,369 +0,0 @@ -/* - * 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_ASTGENERATOR_H -#define TDENGINE_ASTGENERATOR_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include "ttoken.h" -#include "tvariant.h" -#include "parser.h" - -#define ParseTOKENTYPE SToken - -#define NON_ARITHMEIC_EXPR 0 -#define NORMAL_ARITHMETIC 1 -#define AGG_ARIGHTMEIC 2 - -enum SQL_NODE_TYPE { - SQL_NODE_TABLE_COLUMN= 1, - SQL_NODE_SQLFUNCTION = 2, - SQL_NODE_VALUE = 3, - SQL_NODE_EXPR = 4, -}; - -enum SQL_FROM_NODE_TYPE { - SQL_FROM_NODE_SUBQUERY = 1, - SQL_FROM_NODE_TABLES = 2, -}; - -enum SQL_UNION_TYPE { - SQL_TYPE_UNIONALL = 1, - SQL_TYPE_UNION = 2, -}; - -extern char tTokenTypeSwitcher[13]; - -#define toTSDBType(x) \ - do { \ - if ((x) >= tListLen(tTokenTypeSwitcher)) { \ - (x) = TSDB_DATA_TYPE_BINARY; \ - } else { \ - (x) = tTokenTypeSwitcher[(x)]; \ - } \ - } while (0) - -#define TPARSER_HAS_TOKEN(_t) ((_t).n > 0) -#define TPARSER_SET_NONE_TOKEN(_t) ((_t).n = 0) - -typedef struct SListItem { - SVariant pVar; - uint8_t sortOrder; -} SListItem; - -typedef struct SIntervalVal { - int32_t token; - SToken interval; - SToken offset; -} SIntervalVal; - -typedef struct SSessionWindowVal { - SToken col; - SToken gap; -} SSessionWindowVal; - -typedef struct SWindowStateVal { - SToken col; -} SWindowStateVal; - -struct SRelationInfo; - -typedef struct SSqlNode { - struct SRelationInfo *from; // from clause SArray - struct SArray *pSelNodeList; // select clause - struct tSqlExpr *pWhere; // where clause [optional] - SArray *pGroupby; // groupby clause, only for tags[optional], SArray - SArray *pSortOrder; // orderby [optional], SArray - SArray *fillType; // fill type[optional], SArray - SIntervalVal interval; // (interval, interval_offset) [optional] - SSessionWindowVal sessionVal; // session window [optional] - SWindowStateVal windowstateVal; // window_state(col) [optional] - SToken sliding; // sliding window [optional] - SLimit limit; // limit offset [optional] - SLimit slimit; // group limit offset [optional] - SToken sqlstr; // sql string in select clause - struct tSqlExpr *pHaving; // having clause [optional] -} SSqlNode; - -typedef struct SSubclause { - int32_t unionType; - SArray *node; -} SSubclause; - -typedef struct SRelElement { - union { - SToken tableName; - SSubclause *pSubquery; - }; - - SToken aliasName; -} SRelElement; - -typedef struct SRelationInfo { - int32_t type; // nested query|table name list - SArray *list; // SArray -} SRelationInfo; - -typedef struct SCreatedTableInfo { - SToken name; // table name token - SToken stbName; // super table name token , for using clause - SArray *pTagNames; // create by using super table, tag name - SArray *pTagVals; // create by using super table, tag value. SArray - char *fullname; // table full name - int8_t igExist; // ignore if exists -} SCreatedTableInfo; - -typedef struct SCreateTableSql { - SToken name; // table name, create table [name] xxx - int8_t type; // create normal table/from super table/ stream - bool existCheck; - - struct { - SArray *pTagColumns; // SArray - SArray *pColumns; // SArray - } colInfo; - - SArray *childTableInfo; // SArray - SSqlNode *pSelect; -} SCreateTableSql; - -typedef struct SAlterTableInfo { - SToken name; - int16_t tableType; - int16_t type; - STagData tagData; - SArray *pAddColumns; // SArray - SArray *varList; // set t=val or: change src dst, SArray -} SAlterTableInfo; - -typedef struct SCreateDbInfo { - SToken dbname; - int32_t replica; - int32_t cacheBlockSize; - int32_t numOfVgroups; - int32_t numOfBlocks; - int32_t daysPerFile; - int32_t minRowsPerBlock; - int32_t maxRowsPerBlock; - int32_t fsyncPeriod; - int64_t commitTime; - int32_t walLevel; - int32_t quorum; - int32_t compressionLevel; - SToken precision; - bool ignoreExists; - int8_t update; - int8_t cachelast; - SArray *keep; - int8_t streamMode; -} SCreateDbInfo; - -typedef struct SCreateFuncInfo { - SToken name; - SToken path; - int32_t type; - int32_t bufSize; - SField output; -} SCreateFuncInfo; - -typedef struct SCreateAcctInfo { - int32_t maxUsers; - int32_t maxDbs; - int32_t maxTimeSeries; - int32_t maxStreams; - int32_t maxPointsPerSecond; - int64_t maxStorage; - int64_t maxQueryTime; - int32_t maxConnections; - SToken stat; -} SCreateAcctInfo; - -typedef struct SShowInfo { - uint8_t showType; - SToken prefix; - SToken pattern; -} SShowInfo; - -typedef struct SUserInfo { - SToken user; - SToken passwd; - SToken privilege; - int16_t type; -} SUserInfo; - -typedef struct SMiscInfo { - SArray *a; // SArray - bool existsCheck; - int16_t dbType; - int16_t tableType; - SUserInfo user; - union { - SCreateDbInfo dbOpt; - SCreateAcctInfo acctOpt; - SCreateFuncInfo funcOpt; - SShowInfo showOpt; - SToken id; - }; -} SMiscInfo; - -typedef struct SSqlInfo { - int32_t type; - bool valid; - SSubclause sub; - char msg[256]; - SArray *funcs; - union { - SCreateTableSql *pCreateTableInfo; - SAlterTableInfo *pAlterInfo; - SMiscInfo *pMiscInfo; - }; -} SSqlInfo; - -typedef struct tSqlExpr { - uint16_t type; // sql node type - uint32_t tokenId; // TK_LE: less than(binary expr) - - // The complete string of the function(col, param), and the function name is kept in exprToken - struct { - SToken operand; - struct SArray *paramList; // function parameters list - } Expr; - - SToken columnName; // table column info - SVariant value; // the use input value - SToken exprToken; // original sql expr string or function name of sql function - struct tSqlExpr *pLeft; // the left child - struct tSqlExpr *pRight; // the right child -} tSqlExpr; - -// used in select clause. select from xxx -typedef struct tSqlExprItem { - tSqlExpr *pNode; // The list of expressions - int32_t functionId; - char *aliasName; // alias name, null-terminated string - bool distinct; -} tSqlExprItem; - -SArray *tListItemAppend(SArray *pList, SVariant *pVar, uint8_t sortOrder); -SArray *tListItemInsert(SArray *pList, SVariant *pVar, uint8_t sortOrder, int32_t index); -SArray *tListItemAppendToken(SArray *pList, SToken *pAliasToken, uint8_t sortOrder); - -SRelationInfo *setTableNameList(SRelationInfo *pRelationInfo, SToken *pName, SToken *pAlias); -void * destroyRelationInfo(SRelationInfo *pFromInfo); -SRelationInfo *addSubquery(SRelationInfo *pRelationInfo, SSubclause *pSub, SToken *pAlias); - -// sql expr leaf node -tSqlExpr *tSqlExprCreateIdValue(SToken *pToken, int32_t optrType); -tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SToken *pFuncToken, SToken *endToken, int32_t optType); -SArray * tRecordFuncName(SArray *pList, SToken *pToken); - -tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType); -tSqlExpr *tSqlExprClone(tSqlExpr *pSrc); -void tSqlExprCompact(tSqlExpr **pExpr); -bool tSqlExprIsLeaf(tSqlExpr *pExpr); -bool tSqlExprIsParentOfLeaf(tSqlExpr *pExpr); -void tSqlExprDestroy(tSqlExpr *pExpr); -SArray * tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SToken *pDistinct, SToken *pToken); -void tSqlExprListDestroy(SArray *pList); -void tSqlExprEvaluate(tSqlExpr* pExpr); - -SSqlNode *tSetQuerySqlNode(SToken *pSelectToken, SArray *pSelNodeList, SRelationInfo *pFrom, tSqlExpr *pWhere, - SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, SSessionWindowVal *ps, - SWindowStateVal *pw, SToken *pSliding, SArray *pFill, SLimit *pLimit, SLimit *pgLimit, tSqlExpr *pHaving); -int32_t tSqlExprCompare(tSqlExpr *left, tSqlExpr *right); - -SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SSqlNode *pSelect, int32_t type); - -SAlterTableInfo * tSetAlterTableInfo(SToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableType); -SCreatedTableInfo createNewChildTableInfo(SToken *pTableName, SArray *pTagNames, SArray *pTagVals, SToken *pToken, - SToken *igExists); -/*! - * test - * @param pSqlNode - */ -void destroyAllSqlNode(struct SSubclause *pSqlNode); -void destroySqlNode(SSqlNode *pSql); -void freeCreateTableInfo(void* p); - -SSqlInfo *setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SToken *pTableName, int32_t type); -SSubclause* setSubclause(SSubclause* sub, void *pSqlNode); -SSubclause* appendSelectClause(SSubclause *sub, int32_t unionType, void *pSubclause); - -void setCreatedTableName(SSqlInfo *pInfo, SToken *pTableNameToken, SToken *pIfNotExists); -void* destroyCreateTableSql(SCreateTableSql* pCreate); -void setDropFuncInfo(SSqlInfo *pInfo, int32_t type, SToken* pToken); -void setCreateFuncInfo(SSqlInfo *pInfo, int32_t type, SToken *pName, SToken *pPath, SField *output, SToken* bufSize, int32_t funcType); - -void destroySqlInfo(SSqlInfo *pInfo); - -void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...); -void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SToken* pToken, SToken* existsCheck,int16_t dbType,int16_t tableType); -void setShowOptions(SSqlInfo *pInfo, int32_t type, SToken* prefix, SToken* pPatterns); - -void setCreateDbInfo(SSqlInfo *pInfo, int32_t type, SToken *pToken, SCreateDbInfo *pDB, SToken *pIgExists); - -void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SToken *pName, SToken *pPwd, SCreateAcctInfo *pAcctInfo); -void setCreateUserSql(SSqlInfo *pInfo, SToken *pName, SToken *pPasswd); -void setKillSql(SSqlInfo *pInfo, int32_t type, SToken *ip); -void setAlterUserSql(SSqlInfo *pInfo, int16_t type, SToken *pName, SToken* pPwd, SToken *pPrivilege); - -void setCompactVnodeSql(SSqlInfo *pInfo, int32_t type, SArray *pParam); - -void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo); -void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo); - -// prefix show db.tables; -void tSetDbName(SToken *pCpxName, SToken *pDb); - -void tSetColumnInfo(struct SField *pField, SToken *pName, struct SField *pType); -void tSetColumnType(struct SField *pField, SToken *type); - -/** - * The main parse function. - * @param yyp The parser - * @param yymajor The major token code number - * @param yyminor The value for the token - */ -void Parse(void *yyp, int yymajor, ParseTOKENTYPE yyminor, SSqlInfo *); - -/** - * Free the allocated resources in case of failure. - * @param p The parser to be deleted - * @param freeProc Function used to reclaim memory - */ -void ParseFree(void *p, void (*freeProc)(void *)); - -/** - * Allocated callback function. - * @param mallocProc The parser allocator - * @return - */ -void *ParseAlloc(void *(*mallocProc)(size_t)); - -/** - * - * @param str sql string - * @return sql ast - */ -SSqlInfo doGenerateAST(const char *str); - -#ifdef __cplusplus -} -#endif - -#endif // TDENGINE_ASTGENERATOR_H diff --git a/source/libs/parser/inc/astToMsg.h b/source/libs/parser/inc/astToMsg.h deleted file mode 100644 index 77d6900acf..0000000000 --- a/source/libs/parser/inc/astToMsg.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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_ASTTOMSG_H -#define TDENGINE_ASTTOMSG_H - -#include "parserInt.h" -#include "tmsg.h" - -char* buildUserManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen); -char* buildAcctManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen); -char* buildDropUserMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen); -char* buildShowMsg(SShowInfo* pShowInfo, int32_t* outputLen, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); -char* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, int32_t* outputLen, SParseContext* pCtx, SMsgBuf* pMsgBuf); -char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* outputLen, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); -char* buildDropStableReq(SSqlInfo* pInfo, int32_t* outputLen, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); -char* buildCreateDnodeMsg(SSqlInfo* pInfo, int32_t* outputLen, SMsgBuf* pMsgBuf); -char* buildDropDnodeMsg(SSqlInfo* pInfo, int32_t* outputLen, SMsgBuf* pMsgBuf); - -#endif // TDENGINE_ASTTOMSG_H diff --git a/source/libs/parser/inc/insertParser.h b/source/libs/parser/inc/insertParser.h index 796bd9b429..311db19bcd 100644 --- a/source/libs/parser/inc/insertParser.h +++ b/source/libs/parser/inc/insertParser.h @@ -22,7 +22,7 @@ extern "C" { #include "parser.h" -int32_t parseInsertSql(SParseContext* pContext, SVnodeModifOpStmtInfo** pInfo); +int32_t parseInsertSql(SParseContext* pContext, SQuery** pQuery); #ifdef __cplusplus } diff --git a/source/libs/parser/inc/parserImpl.h b/source/libs/parser/inc/parserImpl.h index f2777a2368..4ff142ae58 100644 --- a/source/libs/parser/inc/parserImpl.h +++ b/source/libs/parser/inc/parserImpl.h @@ -20,11 +20,11 @@ extern "C" { #endif -#include "querynodes.h" -#include "newParser.h" +#include "parser.h" -int32_t doParse(SParseContext* pParseCxt, SQuery* pQuery); +int32_t doParse(SParseContext* pParseCxt, SQuery** pQuery); int32_t doTranslate(SParseContext* pParseCxt, SQuery* pQuery); +int32_t parseQuerySql(SParseContext* pCxt, SQuery** pQuery); #ifdef __cplusplus } diff --git a/source/libs/parser/inc/parserInt.h b/source/libs/parser/inc/parserInt.h deleted file mode 100644 index abb19b3ab6..0000000000 --- a/source/libs/parser/inc/parserInt.h +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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 _TD_PARSER_INT_H_ -#define _TD_PARSER_INT_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include "catalog.h" -#include "tname.h" -#include "astGenerator.h" - -struct SSqlNode; - - -typedef struct SInternalField { - TAOS_FIELD field; - bool visible; - SExprInfo *pExpr; -} SInternalField; - -typedef struct SMsgBuf { - int32_t len; - char *buf; -} SMsgBuf; - -void clearTableMetaInfo(STableMetaInfo* pTableMetaInfo); - -void clearAllTableMetaInfo(SQueryStmtInfo* pQueryInfo, bool removeMeta, uint64_t id); - -/** - * Validate the sql info, according to the corresponding metadata info from catalog. - * @param pCtx - * @param pInfo - * @param pQueryInfo - * @param msgBuf - * @param msgBufLen - * @return - */ -int32_t qParserValidateSqlNode(SParseContext *pCtx, SSqlInfo* pInfo, SQueryStmtInfo* pQueryInfo, char* msgBuf, int32_t msgBufLen); - -/** - * validate the ddl ast, and convert the ast to the corresponding message format - * @param pSqlInfo - * @param output - * @param type - * @return - */ -SDclStmtInfo* qParserValidateDclSqlNode(SSqlInfo* pInfo, SParseContext* pCtx, char* msgBuf, int32_t msgBufLen); - -/** - * - * @param pInfo - * @param pCtx - * @param msgBuf - * @param msgBufLen - * @return - */ -SVnodeModifOpStmtInfo* qParserValidateCreateTbSqlNode(SSqlInfo* pInfo, SParseContext* pCtx, char* msgBuf, int32_t msgBufLen); - -/** - * Evaluate the numeric and timestamp arithmetic expression in the WHERE clause. - * @param pNode - * @param tsPrecision - * @param msg - * @param msgBufLen - * @return - */ -int32_t evaluateSqlNode(SSqlNode* pNode, int32_t tsPrecision, SMsgBuf* pMsgBuf); - -int32_t validateSqlNode(SSqlNode* pSqlNode, SQueryStmtInfo* pQueryInfo, SMsgBuf* pMsgBuf); - -SQueryStmtInfo* createQueryInfo(); - -void destroyQueryInfo(SQueryStmtInfo* pQueryInfo); - -int32_t checkForInvalidExpr(SQueryStmtInfo* pQueryInfo, SMsgBuf* pMsgBuf); - -/** - * Extract request meta info from the sql statement - * @param pSqlInfo - * @param pMetaInfo - * @param msg - * @param msgBufLen - * @return - */ -int32_t qParserExtractRequestedMetaInfo(const SSqlInfo* pSqlInfo, SCatalogReq* pMetaInfo, SParseContext *pCtx, char* msg, int32_t msgBufLen); - -/** - * Destroy the meta data request structure. - * @param pMetaInfo - */ -void qParserCleanupMetaRequestInfo(SCatalogReq* pMetaInfo); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_PARSER_INT_H_*/ \ No newline at end of file diff --git a/source/libs/parser/inc/parserUtil.h b/source/libs/parser/inc/parserUtil.h index d660d36d3f..60f496b994 100644 --- a/source/libs/parser/inc/parserUtil.h +++ b/source/libs/parser/inc/parserUtil.h @@ -21,54 +21,19 @@ extern "C" { #endif #include "os.h" +#include "query.h" +#include "tmsg.h" #include "ttoken.h" -#include "parserInt.h" -#define UTIL_TABLE_IS_SUPER_TABLE(metaInfo) \ - (((metaInfo)->pTableMeta != NULL) && ((metaInfo)->pTableMeta->tableType == TSDB_SUPER_TABLE)) - -#define UTIL_TABLE_IS_CHILD_TABLE(metaInfo) \ - (((metaInfo)->pTableMeta != NULL) && ((metaInfo)->pTableMeta->tableType == TSDB_CHILD_TABLE)) - -#define UTIL_TABLE_IS_NORMAL_TABLE(metaInfo) \ - (!(UTIL_TABLE_IS_SUPER_TABLE(metaInfo) || UTIL_TABLE_IS_CHILD_TABLE(metaInfo))) - -#define UTIL_TABLE_IS_TMP_TABLE(metaInfo) \ - (((metaInfo)->pTableMeta != NULL) && ((metaInfo)->pTableMeta->tableType == TSDB_TEMP_TABLE)) - -TAOS_FIELD createField(const SSchema* pSchema); -void setColumn(SColumn* pColumn, uint64_t uid, const char* tableName, int8_t flag, const SSchema* pSchema); -SColumn createColumn(uint64_t uid, const char* tableName, int8_t flag, const SSchema* pSchema); - -SInternalField* insertFieldInfo(SFieldInfo* pFieldInfo, int32_t index, SSchema* field); -int32_t getNumOfFields(SFieldInfo* pFieldInfo); -SInternalField* getInternalField(SFieldInfo* pFieldInfo, int32_t index); - -int32_t parserValidateIdToken(SToken* pToken); -int32_t parserValidatePassword(SToken* pToken, SMsgBuf* pMsgBuf); -int32_t parserValidateNameToken(SToken* pToken); +typedef struct SMsgBuf { + int32_t len; + char *buf; +} SMsgBuf; int32_t buildInvalidOperationMsg(SMsgBuf* pMsgBuf, const char* msg); int32_t buildSyntaxErrMsg(SMsgBuf* pBuf, const char* additionalInfo, const char* sourceStr); -STableMetaInfo* addEmptyMetaInfo(SQueryStmtInfo* pQueryInfo); - -void columnListCopyAll(SArray* dst, const SArray* src); - -SColumn* columnListInsert(SArray* pColumnList, uint64_t uid, SSchema* pSchema, int32_t flag); -SColumn* insertPrimaryTsColumn(SArray* pColumnList, const char* colName, uint64_t tableUid); - -void cleanupTagCond(STagCond* pTagCond); -void cleanupColumnCond(SArray** pCond); - -uint32_t convertRelationalOperator(SToken *pToken); -int32_t getExprFunctionId(SExprInfo *pExprInfo); - -STableMeta* tableMetaDup(const STableMeta* pTableMeta); - -bool isDclSqlStatement(SSqlInfo* pSqlInfo); -bool isDdlSqlStatement(SSqlInfo* pSqlInfo); -bool isDqlSqlStatement(SSqlInfo* pSqlInfo); +int32_t parserValidateIdToken(SToken* pToken); typedef struct SKvParam { SKVRowBuilder *builder; @@ -78,10 +43,12 @@ typedef struct SKvParam { int32_t KvRowAppend(const void *value, int32_t len, void *param); -typedef int32_t (*_row_append_fn_t)(const void *value, int32_t len, void *param); -int32_t parseValueToken(char** end, SToken* pToken, SSchema* pSchema, int16_t timePrec, char* tmpTokenBuf, _row_append_fn_t func, void* param, SMsgBuf* pMsgBuf); - -int32_t createSName(SName* pName, SToken* pTableName, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); +STableMeta* tableMetaDup(const STableMeta* pTableMeta); +SSchema *getTableColumnSchema(const STableMeta *pTableMeta); +SSchema *getTableTagSchema(const STableMeta* pTableMeta); +int32_t getNumOfColumns(const STableMeta* pTableMeta); +int32_t getNumOfTags(const STableMeta* pTableMeta); +STableComInfo getTableInfo(const STableMeta* pTableMeta); #ifdef __cplusplus } diff --git a/source/libs/parser/inc/queryInfoUtil.h b/source/libs/parser/inc/queryInfoUtil.h deleted file mode 100644 index 638c3d11bf..0000000000 --- a/source/libs/parser/inc/queryInfoUtil.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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_QUERYINFOUTIL_H -#define TDENGINE_QUERYINFOUTIL_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "parserInt.h" - -SSchema* getTbnameColumnSchema(); - -int32_t getNumOfColumns(const STableMeta* pTableMeta); -int32_t getNumOfTags(const STableMeta* pTableMeta); -SSchema *getTableColumnSchema(const STableMeta *pTableMeta); -SSchema *getTableTagSchema(const STableMeta* pTableMeta); - -SArray *getCurrentExprList(SQueryStmtInfo* pQueryInfo); -size_t getNumOfExprs(SQueryStmtInfo* pQueryInfo); - -void addExprInfo(SArray* pExprList, int32_t index, SExprInfo* pExprInfo, int32_t level); -void updateExprInfo(SExprInfo* pExprInfo, int16_t functionId, int32_t colId, int16_t srcColumnIndex, int16_t resType, int16_t resSize); - -SExprInfo* getExprInfo(SQueryStmtInfo* pQueryInfo, int32_t index); - -void addExprInfoParam(SSqlExpr* pExpr, char* argument, int32_t type, int32_t bytes); - -void cleanupFieldInfo(SFieldInfo* pFieldInfo); - -STableComInfo getTableInfo(const STableMeta* pTableMeta); -SArray *extractFunctionList(SArray* pExprInfoList); - -#ifdef __cplusplus -} -#endif - -#endif // TDENGINE_QUERYINFOUTIL_H diff --git a/source/libs/parser/src/astGenerator.c b/source/libs/parser/src/astGenerator.c deleted file mode 100644 index 5dce5120f9..0000000000 --- a/source/libs/parser/src/astGenerator.c +++ /dev/null @@ -1,1158 +0,0 @@ -/* - * 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 . - */ - -#include "os.h" -#include "taos.h" -#include "tmsg.h" -#include "parserInt.h" -#include "tmsgtype.h" -#include "astGenerator.h" - -SArray *tListItemAppend(SArray *pList, SVariant *pVar, uint8_t sortOrder) { - if (pList == NULL) { - pList = taosArrayInit(4, sizeof(SListItem)); - } - - if (pVar == NULL) { - return pList; - } - - /* - * Here we do not employ the assign function, since we need the pz attribute of structure , which is the point to char string. - * Otherwise, the original pointer may be lost, which causes memory leak. - */ - SListItem item; - item.pVar = *pVar; - item.sortOrder = sortOrder; - - taosArrayPush(pList, &item); - return pList; -} - -SArray *tListItemInsert(SArray *pList, SVariant *pVar, uint8_t sortOrder, int32_t index) { - if (pList == NULL || pVar == NULL || index >= taosArrayGetSize(pList)) { - return tListItemAppend(pList, pVar, sortOrder); - } - - SListItem item; - item.pVar = *pVar; - item.sortOrder = sortOrder; - - taosArrayInsert(pList, index, &item); - return pList; -} - -SArray *tListItemAppendToken(SArray *pList, SToken *pAliasToken, uint8_t sortOrder) { - if (pList == NULL) { - pList = taosArrayInit(4, sizeof(SListItem)); - } - - if (pAliasToken) { - SListItem item; - taosVariantCreate(&item.pVar, pAliasToken->z, pAliasToken->n, pAliasToken->type); - item.sortOrder = sortOrder; - - taosArrayPush(pList, &item); - } - - return pList; -} - -SRelationInfo *setTableNameList(SRelationInfo *pRelationInfo, SToken *pName, SToken *pAlias) { - if (pRelationInfo == NULL) { - pRelationInfo = calloc(1, sizeof(SRelationInfo)); - pRelationInfo->list = taosArrayInit(4, sizeof(SRelElement)); - } - - pRelationInfo->type = SQL_FROM_NODE_TABLES; - SRelElement p = {.tableName = *pName}; - if (pAlias != NULL) { - p.aliasName = *pAlias; - } else { - TPARSER_SET_NONE_TOKEN(p.aliasName); - } - - taosArrayPush(pRelationInfo->list, &p); - return pRelationInfo; -} - -void *destroyRelationInfo(SRelationInfo *pRelationInfo) { - if (pRelationInfo == NULL) { - return NULL; - } - - if (pRelationInfo->type == SQL_FROM_NODE_TABLES) { - taosArrayDestroy(pRelationInfo->list); - } else { - size_t size = taosArrayGetSize(pRelationInfo->list); - for(int32_t i = 0; i < size; ++i) { - SSubclause* pa = taosArrayGetP(pRelationInfo->list, i); - destroyAllSqlNode(pa); - } - taosArrayDestroy(pRelationInfo->list); - } - - tfree(pRelationInfo); - return NULL; -} - -SRelationInfo *addSubquery(SRelationInfo *pRelationInfo, SSubclause *pSub, SToken *pAlias) { - if (pRelationInfo == NULL) { - pRelationInfo = calloc(1, sizeof(SRelationInfo)); - pRelationInfo->list = taosArrayInit(4, sizeof(SRelElement)); - } - - pRelationInfo->type = SQL_FROM_NODE_SUBQUERY; - - SRelElement p = {.pSubquery = pSub}; - if (pAlias != NULL) { - p.aliasName = *pAlias; - } else { - TPARSER_SET_NONE_TOKEN(p.aliasName); - } - - taosArrayPush(pRelationInfo->list, &p); - return pRelationInfo; -} - -// sql expr leaf node -tSqlExpr *tSqlExprCreateIdValue(SToken *pToken, int32_t optrType) { - tSqlExpr *pSqlExpr = calloc(1, sizeof(tSqlExpr)); - - if (pToken != NULL) { - pSqlExpr->exprToken = *pToken; - } - - if (optrType == TK_NULL) { -// if (pToken) { -// pToken->type = TSDB_DATA_TYPE_NULL; -// tVariantCreate(&pSqlExpr->value, pToken); -// } - pSqlExpr->tokenId = optrType; - pSqlExpr->type = SQL_NODE_VALUE; - } else if (optrType == TK_INTEGER || optrType == TK_STRING || optrType == TK_FLOAT || optrType == TK_BOOL) { - pSqlExpr->tokenId = optrType; - pSqlExpr->type = SQL_NODE_VALUE; - } else if (optrType == TK_NOW || optrType == TK_VARIABLE) { - pSqlExpr->tokenId = optrType; // TK_TIMESTAMP used to denote this is a timestamp value - pSqlExpr->type = SQL_NODE_VALUE; - } else { - // Here it must be the column name (tk_id) if it is not a number or string. - assert(optrType == TK_ID || optrType == TK_ALL); - if (pToken != NULL) { - pSqlExpr->columnName = *pToken; - } - - pSqlExpr->tokenId = optrType; - pSqlExpr->type = SQL_NODE_TABLE_COLUMN; - } - - return pSqlExpr; -} - -tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SToken *pFuncToken, SToken *endToken, int32_t optType) { - if (pFuncToken == NULL) { - return NULL; - } - - tSqlExpr *pExpr = calloc(1, sizeof(tSqlExpr)); - pExpr->tokenId = optType; - pExpr->type = SQL_NODE_SQLFUNCTION; - pExpr->Expr.paramList = pParam; - - int32_t len = (int32_t)((endToken->z + endToken->n) - pFuncToken->z); - pExpr->Expr.operand = (*pFuncToken); - - pExpr->exprToken.n = len; - pExpr->exprToken.z = pFuncToken->z; - pExpr->exprToken.type = pFuncToken->type; - - return pExpr; -} - -SArray *tRecordFuncName(SArray *pList, SToken *pToken) { - assert(pList != NULL && pToken != NULL); - taosArrayPush(pList, pToken); - return pList; -} - -tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType) { - tSqlExpr *pExpr = calloc(1, sizeof(tSqlExpr)); - pExpr->type = SQL_NODE_EXPR; - - if (pLeft != NULL && pRight != NULL && (optrType != TK_IN)) { - const char* endPos = pRight->exprToken.z + pRight->exprToken.n; - pExpr->exprToken.z = pLeft->exprToken.z; - pExpr->exprToken.n = (uint32_t)(endPos - pExpr->exprToken.z); - pExpr->exprToken.type = pLeft->exprToken.type; - } - - if (optrType == TK_IN) { - pExpr->tokenId = optrType; - pExpr->pLeft = pLeft; - - tSqlExpr *pRSub = calloc(1, sizeof(tSqlExpr)); - pRSub->tokenId = TK_SET; // TODO refactor ..... - pRSub->Expr.paramList = (SArray *)pRight; - - pExpr->pRight = pRSub; - } else { - pExpr->tokenId = optrType; - pExpr->pLeft = pLeft; - - if (pLeft != NULL && pRight == NULL) { - pRight = calloc(1, sizeof(tSqlExpr)); - } - - pExpr->pRight = pRight; - } - - return pExpr; -} - -tSqlExpr *tSqlExprClone(tSqlExpr *pSrc) { - tSqlExpr *pExpr = malloc(sizeof(tSqlExpr)); - memcpy(pExpr, pSrc, sizeof(*pSrc)); - - if (pSrc->pLeft) { - pExpr->pLeft = tSqlExprClone(pSrc->pLeft); - } - - if (pSrc->pRight) { - pExpr->pRight = tSqlExprClone(pSrc->pRight); - } - - memset(&pExpr->value, 0, sizeof(pExpr->value)); - taosVariantAssign(&pExpr->value, &pSrc->value); - - //we don't clone paramList now because clone is only used for between/and - assert(pSrc->Expr.paramList == NULL); - return pExpr; -} - -void tSqlExprCompact(tSqlExpr **pExpr) { - if (*pExpr == NULL || tSqlExprIsParentOfLeaf(*pExpr)) { - return; - } - - if ((*pExpr)->pLeft) { - tSqlExprCompact(&(*pExpr)->pLeft); - } - - if ((*pExpr)->pRight) { - tSqlExprCompact(&(*pExpr)->pRight); - } - - if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight == NULL && ((*pExpr)->tokenId == TK_OR || (*pExpr)->tokenId == TK_AND)) { - tSqlExprDestroy(*pExpr); - *pExpr = NULL; - } else if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight != NULL) { - tSqlExpr* tmpPtr = (*pExpr)->pRight; - (*pExpr)->pRight = NULL; - - tSqlExprDestroy(*pExpr); - (*pExpr) = tmpPtr; - } else if ((*pExpr)->pRight == NULL && (*pExpr)->pLeft != NULL) { - tSqlExpr* tmpPtr = (*pExpr)->pLeft; - (*pExpr)->pLeft = NULL; - - tSqlExprDestroy(*pExpr); - (*pExpr) = tmpPtr; - } -} - -bool tSqlExprIsLeaf(tSqlExpr *pExpr) { - return (pExpr->pRight == NULL && pExpr->pLeft == NULL) && - (pExpr->tokenId == 0 || - (pExpr->tokenId == TK_ID) || - (pExpr->tokenId == TK_BOOL || pExpr->tokenId == TK_STRING || pExpr->tokenId == TK_FLOAT) || - (pExpr->tokenId == TK_NULL) || - (pExpr->tokenId == TK_SET)); -} - -bool tSqlExprIsParentOfLeaf(tSqlExpr *pExpr) { - return (pExpr->pLeft != NULL && pExpr->pRight != NULL) && - (tSqlExprIsLeaf(pExpr->pLeft) && tSqlExprIsLeaf(pExpr->pRight)); -} - -static void doDestroySqlExprNode(tSqlExpr *pExpr) { - if (pExpr == NULL) { - return; - } - - taosVariantDestroy(&pExpr->value); - tSqlExprListDestroy(pExpr->Expr.paramList); - free(pExpr); -} - -void tSqlExprDestroy(tSqlExpr *pExpr) { - if (pExpr == NULL) { - return; - } - - tSqlExprDestroy(pExpr->pLeft); - pExpr->pLeft = NULL; - tSqlExprDestroy(pExpr->pRight); - pExpr->pRight = NULL; - - doDestroySqlExprNode(pExpr); -} - -SArray * tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SToken *pDistinct, SToken *pToken) { - - if (pList == NULL) { - pList = taosArrayInit(4, sizeof(tSqlExprItem)); - } - - if (pNode || pToken) { - struct tSqlExprItem item = {0}; - - item.pNode = pNode; - item.distinct = (pDistinct != NULL); - - if (pToken) { // set the as clause - item.aliasName = malloc(pToken->n + 1); - strncpy(item.aliasName, pToken->z, pToken->n); - item.aliasName[pToken->n] = 0; - - strdequote(item.aliasName); - } - - taosArrayPush(pList, &item); - } - - return pList; -} - -static void freeExprElem(void* item) { - tSqlExprItem* exprItem = item; - - tfree(exprItem->aliasName); - tSqlExprDestroy(exprItem->pNode); -} - -void tSqlExprListDestroy(SArray *pList) { - if (pList == NULL) { - return; - } - taosArrayDestroyEx(pList, freeExprElem); -} - -void tSqlExprEvaluate(tSqlExpr* pExpr) { - tSqlExpr *pLeft = pExpr->pLeft; - tSqlExpr *pRight = pExpr->pRight; - - if (pLeft == NULL || pRight == NULL) { - return; - } - - int32_t optrType = pExpr->tokenId; - - if ((optrType == TK_PLUS || optrType == TK_MINUS || optrType == TK_STAR || optrType == TK_DIVIDE || - optrType == TK_REM)) { - /* - * if a exprToken is noted as the TK_TIMESTAMP, the time precision is microsecond - * Otherwise, the time precision is adaptive, determined by the time precision from databases. - */ - int32_t ltoken = pLeft->tokenId; - int32_t rtoken = pRight->tokenId; - - if ((ltoken == TK_INTEGER && rtoken == TK_INTEGER) || (ltoken == TK_TIMESTAMP && rtoken == TK_TIMESTAMP)) { - pExpr->value.nType = TSDB_DATA_TYPE_BIGINT; - pExpr->tokenId = ltoken; - pExpr->type = SQL_NODE_VALUE; - - switch (optrType) { - case TK_PLUS: { - pExpr->value.i = pLeft->value.i + pRight->value.i; - break; - } - case TK_MINUS: { - pExpr->value.i = pLeft->value.i - pRight->value.i; - break; - } - case TK_STAR: { - pExpr->value.i = pLeft->value.i * pRight->value.i; - break; - } - case TK_DIVIDE: { - pExpr->tokenId = TK_FLOAT; - pExpr->value.nType = TSDB_DATA_TYPE_DOUBLE; - pExpr->value.d = (double)pLeft->value.i / pRight->value.i; - break; - } - case TK_REM: { - pExpr->value.i = pLeft->value.i % pRight->value.i; - break; - } - default: - assert(0); - } - - tSqlExprDestroy(pLeft); - tSqlExprDestroy(pRight); - - pExpr->pLeft = NULL; - pExpr->pRight = NULL; - } else if ((ltoken == TK_FLOAT && rtoken == TK_INTEGER) || (ltoken == TK_INTEGER && rtoken == TK_FLOAT) || - (ltoken == TK_FLOAT && rtoken == TK_FLOAT)) { - pExpr->value.nType = TSDB_DATA_TYPE_DOUBLE; - pExpr->tokenId = TK_FLOAT; - pExpr->type = SQL_NODE_VALUE; - - double left = (pLeft->value.nType == TSDB_DATA_TYPE_DOUBLE) ? pLeft->value.d : pLeft->value.i; - double right = (pRight->value.nType == TSDB_DATA_TYPE_DOUBLE) ? pRight->value.d : pRight->value.i; - - switch (optrType) { - case TK_PLUS: { - pExpr->value.d = left + right; - break; - } - case TK_MINUS: { - pExpr->value.d = left - right; - break; - } - case TK_STAR: { - pExpr->value.d = left * right; - break; - } - case TK_DIVIDE: { - pExpr->value.d = left / right; - break; - } - case TK_REM: { - pExpr->value.d = left - ((int64_t)(left / right)) * right; - break; - } - default: - assert(0); - } - - tSqlExprDestroy(pLeft); - tSqlExprDestroy(pRight); - - pExpr->pLeft = NULL; - pExpr->pRight = NULL; - } - } -} - -SSqlNode *tSetQuerySqlNode(SToken *pSelectToken, SArray *pSelNodeList, SRelationInfo *pFrom, tSqlExpr *pWhere, - SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, - SSessionWindowVal *pSession, SWindowStateVal *pWindowStateVal, SToken *pSliding, SArray *pFill, SLimit *pLimit, - SLimit *psLimit, tSqlExpr *pHaving) { - assert(pSelNodeList != NULL); - - SSqlNode *pSqlNode = calloc(1, sizeof(SSqlNode)); - - // all later sql string are belonged to the stream sql - pSqlNode->sqlstr = *pSelectToken; - pSqlNode->sqlstr.n = (uint32_t)strlen(pSqlNode->sqlstr.z); - - pSqlNode->pSelNodeList = pSelNodeList; - pSqlNode->from = pFrom; - pSqlNode->pGroupby = pGroupby; - pSqlNode->pSortOrder = pSortOrder; - pSqlNode->pWhere = pWhere; - pSqlNode->fillType = pFill; - pSqlNode->pHaving = pHaving; - - if (pLimit != NULL) { - pSqlNode->limit = *pLimit; - } else { - pSqlNode->limit.limit = -1; - pSqlNode->limit.offset = 0; - } - - if (psLimit != NULL) { - pSqlNode->slimit = *psLimit; - } else { - pSqlNode->slimit.limit = -1; - pSqlNode->slimit.offset = 0; - } - - if (pInterval != NULL) { - pSqlNode->interval = *pInterval; - } else { - TPARSER_SET_NONE_TOKEN(pSqlNode->interval.interval); - TPARSER_SET_NONE_TOKEN(pSqlNode->interval.offset); - } - - if (pSliding != NULL) { - pSqlNode->sliding = *pSliding; - } else { - TPARSER_SET_NONE_TOKEN(pSqlNode->sliding); - } - - if (pSession != NULL) { - pSqlNode->sessionVal = *pSession; - } else { - TPARSER_SET_NONE_TOKEN(pSqlNode->sessionVal.gap); - TPARSER_SET_NONE_TOKEN(pSqlNode->sessionVal.col); - } - - if (pWindowStateVal != NULL) { - pSqlNode->windowstateVal = *pWindowStateVal; - } else { - TPARSER_SET_NONE_TOKEN(pSqlNode->windowstateVal.col); - } - - return pSqlNode; -} - -static FORCE_INLINE int32_t tStrTokenCompare(SToken* left, SToken* right) { - return (left->type == right->type && left->n == right->n && strncasecmp(left->z, right->z, left->n) == 0) ? 0 : 1; -} - -int32_t tSqlExprCompare(tSqlExpr *left, tSqlExpr *right) { - if ((left == NULL && right) || (left && right == NULL) || (left == NULL && right == NULL)) { - return 1; - } - - if (left->type != right->type) { - return 1; - } - - if (left->tokenId != right->tokenId) { - return 1; - } - - if ((left->pLeft && right->pLeft == NULL) - || (left->pLeft == NULL && right->pLeft) - || (left->pRight && right->pRight == NULL) - || (left->pRight == NULL && right->pRight) - || (left->Expr.paramList && right->Expr.paramList == NULL) - || (left->Expr.paramList == NULL && right->Expr.paramList)) { - return 1; - } - - if (taosVariantCompare(&left->value, &right->value)) { - return 1; - } - - if (tStrTokenCompare(&left->columnName, &right->columnName)) { - return 1; - } - - if (right->Expr.paramList && left->Expr.paramList) { - size_t size = taosArrayGetSize(right->Expr.paramList); - if (left->Expr.paramList && taosArrayGetSize(left->Expr.paramList) != size) { - return 1; - } - - for (int32_t i = 0; i < size; i++) { - tSqlExprItem* pLeftElem = taosArrayGet(left->Expr.paramList, i); - tSqlExpr* pSubLeft = pLeftElem->pNode; - tSqlExprItem* pRightElem = taosArrayGet(right->Expr.paramList, i); - tSqlExpr* pSubRight = pRightElem->pNode; - - if (tSqlExprCompare(pSubLeft, pSubRight)) { - return 1; - } - } - } - - if (left->pLeft && tSqlExprCompare(left->pLeft, right->pLeft)) { - return 1; - } - - if (left->pRight && tSqlExprCompare(left->pRight, right->pRight)) { - return 1; - } - - return 0; -} - -SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SSqlNode *pSelect, int32_t type) { - SCreateTableSql *pCreate = calloc(1, sizeof(SCreateTableSql)); - - switch (type) { - case TSDB_SQL_CREATE_TABLE: { - pCreate->colInfo.pColumns = pCols; - assert(pTags == NULL); - break; - } - case TSDB_SQL_CREATE_STABLE: { - pCreate->colInfo.pColumns = pCols; - pCreate->colInfo.pTagColumns = pTags; - assert(pTags != NULL && pCols != NULL); - break; - } -// case TSQL_CREATE_STREAM: { -// pCreate->pSelect = pSelect; -// break; -// } - - default: - assert(false); - } - - pCreate->type = type; - return pCreate; -} - -SAlterTableInfo *tSetAlterTableInfo(SToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableType) { - SAlterTableInfo *pAlterTable = calloc(1, sizeof(SAlterTableInfo)); - - pAlterTable->name = *pTableName; - pAlterTable->type = type; - pAlterTable->tableType = tableType; - - if (type == TSDB_ALTER_TABLE_ADD_COLUMN || type == TSDB_ALTER_TABLE_ADD_TAG || type == TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES || type == TSDB_ALTER_TABLE_UPDATE_TAG_BYTES) { - pAlterTable->pAddColumns = pCols; - assert(pVals == NULL); - } else { - /* - * ALTER_TABLE_TAGS_CHG, ALTER_TABLE_TAGS_SET, ALTER_TABLE_TAGS_DROP, - * ALTER_TABLE_DROP_COLUMN - */ - pAlterTable->varList = pVals; - assert(pCols == NULL); - } - - return pAlterTable; -} - -SCreatedTableInfo createNewChildTableInfo(SToken *pTableName, SArray *pTagNames, SArray *pTagVals, SToken *pToken, SToken* igExists) { - SCreatedTableInfo info; - memset(&info, 0, sizeof(SCreatedTableInfo)); - - info.name = *pToken; - info.pTagNames = pTagNames; - info.pTagVals = pTagVals; - info.stbName = *pTableName; - info.igExist = (igExists->n > 0)? 1:0; - - return info; -} - -void destroyAllSqlNode(struct SSubclause *pSub) { - if (pSub->node == NULL) { - return; - } - - size_t size = taosArrayGetSize(pSub->node); - for(int32_t i = 0; i < size; ++i) { - SSqlNode *pNode = taosArrayGetP(pSub->node, i); - destroySqlNode(pNode); - } - - taosArrayDestroy(pSub->node); -} - -static void freeItem(void *pItem) { - SListItem* p = (SListItem*) pItem; - taosVariantDestroy(&p->pVar); -} - -void destroySqlNode(SSqlNode *pSqlNode) { - if (pSqlNode == NULL) { - return; - } - - tSqlExprListDestroy(pSqlNode->pSelNodeList); - pSqlNode->pSelNodeList = NULL; - - tSqlExprDestroy(pSqlNode->pWhere); - pSqlNode->pWhere = NULL; - - taosArrayDestroyEx(pSqlNode->pSortOrder, freeItem); - pSqlNode->pSortOrder = NULL; - - taosArrayDestroyEx(pSqlNode->pGroupby, freeItem); - pSqlNode->pGroupby = NULL; - - pSqlNode->from = destroyRelationInfo(pSqlNode->from); - - taosArrayDestroyEx(pSqlNode->fillType, freeItem); - pSqlNode->fillType = NULL; - - tSqlExprDestroy(pSqlNode->pHaving); - free(pSqlNode); -} - -void freeCreateTableInfo(void* p) { - SCreatedTableInfo* pInfo = (SCreatedTableInfo*) p; - taosArrayDestroy(pInfo->pTagNames); - taosArrayDestroy(pInfo->pTagVals); - tfree(pInfo->fullname); -} - -SSqlInfo* setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SToken *pTableName, int32_t type) { - pInfo->type = type; - - if (type == TSDB_SQL_SELECT) { - pInfo->sub = *(SSubclause*) pSqlExprInfo; - tfree(pSqlExprInfo); - } else { - pInfo->pCreateTableInfo = pSqlExprInfo; - } - - if (pTableName != NULL) { - pInfo->pCreateTableInfo->name = *pTableName; - } - - return pInfo; -} - -SSubclause* setSubclause(SSubclause* pSub, void *pSqlNode) { - if (pSub == NULL) { - pSub = malloc(sizeof(SSubclause)); - - pSub->unionType = SQL_TYPE_UNIONALL; - pSub->node = taosArrayInit(1, POINTER_BYTES); - } - - taosArrayPush(pSub->node, &pSqlNode); - return pSub; -} - -SSubclause* appendSelectClause(SSubclause *pSub, int32_t type, void *pSubclause) { - taosArrayPush(pSub->node, &pSubclause); - if (type == SQL_TYPE_UNION) { - pSub->unionType = type; - } - - return pSub; -} - -void setCreatedTableName(SSqlInfo *pInfo, SToken *pTableNameToken, SToken *pIfNotExists) { - pInfo->pCreateTableInfo->name = *pTableNameToken; - pInfo->pCreateTableInfo->existCheck = (pIfNotExists->n != 0); -} - -void* destroyCreateTableSql(SCreateTableSql* pCreate) { - destroySqlNode(pCreate->pSelect); - - taosArrayDestroy(pCreate->colInfo.pColumns); - taosArrayDestroy(pCreate->colInfo.pTagColumns); - - taosArrayDestroyEx(pCreate->childTableInfo, freeCreateTableInfo); - tfree(pCreate); - - return NULL; -} - -void setDropFuncInfo(SSqlInfo *pInfo, int32_t type, SToken* pToken) { - pInfo->type = type; - - if (pInfo->pMiscInfo == NULL) { - pInfo->pMiscInfo = (SMiscInfo *)calloc(1, sizeof(SMiscInfo)); - pInfo->pMiscInfo->a = taosArrayInit(4, sizeof(SToken)); - } - - taosArrayPush(pInfo->pMiscInfo->a, pToken); -} - -void setCreateFuncInfo(SSqlInfo *pInfo, int32_t type, SToken *pName, SToken *pPath, SField *output, SToken* bufSize, int32_t funcType) { - pInfo->type = type; - if (pInfo->pMiscInfo == NULL) { - pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); - } - - pInfo->pMiscInfo->funcOpt.name = *pName; - pInfo->pMiscInfo->funcOpt.path = *pPath; - pInfo->pMiscInfo->funcOpt.output = *output; - pInfo->pMiscInfo->funcOpt.type = funcType; - if (bufSize->n > 0) { - pInfo->pMiscInfo->funcOpt.bufSize = strtol(bufSize->z, NULL, 10); - } else { - pInfo->pMiscInfo->funcOpt.bufSize = 0; - } -} - -void destroySqlInfo(SSqlInfo *pInfo) { - if (pInfo == NULL) { - return; - } - - taosArrayDestroy(pInfo->funcs); - if (pInfo->type == TSDB_SQL_SELECT) { - destroyAllSqlNode(&pInfo->sub); - } else if (pInfo->type == TSDB_SQL_CREATE_STABLE || pInfo->type == TSDB_SQL_CREATE_TABLE) { - pInfo->pCreateTableInfo = destroyCreateTableSql(pInfo->pCreateTableInfo); - } else if (pInfo->type == TSDB_SQL_ALTER_TABLE) { - taosArrayDestroyEx(pInfo->pAlterInfo->varList, freeItem); - taosArrayDestroy(pInfo->pAlterInfo->pAddColumns); - tfree(pInfo->pAlterInfo->tagData.data); - tfree(pInfo->pAlterInfo); - } else if (pInfo->type == TSDB_SQL_COMPACT_VNODE) { - tSqlExprListDestroy(pInfo->sub.node); - } else { - if (pInfo->pMiscInfo != NULL) { - taosArrayDestroy(pInfo->pMiscInfo->a); - } - - if (pInfo->pMiscInfo != NULL && (pInfo->type == TSDB_SQL_CREATE_DB || pInfo->type == TSDB_SQL_ALTER_DB)) { - taosArrayDestroyEx(pInfo->pMiscInfo->dbOpt.keep, freeItem); - } - - tfree(pInfo->pMiscInfo); - } -} - -void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParam, ...) { - pInfo->type = type; - if (nParam == 0) { - return; - } - - if (pInfo->pMiscInfo == NULL) { - pInfo->pMiscInfo = (SMiscInfo *)calloc(1, sizeof(SMiscInfo)); - pInfo->pMiscInfo->a = taosArrayInit(4, sizeof(SToken)); - } - - va_list va; - va_start(va, nParam); - - while ((nParam--) > 0) { - SToken *pToken = va_arg(va, SToken *); - taosArrayPush(pInfo->pMiscInfo->a, pToken); - } - - va_end(va); -} - -void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SToken* pToken, SToken* existsCheck, int16_t dbType, int16_t tableType) { - pInfo->type = type; - - if (pInfo->pMiscInfo == NULL) { - pInfo->pMiscInfo = (SMiscInfo *)calloc(1, sizeof(SMiscInfo)); - pInfo->pMiscInfo->a = taosArrayInit(4, sizeof(SToken)); - } - - taosArrayPush(pInfo->pMiscInfo->a, pToken); - - pInfo->pMiscInfo->existsCheck = (existsCheck->n == 1); - pInfo->pMiscInfo->dbType = dbType; - pInfo->pMiscInfo->tableType = tableType; -} - -void setShowOptions(SSqlInfo *pInfo, int32_t type, SToken* prefix, SToken* pPatterns) { - if (pInfo->pMiscInfo == NULL) { - pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); - } - - pInfo->type = TSDB_SQL_SHOW; - - SShowInfo* pShowInfo = &pInfo->pMiscInfo->showOpt; - pShowInfo->showType = type; - - if (prefix != NULL && prefix->type != 0) { - pShowInfo->prefix = *prefix; - } else { - pShowInfo->prefix.type = 0; - } - - if (pPatterns != NULL && pPatterns->type != 0) { - pShowInfo->pattern = *pPatterns; - } else { - pShowInfo->pattern.type = 0; - } -} - -void setCreateDbInfo(SSqlInfo *pInfo, int32_t type, SToken *pToken, SCreateDbInfo *pDB, SToken *pIgExists) { - pInfo->type = type; - if (pInfo->pMiscInfo == NULL) { - pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); - } - - pInfo->pMiscInfo->dbOpt = *pDB; - pInfo->pMiscInfo->dbOpt.dbname = *pToken; - pInfo->pMiscInfo->dbOpt.ignoreExists = pIgExists->n; // sql.y has: ifnotexists(X) ::= IF NOT EXISTS. {X.n = 1;} -} - -void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SToken *pName, SToken *pPwd, SCreateAcctInfo *pAcctInfo) { - pInfo->type = type; - if (pInfo->pMiscInfo == NULL) { - pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); - } - - pInfo->pMiscInfo->acctOpt = *pAcctInfo; - - assert(pName != NULL); - pInfo->pMiscInfo->user.user = *pName; - - if (pPwd != NULL) { - pInfo->pMiscInfo->user.passwd = *pPwd; - } -} - -void setCreateUserSql(SSqlInfo *pInfo, SToken *pName, SToken *pPasswd) { - pInfo->type = TSDB_SQL_CREATE_USER; - if (pInfo->pMiscInfo == NULL) { - pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); - } - - assert(pName != NULL && pPasswd != NULL); - - pInfo->pMiscInfo->user.user = *pName; - pInfo->pMiscInfo->user.passwd = *pPasswd; -} - -void setKillSql(SSqlInfo *pInfo, int32_t type, SToken *id) { - pInfo->type = type; - if (pInfo->pMiscInfo == NULL) { - pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); - } - - assert(id != NULL); - pInfo->pMiscInfo->id = *id; -} - -void setAlterUserSql(SSqlInfo *pInfo, int16_t type, SToken *pName, SToken* pPwd, SToken *pPrivilege) { - pInfo->type = TSDB_SQL_ALTER_USER; - if (pInfo->pMiscInfo == NULL) { - pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); - } - - assert(pName != NULL); - - SUserInfo* pUser = &pInfo->pMiscInfo->user; - pUser->type = type; - pUser->user = *pName; - - if (pPwd != NULL) { - pUser->passwd = *pPwd; - } else { - pUser->passwd.type = TSDB_DATA_TYPE_NULL; - } - - if (pPrivilege != NULL) { - pUser->privilege = *pPrivilege; - } else { - pUser->privilege.type = TSDB_DATA_TYPE_NULL; - } -} - -void setCompactVnodeSql(SSqlInfo *pInfo, int32_t type, SArray *pParam) { - pInfo->type = type; - pInfo->sub.node = pParam; -} - -void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo) { - pDBInfo->compressionLevel= -1; - pDBInfo->walLevel = -1; - pDBInfo->fsyncPeriod = -1; - pDBInfo->commitTime = -1; - pDBInfo->numOfVgroups = 2; - pDBInfo->cacheBlockSize = -1; - pDBInfo->numOfBlocks = -1; - pDBInfo->maxRowsPerBlock = -1; - pDBInfo->minRowsPerBlock = -1; - pDBInfo->daysPerFile = -1; - pDBInfo->replica = -1; - pDBInfo->quorum = -1; - pDBInfo->keep = NULL; - pDBInfo->update = -1; - pDBInfo->cachelast = -1; - - memset(&pDBInfo->precision, 0, sizeof(SToken)); -} - -// prefix show db.tables; -void tSetDbName(SToken *pCpxName, SToken *pDb) { - pCpxName->type = pDb->type; - pCpxName->z = pDb->z; - pCpxName->n = pDb->n; -} - -void tSetColumnInfo(SField *pField, SToken *pName, SField *pType) { - int32_t maxLen = sizeof(pField->name) / sizeof(pField->name[0]); - - // The column name is too long, set it to be invalid. - if ((int32_t) pName->n >= maxLen) { - pField->name[0] = 0; - } else { - strncpy(pField->name, pName->z, pName->n); - pField->name[pName->n] = 0; - } - - // denote an invalid data type in the column definition. - pField->type = pType->type; - if(!isValidDataType(pField->type)){ - pField->bytes = 0; - } else { - pField->bytes = pType->bytes; - } -} - -static int32_t tryParseNameTwoParts(SToken *type) { - int32_t t = -1; - - char* str = strndup(type->z, type->n); - if (str == NULL) { - return t; - } - - char* p = strtok(str, " "); - if (p == NULL) { - tfree(str); - return t; - } else { - char* unsign = strtok(NULL, " "); - if (unsign == NULL) { - tfree(str); - return t; - } - - if (strncasecmp(unsign, "UNSIGNED", 8) == 0) { - for(int32_t j = TSDB_DATA_TYPE_TINYINT; j <= TSDB_DATA_TYPE_BIGINT; ++j) { - if (strcasecmp(p, tDataTypes[j].name) == 0) { - t = j; - break; - } - } - - tfree(str); - - if (t == -1) { - return -1; - } - - switch(t) { - case TSDB_DATA_TYPE_TINYINT: return TSDB_DATA_TYPE_UTINYINT; - case TSDB_DATA_TYPE_SMALLINT: return TSDB_DATA_TYPE_USMALLINT; - case TSDB_DATA_TYPE_INT: return TSDB_DATA_TYPE_UINT; - case TSDB_DATA_TYPE_BIGINT: return TSDB_DATA_TYPE_UBIGINT; - default: - return -1; - } - - } else { - tfree(str); - return -1; - } - } -} - -void tSetColumnType(SField *pField, SToken *type) { - // set the field type invalid - pField->type = -1; - pField->name[0] = 0; - - int32_t i = 0; - while (i < tListLen(tDataTypes)) { - if ((type->n == tDataTypes[i].nameLen) && - (strncasecmp(type->z, tDataTypes[i].name, tDataTypes[i].nameLen) == 0)) { - break; - } - - i += 1; - } - - // no qualified data type found, try unsigned data type - if (i == tListLen(tDataTypes)) { - i = tryParseNameTwoParts(type); - if (i == -1) { - return; - } - } - - pField->type = i; - pField->bytes = tDataTypes[i].bytes; - - if (i == TSDB_DATA_TYPE_NCHAR) { - /* - * for nchar, the TOKENTYPE is the number of character, so the length is the - * number of bytes in UCS-4 format, which is 4 times larger than the number of characters - */ - if (type->type == 0) { - pField->bytes = 0; - } else { - int32_t bytes = -(int32_t)(type->type); - if (bytes > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) { - // overflowed. set bytes to -1 so that error can be reported - bytes = -1; - } else { - bytes = bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE; - } - pField->bytes = (int16_t)bytes; - } - } else if (i == TSDB_DATA_TYPE_BINARY) { - /* for binary, the TOKENTYPE is the length of binary */ - if (type->type == 0) { - pField->bytes = 0; - } else { - int32_t bytes = -(int32_t)(type->type); - if (bytes > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) { - // overflowed. set bytes to -1 so that error can be reported - bytes = -1; - } else { - bytes += VARSTR_HEADER_SIZE; - } - - pField->bytes = (int16_t)bytes; - } - } -} - -SSqlInfo doGenerateAST(const char *pStr) { - void *pParser = ParseAlloc(malloc); - - SSqlInfo sqlInfo = {0}; - sqlInfo.valid = true; - sqlInfo.funcs = taosArrayInit(4, sizeof(SToken)); - - int32_t i = 0; - while (1) { - SToken t0 = {0}; - - if (pStr[i] == 0) { - Parse(pParser, 0, t0, &sqlInfo); - goto abort_parse; - } - - t0.n = tGetToken((char *)&pStr[i], &t0.type); - t0.z = (char *)(pStr + i); - i += t0.n; - - switch (t0.type) { - case TK_SPACE: - case TK_COMMENT: { - break; - } - case TK_SEMI: { - Parse(pParser, 0, t0, &sqlInfo); - goto abort_parse; - } - - case TK_QUESTION: - case TK_ILLEGAL: { - snprintf(sqlInfo.msg, tListLen(sqlInfo.msg), "unrecognized token: \"%s\"", t0.z); - sqlInfo.valid = false; - goto abort_parse; - } - - case TK_HEX: - case TK_OCT: - case TK_BIN: { - snprintf(sqlInfo.msg, tListLen(sqlInfo.msg), "unsupported token: \"%s\"", t0.z); - sqlInfo.valid = false; - goto abort_parse; - } - - default: - Parse(pParser, t0.type, t0, &sqlInfo); - if (sqlInfo.valid == false) { - goto abort_parse; - } - } - } - -abort_parse: - ParseFree(pParser, free); - return sqlInfo; -} diff --git a/source/libs/parser/src/astToMsg.c b/source/libs/parser/src/astToMsg.c deleted file mode 100644 index 963255527a..0000000000 --- a/source/libs/parser/src/astToMsg.c +++ /dev/null @@ -1,520 +0,0 @@ -/* - * 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 . - */ - -#include "astGenerator.h" -#include "parserInt.h" -#include "parserUtil.h" - -char* buildUserManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen) { - SCreateUserReq createReq = {0}; - - SUserInfo* pUser = &pInfo->pMiscInfo->user; - strncpy(createReq.user, pUser->user.z, pUser->user.n); - createReq.createType = pUser->type; - createReq.superUser = (int8_t)pUser->type; - - if (pUser->type == TSDB_ALTER_USER_PRIVILEGES) { - // pMsg->privilege = (char)pCmd->count; - } else { - strncpy(createReq.pass, pUser->passwd.z, pUser->passwd.n); - } - - int32_t tlen = tSerializeSCreateUserReq(NULL, 0, &createReq); - void* pReq = malloc(tlen); - if (pReq == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - - tSerializeSCreateUserReq(pReq, tlen, &createReq); - *outputLen = tlen; - return pReq; -} - -char* buildAcctManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen) { - SCreateAcctReq createReq = {0}; - - SToken* pName = &pInfo->pMiscInfo->user.user; - SToken* pPwd = &pInfo->pMiscInfo->user.passwd; - - strncpy(createReq.user, pName->z, pName->n); - strncpy(createReq.pass, pPwd->z, pPwd->n); - - SCreateAcctInfo* pAcctOpt = &pInfo->pMiscInfo->acctOpt; - - createReq.maxUsers = pAcctOpt->maxUsers; - createReq.maxDbs = pAcctOpt->maxDbs; - createReq.maxTimeSeries = pAcctOpt->maxTimeSeries; - createReq.maxStreams = pAcctOpt->maxStreams; - createReq.maxStorage = pAcctOpt->maxStorage; - - if (pAcctOpt->stat.n == 0) { - createReq.accessState = -1; - } else { - if (pAcctOpt->stat.z[0] == 'r' && pAcctOpt->stat.n == 1) { - createReq.accessState = TSDB_VN_READ_ACCCESS; - } else if (pAcctOpt->stat.z[0] == 'w' && pAcctOpt->stat.n == 1) { - createReq.accessState = TSDB_VN_WRITE_ACCCESS; - } else if (strncmp(pAcctOpt->stat.z, "all", 3) == 0 && pAcctOpt->stat.n == 3) { - createReq.accessState = TSDB_VN_ALL_ACCCESS; - } else if (strncmp(pAcctOpt->stat.z, "no", 2) == 0 && pAcctOpt->stat.n == 2) { - createReq.accessState = 0; - } - } - - int32_t tlen = tSerializeSCreateAcctReq(NULL, 0, &createReq); - void* pReq = malloc(tlen); - if (pReq == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - - tSerializeSCreateAcctReq(pReq, tlen, &createReq); - *outputLen = tlen; - return pReq; -} - -char* buildDropUserMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgBufLen) { - SDropUserReq dropReq = {0}; - - SToken* pName = taosArrayGet(pInfo->pMiscInfo->a, 0); - if (pName->n >= TSDB_USER_LEN) { - return NULL; - } - - strncpy(dropReq.user, pName->z, pName->n); - - int32_t tlen = tSerializeSDropUserReq(NULL, 0, &dropReq); - void* pReq = malloc(tlen); - if (pReq == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - - tSerializeSDropUserReq(pReq, tlen, &dropReq); - *outputLen = tlen; - return pReq; -} - -char* buildShowMsg(SShowInfo* pShowInfo, int32_t* outputLen, SParseContext* pCtx, SMsgBuf* pMsgBuf) { - SShowReq showReq = {.type = pShowInfo->showType}; - - if (pShowInfo->showType != TSDB_MGMT_TABLE_VNODES) { - SToken* pPattern = &pShowInfo->pattern; - if (pPattern->type > 0) { // only show tables support wildcard query - showReq.payloadLen = pPattern->n; - showReq.payload = malloc(showReq.payloadLen); - strncpy(showReq.payload, pPattern->z, pPattern->n); - } - } else { - SToken* pEpAddr = &pShowInfo->prefix; - assert(pEpAddr->n > 0 && pEpAddr->type > 0); - showReq.payloadLen = pEpAddr->n; - showReq.payload = malloc(showReq.payloadLen); - strncpy(showReq.payload, pEpAddr->z, pEpAddr->n); - } - - if (pShowInfo->showType == TSDB_MGMT_TABLE_STB || pShowInfo->showType == TSDB_MGMT_TABLE_VGROUP) { - SName n = {0}; - - if (pShowInfo->prefix.n > 0) { - if (pShowInfo->prefix.n >= TSDB_DB_FNAME_LEN) { - terrno = buildInvalidOperationMsg(pMsgBuf, "prefix name is too long"); - tFreeSShowReq(&showReq); - return NULL; - } - tNameSetDbName(&n, pCtx->acctId, pShowInfo->prefix.z, pShowInfo->prefix.n); - } else if (pCtx->db == NULL || strlen(pCtx->db) == 0) { - terrno = buildInvalidOperationMsg(pMsgBuf, "database is not specified"); - tFreeSShowReq(&showReq); - return NULL; - } else { - tNameSetDbName(&n, pCtx->acctId, pCtx->db, strlen(pCtx->db)); - } - - tNameGetFullDbName(&n, showReq.db); - } - - int32_t tlen = tSerializeSShowReq(NULL, 0, &showReq); - void* pReq = malloc(tlen); - if (pReq == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - - tSerializeSShowReq(pReq, tlen, &showReq); - tFreeSShowReq(&showReq); - *outputLen = tlen; - return pReq; -} - -static int32_t setKeepOption(SCreateDbReq* pMsg, const SCreateDbInfo* pCreateDb, SMsgBuf* pMsgBuf) { - const char* msg1 = "invalid number of keep options"; - const char* msg2 = "invalid keep value"; - const char* msg3 = "invalid keep value, should be keep0 <= keep1 <= keep2"; - - pMsg->daysToKeep0 = -1; - pMsg->daysToKeep1 = -1; - pMsg->daysToKeep2 = -1; - - SArray* pKeep = pCreateDb->keep; - if (pKeep != NULL) { - size_t s = taosArrayGetSize(pKeep); -#ifdef _STORAGE - if (s >= 4 || s <= 0) { -#else - if (s != 1) { -#endif - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - // tListI* p0 = taosArrayGet(pKeep, 0); - // tVariantListItem* p1 = (s > 1) ? taosArrayGet(pKeep, 1) : p0; - // tVariantListItem* p2 = (s > 2) ? taosArrayGet(pKeep, 2) : p1; - // - // if ((int32_t)p0->pVar.i64 <= 0 || (int32_t)p1->pVar.i64 <= 0 || (int32_t)p2->pVar.i64 <= 0) { - // return buildInvalidOperationMsg(pMsgBuf, msg2); - // } - // if (!(((int32_t)p0->pVar.i64 <= (int32_t)p1->pVar.i64) && ((int32_t)p1->pVar.i64 <= (int32_t)p2->pVar.i64))) { - // return buildInvalidOperationMsg(pMsgBuf, msg3); - // } - // - // pMsg->daysToKeep0 = htonl((int32_t)p0->pVar.i64); - // pMsg->daysToKeep1 = htonl((int32_t)p1->pVar.i64); - // pMsg->daysToKeep2 = htonl((int32_t)p2->pVar.i64); - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t setTimePrecision(SCreateDbReq* pMsg, const SCreateDbInfo* pCreateDbInfo, SMsgBuf* pMsgBuf) { - const char* msg = "invalid time precision"; - - pMsg->precision = TSDB_TIME_PRECISION_MILLI; // millisecond by default - - SToken* pToken = (SToken*)&pCreateDbInfo->precision; - if (pToken->n > 0) { - pToken->n = strdequote(pToken->z); - - if (strncmp(pToken->z, TSDB_TIME_PRECISION_MILLI_STR, pToken->n) == 0 && - strlen(TSDB_TIME_PRECISION_MILLI_STR) == pToken->n) { - // time precision for this db: million second - pMsg->precision = TSDB_TIME_PRECISION_MILLI; - } else if (strncmp(pToken->z, TSDB_TIME_PRECISION_MICRO_STR, pToken->n) == 0 && - strlen(TSDB_TIME_PRECISION_MICRO_STR) == pToken->n) { - pMsg->precision = TSDB_TIME_PRECISION_MICRO; - } else if (strncmp(pToken->z, TSDB_TIME_PRECISION_NANO_STR, pToken->n) == 0 && - strlen(TSDB_TIME_PRECISION_NANO_STR) == pToken->n) { - pMsg->precision = TSDB_TIME_PRECISION_NANO; - } else { - return buildInvalidOperationMsg(pMsgBuf, msg); - } - } - - return TSDB_CODE_SUCCESS; -} - -static void doSetDbOptions(SCreateDbReq* pMsg, const SCreateDbInfo* pCreateDb) { - pMsg->cacheBlockSize = pCreateDb->cacheBlockSize; - pMsg->totalBlocks = pCreateDb->numOfBlocks; - pMsg->daysPerFile = pCreateDb->daysPerFile; - pMsg->commitTime = (int32_t)pCreateDb->commitTime; - pMsg->minRows = pCreateDb->minRowsPerBlock; - pMsg->maxRows = pCreateDb->maxRowsPerBlock; - pMsg->fsyncPeriod = pCreateDb->fsyncPeriod; - pMsg->compression = (int8_t)pCreateDb->compressionLevel; - pMsg->walLevel = (char)pCreateDb->walLevel; - pMsg->replications = pCreateDb->replica; - pMsg->quorum = pCreateDb->quorum; - pMsg->ignoreExist = pCreateDb->ignoreExists; - pMsg->update = pCreateDb->update; - pMsg->cacheLastRow = pCreateDb->cachelast; - pMsg->numOfVgroups = pCreateDb->numOfVgroups; - pMsg->streamMode = pCreateDb->streamMode; -} - -int32_t setDbOptions(SCreateDbReq* pCreateDbMsg, const SCreateDbInfo* pCreateDbSql, SMsgBuf* pMsgBuf) { - doSetDbOptions(pCreateDbMsg, pCreateDbSql); - - if (setKeepOption(pCreateDbMsg, pCreateDbSql, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if (setTimePrecision(pCreateDbMsg, pCreateDbSql, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - return TSDB_CODE_SUCCESS; -} - -// can only perform the parameters based on the macro definitation -static int32_t doCheckDbOptions(SCreateDbReq* pCreate, SMsgBuf* pMsgBuf) { - char msg[512] = {0}; - - if (pCreate->walLevel != -1 && (pCreate->walLevel < TSDB_MIN_WAL_LEVEL || pCreate->walLevel > TSDB_MAX_WAL_LEVEL)) { - snprintf(msg, tListLen(msg), "invalid db option walLevel: %d, only 1-2 allowed", pCreate->walLevel); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - if (pCreate->replications != -1 && - (pCreate->replications < TSDB_MIN_DB_REPLICA_OPTION || pCreate->replications > TSDB_MAX_DB_REPLICA_OPTION)) { - snprintf(msg, tListLen(msg), "invalid db option replications: %d valid range: [%d, %d]", pCreate->replications, - TSDB_MIN_DB_REPLICA_OPTION, TSDB_MAX_DB_REPLICA_OPTION); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - int32_t blocks = pCreate->totalBlocks; - if (blocks != -1 && (blocks < TSDB_MIN_TOTAL_BLOCKS || blocks > TSDB_MAX_TOTAL_BLOCKS)) { - snprintf(msg, tListLen(msg), "invalid db option totalBlocks: %d valid range: [%d, %d]", blocks, - TSDB_MIN_TOTAL_BLOCKS, TSDB_MAX_TOTAL_BLOCKS); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - if (pCreate->quorum != -1 && - (pCreate->quorum < TSDB_MIN_DB_QUORUM_OPTION || pCreate->quorum > TSDB_MAX_DB_QUORUM_OPTION)) { - snprintf(msg, tListLen(msg), "invalid db option quorum: %d valid range: [%d, %d]", pCreate->quorum, - TSDB_MIN_DB_QUORUM_OPTION, TSDB_MAX_DB_QUORUM_OPTION); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - int32_t val = pCreate->daysPerFile; - if (val != -1 && (val < TSDB_MIN_DAYS_PER_FILE || val > TSDB_MAX_DAYS_PER_FILE)) { - snprintf(msg, tListLen(msg), "invalid db option daysPerFile: %d valid range: [%d, %d]", val, TSDB_MIN_DAYS_PER_FILE, - TSDB_MAX_DAYS_PER_FILE); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - val = pCreate->cacheBlockSize; - if (val != -1 && (val < TSDB_MIN_CACHE_BLOCK_SIZE || val > TSDB_MAX_CACHE_BLOCK_SIZE)) { - snprintf(msg, tListLen(msg), "invalid db option cacheBlockSize: %d valid range: [%d, %d]", val, - TSDB_MIN_CACHE_BLOCK_SIZE, TSDB_MAX_CACHE_BLOCK_SIZE); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - if (pCreate->precision != TSDB_TIME_PRECISION_MILLI && pCreate->precision != TSDB_TIME_PRECISION_MICRO && - pCreate->precision != TSDB_TIME_PRECISION_NANO) { - snprintf(msg, tListLen(msg), "invalid db option timePrecision: %d valid value: [%d, %d, %d]", pCreate->precision, - TSDB_TIME_PRECISION_MILLI, TSDB_TIME_PRECISION_MICRO, TSDB_TIME_PRECISION_NANO); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - val = pCreate->commitTime; - if (val != -1 && (val < TSDB_MIN_COMMIT_TIME || val > TSDB_MAX_COMMIT_TIME)) { - snprintf(msg, tListLen(msg), "invalid db option commitTime: %d valid range: [%d, %d]", val, TSDB_MIN_COMMIT_TIME, - TSDB_MAX_COMMIT_TIME); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - val = pCreate->fsyncPeriod; - if (val != -1 && (val < TSDB_MIN_FSYNC_PERIOD || val > TSDB_MAX_FSYNC_PERIOD)) { - snprintf(msg, tListLen(msg), "invalid db option fsyncPeriod: %d valid range: [%d, %d]", val, TSDB_MIN_FSYNC_PERIOD, - TSDB_MAX_FSYNC_PERIOD); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - if (pCreate->compression != -1 && - (pCreate->compression < TSDB_MIN_COMP_LEVEL || pCreate->compression > TSDB_MAX_COMP_LEVEL)) { - snprintf(msg, tListLen(msg), "invalid db option compression: %d valid range: [%d, %d]", pCreate->compression, - TSDB_MIN_COMP_LEVEL, TSDB_MAX_COMP_LEVEL); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - val = pCreate->numOfVgroups; - if (val < TSDB_MIN_VNODES_PER_DB || val > TSDB_MAX_VNODES_PER_DB) { - snprintf(msg, tListLen(msg), "invalid number of vgroups for DB:%d valid range: [%d, %d]", val, - TSDB_MIN_VNODES_PER_DB, TSDB_MAX_VNODES_PER_DB); - } - - val = pCreate->maxRows; - if (val < TSDB_MIN_MAX_ROW_FBLOCK || val > TSDB_MAX_MAX_ROW_FBLOCK) { - snprintf(msg, tListLen(msg), "invalid number of max rows in file block for DB:%d valid range: [%d, %d]", val, - TSDB_MIN_MAX_ROW_FBLOCK, TSDB_MAX_MAX_ROW_FBLOCK); - } - - val = pCreate->minRows; - if (val < TSDB_MIN_MIN_ROW_FBLOCK || val > TSDB_MAX_MIN_ROW_FBLOCK) { - snprintf(msg, tListLen(msg), "invalid number of min rows in file block for DB:%d valid range: [%d, %d]", val, - TSDB_MIN_MIN_ROW_FBLOCK, TSDB_MAX_MIN_ROW_FBLOCK); - } - - return TSDB_CODE_SUCCESS; -} - -char* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, int32_t* outputLen, SParseContext* pCtx, SMsgBuf* pMsgBuf) { - SCreateDbReq createReq = {0}; - - if (setDbOptions(&createReq, pCreateDbInfo, pMsgBuf) != TSDB_CODE_SUCCESS) { - terrno = TSDB_CODE_TSC_INVALID_OPERATION; - return NULL; - } - - SName name = {0}; - int32_t ret = tNameSetDbName(&name, pCtx->acctId, pCreateDbInfo->dbname.z, pCreateDbInfo->dbname.n); - if (ret != TSDB_CODE_SUCCESS) { - terrno = ret; - return NULL; - } - - tNameGetFullDbName(&name, createReq.db); - - if (doCheckDbOptions(&createReq, pMsgBuf) != TSDB_CODE_SUCCESS) { - terrno = TSDB_CODE_TSC_INVALID_OPERATION; - return NULL; - } - - int32_t tlen = tSerializeSCreateDbReq(NULL, 0, &createReq); - void* pReq = malloc(tlen); - if (pReq == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - - tSerializeSCreateDbReq(pReq, tlen, &createReq); - *outputLen = tlen; - return pReq; -} - -char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* outputLen, SParseContext* pParseCtx, - SMsgBuf* pMsgBuf) { - SMCreateStbReq createReq = {0}; - createReq.igExists = pCreateTableSql->existCheck ? 1 : 0; - createReq.pColumns = pCreateTableSql->colInfo.pColumns; - createReq.pTags = pCreateTableSql->colInfo.pTagColumns; - createReq.numOfColumns = (int32_t)taosArrayGetSize(pCreateTableSql->colInfo.pColumns); - createReq.numOfTags = (int32_t)taosArrayGetSize(pCreateTableSql->colInfo.pTagColumns); - - SName n = {0}; - if (createSName(&n, &pCreateTableSql->name, pParseCtx, pMsgBuf) != 0) { - return NULL; - } - - if (tNameExtractFullName(&n, createReq.name) != 0) { - buildInvalidOperationMsg(pMsgBuf, "invalid table name or database not specified"); - return NULL; - } - - int32_t tlen = tSerializeSMCreateStbReq(NULL, 0, &createReq); - void* pReq = malloc(tlen); - if (pReq == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - - tSerializeSMCreateStbReq(pReq, tlen, &createReq); - *outputLen = tlen; - return pReq; -} - -char* buildDropStableReq(SSqlInfo* pInfo, int32_t* outputLen, SParseContext* pParseCtx, SMsgBuf* pMsgBuf) { - SToken* tableName = taosArrayGet(pInfo->pMiscInfo->a, 0); - - SName name = {0}; - int32_t code = createSName(&name, tableName, pParseCtx, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - terrno = buildInvalidOperationMsg(pMsgBuf, "invalid table name"); - return NULL; - } - - SMDropStbReq dropReq = {0}; - code = tNameExtractFullName(&name, dropReq.name); - - assert(code == TSDB_CODE_SUCCESS && name.type == TSDB_TABLE_NAME_T); - dropReq.igNotExists = pInfo->pMiscInfo->existsCheck ? 1 : 0; - - int32_t tlen = tSerializeSMDropStbReq(NULL, 0, &dropReq); - void* pReq = malloc(tlen); - if (pReq == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - - tSerializeSMDropStbReq(pReq, tlen, &dropReq); - *outputLen = tlen; - return pReq; -} - -char* buildCreateDnodeMsg(SSqlInfo* pInfo, int32_t* outputLen, SMsgBuf* pMsgBuf) { - const char* msg1 = "invalid host name (name too long, maximum length 128)"; - const char* msg2 = "dnode name can not be string"; - const char* msg3 = "port should be an integer that is less than 65535 and greater than 0"; - const char* msg4 = "failed prepare create dnode message"; - - if (taosArrayGetSize(pInfo->pMiscInfo->a) != 2) { - buildInvalidOperationMsg(pMsgBuf, msg1); - return NULL; - } - - SToken* id = taosArrayGet(pInfo->pMiscInfo->a, 0); - if (id->type != TK_ID && id->type != TK_IPTOKEN) { - buildInvalidOperationMsg(pMsgBuf, msg2); - return NULL; - } - - SToken* port = taosArrayGet(pInfo->pMiscInfo->a, 1); - if (port->type != TK_INTEGER) { - buildInvalidOperationMsg(pMsgBuf, msg3); - return NULL; - } - - bool isSign = false; - int64_t val = 0; - - toInteger(port->z, port->n, 10, &val, &isSign); - if (val >= UINT16_MAX || val <= 0) { - buildInvalidOperationMsg(pMsgBuf, msg3); - return NULL; - } - - SCreateDnodeReq createReq = {0}; - - strncpy(createReq.fqdn, id->z, id->n); - createReq.port = val; - - int32_t tlen = tSerializeSCreateDnodeReq(NULL, 0, &createReq); - void* pReq = malloc(tlen); - if (pReq == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - - tSerializeSCreateDnodeReq(pReq, tlen, &createReq); - *outputLen = tlen; - return pReq; -} - -char* buildDropDnodeMsg(SSqlInfo* pInfo, int32_t* outputLen, SMsgBuf* pMsgBuf) { - SDropDnodeReq dropReq = {0}; - - SToken* pzName = taosArrayGet(pInfo->pMiscInfo->a, 0); - - char* end = NULL; - dropReq.dnodeId = strtoll(pzName->z, &end, 10); - - if (end - pzName->z != pzName->n) { - buildInvalidOperationMsg(pMsgBuf, "invalid dnode id"); - return NULL; - } - - int32_t tlen = tSerializeSDropDnodeReq(NULL, 0, &dropReq); - void* pReq = malloc(tlen); - if (pReq == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - - tSerializeSDropDnodeReq(pReq, tlen, &dropReq); - *outputLen = tlen; - return pReq; -} \ No newline at end of file diff --git a/source/libs/parser/src/astValidate.c b/source/libs/parser/src/astValidate.c deleted file mode 100644 index ffc9f4a3f6..0000000000 --- a/source/libs/parser/src/astValidate.c +++ /dev/null @@ -1,4002 +0,0 @@ -/* - * 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 . - */ - -#include "astGenerator.h" -#include "function.h" -#include "parserInt.h" -#include "parserUtil.h" -#include "queryInfoUtil.h" -#include "tbuffer.h" -#include "tglobal.h" -#include "tmsgtype.h" -#include "ttime.h" -#include "astToMsg.h" - -#define TSQL_TBNAME_L "tbname" -#define DEFAULT_PRIMARY_TIMESTAMP_COL_NAME "_c0" -#define VALID_COLUMN_INDEX(index) (((index).tableIndex >= 0) && ((index).columnIndex >= TSDB_TBNAME_COLUMN_INDEX)) - -#define TSWINDOW_IS_EQUAL(t1, t2) (((t1).skey == (t2).skey) && ((t1).ekey == (t2).ekey)) - -// -1 is tbname column index, so here use the -2 as the initial value -#define COLUMN_INDEX_INITIAL_VAL (-2) -#define COLUMN_INDEX_INITIALIZER { COLUMN_INDEX_INITIAL_VAL, COLUMN_INDEX_INITIAL_VAL } - -static int32_t resColId = 5000; -int32_t getNewResColId() { - return resColId++; -} - -static int32_t validateSelectNodeList(SQueryStmtInfo* pQueryInfo, SArray* pSelNodeList, bool outerQuery, SMsgBuf* pMsgBuf); -static int32_t extractFunctionParameterInfo(SQueryStmtInfo* pQueryInfo, int32_t tokenId, STableMetaInfo** pTableMetaInfo, SSchema* columnSchema, - tExprNode** pNode, SColumnIndex* pIndex, tSqlExprItem* pParamElem, SMsgBuf* pMsgBuf); - -void setTokenAndResColumnName(tSqlExprItem* pItem, char* resColumnName, char* rawName, int32_t nameLength) { - memset(resColumnName, 0, nameLength); - - int32_t len = ((int32_t)pItem->pNode->exprToken.n < nameLength) ? (int32_t)pItem->pNode->exprToken.n : nameLength; - strncpy(rawName, pItem->pNode->exprToken.z, len); - - if (pItem->aliasName != NULL) { - assert(strlen(pItem->aliasName) < nameLength); - tstrncpy(resColumnName, pItem->aliasName, len); - } else { - strncpy(resColumnName, rawName, len); - } -} - -static int32_t evaluateSqlNodeImpl(tSqlExpr* pExpr, int32_t tsPrecision) { - int32_t code = 0; - if (pExpr->type == SQL_NODE_EXPR) { - code = evaluateSqlNodeImpl(pExpr->pLeft, tsPrecision); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - code = evaluateSqlNodeImpl(pExpr->pRight, tsPrecision); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - if (pExpr->pLeft->type == SQL_NODE_VALUE && pExpr->pRight->type == SQL_NODE_VALUE) { - tSqlExpr* pLeft = pExpr->pLeft; - tSqlExpr* pRight = pExpr->pRight; - if ((pLeft->tokenId == TK_TIMESTAMP && (pRight->tokenId == TK_INTEGER || pRight->tokenId == TK_FLOAT)) || - ((pRight->tokenId == TK_TIMESTAMP && (pLeft->tokenId == TK_INTEGER || pLeft->tokenId == TK_FLOAT)))) { - return TSDB_CODE_TSC_SQL_SYNTAX_ERROR; - } else if (pLeft->tokenId == TK_TIMESTAMP && pRight->tokenId == TK_TIMESTAMP) { - tSqlExprEvaluate(pExpr); - } else { - tSqlExprEvaluate(pExpr); - } - } else { - // Other types of expressions are not evaluated, they will be handled during the validation of the abstract syntax tree. - } - } else if (pExpr->type == SQL_NODE_VALUE) { - if (pExpr->tokenId == TK_NOW) { - pExpr->value.i = taosGetTimestamp(tsPrecision); - pExpr->value.nType = TSDB_DATA_TYPE_BIGINT; - pExpr->tokenId = TK_TIMESTAMP; - } else if (pExpr->tokenId == TK_VARIABLE) { - char unit = 0; - SToken* pToken = &pExpr->exprToken; - int32_t ret = parseAbsoluteDuration(pToken->z, pToken->n, &pExpr->value.i, &unit, tsPrecision); - if (ret != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_SQL_SYNTAX_ERROR; - } - - pExpr->value.nType = TSDB_DATA_TYPE_BIGINT; - pExpr->tokenId = TK_TIMESTAMP; - } else if (pExpr->tokenId == TK_NULL) { - pExpr->value.nType = TSDB_DATA_TYPE_NULL; - } else if (pExpr->tokenId == TK_INTEGER || pExpr->tokenId == TK_STRING || pExpr->tokenId == TK_FLOAT || pExpr->tokenId == TK_BOOL) { - SToken* pToken = &pExpr->exprToken; - - int32_t tokenType = pToken->type; - toTSDBType(tokenType); - taosVariantCreate(&pExpr->value, pToken->z, pToken->n, tokenType); - } - - return TSDB_CODE_SUCCESS; - // other types of data are handled in the parent level. - } else if (pExpr->type == SQL_NODE_SQLFUNCTION) { - SArray* pParam = pExpr->Expr.paramList; - - if (pParam != NULL) { - for (int32_t i = 0; i < taosArrayGetSize(pParam); ++i) { - tSqlExprItem* pItem = taosArrayGet(pParam, i); - evaluateSqlNodeImpl(pItem->pNode, tsPrecision); - } - } - } - - return TSDB_CODE_SUCCESS; -} - -void destroyFilterInfo(SColumnFilterList* pFilterList) { - if (pFilterList->filterInfo == NULL) { - pFilterList->numOfFilters = 0; - return; - } - - for(int32_t i = 0; i < pFilterList->numOfFilters; ++i) { - if (pFilterList->filterInfo[i].filterstr) { - tfree(pFilterList->filterInfo[i].pz); - } - } - - tfree(pFilterList->filterInfo); - pFilterList->numOfFilters = 0; -} - -void columnDestroy(SColumn* pCol) { - destroyFilterInfo(&pCol->info.flist); - free(pCol); -} - -void destroyColumnList(SArray* pColumnList) { - if (pColumnList == NULL) { - return; - } - - size_t num = taosArrayGetSize(pColumnList); - for (int32_t i = 0; i < num; ++i) { - SColumn* pCol = taosArrayGetP(pColumnList, i); - columnDestroy(pCol); - } - - taosArrayDestroy(pColumnList); -} - -void clearTableMetaInfo(STableMetaInfo* pTableMetaInfo) { - if (pTableMetaInfo == NULL) { - return; - } - - tfree(pTableMetaInfo->pTableMeta); - tfree(pTableMetaInfo->vgroupList); - - destroyColumnList(pTableMetaInfo->tagColList); - pTableMetaInfo->tagColList = NULL; - - free(pTableMetaInfo); -} - -static STableMeta* extractTempTableMetaFromSubquery(SQueryStmtInfo* pUpstream) { - STableMetaInfo* pUpstreamTableMetaInfo = getMetaInfo(pUpstream, 0); - - int32_t numOfColumns = pUpstream->fieldsInfo.numOfOutput; - STableMeta *meta = calloc(1, sizeof(STableMeta) + sizeof(SSchema) * numOfColumns); - meta->tableType = TSDB_TEMP_TABLE; - - STableComInfo *info = &meta->tableInfo; - info->numOfColumns = numOfColumns; - info->precision = pUpstreamTableMetaInfo->pTableMeta->tableInfo.precision; - info->numOfTags = 0; - - int32_t n = 0; - for(int32_t i = 0; i < numOfColumns; ++i) { - SInternalField* pField = getInternalField(&pUpstream->fieldsInfo, i); - if (!pField->visible) { - continue; - } - - meta->schema[n] = pField->pExpr->base.resSchema; - info->rowSize += meta->schema[n].bytes; - n += 1; - } - - info->numOfColumns = n; - return meta; -} - -SQueryStmtInfo *createQueryInfo() { - SQueryStmtInfo* pQueryInfo = calloc(1, sizeof(SQueryStmtInfo)); - - pQueryInfo->fieldsInfo.internalField = taosArrayInit(4, sizeof(SInternalField)); - pQueryInfo->colList = taosArrayInit(4, POINTER_BYTES); - pQueryInfo->udColumnId = TSDB_UD_COLUMN_INDEX; - pQueryInfo->limit.limit = -1; - pQueryInfo->limit.offset = 0; - - pQueryInfo->slimit.limit = -1; - pQueryInfo->slimit.offset = 0; - pQueryInfo->pDownstream = taosArrayInit(4, POINTER_BYTES); - pQueryInfo->window = TSWINDOW_INITIALIZER; - - pQueryInfo->exprList = calloc(10, POINTER_BYTES); - - for(int32_t i = 0; i < 10; ++i) { - pQueryInfo->exprList[i] = taosArrayInit(4, POINTER_BYTES); - } - - pQueryInfo->exprListLevelIndex = 0; - - return pQueryInfo; -} - -static void destroyQueryInfoImpl(SQueryStmtInfo* pQueryInfo) { - cleanupTagCond(&pQueryInfo->tagCond); - cleanupColumnCond(&pQueryInfo->colCond); - cleanupFieldInfo(&pQueryInfo->fieldsInfo); - - dropAllExprInfo(pQueryInfo->exprList, 10); - - tfree(pQueryInfo->exprList); - - columnListDestroy(pQueryInfo->colList); - pQueryInfo->colList = NULL; - - if (pQueryInfo->groupbyExpr.columnInfo != NULL) { - taosArrayDestroy(pQueryInfo->groupbyExpr.columnInfo); - pQueryInfo->groupbyExpr.columnInfo = NULL; - } - - pQueryInfo->fillType = 0; - - tfree(pQueryInfo->fillVal); - tfree(pQueryInfo->buf); - - taosArrayDestroy(pQueryInfo->pDownstream); - pQueryInfo->pDownstream = NULL; - pQueryInfo->bufLen = 0; -} - -void destroyQueryInfo(SQueryStmtInfo* pQueryInfo) { - while (pQueryInfo != NULL) { - SQueryStmtInfo* p = pQueryInfo->sibling; - - size_t numOfUpstream = taosArrayGetSize(pQueryInfo->pDownstream); - for (int32_t i = 0; i < numOfUpstream; ++i) { - SQueryStmtInfo* pDownstream = taosArrayGetP(pQueryInfo->pDownstream, i); - destroyQueryInfoImpl(pDownstream); - clearAllTableMetaInfo(pDownstream, false, 0); - tfree(pDownstream); - } - - destroyQueryInfoImpl(pQueryInfo); - clearAllTableMetaInfo(pQueryInfo, false, 0); - tfree(pQueryInfo); - pQueryInfo = p; - } -} - -static int32_t doValidateSubquery(SSqlNode* pSqlNode, int32_t index, SQueryStmtInfo* pQueryInfo, SMsgBuf* pMsgBuf) { - SRelElement* subInfo = taosArrayGet(pSqlNode->from->list, index); - - // union all is not support currently - SSqlNode* p = taosArrayGetP(subInfo->pSubquery->node, 0); - if (taosArrayGetSize(subInfo->pSubquery->node) >= 2) { - return buildInvalidOperationMsg(pMsgBuf, "not support union in subquery"); - } - - SQueryStmtInfo* pSub = createQueryInfo(); - - SArray *pUdfInfo = NULL; - if (pQueryInfo->pUdfInfo) { - pUdfInfo = taosArrayDup(pQueryInfo->pUdfInfo); - } - - pSub->pUdfInfo = pUdfInfo; - int32_t code = validateSqlNode(p, pSub, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - // create dummy table meta info - STableMetaInfo* pTableMetaInfo1 = calloc(1, sizeof(STableMetaInfo)); - if (pTableMetaInfo1 == NULL) { - return TSDB_CODE_TSC_OUT_OF_MEMORY; - } - - pTableMetaInfo1->pTableMeta = extractTempTableMetaFromSubquery(pSub); - - if (subInfo->aliasName.n > 0) { - if (subInfo->aliasName.n >= TSDB_TABLE_FNAME_LEN) { - tfree(pTableMetaInfo1); - return buildInvalidOperationMsg(pMsgBuf, "subquery alias name too long"); - } - - tstrncpy(pTableMetaInfo1->aliasName, subInfo->aliasName.z, subInfo->aliasName.n + 1); - } - - taosArrayPush(pQueryInfo->pDownstream, &pSub); - - // NOTE: order mix up in subquery not support yet. - pQueryInfo->order = pSub->order; - - STableMetaInfo** tmp = realloc(pQueryInfo->pTableMetaInfo, (pQueryInfo->numOfTables + 1) * POINTER_BYTES); - if (tmp == NULL) { - tfree(pTableMetaInfo1); - return TSDB_CODE_TSC_OUT_OF_MEMORY; - } - - pQueryInfo->pTableMetaInfo = tmp; - - pQueryInfo->pTableMetaInfo[pQueryInfo->numOfTables] = pTableMetaInfo1; - pQueryInfo->numOfTables += 1; - - // all columns are added into the table column list - STableMeta* pMeta = pTableMetaInfo1->pTableMeta; - int32_t startOffset = (int32_t) taosArrayGetSize(pQueryInfo->colList); - - for(int32_t i = 0; i < pMeta->tableInfo.numOfColumns; ++i) { - columnListInsert(pQueryInfo->colList, pMeta->uid, &pMeta->schema[i], TSDB_COL_NORMAL); - } - - return TSDB_CODE_SUCCESS; -} - -int32_t getTableIndexImpl(SToken* pTableToken, SQueryStmtInfo* pQueryInfo, SColumnIndex* pIndex) { - if (pTableToken->n == 0) { // only one table and no table name prefix in column name - if (pQueryInfo->numOfTables == 1) { - pIndex->tableIndex = 0; - } else { - pIndex->tableIndex = COLUMN_INDEX_INITIAL_VAL; - } - - return TSDB_CODE_SUCCESS; - } - - pIndex->tableIndex = COLUMN_INDEX_INITIAL_VAL; - for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, i); - char* name = pTableMetaInfo->aliasName; - if (strncasecmp(name, pTableToken->z, pTableToken->n) == 0 && strlen(name) == pTableToken->n) { - pIndex->tableIndex = i; - return TSDB_CODE_SUCCESS; - } - } - - return TSDB_CODE_TSC_INVALID_OPERATION; -} - -void extractTableNameFromToken(SToken* pToken, SToken* pTable) { - const char sep = TS_PATH_DELIMITER[0]; - - if (pToken == pTable || pToken == NULL || pTable == NULL) { - return; - } - - char* r = strnchr(pToken->z, sep, pToken->n, false); - - if (r != NULL) { // record the table name token - pTable->n = (uint32_t)(r - pToken->z); - pTable->z = pToken->z; - - r += 1; - pToken->n -= (uint32_t)(r - pToken->z); - pToken->z = r; - } -} - -int32_t getTableIndexByName(SToken* pToken, SQueryStmtInfo* pQueryInfo, SColumnIndex* pIndex) { - SToken tableToken = {0}; - extractTableNameFromToken(pToken, &tableToken); - - if (getTableIndexImpl(&tableToken, pQueryInfo, pIndex) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - return TSDB_CODE_SUCCESS; -} - -static int16_t doGetColumnIndex(SQueryStmtInfo* pQueryInfo, int32_t index, const SToken* pToken, int16_t* type) { - STableMeta* pTableMeta = getMetaInfo(pQueryInfo, index)->pTableMeta; - - int32_t numOfCols = getNumOfColumns(pTableMeta) + getNumOfTags(pTableMeta); - SSchema* pSchema = getTableColumnSchema(pTableMeta); - - int16_t columnIndex = COLUMN_INDEX_INITIAL_VAL; - - for (int32_t i = 0; i < numOfCols; ++i) { - if (pToken->n != strlen(pSchema[i].name)) { - continue; - } - - if (strncasecmp(pSchema[i].name, pToken->z, pToken->n) == 0) { - columnIndex = i; - break; - } - } - - *type = (columnIndex >= getNumOfColumns(pTableMeta))? TSDB_COL_TAG:TSDB_COL_NORMAL; - return columnIndex; -} - -static bool isTablenameToken(SToken* token) { - SToken tmpToken = *token; - SToken tableToken = {0}; - - extractTableNameFromToken(&tmpToken, &tableToken); - return (tmpToken.n == strlen(TSQL_TBNAME_L) && strncasecmp(TSQL_TBNAME_L, tmpToken.z, tmpToken.n) == 0); -} - -int32_t doGetColumnIndexByName(SToken* pToken, SQueryStmtInfo* pQueryInfo, SColumnIndex* pIndex, SMsgBuf* pMsgBuf) { - const char* msg0 = "ambiguous column name"; - const char* msg1 = "invalid column name"; - - pIndex->type = TSDB_COL_NORMAL; - - if (isTablenameToken(pToken)) { - pIndex->columnIndex = TSDB_TBNAME_COLUMN_INDEX; - pIndex->type = TSDB_COL_TAG; - } else if (strlen(DEFAULT_PRIMARY_TIMESTAMP_COL_NAME) == pToken->n && - strncasecmp(pToken->z, DEFAULT_PRIMARY_TIMESTAMP_COL_NAME, pToken->n) == 0) { - pIndex->columnIndex = PRIMARYKEY_TIMESTAMP_COL_ID; // just make runtime happy, need fix java test case InsertSpecialCharacterJniTest - } else if (pToken->n == 0) { - pIndex->columnIndex = PRIMARYKEY_TIMESTAMP_COL_ID; // just make runtime happy, need fix java test case InsertSpecialCharacterJniTest - } else { - // not specify the table name, try to locate the table index by column name - if (pIndex->tableIndex == COLUMN_INDEX_INITIAL_VAL) { - for (int16_t i = 0; i < pQueryInfo->numOfTables; ++i) { - int16_t colIndex = doGetColumnIndex(pQueryInfo, i, pToken, &pIndex->type); - - if (colIndex != COLUMN_INDEX_INITIAL_VAL) { - if (pIndex->columnIndex != COLUMN_INDEX_INITIAL_VAL) { - return buildInvalidOperationMsg(pMsgBuf, msg0); - } else { - pIndex->tableIndex = i; - pIndex->columnIndex = colIndex; - } - } - } - } else { // table index is valid, get the column index - pIndex->columnIndex = doGetColumnIndex(pQueryInfo, pIndex->tableIndex, pToken, &pIndex->type); - } - - if (pIndex->columnIndex == COLUMN_INDEX_INITIAL_VAL) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - } - - if (VALID_COLUMN_INDEX(*pIndex)) { - return TSDB_CODE_SUCCESS; - } else { - return TSDB_CODE_TSC_INVALID_OPERATION; - } -} - -int32_t getColumnIndexByName(const SToken* pToken, SQueryStmtInfo* pQueryInfo, SColumnIndex* pIndex, SMsgBuf* pMsgBuf) { - if (pQueryInfo->pTableMetaInfo == NULL || pQueryInfo->numOfTables == 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - SToken tmpToken = *pToken; - if (getTableIndexByName(&tmpToken, pQueryInfo, pIndex) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - return doGetColumnIndexByName(&tmpToken, pQueryInfo, pIndex, pMsgBuf); -} - -int32_t validateGroupbyNode(SQueryStmtInfo* pQueryInfo, SArray* pList, SMsgBuf* pMsgBuf) { - const char* msg1 = "too many columns in group by clause"; - const char* msg2 = "invalid column name in group by clause"; - const char* msg3 = "columns from one table allowed as group by columns"; - const char* msg4 = "join query does not support group by"; - const char* msg5 = "not allowed column type for group by"; - const char* msg6 = "tags not allowed for table query"; - const char* msg7 = "normal column and tags can not be mixed up in group by clause"; - const char* msg8 = "normal column can only locate at the end of group by clause"; - - SGroupbyExpr* pGroupExpr = &(pQueryInfo->groupbyExpr); - pGroupExpr->columnInfo = taosArrayInit(4, sizeof(SColIndex)); - if (pGroupExpr->columnInfo == NULL) { - return TSDB_CODE_TSC_OUT_OF_MEMORY; - } - - // todo : handle two tables situation - STableMetaInfo* pTableMetaInfo = NULL; - if (pList == NULL) { - return TSDB_CODE_SUCCESS; - } - - if (pQueryInfo->numOfTables > 1) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - size_t num = taosArrayGetSize(pList); - if (num > TSDB_MAX_TAGS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - int32_t numOfGroupbyCols = 0; - SSchema *pSchema = NULL; - int32_t tableIndex = COLUMN_INDEX_INITIAL_VAL; - bool groupbyTag = false; - - for (int32_t i = 0; i < num; ++i) { - SListItem * pItem = taosArrayGet(pList, i); - SVariant* pVar = &pItem->pVar; - - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - SToken token = {pVar->nLen, pVar->nType, pVar->pz}; - if (getColumnIndexByName(&token, pQueryInfo, &index, pMsgBuf) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - // Group by multiple tables is not supported. - if (tableIndex == COLUMN_INDEX_INITIAL_VAL) { - tableIndex = index.tableIndex; - } else if (tableIndex != index.tableIndex) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - - if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { - pSchema = getTbnameColumnSchema(); - } else { - pSchema = getOneColumnSchema(pTableMeta, index.columnIndex); - } - - bool groupTag = TSDB_COL_IS_TAG(index.type); - if (groupTag) { - if (!UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { - return buildInvalidOperationMsg(pMsgBuf, msg6); - } - - groupbyTag = true; - - SColumn c = createColumn(pTableMeta->uid, pTableMetaInfo->aliasName, TSDB_COL_TAG, pSchema); - taosArrayPush(pGroupExpr->columnInfo, &c); - - columnListInsert(pTableMetaInfo->tagColList, pTableMeta->uid, pSchema, TSDB_COL_TAG); - } else { - // check if the column type is valid, here only support the bool/tinyint/smallint/bigint group by - if (pSchema->type == TSDB_DATA_TYPE_FLOAT || pSchema->type == TSDB_DATA_TYPE_DOUBLE) { - return buildInvalidOperationMsg(pMsgBuf, msg5); - } - - SColumn c = createColumn(pTableMeta->uid, pTableMetaInfo->aliasName, TSDB_COL_NORMAL, pSchema); - taosArrayPush(pGroupExpr->columnInfo, &c); - - columnListInsert(pQueryInfo->colList, pTableMeta->uid, pSchema, TSDB_COL_NORMAL); - - numOfGroupbyCols++; - pQueryInfo->info.groupbyColumn = true; - } - } - - if (numOfGroupbyCols > 0 && groupbyTag) { - return buildInvalidOperationMsg(pMsgBuf, msg7); - } - - // todo ??? - // 1. the normal column in the group by clause can only located at the end position - for(int32_t i = 0; i < num; ++i) { - SColIndex* pIndex = taosArrayGet(pGroupExpr->columnInfo, i); - if (TSDB_COL_IS_NORMAL_COL(pIndex->flag) && i != num - 1) { - return buildInvalidOperationMsg(pMsgBuf, msg8); - } - } - - pGroupExpr->groupbyTag = groupbyTag; - return TSDB_CODE_SUCCESS; -} - -int32_t checkForUnsupportedQuery(SQueryStmtInfo* pQueryInfo, SMsgBuf* pMsgBuf) { - const char* msg1 = "not support percentile/interp/block_dist in the outer query yet"; - - for (int32_t i = 0; i < getNumOfExprs(pQueryInfo); ++i) { - SExprInfo* pExpr = getExprInfo(pQueryInfo, i); - assert(pExpr->pExpr->nodeType == TEXPR_UNARYEXPR_NODE); - - int32_t f = getExprFunctionId(pExpr); - if (f == FUNCTION_PERCT || f == FUNCTION_INTERP) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - if (f == FUNCTION_BLKINFO && taosArrayGetSize(pQueryInfo->pDownstream) > 0) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - -#if 0 - //todo planner handle this - if (/*(timeWindowQuery || pQueryInfo->stateWindow) &&*/ f == FUNCTION_LAST) { - pExpr->base.numOfParams = 1; - pExpr->base.param[0].i = TSDB_ORDER_ASC; - pExpr->base.param[0].nType = TSDB_DATA_TYPE_INT; - } -#endif - } -} - -int32_t validateWhereNode(SQueryStmtInfo *pQueryInfo, tSqlExpr* pWhereExpr, SMsgBuf* pMsgBuf) { - return 0; -} - -static int32_t parseIntervalOffset(SQueryStmtInfo* pQueryInfo, SToken* offsetToken, int32_t precision, SMsgBuf* pMsgBuf) { - const char* msg1 = "interval offset cannot be negative"; - const char* msg2 = "interval offset should be shorter than interval"; - const char* msg3 = "cannot use 'year' as offset when interval is 'month'"; - - SToken* t = offsetToken; - SInterval* pInterval = &pQueryInfo->interval; - - if (t->n == 0) { - pInterval->offsetUnit = pInterval->intervalUnit; - pInterval->offset = 0; - return TSDB_CODE_SUCCESS; - } - - if (parseNatualDuration(t->z, t->n, &pInterval->offset, &pInterval->offsetUnit, precision) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if (pInterval->offset < 0) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - if (!TIME_IS_VAR_DURATION(pInterval->offsetUnit)) { - if (!TIME_IS_VAR_DURATION(pInterval->intervalUnit)) { - if (pInterval->offset > pInterval->interval) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - } - } else if (pInterval->offsetUnit == pInterval->intervalUnit) { - if (pInterval->offset >= pInterval->interval) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - } else if (pInterval->intervalUnit == 'n' && pInterval->offsetUnit == 'y') { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } else if (pInterval->intervalUnit == 'y' && pInterval->offsetUnit == 'n') { - if (pInterval->interval * 12 <= pQueryInfo->interval.offset) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - } else { - // TODO: offset should be shorter than interval, but how to check - // conflicts like 30days offset and 1 month interval - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t parseSlidingClause(SQueryStmtInfo* pQueryInfo, SToken* pSliding, int32_t precision, SMsgBuf* pMsgBuf) { - const char* msg1 = "sliding value no larger than the interval value"; - const char* msg2 = "sliding value can not less than 1% of interval value"; - const char* msg3 = "does not support sliding when interval is natural month/year"; - const char* msg4 = "sliding value too small"; - - const static int32_t INTERVAL_SLIDING_FACTOR = 100; - - SInterval* pInterval = &pQueryInfo->interval; - if (pSliding->n == 0) { - pInterval->slidingUnit = pInterval->intervalUnit; - pInterval->sliding = pInterval->interval; - return TSDB_CODE_SUCCESS; - } - - if (TIME_IS_VAR_DURATION(pInterval->intervalUnit)) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - parseAbsoluteDuration(pSliding->z, pSliding->n, &pInterval->sliding, &pInterval->slidingUnit, precision); - - // less than the threshold - if (pInterval->sliding < convertTimePrecision(tsMinSlidingTime, TSDB_TIME_PRECISION_MILLI, precision)) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - if (pInterval->sliding > pInterval->interval) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - if ((pInterval->interval != 0) && (pInterval->interval/pInterval->sliding > INTERVAL_SLIDING_FACTOR)) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - return TSDB_CODE_SUCCESS; -} - -static void setTsOutputExprInfo(SQueryStmtInfo* pQueryInfo, STableMetaInfo* pTableMetaInfo, int32_t outputIndex, int32_t tableIndex); - -// validate the interval info -int32_t validateIntervalNode(SQueryStmtInfo *pQueryInfo, SSqlNode* pSqlNode, SMsgBuf* pMsgBuf) { - const char* msg1 = "sliding cannot be used without interval"; - const char* msg2 = "only point interpolation query requires keyword EVERY"; - const char* msg3 = "interval value is too small"; - - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, 0); - STableComInfo tinfo = getTableInfo(pTableMetaInfo->pTableMeta); - - if (!TPARSER_HAS_TOKEN(pSqlNode->interval.interval)) { - if (TPARSER_HAS_TOKEN(pSqlNode->sliding)) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } else { - return TSDB_CODE_SUCCESS; - } - } - - // interval is not null - SToken *t = &pSqlNode->interval.interval; - if (parseNatualDuration(t->z, t->n, &pQueryInfo->interval.interval, - &pQueryInfo->interval.intervalUnit, tinfo.precision) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if (pQueryInfo->interval.interval <= 0) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - if (!TIME_IS_VAR_DURATION(pQueryInfo->interval.intervalUnit)) { - // interval cannot be less than 10 milliseconds - if (convertTimePrecision(pQueryInfo->interval.interval, tinfo.precision, TSDB_TIME_PRECISION_MICRO) < tsMinIntervalTime) { - char msg[50] = {0}; - snprintf(msg, 50, "interval time window can not be less than %d %s", tsMinIntervalTime, TSDB_TIME_PRECISION_MICRO_STR); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - } - - if (parseIntervalOffset(pQueryInfo, &pSqlNode->interval.offset, tinfo.precision, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if (parseSlidingClause(pQueryInfo, &pSqlNode->sliding, tinfo.precision, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if (tsCompatibleModel) { - SExprInfo* pFirstExpr = getExprInfo(pQueryInfo, 0); - if (pFirstExpr->pExpr->nodeType != TEXPR_FUNCTION_NODE || strcasecmp(pFirstExpr->pExpr->_function.functionName, "dummy") != 0) { - setTsOutputExprInfo(pQueryInfo, pTableMetaInfo, 0, 0); - } - } - - // It is a time window query - pQueryInfo->info.timewindow = true; - return TSDB_CODE_SUCCESS; -} - -int32_t validateSessionNode(SQueryStmtInfo *pQueryInfo, SSessionWindowVal* pSession, int32_t precision, SMsgBuf* pMsgBuf) { - const char* msg1 = "gap should be fixed time window"; - const char* msg2 = "only one type time window allowed"; - const char* msg3 = "invalid column name"; - const char* msg4 = "invalid time window"; - const char* msg5 = "only the primary time stamp column can be used in session window"; - - // no session window - if (!TPARSER_HAS_TOKEN(pSession->gap)) { - return TSDB_CODE_SUCCESS; - } - - SToken* col = &pSession->col; - SToken* gap = &pSession->gap; - - char timeUnit = 0; - if (parseNatualDuration(gap->z, gap->n, &pQueryInfo->sessionWindow.gap, &timeUnit, precision) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - if (TIME_IS_VAR_DURATION(timeUnit)) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - if (pQueryInfo->sessionWindow.gap != 0 && pQueryInfo->interval.interval != 0) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - if (pQueryInfo->sessionWindow.gap == 0) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if ((getColumnIndexByName(col, pQueryInfo, &index, pMsgBuf) != TSDB_CODE_SUCCESS)) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - if (index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_ID) { - return buildInvalidOperationMsg(pMsgBuf, msg5); - } - - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - - SSchema* pSchema = getOneColumnSchema(pTableMeta, index.columnIndex); - pQueryInfo->sessionWindow.col = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, index.type, pSchema); - pQueryInfo->info.sessionWindow = true; - return TSDB_CODE_SUCCESS; -} - -// parse the window_state -int32_t validateStateWindowNode(SQueryStmtInfo *pQueryInfo, SWindowStateVal* pWindowState, SMsgBuf* pMsgBuf) { - const char* msg1 = "invalid column name"; - const char* msg2 = "invalid column type to create state window"; - const char* msg3 = "not support state_window with group by"; - const char* msg4 = "function not support for super table query"; - const char* msg5 = "not support state_window on tag column"; - - SToken *col = &(pWindowState->col) ; - if (!TPARSER_HAS_TOKEN(*col)) { - return TSDB_CODE_SUCCESS; - } - - SGroupbyExpr* pGroupExpr = &pQueryInfo->groupbyExpr; - if (taosArrayGetSize(pGroupExpr->columnInfo) > 0) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByName(col, pQueryInfo, &index, pMsgBuf) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - STableMetaInfo *pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - - if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - if (TSDB_COL_IS_TAG(index.type)) { - return buildInvalidOperationMsg(pMsgBuf, msg5); - } - - SSchema* pSchema = getOneColumnSchema(pTableMeta, index.columnIndex); - if (pSchema->type == TSDB_DATA_TYPE_TIMESTAMP || IS_FLOAT_TYPE(pSchema->type)) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - pQueryInfo->stateWindow.col = createColumn(pTableMeta->uid, pTableMetaInfo->aliasName, index.type, pSchema); - pQueryInfo->info.stateWindow = true; - - columnListInsert(pQueryInfo->colList, pTableMeta->uid, pSchema, index.type); - return TSDB_CODE_SUCCESS; -} - -// parse the having clause in the first place -int32_t validateHavingNode(SQueryStmtInfo *pQueryInfo, SSqlNode* pSqlNode, SMsgBuf* pMsgBuf) { - return 0; -} - -int32_t validateLimitNode(SQueryStmtInfo *pQueryInfo, SSqlNode* pSqlNode, SMsgBuf* pMsgBuf) { - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, 0); - - const char* msg1 = "slimit/soffset only available for STable query"; - const char* msg2 = "slimit/soffset can not apply to projection query"; - const char* msg3 = "soffset/offset can not be less than 0"; - - // handle the limit offset value, validate the limit - pQueryInfo->limit = pSqlNode->limit; - pQueryInfo->slimit = pSqlNode->slimit; - -// tscDebug("0x%"PRIx64" limit:%" PRId64 ", offset:%" PRId64 " slimit:%" PRId64 ", soffset:%" PRId64, pSql->self, -// pQueryInfo->limit.limit, pQueryInfo->limit.offset, pQueryInfo->slimit.limit, pQueryInfo->slimit.offset); - - if (pQueryInfo->slimit.offset < 0 || pQueryInfo->limit.offset < 0) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - if (pQueryInfo->limit.limit == 0) { -// tscDebug("0x%"PRIx64" limit 0, no output result", pSql->self); - pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; - return TSDB_CODE_SUCCESS; - } - - if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { -// if (!tscQueryTags(pQueryInfo)) { // local handle the super table tag query -// if (tscIsProjectionQueryOnSTable(pQueryInfo, 0)) { -// if (pQueryInfo->slimit.limit > 0 || pQueryInfo->slimit.offset > 0) { -// return buildInvalidOperationMsg(pMsgBuf, msg2); -// } -// -// // for projection query on super table, all queries are subqueries -// if (tscNonOrderedProjectionQueryOnSTable(pQueryInfo, 0) && -// !TSDB_QUERY_HAS_TYPE(pQueryInfo->type, TSDB_QUERY_TYPE_JOIN_QUERY)) { -// pQueryInfo->type |= TSDB_QUERY_TYPE_SUBQUERY; -// } -// } -// } - - if (pQueryInfo->slimit.limit == 0) { -// tscDebug("0x%"PRIx64" slimit 0, no output result", pSql->self); - pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; - return TSDB_CODE_SUCCESS; - } - - // No tables included. No results generated. Query results are empty. - if (pTableMetaInfo->vgroupList->numOfVgroups == 0) { -// tscDebug("0x%"PRIx64" no table in super table, no output result", pSql->self); - pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; - return TSDB_CODE_SUCCESS; - } - } else { - if (pQueryInfo->slimit.limit != -1 || pQueryInfo->slimit.offset != 0) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - } - - return TSDB_CODE_SUCCESS; -} - -int32_t validateOrderbyNode(SQueryStmtInfo *pQueryInfo, SSqlNode* pSqlNode, SMsgBuf* pMsgBuf) { - const char* msg1 = "invalid column name in orderby clause"; - const char* msg2 = "too many order by columns"; - const char* msg3 = "only one column allowed in orderby"; - const char* msg4 = "invalid order by column index"; - - if (pSqlNode->pSortOrder == NULL) { - return TSDB_CODE_SUCCESS; - } - - pQueryInfo->order = taosArrayInit(4, sizeof(SOrder)); - - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, 0); - SArray* pSortOrder = pSqlNode->pSortOrder; - - /* - * for table query, there is only one or none order option is allowed, which is the - * ts or values(top/bottom) order is supported. - * - * for super table query, the order option must be less than 3. - */ - size_t size = taosArrayGetSize(pSortOrder); - if ((UTIL_TABLE_IS_NORMAL_TABLE(pTableMetaInfo) || UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) && (pQueryInfo->info.projectionQuery)) { - if (size > 1) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - } - - // handle the first part of order by - bool found = false; - for(int32_t i = 0; i < taosArrayGetSize(pSortOrder); ++i) { - SListItem* pItem = taosArrayGet(pSortOrder, i); - - SVariant* pVar = &pItem->pVar; - if (pVar->nType == TSDB_DATA_TYPE_BINARY) { - SOrder order = {0}; - - // find the orde column among the result field. - for (int32_t j = 0; j < getNumOfFields(&pQueryInfo->fieldsInfo); ++j) { - SInternalField* pInfo = taosArrayGet(pQueryInfo->fieldsInfo.internalField, j); - SSchema* pSchema = &pInfo->pExpr->base.resSchema; - if (strcasecmp(pVar->pz, pSchema->name) == 0) { - setColumn(&order.col, pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, TSDB_COL_TMP, pSchema); - - order.order = pItem->sortOrder; - taosArrayPush(pQueryInfo->order, &order); - found = true; - break; - } - } - - if (!found) { - return buildInvalidOperationMsg(pMsgBuf, "invalid order by column"); - } - - } else { // order by [1|2|3] - if (pVar->i > getNumOfFields(&pQueryInfo->fieldsInfo)) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - int32_t index = pVar->i - 1; - SExprInfo* pExprInfo = getExprInfo(pQueryInfo, index); - - SOrder c = {0}; - setColumn(&c.col, pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, TSDB_COL_TMP, &pExprInfo->base.resSchema); - c.order = pItem->sortOrder; - taosArrayPush(pQueryInfo->order, &c); - } - } - - return TSDB_CODE_SUCCESS; -} - -#if 0 -// set order by info -int32_t checkForInvalidOrderby(SQueryStmtInfo *pQueryInfo, SSqlNode* pSqlNode, SMsgBuf* pMsgBuf) { - const char* msg0 = "only one column allowed in orderby"; - const char* msg1 = "invalid column name in orderby clause"; - const char* msg2 = "too many order by columns"; - const char* msg3 = "only primary timestamp/tbname/first tag in groupby clause allowed"; - const char* msg4 = "only tag in groupby clause allowed in order clause"; - const char* msg5 = "only primary timestamp/column in top/bottom function allowed as order column"; - const char* msg6 = "only primary timestamp allowed as the second order column"; - const char* msg7 = "only primary timestamp/column in groupby clause allowed as order column"; - const char* msg8 = "only column in groupby clause allowed as order column"; - const char* msg9 = "orderby column must projected in subquery"; - const char* msg10 = "not support distinct mixed with order by"; - -// setDefaultOrderInfo(pQueryInfo); - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, 0); - SSchema* pSchema = getTableColumnSchema(pTableMetaInfo->pTableMeta); - int32_t numOfCols = getNumOfColumns(pTableMetaInfo->pTableMeta); - - if (pSqlNode->pSortOrder == NULL) { - return TSDB_CODE_SUCCESS; - } - - SArray* pSortOrder = pSqlNode->pSortOrder; - - /* - * for table query, there is only one or none order option is allowed, which is the - * ts or values(top/bottom) order is supported. - * - * for super table query, the order option must be less than 3. - */ - size_t size = taosArrayGetSize(pSortOrder); - if (UTIL_TABLE_IS_NORMAL_TABLE(pTableMetaInfo) || UTIL_TABLE_IS_TMP_TABLE(pTableMetaInfo)) { - if (size > 1) { - return buildInvalidOperationMsg(pMsgBuf, msg0); - } - } else { - if (size > 2) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - } - -#if 0 - if (size > 0 && pQueryInfo->distinct) { - return buildInvalidOperationMsg(pMsgBuf, msg10); - } -#endif - - // handle the first part of order by - SVariant* pVar = taosArrayGet(pSortOrder, 0); - -#if 0 - // e.g., order by 1 asc, return directly with out further check. - if (pVar->nType >= TSDB_DATA_TYPE_TINYINT && pVar->nType <= TSDB_DATA_TYPE_BIGINT) { - return TSDB_CODE_SUCCESS; - } -#endif - - SToken columnName = {pVar->nLen, pVar->nType, pVar->pz}; - - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { // super table query - if (getColumnIndexByName(&columnName, pQueryInfo, &index, pMsgBuf) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - bool orderByTags = false; - bool orderByTS = false; - bool orderByGroupbyCol = false; - - if (TSDB_COL_IS_TAG(index.type) && index.columnIndex != TSDB_TBNAME_COLUMN_INDEX) { - // it is a tag column - if (pQueryInfo->groupbyExpr.columnInfo == NULL) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - int32_t relTagIndex = index.columnIndex - numOfCols; - SColIndex* pColIndex = taosArrayGet(pQueryInfo->groupbyExpr.columnInfo, 0); - if (relTagIndex == pColIndex->colIndex) { - orderByTags = true; - } - } else if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { - orderByTags = true; - } - - if (PRIMARYKEY_TIMESTAMP_COL_ID == index.columnIndex) { - orderByTS = true; - } - - SArray *columnInfo = pQueryInfo->groupbyExpr.columnInfo; - if (columnInfo != NULL && taosArrayGetSize(columnInfo) > 0) { - SColIndex* pColIndex = taosArrayGet(columnInfo, 0); - if (PRIMARYKEY_TIMESTAMP_COL_ID != index.columnIndex && pColIndex->colIndex == index.columnIndex) { - orderByGroupbyCol = true; - } - } - - if (!(orderByTags || orderByTS || orderByGroupbyCol) /*&& !isTopBottomQuery(pQueryInfo)*/) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } else { // order by top/bottom result value column is not supported in case of interval query. - assert(!(orderByTags && orderByTS && orderByGroupbyCol)); - } - - size_t s = taosArrayGetSize(pSortOrder); - if (s == 1) { - if (orderByTags) { - pQueryInfo->groupbyExpr.orderIndex = index.columnIndex - numOfCols; - - SListItem* p1 = taosArrayGet(pSqlNode->pSortOrder, 0); - pQueryInfo->groupbyExpr.orderType = p1->sortOrder; - } else if (orderByGroupbyCol) { - SListItem* p1 = taosArrayGet(pSqlNode->pSortOrder, 0); - - pQueryInfo->groupbyExpr.orderType = p1->sortOrder; - pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; - } else if (isTopBottomQuery(pQueryInfo)) { - /* order of top/bottom query in interval is not valid */ - int32_t pos = tscExprTopBottomIndex(pQueryInfo); - assert(pos > 0); - - SExprInfo* pExpr = getExprInfo(pQueryInfo, pos - 1); -// assert(getExprFunctionId(pExpr) == FUNCTION_TS); - - pExpr = getExprInfo(pQueryInfo, pos); - - // other tag are not allowed - if (pExpr->base.pColumns->colIndex != index.columnIndex && index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_ID) { - return buildInvalidOperationMsg(pMsgBuf, msg5); - } - - SListItem* p1 = taosArrayGet(pSqlNode->pSortOrder, 0); - pQueryInfo->order.order = p1->sortOrder; - pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; - return TSDB_CODE_SUCCESS; - } else { - SListItem* p1 = taosArrayGet(pSqlNode->pSortOrder, 0); - - pQueryInfo->order.order = p1->sortOrder; - pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_ID; - - // orderby ts query on super table - if (tscOrderedProjectionQueryOnSTable(pQueryInfo, 0)) { - bool found = false; - for (int32_t i = 0; i < getNumOfExprs(pQueryInfo); ++i) { - SExprInfo* pExpr = getExprInfo(pQueryInfo, i); - if (getExprFunctionId(pExpr) == FUNCTION_PRJ && pExpr->base.pColumns->colId == PRIMARYKEY_TIMESTAMP_COL_ID) { - found = true; - break; - } - } - - if (!found && pQueryInfo->pDownstream) { - return buildInvalidOperationMsg(pMsgBuf, msg9); - } - - // this is a invisible output column, in order to used to sort the result. - setTsOutputExprInfo(pQueryInfo, pTableMetaInfo, 0, index.tableIndex); - } - } - } else { - SListItem *pItem = taosArrayGet(pSqlNode->pSortOrder, 0); - if (orderByTags) { - pQueryInfo->groupbyExpr.orderIndex = index.columnIndex - numOfCols; - pQueryInfo->groupbyExpr.orderType = pItem->sortOrder; - } else if (orderByGroupbyCol) { - pQueryInfo->order.order = pItem->sortOrder; - pQueryInfo->order.orderColId = index.columnIndex; - } else { - pQueryInfo->order.order = pItem->sortOrder; - pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_ID; - } - - pItem = taosArrayGet(pSqlNode->pSortOrder, 1); - SVariant* pVar2 = &pItem->pVar; - SToken cname = {pVar2->nLen, pVar2->nType, pVar2->pz}; - if (getColumnIndexByName(&cname, pQueryInfo, &index, pMsgBuf) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - if (index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_ID) { - return buildInvalidOperationMsg(pMsgBuf, msg6); - } else { - SListItem* p1 = taosArrayGet(pSortOrder, 1); - pQueryInfo->order.order = p1->sortOrder; - pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_ID; - } - } - - } else if (UTIL_TABLE_IS_NORMAL_TABLE(pTableMetaInfo) || UTIL_TABLE_IS_CHILD_TABLE(pTableMetaInfo)) { // check order by clause for normal table & temp table - if (getColumnIndexByName(&columnName, pQueryInfo, &index, pMsgBuf) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - if (index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_ID && !isTopBottomQuery(pQueryInfo)) { - bool validOrder = false; - SArray *columnInfo = pQueryInfo->groupbyExpr.columnInfo; - if (columnInfo != NULL && taosArrayGetSize(columnInfo) > 0) { - SColIndex* pColIndex = taosArrayGet(columnInfo, 0); - validOrder = (pColIndex->colIndex == index.columnIndex); - } - - if (!validOrder) { - return buildInvalidOperationMsg(pMsgBuf, msg7); - } - - SListItem* p1 = taosArrayGet(pSqlNode->pSortOrder, 0); - pQueryInfo->groupbyExpr.orderIndex = pSchema[index.columnIndex].colId; - pQueryInfo->groupbyExpr.orderType = p1->sortOrder; - } - - if (isTopBottomQuery(pQueryInfo)) { - SArray *columnInfo = pQueryInfo->groupbyExpr.columnInfo; - if (columnInfo != NULL && taosArrayGetSize(columnInfo) > 0) { - SColIndex* pColIndex = taosArrayGet(columnInfo, 0); - - if (pColIndex->colIndex == index.columnIndex) { - return buildInvalidOperationMsg(pMsgBuf, msg8); - } - } else { - int32_t pos = tscExprTopBottomIndex(pQueryInfo); - assert(pos > 0); - SExprInfo* pExpr = getExprInfo(pQueryInfo, pos - 1); - assert(getExprFunctionId(pExpr) == FUNCTION_TS); - - pExpr = getExprInfo(pQueryInfo, pos); - - if (pExpr->base.pColumns->colIndex != index.columnIndex && index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_ID) { - return buildInvalidOperationMsg(pMsgBuf, msg5); - } - } - - SListItem* pItem = taosArrayGet(pSqlNode->pSortOrder, 0); - pQueryInfo->order.order = pItem->sortOrder; - - pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; - return TSDB_CODE_SUCCESS; - } - - SListItem* pItem = taosArrayGet(pSqlNode->pSortOrder, 0); - pQueryInfo->order.order = pItem->sortOrder; - pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; - } else { - // handle the temp table order by clause. You can order by any single column in case of the temp table, created by - // inner subquery. - assert(UTIL_TABLE_IS_TMP_TABLE(pTableMetaInfo) && taosArrayGetSize(pSqlNode->pSortOrder) == 1); - - if (getColumnIndexByName(&columnName, pQueryInfo, &index, pMsgBuf) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - SListItem* pItem = taosArrayGet(pSqlNode->pSortOrder, 0); - pQueryInfo->order.order = pItem->sortOrder; - pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; - } - - return TSDB_CODE_SUCCESS; -} -#endif - -static int32_t checkFillQueryRange(SQueryStmtInfo* pQueryInfo, SMsgBuf* pMsgBuf) { - const char* msg1 = "start(end) time of time range required or time range too large"; - - if (pQueryInfo->interval.interval == 0) { - return TSDB_CODE_SUCCESS; - } - - // TODO disable this check temporarily -// bool initialWindows = TSWINDOW_IS_EQUAL(pQueryInfo->window, TSWINDOW_INITIALIZER); -// if (initialWindows) { -// return buildInvalidOperationMsg(pMsgBuf, msg1); -// } - - int64_t timeRange = TABS(pQueryInfo->window.skey - pQueryInfo->window.ekey); - - int64_t intervalRange = 0; - if (!TIME_IS_VAR_DURATION(pQueryInfo->interval.intervalUnit)) { - intervalRange = pQueryInfo->interval.interval; - - // number of result is not greater than 10,000,000 - if ((timeRange == 0) || (timeRange / intervalRange) >= MAX_INTERVAL_TIME_WINDOW) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - } - - return TSDB_CODE_SUCCESS; -} - -int32_t validateFillNode(SQueryStmtInfo *pQueryInfo, SSqlNode* pSqlNode, SMsgBuf* pMsgBuf) { - SArray* pFillToken = pSqlNode->fillType; - if (pSqlNode->fillType == NULL) { - return TSDB_CODE_SUCCESS; - } - - SListItem* pItem = taosArrayGet(pFillToken, 0); - - const int32_t START_INTERPO_COL_IDX = 1; - - const char* msg1 = "value is expected"; - const char* msg2 = "invalid fill option"; - const char* msg4 = "illegal value or data overflow"; - const char* msg6 = "not supported function now"; - - /* - * fill options are set at the end position, when all columns are set properly - * the columns may be increased due to group by operation - */ - if (checkFillQueryRange(pQueryInfo, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - - if (pItem->pVar.nType != TSDB_DATA_TYPE_BINARY) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - int32_t numOfFields = (int32_t) getNumOfFields(&pQueryInfo->fieldsInfo); - - pQueryInfo->fillVal = calloc(numOfFields, sizeof(int64_t)); - if (pQueryInfo->fillVal == NULL) { - return TSDB_CODE_TSC_OUT_OF_MEMORY; - } - - pQueryInfo->numOfFillVal = (int32_t)numOfFields; - if (strncasecmp(pItem->pVar.pz, "none", 4) == 0 && pItem->pVar.nLen == 4) { - pQueryInfo->fillType = TSDB_FILL_NONE; - } else if (strncasecmp(pItem->pVar.pz, "null", 4) == 0 && pItem->pVar.nLen == 4) { - pQueryInfo->fillType = TSDB_FILL_NULL; - for (int32_t i = START_INTERPO_COL_IDX; i < numOfFields; ++i) { - TAOS_FIELD* pField = &getInternalField(&pQueryInfo->fieldsInfo, i)->field; - setNull((char*)&pQueryInfo->fillVal[i], pField->type, pField->bytes); - } - } else if (strncasecmp(pItem->pVar.pz, "prev", 4) == 0 && pItem->pVar.nLen == 4) { - pQueryInfo->fillType = TSDB_FILL_PREV; -// if (pQueryInfo->info.interpQuery && pQueryInfo->order.order == TSDB_ORDER_DESC) { -// return buildInvalidOperationMsg(pMsgBuf, msg6); -// } - } else if (strncasecmp(pItem->pVar.pz, "next", 4) == 0 && pItem->pVar.nLen == 4) { - pQueryInfo->fillType = TSDB_FILL_NEXT; - } else if (strncasecmp(pItem->pVar.pz, "linear", 6) == 0 && pItem->pVar.nLen == 6) { - pQueryInfo->fillType = TSDB_FILL_LINEAR; - } else if (strncasecmp(pItem->pVar.pz, "value", 5) == 0 && pItem->pVar.nLen == 5) { - pQueryInfo->fillType = TSDB_FILL_SET_VALUE; - - size_t num = taosArrayGetSize(pFillToken); - if (num == 1) { // no actual value, return with error code - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - int32_t startPos = 1; - int32_t numOfFillVal = (int32_t)(num - 1); - - // for point interpolation query, we do not have the timestamp column - if (pQueryInfo->info.interpQuery) { - startPos = 0; - if (numOfFillVal > numOfFields) { - numOfFillVal = numOfFields; - } - } else { - numOfFillVal = TMIN(num, numOfFields); - } - - int32_t j = 1; - - for (int32_t i = startPos; i < numOfFillVal; ++i, ++j) { - TAOS_FIELD* pField = &getInternalField(&pQueryInfo->fieldsInfo, i)->field; - if (pField->type == TSDB_DATA_TYPE_BINARY || pField->type == TSDB_DATA_TYPE_NCHAR) { - setVardataNull((char*) &pQueryInfo->fillVal[i], pField->type); - continue; - } - - SVariant* p = taosArrayGet(pFillToken, j); - int32_t ret = taosVariantDump(p, (char*)&pQueryInfo->fillVal[i], pField->type, true); - if (ret != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - } - - if ((num < numOfFields) || ((num - 1 < numOfFields) && (pQueryInfo->info.interpQuery))) { - SListItem* lastItem = taosArrayGetLast(pFillToken); - - for (int32_t i = numOfFillVal; i < numOfFields; ++i) { - TAOS_FIELD* pField = &getInternalField(&pQueryInfo->fieldsInfo, i)->field; - - if (pField->type == TSDB_DATA_TYPE_BINARY || pField->type == TSDB_DATA_TYPE_NCHAR) { - setVardataNull((char*) &pQueryInfo->fillVal[i], pField->type); - } else { - taosVariantDump(&lastItem->pVar, (char*)&pQueryInfo->fillVal[i], pField->type, true); - } - } - } - } else { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - return TSDB_CODE_SUCCESS; -} - -static void pushDownAggFuncExprInfo(SQueryStmtInfo* pQueryInfo); -static void addColumnNodeFromLowerLevel(SQueryStmtInfo* pQueryInfo); - -static void freeItemHelper(void* pItem) { - void** p = pItem; - if (*p != NULL) { - tfree(*p); - } -} - -int32_t validateSqlNode(SSqlNode* pSqlNode, SQueryStmtInfo* pQueryInfo, SMsgBuf* pMsgBuf) { - assert(pSqlNode != NULL && (pSqlNode->from == NULL || taosArrayGetSize(pSqlNode->from->list) > 0)); - - const char* msg1 = "point interpolation query needs timestamp"; - const char* msg2 = "too many tables in from clause"; - const char* msg3 = "start(end) time of query range required or time range too large"; - const char* msg4 = "interval query not supported, since the result of sub query not include valid timestamp column"; - const char* msg5 = "only tag query not compatible with normal column filter"; - const char* msg7 = "derivative/twa/irate requires timestamp column exists in subquery"; - const char* msg8 = "condition missing for join query"; - - int32_t code = TSDB_CODE_SUCCESS; - - /* - * handle the sql expression without from subclause - * select server_status(); - * select server_version(); - * select client_version(); - * select database(); - * select 1+2; - * select now(); - */ - if (pSqlNode->from == NULL) { - assert(pSqlNode->fillType == NULL && pSqlNode->pGroupby == NULL && pSqlNode->pWhere == NULL && - pSqlNode->pSortOrder == NULL); - assert(0); -// return doLocalQueryProcess(pCmd, pQueryInfo, pSqlNode); - } - - if (pSqlNode->from->type == SQL_FROM_NODE_SUBQUERY) { - pQueryInfo->numOfTables = 0; - - // parse the subquery in the first place - int32_t numOfSub = (int32_t)taosArrayGetSize(pSqlNode->from->list); - for (int32_t i = 0; i < numOfSub; ++i) { - SRelElement* subInfo = taosArrayGet(pSqlNode->from->list, i); - code = doValidateSubquery(pSqlNode, i, pQueryInfo, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - } - - // parse the group by clause in the first place - if (validateGroupbyNode(pQueryInfo, pSqlNode->pGroupby, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if (validateSelectNodeList(pQueryInfo, pSqlNode->pSelNodeList, true, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - code = checkForUnsupportedQuery(pQueryInfo, pMsgBuf); - - STableMeta* pTableMeta = getMetaInfo(pQueryInfo, 0)->pTableMeta; - SSchema* pSchema = getOneColumnSchema(pTableMeta, 0); - int32_t precision = pTableMeta->tableInfo.precision; - -#if 0 - if (pSchema->type != TSDB_DATA_TYPE_TIMESTAMP) { - int32_t numOfExprs = (int32_t)getNumOfExprs(pQueryInfo); - - for (int32_t i = 0; i < numOfExprs; ++i) { - SExprInfo* pExpr = getExprInfo(pQueryInfo, i); - - int32_t f = pExpr->pExpr->_node.functionId; - if (f == FUNCTION_DERIVATIVE || f == FUNCTION_TWA || f == FUNCTION_IRATE) { - return buildInvalidOperationMsg(pMsgBuf, msg7); - } - } - } -#endif - - // validate the query filter condition info - if (pSqlNode->pWhere != NULL) { - if (validateWhereNode(pQueryInfo, pSqlNode->pWhere, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - } else { - if (pQueryInfo->numOfTables > 1) { - return buildInvalidOperationMsg(pMsgBuf, msg8); - } - } - - // validate the interval info - if (validateIntervalNode(pQueryInfo, pSqlNode, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } else { - if (validateSessionNode(pQueryInfo, &pSqlNode->sessionVal, precision, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // parse the window_state - if (validateStateWindowNode(pQueryInfo, &pSqlNode->windowstateVal, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - } - - // parse the having clause in the first place - int32_t joinQuery = (pSqlNode->from != NULL && taosArrayGetSize(pSqlNode->from->list) > 1); - if (validateHavingNode(pQueryInfo, pSqlNode, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if ((code = validateLimitNode(pQueryInfo, pSqlNode, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - - // set order by info - if (validateOrderbyNode(pQueryInfo, pSqlNode, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if ((code = validateFillNode(pQueryInfo, pSqlNode, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - } else { - pQueryInfo->command = TSDB_SQL_SELECT; - if (taosArrayGetSize(pSqlNode->from->list) > TSDB_MAX_JOIN_TABLE_NUM) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, 0); - pQueryInfo->info.stableQuery = UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo); - - int32_t precision = pTableMetaInfo->pTableMeta->tableInfo.precision; - - // parse the group by clause in the first place - if (validateGroupbyNode(pQueryInfo, pSqlNode->pGroupby, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // set where info - if (pSqlNode->pWhere != NULL) { - if (validateWhereNode(pQueryInfo, pSqlNode->pWhere, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - } else { - if (taosArrayGetSize(pSqlNode->from->list) > 1) { // Cross join not allowed yet - return buildInvalidOperationMsg(pMsgBuf, "cross join not supported yet"); - } - } - - if (validateSelectNodeList(pQueryInfo, pSqlNode->pSelNodeList, false, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // parse the window_state - if (validateStateWindowNode(pQueryInfo, &pSqlNode->windowstateVal, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // set interval value - if (validateIntervalNode(pQueryInfo, pSqlNode, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // parse the having clause in the first place - if (validateHavingNode(pQueryInfo, pSqlNode, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - /* - * transfer sql functions that need secondary merge into another format - * in dealing with super table queries such as: count/first/last - */ - if (validateSessionNode(pQueryInfo, &pSqlNode->sessionVal, precision, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // no result due to invalid query time range - if (pQueryInfo->window.skey > pQueryInfo->window.ekey) { - pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; - return TSDB_CODE_SUCCESS; - } - - // set order by info - if (validateOrderbyNode(pQueryInfo, pSqlNode, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if ((code = validateLimitNode(pQueryInfo, pSqlNode, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - - if ((code = validateFillNode(pQueryInfo, pSqlNode, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - } - - pushDownAggFuncExprInfo(pQueryInfo); - - for(int32_t i = 0; i < 1; ++i) { - SArray* functionList = extractFunctionList(pQueryInfo->exprList[i]); - extractFunctionDesc(functionList, &pQueryInfo->info); - - code = checkForInvalidExpr(pQueryInfo, pMsgBuf); - taosArrayDestroyEx(functionList, freeItemHelper); - - if (code != TSDB_CODE_SUCCESS) { - return code; - } - } - - return TSDB_CODE_SUCCESS; // Does not build query message here -} - -static bool isTagOrPrimaryTs(SExprInfo* pExprInfo) { - if (pExprInfo->pExpr->nodeType != TEXPR_COL_NODE) { - return false; - } - - assert(pExprInfo->base.pColumns->info.colId == pExprInfo->pExpr->pSchema->colId); - return (TSDB_COL_IS_TAG(pExprInfo->base.pColumns->flag) || pExprInfo->pExpr->pSchema->colId == PRIMARYKEY_TIMESTAMP_COL_ID); -} - -// todo extract the table column in expression - -static bool isGroupbyCol(SExprInfo* pExprInfo, SGroupbyExpr* pGroupbyExpr) { - assert(pExprInfo != NULL && pGroupbyExpr != NULL); - - int32_t nodeType = pExprInfo->pExpr->nodeType; - assert(nodeType == TEXPR_COL_NODE || nodeType == TEXPR_BINARYEXPR_NODE); - - for(int32_t i = 0; i < taosArrayGetSize(pGroupbyExpr->columnInfo); ++i) { - SColumn* pCol = taosArrayGet(pGroupbyExpr->columnInfo, i); - if (pCol->info.colId == pExprInfo->pExpr->pSchema->colId) { - return true; - } - } - - return false; -} - -static bool isAllAggExpr(SArray* pList) { - assert(pList != NULL); - - for (int32_t k = 0; k < taosArrayGetSize(pList); ++k) { - SExprInfo* p = taosArrayGetP(pList, k); - if (p->pExpr->nodeType != TEXPR_FUNCTION_NODE || !qIsAggregateFunction(p->pExpr->_function.functionName)) { - return false; - } - } - - return true; -} - -static bool isAllProjectExpr(SArray *pList) { - assert(pList != NULL); - - for(int32_t i = 0; i < taosArrayGetSize(pList); ++i) { - SExprInfo* p = taosArrayGetP(pList, i); - if (p->pExpr->nodeType == TEXPR_FUNCTION_NODE && !qIsAggregateFunction(p->pExpr->_function.functionName)) { - return false; - } - } - - return true; -} - -static SExprInfo* createColumnNodeFromAggFunc(SSchema* pSchema); - -static void pushDownAggFuncExprInfo(SQueryStmtInfo* pQueryInfo) { - assert(pQueryInfo != NULL); - - size_t level = getExprFunctionLevel(pQueryInfo); - for(int32_t i = 0; i < level - 1; ++i) { - SArray* p = pQueryInfo->exprList[i]; - - // If direct lower level expressions are all aggregate function, check if current function can be push down or not - SArray* pNext = pQueryInfo->exprList[i + 1]; - if (!isAllAggExpr(pNext)) { - continue; - } - - for (int32_t j = 0; j < taosArrayGetSize(p); ++j) { - SExprInfo* pExpr = taosArrayGetP(p, j); - - if (pExpr->pExpr->nodeType == TEXPR_FUNCTION_NODE && qIsAggregateFunction(pExpr->pExpr->_function.functionName)) { - bool canPushDown = true; - for (int32_t k = 0; k < taosArrayGetSize(pNext); ++k) { - SExprInfo* pNextLevelExpr = taosArrayGetP(pNext, k); - // pExpr depends on the output of the down level, so it can not be push downwards - if (pExpr->base.pColumns->info.colId == pNextLevelExpr->base.resSchema.colId) { - canPushDown = false; - break; - } - } - - if (canPushDown) { - taosArrayInsert(pNext, j, &pExpr); - taosArrayRemove(p, j); - - // Add the project function of the current level, to output the calculated result - SExprInfo* pNew = createColumnNodeFromAggFunc(&pExpr->base.resSchema); - taosArrayInsert(p, j, &pNew); - } - } - } - } -} - -// todo change the logic plan data -static void addColumnNodeFromLowerLevel(SQueryStmtInfo* pQueryInfo) { - assert(pQueryInfo != NULL); - - size_t level = getExprFunctionLevel(pQueryInfo); - for (int32_t i = 0; i < level - 1; ++i) { - SArray* p = pQueryInfo->exprList[i]; - if (isAllAggExpr(p)) { - continue; - } - - // If direct lower level expressions are all aggregate function, check if current function can be push down or not - SArray* pNext = pQueryInfo->exprList[i + 1]; - if (isAllAggExpr(pNext)) { - continue; - } - - for (int32_t j = 0; j < taosArrayGetSize(pNext); ++j) { - SExprInfo* pExpr = taosArrayGetP(p, j); - - bool exists = false; - for (int32_t k = 0; k < taosArrayGetSize(p); ++k) { - SExprInfo* pNextLevelExpr = taosArrayGetP(pNext, k); - // pExpr depends on the output of the down level, so it can not be push downwards - if (pExpr->base.pColumns->info.colId == pNextLevelExpr->base.resSchema.colId) { - exists = true; - break; - } - } - - if (!exists) { - SExprInfo* pNew = calloc(1, sizeof(SExprInfo)); - pNew->pExpr = exprdup(pExpr->pExpr); - memcpy(&pNew->base, &pExpr->base, sizeof(SSqlExpr)); - - int32_t pos = taosArrayGetSize(p); - // Add the project function of the current level, to output the calculated result - taosArrayInsert(p, pos - 1, &pExpr); - } - } - } -} - -int32_t checkForInvalidExpr(SQueryStmtInfo* pQueryInfo, SMsgBuf* pMsgBuf) { - assert(pQueryInfo != NULL && pMsgBuf != NULL); - - const char* msg1 = "invalid query expression"; - const char* msg2 = "top/bottom query does not support order by value in time window query"; - const char* msg3 = "fill only available in time window query"; - const char* msg4 = "top/bottom not support fill"; - const char* msg5 = "scalar function can not be used in time window query"; - const char* msg6 = "not support distinct mixed with join"; - const char* msg7 = "not support distinct mixed with groupby"; - const char* msg8 = "block_dist not support subquery, only support stable/table"; - const char* msg9 = "time window aggregate can not be mixed up with group by column"; - - if (pQueryInfo->info.topbotQuery) { - - // 1. invalid sql: - // select top(col, k) from table_name [interval(1d)|session(ts, 1d)|statewindow(col)] order by k asc - // order by normal column is not supported - if (pQueryInfo->order != NULL) { - size_t numOfOrder = taosArrayGetSize(pQueryInfo->order); - if (numOfOrder > 1) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - if (numOfOrder > 0) { - SColumn* pOrderCol = taosArrayGet(pQueryInfo->order, 0); - if (pQueryInfo->info.timewindow && pOrderCol->info.colId != PRIMARYKEY_TIMESTAMP_COL_ID) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - } - } - - // select top(col, k) from table_name interval(10s) fill(prev) - // not support fill in top/bottom query. - if (pQueryInfo->fillType != TSDB_FILL_NONE) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - // select top(col, k), count(*) from table_name - size_t size = getNumOfExprs(pQueryInfo); - for (int32_t i = 0; i < size; ++i) { - SExprInfo* pExpr = getExprInfo(pQueryInfo, i); - - if (pExpr->pExpr->nodeType == TEXPR_COL_NODE) { - if (!isTagOrPrimaryTs(pExpr) && !isGroupbyCol(pExpr, &pQueryInfo->groupbyExpr)) { - return buildInvalidOperationMsg(pMsgBuf, "invalid expression in select clause"); - } - - } else if (pExpr->pExpr->nodeType == TEXPR_BINARYEXPR_NODE) { - continue; - // todo extract all column node in tree, and check for each node - - continue; - } - - // dummy column is also the placeholder for primary timestamp column in the result. - const char* functionName = pExpr->pExpr->_function.functionName; - if (strcmp(functionName, "top") != 0 && strcmp(functionName, "bottom") != 0 && strcmp(functionName, "dummy") != 0) { - if (qIsAggregateFunction(functionName)) { - return buildInvalidOperationMsg(pMsgBuf, "invalid expression in select clause"); - } - - // the primary key is valid - if (pExpr->pExpr->nodeType == TEXPR_COL_NODE) { - if (pExpr->pExpr->pSchema->colId == PRIMARYKEY_TIMESTAMP_COL_ID) { - continue; - } - } - - continue; - } - } - } - - /* - * 2. invalid sql: - * select count(tbname)/count(tag1)/count(tag2) from super_table_name [interval(1d)|session(ts, 1d)|statewindow(col)]; - */ - if (pQueryInfo->info.timewindow) { - size_t size = getNumOfExprs(pQueryInfo); - for (int32_t i = 0; i < size; ++i) { - SExprInfo* pExpr = getExprInfo(pQueryInfo, i); - if (pExpr->pExpr->nodeType != TEXPR_FUNCTION_NODE) { - continue; - } - - int32_t functionId = getExprFunctionId(pExpr); - if (functionId == FUNCTION_COUNT && TSDB_COL_IS_TAG(pExpr->base.pColumns->flag)) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - } - } - - /* - * 3. invalid sql: - * select tbname, tags_fields from super_table_name [interval(1s)|session(ts,1s)|statewindow(col)] - */ - if (pQueryInfo->info.onlyTagQuery && pQueryInfo->info.timewindow) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - /* - * 4. invalid sql: - * select * from table_name fill(prev|next|null|none) - */ - if (!pQueryInfo->info.timewindow && !pQueryInfo->info.interpQuery && pQueryInfo->fillType != TSDB_FILL_NONE) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - /* - * 5. invalid sql: - * select diff(col)|derivative(col)|* from table_name interval(1s)|session(20s)|statewindow(col) - * projection query not compatible with the time window query - */ - if (pQueryInfo->info.timewindow && pQueryInfo->info.projectionQuery) { - return buildInvalidOperationMsg(pMsgBuf, msg5); - } - - /* - * 6. invalid sql: - * distinct + join not supported. - * select distinct a,b from table1, table2 where table1.ts=table2.ts - * - * distinct + group by not supported: - * select distinct count(a) from table_name group by col1; - */ - if (pQueryInfo->info.distinct) { - if (pQueryInfo->info.join) { - return buildInvalidOperationMsg(pMsgBuf, msg6); - } - - if (taosArrayGetSize(pQueryInfo->groupbyExpr.columnInfo) != 0) { - return buildInvalidOperationMsg(pMsgBuf, msg7); - } - } - - /* - * 7. invalid sql: - * nested subquery not support block_dist query - * select block_dist() from (select * from table_name) - */ - - /* - * 8. invalid sql: - * select count(*) from table_name [interval(10s)|session(ts, 10s)|state_window(col_name)] group by col_name - */ - if ((pQueryInfo->info.timewindow || pQueryInfo->info.stateWindow || pQueryInfo->info.sessionWindow) && - pQueryInfo->info.groupbyColumn) { - return buildInvalidOperationMsg(pMsgBuf, msg9); - } - - /* - * 9. invalid sql: - * select count(*), col_name from table_name - */ - if (pQueryInfo->info.agg) { - bool isSelectivity = false; - - if (pQueryInfo->info.projectionQuery) { - size_t size = getNumOfExprs(pQueryInfo); - for (int32_t i = 0; i < size; ++i) { - SExprInfo* pExpr = getExprInfo(pQueryInfo, i); - if (pExpr->pExpr->nodeType == TEXPR_FUNCTION_NODE) { - if (!isSelectivity) { - isSelectivity = qIsSelectivityFunction(pExpr->pExpr->_function.functionName); - } - continue; - } - - if (isSelectivity && isTagOrPrimaryTs(pExpr)) { - continue; - } - - if (!isGroupbyCol(pExpr, &pQueryInfo->groupbyExpr)) { - return buildInvalidOperationMsg(pMsgBuf, "invalid expression in select"); - } - } - } - } -} - -int32_t addResColumnInfo(SQueryStmtInfo* pQueryInfo, int32_t outputIndex, SSchema* pSchema, SExprInfo* pSqlExpr) { - SInternalField* pInfo = insertFieldInfo(&pQueryInfo->fieldsInfo, outputIndex, pSchema); - pInfo->pExpr = pSqlExpr; - return TSDB_CODE_SUCCESS; -} - -void setResultColName(char* name, tSqlExprItem* pItem, SToken* pToken, SToken* functionToken, bool multiCols) { - if (pItem->aliasName != NULL) { - tstrncpy(name, pItem->aliasName, TSDB_COL_NAME_LEN); - } else if (multiCols) { - char uname[TSDB_COL_NAME_LEN] = {0}; - int32_t len = TMIN(pToken->n + 1, TSDB_COL_NAME_LEN); - tstrncpy(uname, pToken->z, len); - - if (tsKeepOriginalColumnName) { // keep the original column name - tstrncpy(name, uname, TSDB_COL_NAME_LEN); - } else { - const int32_t size = TSDB_COL_NAME_LEN + FUNCTIONS_NAME_MAX_LENGTH + 2 + 1; - char tmp[TSDB_COL_NAME_LEN + FUNCTIONS_NAME_MAX_LENGTH + 2 + 1] = {0}; - - char f[FUNCTIONS_NAME_MAX_LENGTH] = {0}; - strncpy(f, functionToken->z, functionToken->n); - - snprintf(tmp, size, "%s(%s)", f, uname); - tstrncpy(name, tmp, TSDB_COL_NAME_LEN); - } - } else { // use the user-input result column name - int32_t len = TMIN(pItem->pNode->exprToken.n + 1, TSDB_COL_NAME_LEN); - tstrncpy(name, pItem->pNode->exprToken.z, len); - } -} - -SExprInfo* doAddOneExprInfo(SQueryStmtInfo* pQueryInfo, const char* funcName, SSourceParam* pSourceParam, int32_t outputIndex, - STableMetaInfo* pTableMetaInfo, SSchema* pResultSchema, int32_t interSize, const char* token, bool finalResult) { - SExprInfo* pExpr = createExprInfo(pTableMetaInfo, funcName, pSourceParam, pResultSchema, interSize); - tstrncpy(pExpr->base.token, token, sizeof(pExpr->base.token)); - - SArray* pExprList = getCurrentExprList(pQueryInfo); - addExprInfo(pExprList, outputIndex, pExpr, pQueryInfo->exprListLevelIndex); - - uint64_t uid = pTableMetaInfo->pTableMeta->uid; - - if (pSourceParam->pColumnList != NULL) { - SColumn* pCol = taosArrayGetP(pSourceParam->pColumnList, 0); - - if (TSDB_COL_IS_TAG(pCol->flag) || TSDB_COL_IS_NORMAL_COL(pCol->flag)) { - SArray* p = TSDB_COL_IS_TAG(pCol->flag) ? pTableMetaInfo->tagColList : pQueryInfo->colList; - - for (int32_t i = 0; i < pSourceParam->num; ++i) { - SColumn* pColumn = taosArrayGetP(pSourceParam->pColumnList, i); - SSchema s = createSchema(pColumn->info.type, pColumn->info.bytes, pColumn->info.colId, pColumn->name) ; - columnListInsert(p, uid, &s, pCol->flag); - } - } - - if (TSDB_COL_IS_NORMAL_COL(pCol->flag)) { - char* colName = pTableMetaInfo->pTableMeta->schema[0].name; - insertPrimaryTsColumn(pQueryInfo->colList, colName, uid); - } - } - - if (finalResult) { - addResColumnInfo(pQueryInfo, outputIndex, pResultSchema, pExpr); - } - - return pExpr; -} - -static void extractFunctionName(char* name, const tSqlExprItem* pItem) { - assert(pItem != NULL); - SToken* funcToken = &pItem->pNode->Expr.operand; - memcpy(name, funcToken->z, funcToken->n); -} - -static int32_t addOneExprInfo(SQueryStmtInfo* pQueryInfo, tSqlExprItem* pItem, int32_t functionId, int32_t outputIndex, SSchema* pSchema, SColumnIndex* pColIndex, tExprNode* pNode, bool finalResult, SMsgBuf* pMsgBuf) { - const char* msg1 = "not support column types"; - if (functionId == FUNCTION_SPREAD) { - if (IS_VAR_DATA_TYPE(pSchema->type) || pSchema->type == TSDB_DATA_TYPE_BOOL) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - } - - char name[TSDB_COL_NAME_LEN] = {0}; - SToken t = {.z = pSchema->name, .n = (uint32_t)strnlen(pSchema->name, TSDB_COL_NAME_LEN)}; - setResultColName(name, pItem, &t, &pItem->pNode->Expr.operand, true); - - SResultDataInfo resInfo = {0}; - getResultDataInfo(pSchema->type, pSchema->bytes, functionId, 0, &resInfo, 0, false); - - SSchema resultSchema = createSchema(resInfo.type, resInfo.bytes, getNewResColId(), name); - - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, pColIndex->tableIndex); - SColumn c = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, pColIndex->type, pSchema); - - SSourceParam param = {0}; - addIntoSourceParam(¶m, pNode, &c); - - char fname[FUNCTIONS_NAME_MAX_LENGTH] = {0}; - extractFunctionName(fname, pItem); - doAddOneExprInfo(pQueryInfo, fname, ¶m, outputIndex, pTableMetaInfo, &resultSchema, resInfo.intermediateBytes, name, finalResult); - - return TSDB_CODE_SUCCESS; -} - -static int32_t checkForAliasName(SMsgBuf* pMsgBuf, char* aliasName) { - const char* msg1 = "column alias name too long"; - if (aliasName != NULL && strlen(aliasName) >= TSDB_COL_NAME_LEN) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t sqlExprToExprNode(tExprNode **pExpr, const tSqlExpr* pSqlExpr, SQueryStmtInfo* pQueryInfo, SArray* pCols, bool* keepTableCols, SMsgBuf* pMsgBuf); - -static int64_t getTickPerSecond(SVariant* pVariant, int32_t precision, int64_t* tickPerSec, SMsgBuf *pMsgBuf) { - const char* msg10 = "derivative duration should be greater than 1 Second"; - - if (taosVariantDump(pVariant, (char*) tickPerSec, TSDB_DATA_TYPE_BIGINT, true) < 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if (precision == TSDB_TIME_PRECISION_MILLI) { - *tickPerSec /= TSDB_TICK_PER_SECOND(TSDB_TIME_PRECISION_MICRO); - } else if (precision == TSDB_TIME_PRECISION_MICRO) { - *tickPerSec /= TSDB_TICK_PER_SECOND(TSDB_TIME_PRECISION_MILLI); - } - - if (*tickPerSec <= 0 || *tickPerSec < TSDB_TICK_PER_SECOND(precision)) { - return buildInvalidOperationMsg(pMsgBuf, msg10); - } - - return TSDB_CODE_SUCCESS; -} - -// set the first column ts for top/bottom query -static void setTsOutputExprInfo(SQueryStmtInfo* pQueryInfo, STableMetaInfo* pTableMetaInfo, int32_t outputIndex, int32_t tableIndex) { - SColumnIndex indexTS = {.tableIndex = tableIndex, .columnIndex = PRIMARYKEY_TIMESTAMP_COL_ID, .type = TSDB_COL_NORMAL}; - SSchema s = createSchema(TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, getNewResColId(), "ts"); - - SColumn col = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, TSDB_COL_NORMAL, &s); - - SSourceParam param = {0}; - addIntoSourceParam(¶m, NULL, &col); - - SExprInfo* pExpr = createExprInfo(pTableMetaInfo, "dummy", ¶m, &s, TSDB_KEYSIZE); - strncpy(pExpr->base.token, "ts", tListLen(pExpr->base.token)); - - SArray* pExprList = getCurrentExprList(pQueryInfo); - addExprInfo(pExprList, outputIndex, pExpr, pQueryInfo->exprListLevelIndex); - - SSchema* pSourceSchema = getOneColumnSchema(pTableMetaInfo->pTableMeta, indexTS.columnIndex); - columnListInsert(pQueryInfo->colList, pTableMetaInfo->pTableMeta->uid, pSourceSchema, TSDB_COL_NORMAL); - addResColumnInfo(pQueryInfo, outputIndex, &pExpr->base.resSchema, pExpr); -} - -static int32_t setColumnIndex(SQueryStmtInfo* pQueryInfo, SArray* pParamList, SColumnIndex* index, SSchema* columnSchema, tExprNode** pNode, SMsgBuf* pMsgBuf) { - const char* msg1 = "illegal column name"; - const char* msg2 = "invalid table name"; - - STableMeta* pTableMeta = getMetaInfo(pQueryInfo, 0)->pTableMeta; - if (pParamList == NULL) { - // count(*) is equalled to count(primary_timestamp_key) - *index = (SColumnIndex) {0, 0, false}; - *columnSchema = *(SSchema*) getOneColumnSchema(pTableMeta, index->columnIndex); - } else { - tSqlExprItem* pParamElem = taosArrayGet(pParamList, 0); - - SToken* pToken = &pParamElem->pNode->columnName; - int16_t tokenId = pParamElem->pNode->tokenId; - - // select count(table.*), select count(1), count(2) - if (tokenId == TK_ALL || tokenId == TK_INTEGER || tokenId == TK_FLOAT) { - // check if the table name is valid or not - SToken tmpToken = pParamElem->pNode->columnName; - if (getTableIndexByName(&tmpToken, pQueryInfo, index) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - *index = (SColumnIndex) {0, PRIMARYKEY_TIMESTAMP_COL_ID, false}; - *columnSchema = *(SSchema*) getOneColumnSchema(pTableMeta, index->columnIndex); - } else if (pToken->z != NULL && pToken->n > 0) { - // count the number of table created according to the super table - if (getColumnIndexByName(pToken, pQueryInfo, index, pMsgBuf) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - *columnSchema = *(SSchema*) getOneColumnSchema(pTableMeta, index->columnIndex); - } else { - STableMetaInfo* pTableMetaInfo = NULL; - int32_t code = extractFunctionParameterInfo(pQueryInfo, tokenId, &pTableMetaInfo, columnSchema, pNode, index, pParamElem, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - } - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t doAddAllColumnExprInSelectClause(SQueryStmtInfo *pQueryInfo, STableMetaInfo* pTableMetaInfo, tSqlExprItem* pItem, int32_t functionId, - int32_t tableIndex, int32_t* colIndex, bool finalResult, SMsgBuf* pMsgBuf) { - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - for (int32_t i = 0; i < getNumOfColumns(pTableMeta); ++i) { - SColumnIndex index = {.tableIndex = tableIndex, .columnIndex = i, .type = TSDB_COL_NORMAL}; - - SSchema* pSchema = getOneColumnSchema(pTableMeta, i); - if (addOneExprInfo(pQueryInfo, pItem, functionId, *colIndex, pSchema, &index, NULL, finalResult, pMsgBuf) != 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - (*colIndex)++; - } -} - -static int32_t doHandleOneParam(SQueryStmtInfo *pQueryInfo, tSqlExprItem* pItem, tSqlExprItem* pParamElem, int32_t functionId, - int32_t* outputIndex, bool finalResult, SMsgBuf* pMsgBuf) { - const char* msg3 = "illegal column name"; - const char* msg4 = "invalid table name"; - const char* msg6 = "functions applied to tags are not allowed"; - - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - - if (pParamElem->pNode->tokenId == TK_ALL) { // select table.* - SToken tmpToken = pParamElem->pNode->columnName; - - if (getTableIndexByName(&tmpToken, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - doAddAllColumnExprInSelectClause(pQueryInfo, pTableMetaInfo, pItem, functionId, index.tableIndex, outputIndex, finalResult, pMsgBuf); - } else { - tExprNode* pNode = NULL; - int32_t tokenId = pParamElem->pNode->tokenId; - SSchema columnSchema = {0}; - STableMetaInfo* pTableMetaInfo = {0}; - - int32_t code = extractFunctionParameterInfo(pQueryInfo, tokenId, &pTableMetaInfo, &columnSchema, &pNode, &index, pParamElem, pMsgBuf); - - if (code != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - // functions can not be applied to tags - if (TSDB_COL_IS_TAG(index.type) && (functionId == FUNCTION_INTERP || functionId == FUNCTION_SPREAD)) { - return buildInvalidOperationMsg(pMsgBuf, msg6); - } - - if (addOneExprInfo(pQueryInfo, pItem, functionId, (*outputIndex)++, &columnSchema, &index, pNode, finalResult, pMsgBuf) != 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - } -} - -static int32_t multiColumnListInsert(SQueryStmtInfo* pQueryInfo, SArray* pColumnList, SMsgBuf* pMsgBuf); -static int32_t addScalarExprAndResColumn(SQueryStmtInfo* pQueryInfo, int32_t exprIndex, tSqlExprItem* pItem, SMsgBuf* pMsgBuf); - -int32_t extractFunctionParameterInfo(SQueryStmtInfo* pQueryInfo, int32_t tokenId, STableMetaInfo** pTableMetaInfo, - SSchema* columnSchema, tExprNode** pNode, SColumnIndex* pIndex, - tSqlExprItem* pParamElem, SMsgBuf* pMsgBuf) { - const char* msg1 = "not support column types"; - const char* msg2 = "invalid parameters"; - const char* msg3 = "illegal column name"; - const char* msg4 = "nested function is not supported"; - const char* msg5 = "functions applied to tags are not allowed"; - const char* msg6 = "aggregate function can not be nested in aggregate function"; - const char* msg7 = "invalid function name"; - - pQueryInfo->exprListLevelIndex += 1; - - if (tokenId == TK_ALL || tokenId == TK_ID) { // simple parameter - // simple parameter or nested function - // It is a parameter of a aggregate function, so it can not be still a aggregate function. - // E.g., the sql statement of "select count(count(*)) from table_name" is invalid. - tSqlExpr* pSqlExpr = pParamElem->pNode; - if (pParamElem->pNode->type == SQL_NODE_SQLFUNCTION) { - bool scalarFunc = false; - pParamElem->functionId = qIsBuiltinFunction(pSqlExpr->Expr.operand.z, pSqlExpr->Expr.operand.n, &scalarFunc); - if (pParamElem->functionId == FUNCTION_INVALID_ID) { - return buildInvalidOperationMsg(pMsgBuf, msg7); - } - - if (!scalarFunc) { - return buildInvalidOperationMsg(pMsgBuf, msg6); - } - - SArray* pExprList = getCurrentExprList(pQueryInfo); - size_t n = taosArrayGetSize(pExprList); - - // todo extract the table uid - pIndex->tableIndex = 0; - int32_t code = addScalarExprAndResColumn(pQueryInfo, n, pParamElem, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - SExprInfo** pLastExpr = taosArrayGetLast(pExprList); - *pNode = (*pLastExpr)->pExpr; - *(SSchema*) columnSchema = (*pLastExpr)->base.resSchema; - *pTableMetaInfo = getMetaInfo(pQueryInfo, 0); - } else { - if ((getColumnIndexByName(&pParamElem->pNode->columnName, pQueryInfo, pIndex, pMsgBuf) != TSDB_CODE_SUCCESS)) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - // functions can not be applied to tags - if (TSDB_COL_IS_TAG(pIndex->type)) { - return buildInvalidOperationMsg(pMsgBuf, msg5); - } - - // 2. check if sql function can be applied on this column data type - *pTableMetaInfo = getMetaInfo(pQueryInfo, pIndex->tableIndex); - *columnSchema = *(SSchema*)getOneColumnSchema((*pTableMetaInfo)->pTableMeta, pIndex->columnIndex); - } - } else if (tokenId == TK_PLUS || tokenId == TK_MINUS || tokenId == TK_STAR || tokenId == TK_REM || tokenId == TK_DIVIDE || tokenId == TK_CONCAT) { - pIndex->tableIndex = 0; // todo set the correct table index - pIndex->type = TSDB_COL_TMP; // It is a temporary column generated by arithmetic expression. - - SArray* pExprList = getCurrentExprList(pQueryInfo); - size_t n = taosArrayGetSize(pExprList); - int32_t code = addScalarExprAndResColumn(pQueryInfo, n, pParamElem, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - SExprInfo** pLastExpr = taosArrayGetLast(getCurrentExprList(pQueryInfo)); - *pNode = (*pLastExpr)->pExpr; - *(SSchema*) columnSchema = (*pLastExpr)->base.resSchema; - *pTableMetaInfo = getMetaInfo(pQueryInfo, 0); - } else { - assert(0); - } - - pQueryInfo->exprListLevelIndex -= 1; - return TSDB_CODE_SUCCESS; -} - -static int32_t checkForkParam(tSqlExpr* pSqlExpr, size_t k, SMsgBuf* pMsgBuf) { - const char* msg1 = "invalid parameters"; - - SArray* pParamList = pSqlExpr->Expr.paramList; - - if (k == 0) { - if (pParamList != NULL && taosArrayGetSize(pParamList) != 0) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - } else if (k == 1) { - if (!(pParamList == NULL || taosArrayGetSize(pParamList) == k)) { - return buildInvalidOperationMsg(pMsgBuf, msg1);; - } - } else { - if (pParamList != NULL && taosArrayGetSize(pParamList) != k) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - } - return TSDB_CODE_SUCCESS; -} - -int32_t addAggExprAndResColumn(SQueryStmtInfo* pQueryInfo, int32_t colIndex, tSqlExprItem* pItem, bool finalResult, SMsgBuf* pMsgBuf) { - STableMetaInfo* pTableMetaInfo = NULL; - int32_t functionId = pItem->functionId; - int32_t code = TSDB_CODE_SUCCESS; - - const char* msg1 = "not support column types"; - const char* msg2 = "invalid parameters"; - const char* msg3 = "illegal column name"; - const char* msg4 = "invalid table name"; - const char* msg5 = "parameter is out of range [0, 100]"; - const char* msg6 = "functions applied to tags are not allowed"; - const char* msg7 = "normal table can not apply this function"; - const char* msg8 = "multi-columns selection does not support alias column name"; - const char* msg9 = "diff/derivative can no be applied to unsigned numeric type"; - const char* msg10 = "derivative duration should be greater than 1 Second"; - const char* msg11 = "third parameter in derivative should be 0 or 1"; - const char* msg12 = "parameter is out of range [1, 100]"; - const char* msg13 = "nested function is not supported"; - - if (checkForAliasName(pMsgBuf, pItem->aliasName) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - switch (functionId) { - case FUNCTION_COUNT: { - // more than one parameter for count() function - SArray* pParamList = pItem->pNode->Expr.paramList; - if ((code = checkForkParam(pItem->pNode, 1, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - - tExprNode* pNode = NULL; - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - SSchema columnSchema = {0}; - - code = setColumnIndex(pQueryInfo, pParamList, &index, &columnSchema, &pNode, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - int32_t size = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes; - SSchema s = createSchema(TSDB_DATA_TYPE_BIGINT, size, getNewResColId(), ""); - - char token[TSDB_COL_NAME_LEN] = {0}; - setTokenAndResColumnName(pItem, s.name, token,sizeof(s.name) - 1); - - pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - SColumn c = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, index.type, &columnSchema); - - SSourceParam param = {0}; - addIntoSourceParam(¶m, pNode, &c); - - int32_t outputIndex = getNumOfFields(&pQueryInfo->fieldsInfo); - - char fname[FUNCTIONS_NAME_MAX_LENGTH] = {0}; - extractFunctionName(fname, pItem); - doAddOneExprInfo(pQueryInfo, fname, ¶m, outputIndex, pTableMetaInfo, &s, size, token, finalResult); - return TSDB_CODE_SUCCESS; - } - - case FUNCTION_SUM: - case FUNCTION_AVG: - case FUNCTION_RATE: - case FUNCTION_IRATE: - case FUNCTION_TWA: - case FUNCTION_MIN: - case FUNCTION_MAX: - case FUNCTION_DIFF: - case FUNCTION_DERIVATIVE: - case FUNCTION_STDDEV: - case FUNCTION_LEASTSQR: { - // 1. valid the number of parameters - int32_t numOfParams = (pItem->pNode->Expr.paramList == NULL)? 0: (int32_t) taosArrayGetSize(pItem->pNode->Expr.paramList); - - // no parameters or more than one parameter for function - if (pItem->pNode->Expr.paramList == NULL || - (functionId != FUNCTION_LEASTSQR && functionId != FUNCTION_DERIVATIVE && numOfParams != 1) || - ((functionId == FUNCTION_LEASTSQR || functionId == FUNCTION_DERIVATIVE) && numOfParams != 3)) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - tSqlExprItem* pParamElem = taosArrayGet(pItem->pNode->Expr.paramList, 0); - - tExprNode* pNode = NULL; - int32_t tokenId = pParamElem->pNode->tokenId; - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - SSchema columnSchema = {0}; - code = extractFunctionParameterInfo(pQueryInfo, tokenId, &pTableMetaInfo, &columnSchema, &pNode, &index, pParamElem, pMsgBuf); - - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - if (tokenId == TK_ALL || tokenId == TK_ID) { - if (!IS_NUMERIC_TYPE(columnSchema.type)) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } else if (IS_UNSIGNED_NUMERIC_TYPE(columnSchema.type) && (functionId == FUNCTION_DIFF || functionId == FUNCTION_DERIVATIVE)) { - return buildInvalidOperationMsg(pMsgBuf, msg9); - } - } - - int32_t precision = pTableMetaInfo->pTableMeta->tableInfo.precision; - - SResultDataInfo resInfo = {0}; - if (getResultDataInfo(columnSchema.type, columnSchema.bytes, functionId, 0, &resInfo, 0, false) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // set the first column ts for diff query - int32_t numOfOutput = getNumOfFields(&pQueryInfo->fieldsInfo); - if (functionId == FUNCTION_DIFF || functionId == FUNCTION_DERIVATIVE) { - setTsOutputExprInfo(pQueryInfo, pTableMetaInfo, numOfOutput, index.tableIndex); - numOfOutput += 1; - } - - SSchema s = createSchema(resInfo.type, resInfo.bytes, getNewResColId(), "ts"); - - char token[TSDB_COL_NAME_LEN] = {0}; - setTokenAndResColumnName(pItem, s.name, token, sizeof(s.name) - 1); - - SColumn c = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, index.type, &columnSchema); - - SSourceParam param = {0}; - addIntoSourceParam(¶m, pNode, &c); - - char funcName[FUNCTIONS_NAME_MAX_LENGTH] = {0}; - extractFunctionName(funcName, pItem); - - SExprInfo* pExpr = doAddOneExprInfo(pQueryInfo, funcName, ¶m, numOfOutput, pTableMetaInfo, &s, resInfo.intermediateBytes, token, finalResult); - if (functionId == FUNCTION_LEASTSQR) { // set the leastsquares parameters - char val[8] = {0}; - if (taosVariantDump(&pParamElem[1].pNode->value, val, TSDB_DATA_TYPE_DOUBLE, true) < 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - addExprInfoParam(&pExpr->base, val, TSDB_DATA_TYPE_DOUBLE, DOUBLE_BYTES); - - memset(val, 0, tListLen(val)); - if (taosVariantDump(&pParamElem[2].pNode->value, val, TSDB_DATA_TYPE_DOUBLE, true) < 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - addExprInfoParam(&pExpr->base, val, TSDB_DATA_TYPE_DOUBLE, DOUBLE_BYTES); - } else if (functionId == FUNCTION_IRATE) { - addExprInfoParam(&pExpr->base, (char*) &precision, TSDB_DATA_TYPE_BIGINT, LONG_BYTES); - } else if (functionId == FUNCTION_DERIVATIVE) { - char val[8] = {0}; - - int64_t tickPerSec = 0; - code = getTickPerSecond(&pParamElem[1].pNode->value, precision, &tickPerSec, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - addExprInfoParam(&pExpr->base, (char*) &tickPerSec, TSDB_DATA_TYPE_BIGINT, LONG_BYTES); - memset(val, 0, tListLen(val)); - - if (taosVariantDump(&pParamElem[2].pNode->value, val, TSDB_DATA_TYPE_BIGINT, true) < 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if (GET_INT64_VAL(val) != 0 && GET_INT64_VAL(val) != 1) { - return buildInvalidOperationMsg(pMsgBuf, msg11); - } - - addExprInfoParam(&pExpr->base, val, TSDB_DATA_TYPE_BIGINT, LONG_BYTES); - } - return TSDB_CODE_SUCCESS; - } - - case FUNCTION_FIRST: - case FUNCTION_LAST: - case FUNCTION_SPREAD: - case FUNCTION_LAST_ROW: - case FUNCTION_INTERP: { - bool requireAllFields = (pItem->pNode->Expr.paramList == NULL); - - if (!requireAllFields) { - SArray* pParamList = pItem->pNode->Expr.paramList; - if (taosArrayGetSize(pParamList) < 1) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - if (taosArrayGetSize(pParamList) > 1 && (pItem->aliasName != NULL)) { - return buildInvalidOperationMsg(pMsgBuf, msg8); - } - - // in first/last function, multiple columns can be add to resultset - for (int32_t i = 0; i < taosArrayGetSize(pParamList); ++i) { - tSqlExprItem* pParamElem = taosArrayGet(pParamList, i); - doHandleOneParam(pQueryInfo, pItem, pParamElem, functionId, &colIndex, finalResult, pMsgBuf); - } - } else { // select function(*) from xxx - int32_t numOfFields = 0; - - // multicolumn selection does not support alias name - if (pItem->aliasName != NULL && strlen(pItem->aliasName) > 0) { - return buildInvalidOperationMsg(pMsgBuf, msg8); - } - - for (int32_t j = 0; j < pQueryInfo->numOfTables; ++j) { - pTableMetaInfo = getMetaInfo(pQueryInfo, j); - doAddAllColumnExprInSelectClause(pQueryInfo, pTableMetaInfo, pItem, functionId, j, &colIndex, finalResult, pMsgBuf); - numOfFields += getNumOfColumns(pTableMetaInfo->pTableMeta); - } - } - return TSDB_CODE_SUCCESS; - } - - case FUNCTION_TOP: - case FUNCTION_BOTTOM: - case FUNCTION_PERCT: - case FUNCTION_APERCT: { - // 1. valid the number of parameters - // no parameters or more than one parameter for function - if ((code = checkForkParam(pItem->pNode, 2, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - - tSqlExprItem* pParamElem = taosArrayGet(pItem->pNode->Expr.paramList, 0); - if (pParamElem->pNode->tokenId == TK_ALL) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - tExprNode* pNode = NULL; - int32_t tokenId = pParamElem->pNode->tokenId; - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - SSchema columnSchema = {0}; - code = extractFunctionParameterInfo(pQueryInfo, tokenId, &pTableMetaInfo, &columnSchema, &pNode, &index, pParamElem,pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - // functions can not be applied to tags - if (TSDB_COL_IS_TAG(index.type)) { - return buildInvalidOperationMsg(pMsgBuf, msg6); - } - - pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - - // 2. valid the column type - if (!IS_NUMERIC_TYPE(columnSchema.type)) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - // 3. valid the parameters - if (pParamElem[1].pNode->tokenId == TK_ID) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - SResultDataInfo resInfo = {0}; - getResultDataInfo(columnSchema.type, columnSchema.bytes, functionId, 0, &resInfo, 0, false); - if (functionId == FUNCTION_TOP || functionId == FUNCTION_BOTTOM) { - // set the first column ts for top/bottom query - setTsOutputExprInfo(pQueryInfo, pTableMetaInfo, colIndex, index.tableIndex); - colIndex += 1; // the first column is ts - } - - SSchema s = createSchema(resInfo.type, resInfo.bytes, getNewResColId(), ""); - - char token[TSDB_COL_NAME_LEN] = {0}; - setTokenAndResColumnName(pItem, s.name, token, sizeof(s.name) - 1); - - SColumn c = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, index.type, &columnSchema); - - SSourceParam param = {0}; - addIntoSourceParam(¶m, pNode, &c); - - char funcName[FUNCTIONS_NAME_MAX_LENGTH] = {0}; - extractFunctionName(funcName, pItem); - SExprInfo* pExpr = doAddOneExprInfo(pQueryInfo, funcName, ¶m, colIndex, pTableMetaInfo, &s, resInfo.intermediateBytes, token, finalResult); - - SToken* pParamToken = &pParamElem[1].pNode->exprToken; - pExpr->base.numOfParams += 1; - - SVariant* pVar = &pExpr->base.param[0]; - if (functionId == FUNCTION_PERCT || functionId == FUNCTION_APERCT) { - taosVariantCreate(pVar, pParamToken->z, pParamToken->n, TSDB_DATA_TYPE_DOUBLE); - - /* - * sql function transformation - * for dp = 0, it is actually min, - * for dp = 100, it is max, - */ - if (pVar->d < 0 || pVar->d > TOP_BOTTOM_QUERY_LIMIT) { - return buildInvalidOperationMsg(pMsgBuf, msg5); - } - } else { - taosVariantCreate(pVar, pParamToken->z, pParamToken->n, TSDB_DATA_TYPE_BIGINT); - if (pVar->i <= 0 || pVar->i > 100) { // todo use macro - return buildInvalidOperationMsg(pMsgBuf, msg12); - } - } - - return TSDB_CODE_SUCCESS; - } - - case FUNCTION_TID_TAG: { - pTableMetaInfo = getMetaInfo(pQueryInfo, 0); - if (UTIL_TABLE_IS_NORMAL_TABLE(pTableMetaInfo)) { - return buildInvalidOperationMsg(pMsgBuf, msg7); - } - - // no parameters or more than one parameter for function - if ((code = checkForkParam(pItem->pNode, 1, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - - tSqlExprItem* pParamItem = taosArrayGet(pItem->pNode->Expr.paramList, 0); - tSqlExpr* pParam = pParamItem->pNode; - - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByName(&pParam->columnName, pQueryInfo, &index, pMsgBuf) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - SSchema* pSchema = getTableTagSchema(pTableMetaInfo->pTableMeta); - - // functions can not be applied to normal columns - int32_t numOfCols = getNumOfColumns(pTableMetaInfo->pTableMeta); - if (index.columnIndex < numOfCols && index.columnIndex != TSDB_TBNAME_COLUMN_INDEX) { - return buildInvalidOperationMsg(pMsgBuf, msg6); - } - - if (index.columnIndex > 0) { - index.columnIndex -= numOfCols; - } - - // 2. valid the column type - int16_t colType = 0; - if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { - colType = TSDB_DATA_TYPE_BINARY; - } else { - colType = pSchema[index.columnIndex].type; - } - - if (colType == TSDB_DATA_TYPE_BOOL) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - columnListInsert(pTableMetaInfo->tagColList, pTableMetaInfo->pTableMeta->uid, &pSchema[index.columnIndex], TSDB_COL_TAG); - SSchema* pTagSchema = getTableTagSchema(pTableMetaInfo->pTableMeta); - - SSchema s = (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX)? *getTbnameColumnSchema(): pTagSchema[index.columnIndex]; - - SResultDataInfo resInfo = {0}; - int32_t ret = getResultDataInfo(s.type, s.bytes, FUNCTION_TID_TAG, 0, &resInfo, 0, 0); - assert(ret == TSDB_CODE_SUCCESS); - - SSchema result = createSchema(resInfo.type, resInfo.bytes, getNewResColId(), s.name); - SColumn c = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, index.type, &result); - - SSourceParam param = {0}; - addIntoSourceParam(¶m, NULL, &c); - - /*SExprInfo* pExpr = */doAddOneExprInfo(pQueryInfo, "tbid", ¶m, 0, pTableMetaInfo, &result, 0, s.name, true); - return TSDB_CODE_SUCCESS; - } - - case FUNCTION_BLKINFO: { - // no parameters or more than one parameter for function - if ((code = checkForkParam(pItem->pNode, 0, pMsgBuf))!= TSDB_CODE_SUCCESS) { - return code; - } - - SColumnIndex index = {.tableIndex = 0, .columnIndex = 0, .type = TSDB_COL_NORMAL}; - pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - - SResultDataInfo resInfo = {0}; - getResultDataInfo(TSDB_DATA_TYPE_INT, 4, functionId, 0, &resInfo, 0, 0); - - SSchema s = createSchema(resInfo.type, resInfo.bytes, getNewResColId(), "block_dist"); - SSchema colSchema = {0}; - - char token[TSDB_COL_NAME_LEN] = {0}; - setTokenAndResColumnName(pItem, s.name, token, sizeof(s.name) - 1); - - SColumn c = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, index.type, &colSchema); - - SSourceParam param = {0}; - addIntoSourceParam(¶m, NULL, &c); - - SExprInfo* pExpr = doAddOneExprInfo(pQueryInfo, "block_dist", ¶m, colIndex, pTableMetaInfo, &s, resInfo.intermediateBytes, token, finalResult); - - int64_t rowSize = pTableMetaInfo->pTableMeta->tableInfo.rowSize; - addExprInfoParam(&pExpr->base, (char*) &rowSize, TSDB_DATA_TYPE_BIGINT, 8); - return TSDB_CODE_SUCCESS; - } - - case FUNCTION_COV: { - // 1. valid the number of parameters - // no parameters or more than one parameter for function - if ((code = checkForkParam(pItem->pNode, 2, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - - tSqlExprItem* p1 = taosArrayGet(pItem->pNode->Expr.paramList, 0); - tSqlExprItem* p2 = taosArrayGet(pItem->pNode->Expr.paramList, 1); - - int32_t p1Type = p1->pNode->tokenId, p2Type = p2->pNode->tokenId; - if (p1Type != TK_ID || p2Type != TK_ID) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - // validate the first parameter - tExprNode* pNode1 = NULL; - int32_t tokenId1 = p1->pNode->tokenId; - SColumnIndex index1 = COLUMN_INDEX_INITIALIZER; - SSchema columnSchema1 = {0}; - - code = extractFunctionParameterInfo(pQueryInfo, tokenId1, &pTableMetaInfo, &columnSchema1, &pNode1, &index1, p1, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - // validate the second parameter - tExprNode* pNode2 = NULL; - int32_t tokenId2 = p1->pNode->tokenId; - SColumnIndex index2 = COLUMN_INDEX_INITIALIZER; - SSchema columnSchema2 = {0}; - code = extractFunctionParameterInfo(pQueryInfo, tokenId2, &pTableMetaInfo, &columnSchema2, &pNode2, &index2, p1, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - int32_t srcType1 = columnSchema1.type, srcType2 = columnSchema2.type; - if (IS_VAR_DATA_TYPE(srcType1) || IS_VAR_DATA_TYPE(columnSchema2.type) || srcType1 == TSDB_DATA_TYPE_TIMESTAMP || - srcType1 == TSDB_DATA_TYPE_BOOL || srcType2 == TSDB_DATA_TYPE_TIMESTAMP || srcType2 == TSDB_DATA_TYPE_BOOL) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - SResultDataInfo resInfo = {.type = TSDB_DATA_TYPE_DOUBLE, .bytes = sizeof(double), .intermediateBytes = 0}; - SSchema s = createSchema(resInfo.type, resInfo.bytes, getNewResColId(), ""); - - char token[TSDB_COL_NAME_LEN] = {0}; - setTokenAndResColumnName(pItem, s.name, token, sizeof(s.name) - 1); - - SColumn c1 = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, index1.type, &columnSchema1); - SColumn c2 = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, index2.type, &columnSchema2); - - SSourceParam param = {0}; - addIntoSourceParam(¶m, pNode1, &c1); - addIntoSourceParam(¶m, pNode2, &c2); - - char funcName[FUNCTIONS_NAME_MAX_LENGTH] = {0}; - extractFunctionName(funcName, pItem); - - doAddOneExprInfo(pQueryInfo, funcName, ¶m, colIndex, pTableMetaInfo, &s, resInfo.intermediateBytes, token, finalResult); - return TSDB_CODE_SUCCESS; - } - - default: { -// pUdfInfo = isValidUdf(pQueryInfo->pUdfInfo, pItem->pNode->Expr.operand.z, pItem->pNode->Expr.operand.n); -// if (pUdfInfo == NULL) { -// return buildInvalidOperationMsg(pMsgBuf, msg9); -// } - - tSqlExprItem* pParamElem = taosArrayGet(pItem->pNode->Expr.paramList, 0);; - if (pParamElem->pNode->tokenId != TK_ID) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByName(&pParamElem->pNode->columnName, pQueryInfo, &index, pMsgBuf) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { - return buildInvalidOperationMsg(pMsgBuf, msg6); - } - - pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - - // functions can not be applied to tags - if (index.columnIndex >= getNumOfColumns(pTableMetaInfo->pTableMeta)) { - return buildInvalidOperationMsg(pMsgBuf, msg6); - } - - SResultDataInfo resInfo = {0}; - getResultDataInfo(TSDB_DATA_TYPE_INT, 4, functionId, 0, &resInfo, 0, false/*, pUdfInfo*/); - - SSchema s = createSchema(resInfo.type, resInfo.bytes, getNewResColId(), ""); - SSchema* colSchema = getOneColumnSchema(pTableMetaInfo->pTableMeta, index.tableIndex); - - char token[TSDB_COL_NAME_LEN] = {0}; - setTokenAndResColumnName(pItem, s.name, token, sizeof(s.name) - 1); - - SColumn c = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, index.type, colSchema); - - SSourceParam param = {0}; - addIntoSourceParam(¶m, NULL, &c); - - char funcName[FUNCTIONS_NAME_MAX_LENGTH] = {0}; - extractFunctionName(funcName, pItem); - doAddOneExprInfo(pQueryInfo, funcName, ¶m, colIndex, pTableMetaInfo, &s, resInfo.intermediateBytes, token, finalResult); - return TSDB_CODE_SUCCESS; - } - } - - return TSDB_CODE_TSC_INVALID_OPERATION; -} - -static int32_t validateExprLeafColumnNode(SQueryStmtInfo *pQueryInfo, SToken* pColumnName, SArray* pList, SMsgBuf* pMsgBuf) { - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByName(pColumnName, pQueryInfo, &index, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // if column is timestamp not support arithmetic, so return invalid sql - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - - SSchema* pSchema = getOneColumnSchema(pTableMeta, index.columnIndex); - if (pSchema->type == TSDB_DATA_TYPE_TIMESTAMP) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - SColumn c = createColumn(pTableMeta->uid, pTableMetaInfo->aliasName, index.type, pSchema); - taosArrayPush(pList, &c); - - return TSDB_CODE_SUCCESS; -} - -static int32_t validateExprLeafFunctionNode(SQueryStmtInfo* pQueryInfo, tSqlExpr* pExpr, SMsgBuf* pMsgBuf) { - tSqlExprItem item = {.pNode = pExpr, .aliasName = NULL}; - - // sql function list in selection clause. - // Append the sqlExpr into exprList of pQueryInfo structure sequentially - bool scalar = false; - item.functionId = qIsBuiltinFunction(pExpr->Expr.operand.z, pExpr->Expr.operand.n, &scalar); - if (item.functionId < 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - int32_t outputIndex = (int32_t)getNumOfExprs(pQueryInfo); - - if (scalar) { - printf("scalar function found!\n"); -// if (addScalarExprAndResColumn(pQueryInfo, outputIndex, &item, pMsgBuf) != TSDB_CODE_SUCCESS) { -// return TSDB_CODE_TSC_INVALID_OPERATION; -// } - } else { - if (addAggExprAndResColumn(pQueryInfo, outputIndex, &item, false, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // It is invalid in case of more than one sqlExpr, such as first(ts, k) - last(ts, k) - int32_t inc = (int32_t)getNumOfExprs(pQueryInfo) - outputIndex; - if (inc > 1) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // Not supported data type in expression - for (int32_t i = 0; i < inc; ++i) { - SExprInfo* p1 = getExprInfo(pQueryInfo, i + outputIndex); - int16_t t = p1->base.resSchema.type; - if (t == TSDB_DATA_TYPE_TIMESTAMP) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - } - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t validateScalarFunctionParamNum(tSqlExpr* pSqlExpr, int32_t functionId, SMsgBuf* pMsgBuf) { - int32_t code = TSDB_CODE_SUCCESS; - switch (functionId) { - case FUNCTION_CEIL: { - code = checkForkParam(pSqlExpr, 1, pMsgBuf); - break; - } - case FUNCTION_LENGTH: { - code = checkForkParam(pSqlExpr, 1, pMsgBuf); - break; - } - } - - return code; -} - -// todo merge with the addScalarExprAndResColumn -int32_t doAddOneProjectCol(SQueryStmtInfo* pQueryInfo, int32_t outputColIndex, SSchema* pSchema, const char* aliasName, - int32_t colId, SMsgBuf* pMsgBuf) { - const char* name = (aliasName == NULL)? pSchema->name:aliasName; - SSchema s = createSchema(pSchema->type, pSchema->bytes, colId, name); - - SArray* pColumnList = taosArrayInit(4, sizeof(SColumn)); - SToken colNameToken = {.z = pSchema->name, .n = strlen(pSchema->name)}; - - tSqlExpr sqlNode = {0}; - sqlNode.type = SQL_NODE_TABLE_COLUMN; - sqlNode.columnName = colNameToken; - - tExprNode* pNode = NULL; - bool keepTableCols = true; - int32_t ret = sqlExprToExprNode(&pNode, &sqlNode, pQueryInfo, pColumnList, &keepTableCols, pMsgBuf); - if (ret != TSDB_CODE_SUCCESS) { - tExprTreeDestroy(pNode, NULL); - return buildInvalidOperationMsg(pMsgBuf, "invalid expression in select clause"); - } - - SExprInfo* pExpr = createBinaryExprInfo(pNode, &s); - tstrncpy(pExpr->base.resSchema.name, name, tListLen(pExpr->base.resSchema.name)); - tstrncpy(pExpr->base.token, name, tListLen(pExpr->base.token)); - - SArray* pExprList = getCurrentExprList(pQueryInfo); - addExprInfo(pExprList, outputColIndex, pExpr, pQueryInfo->exprListLevelIndex); - - // extract columns according to the tExprNode tree - size_t num = taosArrayGetSize(pColumnList); - pExpr->base.pColumns = calloc(num, sizeof(SColumn)); - for (int32_t i = 0; i < num; ++i) { - SColumn* pCol = taosArrayGet(pColumnList, i); - pExpr->base.pColumns[i] = *pCol; - } - - pExpr->base.numOfCols = num; - - if (pQueryInfo->exprListLevelIndex == 0) { - int32_t exists = getNumOfFields(&pQueryInfo->fieldsInfo); - addResColumnInfo(pQueryInfo, exists, &pExpr->base.resSchema, pExpr); - } - - pQueryInfo->info.projectionQuery = true; - - taosArrayDestroy(pColumnList); - return TSDB_CODE_SUCCESS; -} - -static int32_t doAddMultipleProjectExprAndResColumns(SQueryStmtInfo* pQueryInfo, SColumnIndex* pIndex, int32_t startPos, SMsgBuf* pMsgBuf) { - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, pIndex->tableIndex); - - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - STableComInfo tinfo = getTableInfo(pTableMeta); - - int32_t numOfTotalColumns = tinfo.numOfColumns; - if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { - numOfTotalColumns += tinfo.numOfTags; - } - - for (int32_t j = 0; j < numOfTotalColumns; ++j) { - SSchema* pSchema = getOneColumnSchema(pTableMetaInfo->pTableMeta, j); - doAddOneProjectCol(pQueryInfo, startPos + j, pSchema, NULL, getNewResColId(), pMsgBuf); - } - - return numOfTotalColumns; -} - -// User input constant value as a new result column -static SColumnIndex createConstantColumnIndex(int32_t* colId) { - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - index.columnIndex = ((*colId)--); - index.tableIndex = 0; - index.type = TSDB_COL_UDC; - return index; -} - -static SSchema createConstantColumnSchema(SVariant* pVal, const SToken* exprStr, const char* name) { - SSchema s = {0}; - - s.type = pVal->nType; - if (IS_VAR_DATA_TYPE(s.type)) { - s.bytes = (int16_t)(pVal->nLen + VARSTR_HEADER_SIZE); - } else { - s.bytes = tDataTypes[pVal->nType].bytes; - } - - s.colId = TSDB_UD_COLUMN_INDEX; - - if (name != NULL) { - tstrncpy(s.name, name, sizeof(s.name)); - } else { - size_t tlen = TMIN(sizeof(s.name), exprStr->n + 1); - tstrncpy(s.name, exprStr->z, tlen); - strdequote(s.name); - } - - return s; -} - -static int32_t handleTbnameProjection(SQueryStmtInfo* pQueryInfo, tSqlExprItem* pItem, SColumnIndex* pIndex, int32_t startPos, bool outerQuery, SMsgBuf* pMsgBuf) { - const char* msg1 = "tbname not allowed in outer query"; - - SSchema colSchema = {0}; - if (outerQuery) { // todo?? - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, pIndex->tableIndex); - - bool existed = false; - SSchema* pSchema = pTableMetaInfo->pTableMeta->schema; - - int32_t numOfCols = getNumOfColumns(pTableMetaInfo->pTableMeta); - for (int32_t i = 0; i < numOfCols; ++i) { - if (strncasecmp(pSchema[i].name, TSQL_TBNAME_L, tListLen(pSchema[i].name)) == 0) { - existed = true; - pIndex->columnIndex = i; - break; - } - } - - if (!existed) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - colSchema = pSchema[pIndex->columnIndex]; - } else { - colSchema = *getTbnameColumnSchema(); - } - - return doAddOneProjectCol(pQueryInfo, startPos, &colSchema, pItem->aliasName, getNewResColId(), pMsgBuf); -} - -int32_t addProjectionExprAndResColumn(SQueryStmtInfo* pQueryInfo, tSqlExprItem* pItem, bool outerQuery, SMsgBuf* pMsgBuf) { - const char* msg1 = "tag for normal table query is not allowed"; - const char* msg2 = "invalid column name"; - - if (checkForAliasName(pMsgBuf, pItem->aliasName) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - int32_t startPos = (int32_t)getNumOfExprs(pQueryInfo); - int32_t tokenId = pItem->pNode->tokenId; - if (tokenId == TK_ALL) { // project on all fields - TSDB_QUERY_SET_TYPE(pQueryInfo->type, TSDB_QUERY_TYPE_PROJECTION_QUERY); - - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getTableIndexByName(&pItem->pNode->columnName, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - // all columns are required - if (index.tableIndex == COLUMN_INDEX_INITIAL_VAL) { // all table columns are required. - for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { - index.tableIndex = i; - int32_t inc = doAddMultipleProjectExprAndResColumns(pQueryInfo, &index, startPos, pMsgBuf); - startPos += inc; - } - } else { - doAddMultipleProjectExprAndResColumns(pQueryInfo, &index, startPos, pMsgBuf); - } - - // add the primary timestamp column even though it is not required by user - STableMeta* pTableMeta = getMetaInfo(pQueryInfo, index.tableIndex)->pTableMeta; - if (pTableMeta->tableType != TSDB_TEMP_TABLE) { - insertPrimaryTsColumn(pQueryInfo->colList, pTableMeta->schema[0].name, pTableMeta->uid); - } - } else if (tokenId == TK_STRING || tokenId == TK_INTEGER || tokenId == TK_FLOAT) { //constant value column - SColumnIndex index = createConstantColumnIndex(&pQueryInfo->udColumnId); - SSchema colSchema = createConstantColumnSchema(&pItem->pNode->value, &pItem->pNode->exprToken, pItem->aliasName); - - char token[TSDB_COL_NAME_LEN] = {0}; - tstrncpy(token, pItem->pNode->exprToken.z, TMIN(TSDB_COL_NAME_LEN, TSDB_COL_NAME_LEN)); - - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - SColumn c = createColumn(pTableMetaInfo->pTableMeta->uid, pTableMetaInfo->aliasName, index.type, &colSchema); - - SSourceParam param = {0}; - addIntoSourceParam(¶m, NULL, &c); - - SExprInfo* pExpr = doAddOneExprInfo(pQueryInfo, "project", ¶m, startPos, pTableMetaInfo, &colSchema, 0, token, true); - // NOTE: the first parameter is reserved for the tag column id during join query process. - pExpr->base.numOfParams = 2; - taosVariantAssign(&pExpr->base.param[1], &pItem->pNode->value); - } else if (tokenId == TK_ID) { // column name - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByName(&pItem->pNode->columnName, pQueryInfo, &index, pMsgBuf) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { - handleTbnameProjection(pQueryInfo, pItem, &index, startPos, outerQuery, pMsgBuf); - } else { - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - if (TSDB_COL_IS_TAG(index.type) && UTIL_TABLE_IS_NORMAL_TABLE(pTableMetaInfo)) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - SSchema* pSchema = getOneColumnSchema(pTableMetaInfo->pTableMeta, index.columnIndex); - doAddOneProjectCol(pQueryInfo, startPos, pSchema, pItem->aliasName, getNewResColId(), pMsgBuf); - } - - // add the primary timestamp column even though it is not required by user - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, index.tableIndex); - if (!UTIL_TABLE_IS_TMP_TABLE(pTableMetaInfo)) { - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - insertPrimaryTsColumn(pQueryInfo->colList, pTableMeta->schema[0].name, pTableMeta->uid); - } - } else { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t validateExprLeafNode(tSqlExpr* pExpr, SQueryStmtInfo* pQueryInfo, SArray* pList, int32_t* type, SMsgBuf* pMsgBuf) { - if (pExpr->type == SQL_NODE_TABLE_COLUMN) { - if (*type == NON_ARITHMEIC_EXPR) { - *type = NORMAL_ARITHMETIC; - } else if (*type == AGG_ARIGHTMEIC) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - int32_t code = validateExprLeafColumnNode(pQueryInfo, &pExpr->columnName, pList, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - } else if ((pExpr->tokenId == TK_FLOAT && (isnan(pExpr->value.d) || isinf(pExpr->value.d))) || - pExpr->tokenId == TK_NULL) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } else if (pExpr->type == SQL_NODE_SQLFUNCTION) { - if (*type == NON_ARITHMEIC_EXPR) { - *type = AGG_ARIGHTMEIC; - } else if (*type == NORMAL_ARITHMETIC) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - int32_t code = validateExprLeafFunctionNode(pQueryInfo, pExpr, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - } - - return TSDB_CODE_SUCCESS; -} - -static uint64_t findTmpSourceColumnInNextLevel(SQueryStmtInfo* pQueryInfo, tExprNode *pExpr) { - // This function must be a aggregate function, so it must be in the next level - pQueryInfo->exprListLevelIndex += 1; - - // set the input column data byte and type. - SArray* pExprList = getCurrentExprList(pQueryInfo); - - bool found = false; - uint64_t uid = 0; - - size_t size = taosArrayGetSize(pExprList); - for (int32_t i = 0; i < size; ++i) { - SExprInfo* p1 = taosArrayGetP(pExprList, i); - - if (strcmp((pExpr)->pSchema->name, p1->base.resSchema.name) == 0) { - memcpy((pExpr)->pSchema, &p1->base.resSchema, sizeof(SSchema)); - found = true; - uid = p1->base.pColumns->uid; - break; - } - } - - assert(found); - pQueryInfo->exprListLevelIndex -= 1; - - return uid; -} - -static tExprNode* doCreateColumnNode(SQueryStmtInfo* pQueryInfo, SColumnIndex* pIndex, bool keepTableCols, SArray* pCols) { - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, pIndex->tableIndex); - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - - tExprNode* pExpr = calloc(1, sizeof(tExprNode)); - - pExpr->nodeType = TEXPR_COL_NODE; - pExpr->pSchema = calloc(1, sizeof(SSchema)); - - SSchema* pSchema = NULL; - if (pIndex->columnIndex == TSDB_TBNAME_COLUMN_INDEX) { - pSchema = getTbnameColumnSchema(); - } else { - pSchema = getOneColumnSchema(pTableMeta, pIndex->columnIndex); - } - - *(SSchema*)(pExpr->pSchema) = *pSchema; - - if (keepTableCols && TSDB_COL_IS_NORMAL_COL(pIndex->type)) { - SColumn c = createColumn(pTableMeta->uid, pTableMetaInfo->aliasName, pIndex->type, pExpr->pSchema); - taosArrayPush(pCols, &c); - } - - if (TSDB_COL_IS_NORMAL_COL(pIndex->type)) { - columnListInsert(pQueryInfo->colList, pTableMeta->uid, pSchema, TSDB_COL_NORMAL); - SSchema* pTsSchema = getOneColumnSchema(pTableMeta, 0); - insertPrimaryTsColumn(pQueryInfo->colList, pTsSchema->name, pTableMeta->uid); - } else { - columnListInsert(pTableMetaInfo->tagColList, pTableMeta->uid, pSchema, TSDB_COL_TAG); - } - - return pExpr; -} - -static SExprInfo* createColumnNodeFromAggFunc(SSchema* pSchema) { - tExprNode* pExprNode = calloc(1, sizeof(tExprNode)); - - pExprNode->nodeType = TEXPR_COL_NODE; - pExprNode->pSchema = calloc(1, sizeof(SSchema)); - *(SSchema*)(pExprNode->pSchema) = *pSchema; - - SExprInfo* pExpr = calloc(1, sizeof(SExprInfo)); - if (pExpr == NULL) { - return NULL; - } - - pExpr->pExpr = pExprNode; - memcpy(&pExpr->base.resSchema, pSchema, sizeof(SSchema)); - return pExpr; -} - -static int32_t validateSqlExpr(const tSqlExpr* pSqlExpr, SQueryStmtInfo *pQueryInfo, SMsgBuf* pMsgBuf); - -static int32_t doProcessFunctionLeafNodeParam(SQueryStmtInfo* pQueryInfo, int32_t* num, tExprNode*** p, SArray* pCols, - bool* keepTableCols, const tSqlExpr* pSqlExpr, SMsgBuf* pMsgBuf) { - SArray* pParamList = pSqlExpr->Expr.paramList; - if (pParamList != NULL) { - *num = taosArrayGetSize(pParamList); - (*p) = calloc((*num), POINTER_BYTES); - - for (int32_t i = 0; i < (*num); ++i) { - tSqlExprItem* pItem = taosArrayGet(pParamList, i); - - int32_t ret = validateSqlExpr(pItem->pNode, pQueryInfo, pMsgBuf); - if (ret != TSDB_CODE_SUCCESS) { - return ret; - } - - int32_t code = sqlExprToExprNode(&(*p)[i], pItem->pNode, pQueryInfo, pCols, keepTableCols, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - } - } else { // handle the case: count(*) + 22 - if (strncasecmp(pSqlExpr->Expr.operand.z, "count", pSqlExpr->Expr.operand.n) != 0) { - return buildInvalidOperationMsg(pMsgBuf, "invalid expression"); - } - - *num = 1; - (*p) = calloc(*num, POINTER_BYTES); - - SColumnIndex index = {.type = TSDB_COL_NORMAL, .tableIndex = 0, .columnIndex = 0}; - (*p)[0] = doCreateColumnNode(pQueryInfo, &index, *keepTableCols, pCols); - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t doValidateExpr(SQueryStmtInfo *pQueryInfo, tSqlExpr* pFuncNode, tSqlExpr* pTableColumnNode, SMsgBuf* pMsgBuf) { - char token[FUNCTIONS_NAME_MAX_LENGTH] = {0}; - strncpy(token, pFuncNode->Expr.operand.z, pFuncNode->Expr.operand.n); - bool isAgg = qIsAggregateFunction(token); - - // count(*) + column is a invalid expression. - if (isAgg) { - return buildInvalidOperationMsg(pMsgBuf, "invalid expression"); - } - return TSDB_CODE_SUCCESS; -} - -int32_t validateSqlExpr(const tSqlExpr* pSqlExpr, SQueryStmtInfo *pQueryInfo, SMsgBuf* pMsgBuf) { - assert(pSqlExpr); - - if (pSqlExpr->type == SQL_NODE_EXPR) { - int32_t valid = validateSqlExpr(pSqlExpr->pLeft, pQueryInfo, pMsgBuf); - if (valid != TSDB_CODE_SUCCESS) { - return valid; - } - - valid = validateSqlExpr(pSqlExpr->pRight, pQueryInfo, pMsgBuf); - if (valid != TSDB_CODE_SUCCESS) { - return valid; - } - - tSqlExpr* pLeft = pSqlExpr->pLeft, *pRight = pSqlExpr->pRight; - if (pLeft->type == SQL_NODE_SQLFUNCTION && pRight->type == SQL_NODE_SQLFUNCTION) { - - char token[FUNCTIONS_NAME_MAX_LENGTH] = {0}; - strncpy(token, pLeft->Expr.operand.z, pLeft->Expr.operand.n); - bool agg1 = qIsAggregateFunction(token); - - strncpy(token, pRight->Expr.operand.z, pRight->Expr.operand.n); - bool agg2 = qIsAggregateFunction(token); - - if (agg1 != agg2) { - return buildInvalidOperationMsg(pMsgBuf, "invalid expression"); - } - } - - if (pLeft->type == SQL_NODE_SQLFUNCTION && pRight->type == SQL_NODE_TABLE_COLUMN) { - int32_t code = doValidateExpr(pQueryInfo, pLeft, pRight, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - } else if (pRight->type == SQL_NODE_SQLFUNCTION && pLeft->type == SQL_NODE_TABLE_COLUMN) { - int32_t code = doValidateExpr(pQueryInfo, pRight, pLeft, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - } - - int32_t tokenId = pSqlExpr->tokenId; - if (pRight->type == SQL_NODE_VALUE && (pRight->value.nType == TSDB_DATA_TYPE_DOUBLE || pRight->value.nType == TSDB_DATA_TYPE_INT) && - pRight->value.d == 0 && tokenId == TK_DIVIDE) { - return buildInvalidOperationMsg(pMsgBuf, "invalid expression (divided by 0)"); - } - - if (tokenId == TK_DIVIDE || tokenId == TK_TIMES || tokenId == TK_MINUS || tokenId == TK_PLUS || tokenId == TK_MODULES) { - if ((pRight->type == SQL_NODE_VALUE && pRight->value.nType == TSDB_DATA_TYPE_BINARY) || - (pLeft->type == SQL_NODE_VALUE && pLeft->value.nType == TSDB_DATA_TYPE_BINARY)) { - return buildInvalidOperationMsg(pMsgBuf, "invalid expression (string in arithmetic expression)"); - } - } - - } else if (pSqlExpr->type == SQL_NODE_TABLE_COLUMN) { - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - - int32_t ret = getColumnIndexByName(&pSqlExpr->columnName, pQueryInfo, &index, pMsgBuf); - if (ret != TSDB_CODE_SUCCESS) { - return ret; - } - } else if (pSqlExpr->type == SQL_NODE_SQLFUNCTION) { - bool scalar = false; - int32_t functionId = qIsBuiltinFunction(pSqlExpr->Expr.operand.z, pSqlExpr->Expr.operand.n, &scalar); - if (functionId < 0) { - return buildInvalidOperationMsg(pMsgBuf, "invalid function name"); - } - - // do check the parameter number for scalar function - if (scalar) { - int32_t ret = validateScalarFunctionParamNum((tSqlExpr*) pSqlExpr, functionId, pMsgBuf); - if (ret != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, "invalid number of function parameters"); - } - } - } - - return TSDB_CODE_SUCCESS; -} - -int32_t sqlExprToExprNode(tExprNode **pExpr, const tSqlExpr* pSqlExpr, SQueryStmtInfo* pQueryInfo, SArray* pCols, bool* keepTableCols, SMsgBuf* pMsgBuf) { - tExprNode* pLeft = NULL; - tExprNode* pRight= NULL; - - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (pSqlExpr->type == SQL_NODE_EXPR) { - if (pSqlExpr->pLeft != NULL) { - int32_t ret = sqlExprToExprNode(&pLeft, pSqlExpr->pLeft, pQueryInfo, pCols, keepTableCols, pMsgBuf); - if (ret != TSDB_CODE_SUCCESS) { - return ret; - } - } - - if (pSqlExpr->pRight != NULL) { - int32_t ret = sqlExprToExprNode(&pRight, pSqlExpr->pRight, pQueryInfo, pCols, keepTableCols, pMsgBuf); - if (ret != TSDB_CODE_SUCCESS) { - tExprTreeDestroy(pLeft, NULL); - return ret; - } - } - - if (pSqlExpr->pLeft == NULL && pSqlExpr->pRight == NULL && pSqlExpr->tokenId == 0) { - *pExpr = calloc(1, sizeof(tExprNode)); - return TSDB_CODE_SUCCESS; - } - } else if (pSqlExpr->type == SQL_NODE_SQLFUNCTION) { - bool scalar = false; - int32_t functionId = qIsBuiltinFunction(pSqlExpr->Expr.operand.z, pSqlExpr->Expr.operand.n, &scalar); - if (functionId < 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if (!scalar) { - pQueryInfo->exprListLevelIndex += 1; - } - - *keepTableCols = false; - - int32_t num = 0; - tExprNode** p = NULL; - int32_t code = doProcessFunctionLeafNodeParam(pQueryInfo, &num, &p, pCols, keepTableCols, pSqlExpr, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - int32_t outputIndex = (int32_t)getNumOfExprs(pQueryInfo); - - if (scalar) { - printf("scalar function found! %s\n", pSqlExpr->exprToken.z); - - // Expression on the results of aggregation functions - *pExpr = calloc(1, sizeof(tExprNode)); - (*pExpr)->nodeType = TEXPR_FUNCTION_NODE; - - (*pExpr)->_function.pChild = p; - (*pExpr)->_function.num = num; - strncpy((*pExpr)->_function.functionName, pSqlExpr->Expr.operand.z, pSqlExpr->Expr.operand.n); - return TSDB_CODE_SUCCESS; - } else { - printf("agg function found, %s\n", pSqlExpr->exprToken.z); - tSqlExprItem item = {.pNode = (tSqlExpr*)pSqlExpr, .aliasName = NULL, .functionId = functionId}; - if (addAggExprAndResColumn(pQueryInfo, outputIndex, &item, false, pMsgBuf) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - pQueryInfo->exprListLevelIndex -= 1; - // convert the aggregate function to be the input data columns for the outer function. - } - } - - if (pSqlExpr->pLeft == NULL) { // it is the leaf node - assert(pSqlExpr->pRight == NULL); - - if (pSqlExpr->type == SQL_NODE_VALUE) { - int32_t ret = TSDB_CODE_SUCCESS; - *pExpr = calloc(1, sizeof(tExprNode)); - (*pExpr)->nodeType = TEXPR_VALUE_NODE; - (*pExpr)->pVal = calloc(1, sizeof(SVariant)); - taosVariantAssign((*pExpr)->pVal, &pSqlExpr->value); - - STableMeta* pTableMeta = getMetaInfo(pQueryInfo, 0)->pTableMeta; - if (pCols != NULL && taosArrayGetSize(pCols) > 0) { - SColIndex* idx = taosArrayGet(pCols, 0); - SSchema* pSchema = getOneColumnSchema(pTableMeta, idx->colIndex); - - // convert time by precision - if (pSchema != NULL && TSDB_DATA_TYPE_TIMESTAMP == pSchema->type && TSDB_DATA_TYPE_BINARY == (*pExpr)->pVal->nType) { -#if 0 - ret = setColumnFilterInfoForTimestamp(pCmd, pQueryInfo, (*pExpr)->pVal); -#endif - } - } - return ret; - } else if (pSqlExpr->type == SQL_NODE_SQLFUNCTION) { - // Expression on the results of aggregation functions - *pExpr = calloc(1, sizeof(tExprNode)); - (*pExpr)->nodeType = TEXPR_COL_NODE; - (*pExpr)->pSchema = calloc(1, sizeof(SSchema)); - strncpy((*pExpr)->pSchema->name, pSqlExpr->exprToken.z, pSqlExpr->exprToken.n); - - // it must be the aggregate function - assert(qIsAggregateFunction((*pExpr)->pSchema->name)); - - uint64_t uid = findTmpSourceColumnInNextLevel(pQueryInfo, *pExpr); - if (!(*keepTableCols)) { - SColumn c = createColumn(uid, NULL, TSDB_COL_TMP, (*pExpr)->pSchema); - taosArrayPush(pCols, &c); - } - } else if (pSqlExpr->type == SQL_NODE_TABLE_COLUMN) { // column name, normal column expression - int32_t ret = getColumnIndexByName(&pSqlExpr->columnName, pQueryInfo, &index, pMsgBuf); - if (ret != TSDB_CODE_SUCCESS) { - return ret; - } - - *pExpr = doCreateColumnNode(pQueryInfo, &index, *keepTableCols, pCols); - return TSDB_CODE_SUCCESS; - } else if (pSqlExpr->tokenId == TK_SET) { - int32_t colType = -1; - STableMeta* pTableMeta = getMetaInfo(pQueryInfo, pQueryInfo->curTableIdx)->pTableMeta; - if (pCols != NULL) { - size_t colSize = taosArrayGetSize(pCols); - - if (colSize > 0) { - SColIndex* idx = taosArrayGet(pCols, colSize - 1); - SSchema* pSchema = getOneColumnSchema(pTableMeta, idx->colIndex); - if (pSchema != NULL) { - colType = pSchema->type; - } - } - } - - SVariant *pVal; - if (colType >= TSDB_DATA_TYPE_TINYINT && colType <= TSDB_DATA_TYPE_BIGINT) { - colType = TSDB_DATA_TYPE_BIGINT; - } else if (colType == TSDB_DATA_TYPE_FLOAT || colType == TSDB_DATA_TYPE_DOUBLE) { - colType = TSDB_DATA_TYPE_DOUBLE; - } - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, pQueryInfo->curTableIdx); - STableComInfo tinfo = getTableInfo(pTableMetaInfo->pTableMeta); -#if 0 - if (serializeExprListToVariant(pSqlExpr->Expr.paramList, &pVal, colType, tinfo.precision) == false) { - return buildInvalidOperationMsg(pMsgBuf, "not support filter expression"); - } -#endif - *pExpr = calloc(1, sizeof(tExprNode)); - (*pExpr)->nodeType = TEXPR_VALUE_NODE; - (*pExpr)->pVal = pVal; - } else { - return buildInvalidOperationMsg(pMsgBuf, "not support filter expression"); - } - } else { - *pExpr = (tExprNode*)calloc(1, sizeof(tExprNode)); - (*pExpr)->nodeType = TEXPR_BINARYEXPR_NODE; - - (*pExpr)->_node.pLeft = pLeft; - (*pExpr)->_node.pRight = pRight; - - SToken t = {.type = pSqlExpr->tokenId}; - (*pExpr)->_node.optr = convertRelationalOperator(&t); - - assert((*pExpr)->_node.optr != 0); - } - return TSDB_CODE_SUCCESS; -} - -static int32_t addScalarExprAndResColumn(SQueryStmtInfo* pQueryInfo, int32_t exprIndex, tSqlExprItem* pItem, SMsgBuf* pMsgBuf) { - SArray* pColumnList = taosArrayInit(4, sizeof(SColumn)); - SSchema s = createSchema(TSDB_DATA_TYPE_DOUBLE, sizeof(double), getNewResColId(), ""); - - int32_t ret = validateSqlExpr(pItem->pNode, pQueryInfo, pMsgBuf); - if (ret != TSDB_CODE_SUCCESS) { - return ret; - } - - tExprNode* pNode = NULL; - bool keepTableCols = true; - ret = sqlExprToExprNode(&pNode, pItem->pNode, pQueryInfo, pColumnList, &keepTableCols, pMsgBuf); - if (ret != TSDB_CODE_SUCCESS) { - tExprTreeDestroy(pNode, NULL); - return buildInvalidOperationMsg(pMsgBuf, "invalid expression in select clause"); - } - - SExprInfo* pExpr = createBinaryExprInfo(pNode, &s); - setTokenAndResColumnName(pItem, pExpr->base.resSchema.name, pExpr->base.token, TSDB_COL_NAME_LEN); - - SArray* pExprList = getCurrentExprList(pQueryInfo); - addExprInfo(pExprList, exprIndex, pExpr, pQueryInfo->exprListLevelIndex); - - // extract columns according to the tExprNode tree - size_t num = taosArrayGetSize(pColumnList); - pExpr->base.pColumns = calloc(num, sizeof(SColumn)); - for (int32_t i = 0; i < num; ++i) { - SColumn* pCol = taosArrayGet(pColumnList, i); - pExpr->base.pColumns[i] = *pCol; - } - - pExpr->base.numOfCols = num; - - pExpr->base.numOfParams = 1; - SBufferWriter bw = tbufInitWriter(NULL, false); - // TRY(0) { - exprTreeToBinary(&bw, pExpr->pExpr); - // } CATCH(code) { - // tbufCloseWriter(&bw); - // UNUSED(code); - // TODO: other error handling - // } END_TRY - - SSqlExpr* pSqlExpr = &pExpr->base; - pSqlExpr->param[0].nLen = (int16_t)tbufTell(&bw); - pSqlExpr->param[0].pz = tbufGetData(&bw, true); - pSqlExpr->param[0].nType = TSDB_DATA_TYPE_BINARY; - - tbufCloseWriter(&bw); - - if (pQueryInfo->exprListLevelIndex == 0) { - int32_t exists = getNumOfFields(&pQueryInfo->fieldsInfo); - addResColumnInfo(pQueryInfo, exists, &pExpr->base.resSchema, pExpr); - } - - // tbufCloseWriter(&bw); // TODO there is a memory leak - - taosArrayDestroy(pColumnList); - return TSDB_CODE_SUCCESS; -} - -int32_t validateSelectNodeList(SQueryStmtInfo* pQueryInfo, SArray* pSelNodeList, bool outerQuery, SMsgBuf* pMsgBuf) { - assert(pSelNodeList != NULL); - - const char* msg1 = "too many items in selection clause"; - const char* msg2 = "functions or others can not be mixed up"; - const char* msg3 = "not support query expression"; - const char* msg4 = "distinct should be in the first place in select clause"; - const char* msg5 = "invalid function name"; - - // too many result columns not support order by in query - if (taosArrayGetSize(pSelNodeList) > TSDB_MAX_COLUMNS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - int32_t code = TSDB_CODE_SUCCESS; - size_t numOfExpr = taosArrayGetSize(pSelNodeList); - - for (int32_t i = 0; i < numOfExpr; ++i) { - int32_t outputIndex = (int32_t) getNumOfExprs(pQueryInfo); - tSqlExprItem* pItem = taosArrayGet(pSelNodeList, i); - int32_t type = pItem->pNode->type; - - if (pItem->distinct) { - if (i != 0 || type == SQL_NODE_SQLFUNCTION || type == SQL_NODE_EXPR) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - pQueryInfo->info.distinct = true; - } - - if (type == SQL_NODE_SQLFUNCTION) { - bool scalarFunc = false; - pItem->functionId = qIsBuiltinFunction(pItem->pNode->Expr.operand.z, pItem->pNode->Expr.operand.n, &scalarFunc); - if (pItem->functionId == FUNCTION_INVALID_ID) { // temporarily disable the udf -// int32_t functionId = FUNCTION_INVALID_ID; -// bool valid = qIsValidUdf(pQueryInfo->pUdfInfo, pItem->pNode->Expr.operand.z, pItem->pNode->Expr.operand.n, &functionId); -// if (!valid) { - return buildInvalidOperationMsg(pMsgBuf, msg5); -// } - -// pItem->functionId = functionId; - } - - if (scalarFunc) { // scalar function - if ((code = addScalarExprAndResColumn(pQueryInfo, outputIndex, pItem, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - } else { // aggregate function - // sql function in selection clause, append sql function info in pSqlCmd structure sequentially - if ((code = addAggExprAndResColumn(pQueryInfo, outputIndex, pItem, true, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - } - } else if (type == SQL_NODE_TABLE_COLUMN || type == SQL_NODE_VALUE) { - // use the dynamic array list to decide if the function is valid or not - // select table_name1.field_name1, table_name2.field_name2 from table_name1, table_name2 - if ((code = addProjectionExprAndResColumn(pQueryInfo, pItem, outerQuery, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - } else if (type == SQL_NODE_EXPR) { - if ((code = addScalarExprAndResColumn(pQueryInfo, i, pItem, pMsgBuf)) != TSDB_CODE_SUCCESS) { - return code; - } - } else { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - } - - return TSDB_CODE_SUCCESS; -} - -int32_t evaluateSqlNode(SSqlNode* pNode, int32_t tsPrecision, SMsgBuf* pMsgBuf) { - assert(pNode != NULL && pMsgBuf != NULL && pMsgBuf->len > 0); - - // Evaluate expression in where clause - if (pNode->pWhere != NULL) { - int32_t code = evaluateSqlNodeImpl(pNode->pWhere, tsPrecision); - if (code != TSDB_CODE_SUCCESS) { - strncpy(pMsgBuf->buf, "invalid time expression in sql", pMsgBuf->len); - return code; - } - } - - // Evaluate the expression in select clause - size_t size = taosArrayGetSize(pNode->pSelNodeList); - for(int32_t i = 0; i < size; ++i) { - tSqlExprItem* pItem = taosArrayGet(pNode->pSelNodeList, i); - int32_t code = evaluateSqlNodeImpl(pItem->pNode, tsPrecision); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - } - - return TSDB_CODE_SUCCESS; -} - -//TODO remove it -int32_t setTableVgroupList(SParseContext *pCtx, SName* name, SVgroupsInfo **pVgList) { - SArray* vgroupList = NULL; - int32_t code = catalogGetTableDistVgInfo(pCtx->pCatalog, pCtx->pTransporter, &pCtx->mgmtEpSet, name, &vgroupList); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - size_t vgroupNum = taosArrayGetSize(vgroupList); - - SVgroupsInfo *vgList = calloc(1, sizeof(SVgroupsInfo) + sizeof(SVgroupInfo) * vgroupNum); - vgList->numOfVgroups = vgroupNum; - - for (int32_t i = 0; i < vgroupNum; ++i) { - SVgroupInfo *vg = taosArrayGet(vgroupList, i); - vgList->vgroups[i] = *vg; - } - - *pVgList = vgList; - taosArrayDestroy(vgroupList); - - return TSDB_CODE_SUCCESS; -} - -int32_t qParserValidateSqlNode(SParseContext *pCtx, SSqlInfo* pInfo, SQueryStmtInfo* pQueryInfo, char* msgBuf, int32_t msgBufLen) { - assert(pCtx != NULL && pInfo != NULL); - int32_t code = 0; - - SMsgBuf m = {.buf = msgBuf, .len = msgBufLen}; - SMsgBuf* pMsgBuf = &m; - - switch (pInfo->type) { -#if 0 - case TSDB_SQL_DROP_TABLE: - case TSDB_SQL_DROP_USER: - case TSDB_SQL_DROP_ACCT: - case TSDB_SQL_DROP_DNODE: - case TSDB_SQL_DROP_DB: { - const char* msg1 = "param name too long"; - const char* msg2 = "invalid name"; - - SToken* pzName = taosArrayGet(pInfo->pMiscInfo->a, 0); - if ((pInfo->type != TSDB_SQL_DROP_DNODE) && (parserValidateIdToken(pzName) != TSDB_CODE_SUCCESS)) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - if (pInfo->type == TSDB_SQL_DROP_DB) { - assert(taosArrayGetSize(pInfo->pMiscInfo->a) == 1); - code = tNameSetDbName(&pTableMetaInfo->name, getAccountId(pSql), pzName); - if (code != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - } else if (pInfo->type == TSDB_SQL_DROP_TABLE) { - assert(taosArrayGetSize(pInfo->pMiscInfo->a) == 1); - - code = tscSetTableFullName(&pTableMetaInfo->name, pzName, pSql); - if(code != TSDB_CODE_SUCCESS) { - return code; - } - } else if (pInfo->type == TSDB_SQL_DROP_DNODE) { - if (pzName->type == TK_STRING) { - pzName->n = strdequote(pzName->z); - } - strncpy(pCmd->payload, pzName->z, pzName->n); - } else { // drop user/account - if (pzName->n >= TSDB_USER_LEN) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - strncpy(pCmd->payload, pzName->z, pzName->n); - } - - break; - } - - case TSDB_SQL_RESET_CACHE: { - return TSDB_CODE_SUCCESS; - } - - case TSDB_SQL_CREATE_FUNCTION: - case TSDB_SQL_DROP_FUNCTION: { - code = handleUserDefinedFunc(pSql, pInfo); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - break; - } - - case TSDB_SQL_DESCRIBE_TABLE: { - const char* msg1 = "invalid table name"; - - SToken* pToken = taosArrayGet(pInfo->pMiscInfo->a, 0); - if (tscValidateName(pToken) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - // additional msg has been attached already - code = tscSetTableFullName(&pTableMetaInfo->name, pToken, pSql); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - return tscGetTableMeta(pSql, pTableMetaInfo); - } - case TSDB_SQL_SHOW_CREATE_STABLE: - case TSDB_SQL_SHOW_CREATE_TABLE: { - const char* msg1 = "invalid table name"; - - SToken* pToken = taosArrayGet(pInfo->pMiscInfo->a, 0); - if (tscValidateName(pToken) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - code = tscSetTableFullName(&pTableMetaInfo->name, pToken, pSql); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - return tscGetTableMeta(pSql, pTableMetaInfo); - } - case TSDB_SQL_SHOW_CREATE_DATABASE: { - const char* msg1 = "invalid database name"; - - SToken* pToken = taosArrayGet(pInfo->pMiscInfo->a, 0); - if (tscValidateName(pToken) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - if (pToken->n > TSDB_DB_NAME_LEN) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - return tNameSetDbName(&pTableMetaInfo->name, getAccountId(pSql), pToken); - } - case TSDB_SQL_CFG_DNODE: { - const char* msg2 = "invalid configure options or values, such as resetlog / debugFlag 135 / balance 'vnode:2-dnode:2' / monitor 1 "; - const char* msg3 = "invalid dnode ep"; - - /* validate the ip address */ - SMiscInfo* pMiscInfo = pInfo->pMiscInfo; - - /* validate the parameter names and options */ - if (validateDNodeConfig(pMiscInfo) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - char* pMsg = pCmd->payload; - - SMCfgDnodeReq* pCfg = (SMCfgDnodeReq*)pMsg; - - SToken* t0 = taosArrayGet(pMiscInfo->a, 0); - SToken* t1 = taosArrayGet(pMiscInfo->a, 1); - - t0->n = strdequote(t0->z); - strncpy(pCfg->ep, t0->z, t0->n); - - if (validateEp(pCfg->ep) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - strncpy(pCfg->config, t1->z, t1->n); - - if (taosArrayGetSize(pMiscInfo->a) == 3) { - SToken* t2 = taosArrayGet(pMiscInfo->a, 2); - - pCfg->config[t1->n] = ' '; // add sep - strncpy(&pCfg->config[t1->n + 1], t2->z, t2->n); - } - - break; - } - - case TSDB_SQL_CFG_LOCAL: { - SMiscInfo *pMiscInfo = pInfo->pMiscInfo; - const char *msg = "invalid configure options or values"; - - // validate the parameter names and options - if (validateLocalConfig(pMiscInfo) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - int32_t numOfToken = (int32_t) taosArrayGetSize(pMiscInfo->a); - assert(numOfToken >= 1 && numOfToken <= 2); - - SToken* t = taosArrayGet(pMiscInfo->a, 0); - strncpy(pCmd->payload, t->z, t->n); - if (numOfToken == 2) { - SToken* t1 = taosArrayGet(pMiscInfo->a, 1); - pCmd->payload[t->n] = ' '; // add sep - strncpy(&pCmd->payload[t->n + 1], t1->z, t1->n); - } - return TSDB_CODE_SUCCESS; - } - - case TSDB_SQL_SELECT: { - const char * msg1 = "no nested query supported in union clause"; - code = loadAllTableMeta(pSql, pInfo); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - pQueryInfo = tscGetQueryInfo(pCmd); - - size_t size = taosArrayGetSize(pInfo->list); - for (int32_t i = 0; i < size; ++i) { - SSqlNode* pSqlNode = taosArrayGetP(pInfo->list, i); - - tscTrace("0x%"PRIx64" start to parse the %dth subclause, total:%"PRIzu, pSql->self, i, size); - - if (size > 1 && pSqlNode->from && pSqlNode->from->type == SQL_FROM_NODE_SUBQUERY) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - -// normalizeSqlNode(pSqlNode); // normalize the column name in each function - if ((code = validateSqlNode(pSql, pSqlNode, pQueryInfo)) != TSDB_CODE_SUCCESS) { - return code; - } - - tscPrintSelNodeList(pSql, i); - - if ((i + 1) < size && pQueryInfo->sibling == NULL) { - if ((code = tscAddQueryInfo(pCmd)) != TSDB_CODE_SUCCESS) { - return code; - } - - SArray *pUdfInfo = NULL; - if (pQueryInfo->pUdfInfo) { - pUdfInfo = taosArrayDup(pQueryInfo->pUdfInfo); - } - - pQueryInfo = pCmd->active; - pQueryInfo->pUdfInfo = pUdfInfo; - pQueryInfo->udfCopy = true; - } - } - - if ((code = normalizeVarDataTypeLength(pCmd)) != TSDB_CODE_SUCCESS) { - return code; - } - - // set the command/global limit parameters from the first subclause to the sqlcmd object - pCmd->active = pCmd->pQueryInfo; - pCmd->command = pCmd->pQueryInfo->command; - - STableMetaInfo* pTableMetaInfo1 = getMetaInfo(pCmd->active, 0); - if (pTableMetaInfo1->pTableMeta != NULL) { - pSql->res.precision = tscGetTableInfo(pTableMetaInfo1->pTableMeta).precision; - } - - return TSDB_CODE_SUCCESS; // do not build query message here - } - - case TSDB_SQL_ALTER_TABLE: { - if ((code = setAlterTableInfo(pSql, pInfo)) != TSDB_CODE_SUCCESS) { - return code; - } - - break; - } - - case TSDB_SQL_KILL_QUERY: - case TSDB_SQL_KILL_STREAM: - case TSDB_SQL_KILL_CONNECTION: { - if ((code = setKillInfo(pSql, pInfo, pInfo->type)) != TSDB_CODE_SUCCESS) { - return code; - } - break; - } - - case TSDB_SQL_SYNC_DB_REPLICA: { - const char* msg1 = "invalid db name"; - SToken* pzName = taosArrayGet(pInfo->pMiscInfo->a, 0); - - assert(taosArrayGetSize(pInfo->pMiscInfo->a) == 1); - code = tNameSetDbName(&pTableMetaInfo->name, getAccountId(pSql), pzName); - if (code != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - break; - } - case TSDB_SQL_COMPACT_VNODE:{ - const char* msg = "invalid compact"; - if (setCompactVnodeInfo(pSql, pInfo) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg); - } - break; - } - default: - return buildInvalidOperationMsg(pMsgBuf, "not support sql expression"); - } -#endif - } - - SCatalogReq req = {0}; - SMetaData data = {0}; - - // TODO: check if the qnode info has been cached already - req.qNodeRequired = true; - code = qParserExtractRequestedMetaInfo(pInfo, &req, pCtx, msgBuf, msgBufLen); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - // load the meta data from catalog -// code = catalogGetAllMeta(pCtx->pCatalog, pCtx->pTransporter, &pCtx->mgmtEpSet, &req, &data); - STableMeta* pmt = NULL; - - SName* name = taosArrayGet(req.pTableName, 0); - code = catalogGetTableMeta(pCtx->pCatalog, pCtx->pTransporter, &pCtx->mgmtEpSet, name, &pmt); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - data.pTableMeta = taosArrayInit(1, POINTER_BYTES); - taosArrayPush(data.pTableMeta, &pmt); - - pQueryInfo->pTableMetaInfo = calloc(1, POINTER_BYTES); - pQueryInfo->pTableMetaInfo[0] = calloc(1, sizeof(STableMetaInfo)); - pQueryInfo->pTableMetaInfo[0]->pTableMeta = pmt; - pQueryInfo->pTableMetaInfo[0]->name = *name; - pQueryInfo->numOfTables = 1; - pQueryInfo->pTableMetaInfo[0]->tagColList = taosArrayInit(4, POINTER_BYTES); - strcpy(pQueryInfo->pTableMetaInfo[0]->aliasName, name->tname); - - code = setTableVgroupList(pCtx, name, &pQueryInfo->pTableMetaInfo[0]->vgroupList); - if (code != TSDB_CODE_SUCCESS) { - taosArrayDestroy(data.pTableMeta); - return code; - } - - // evaluate the sqlnode - STableMeta* pTableMeta = (STableMeta*) taosArrayGetP(data.pTableMeta, 0); - assert(pTableMeta != NULL); - - SMsgBuf buf = {.buf = msgBuf, .len = msgBufLen}; - - size_t len = taosArrayGetSize(pInfo->sub.node); - for(int32_t i = 0; i < len; ++i) { - SSqlNode* p = taosArrayGetP(pInfo->sub.node, i); - code = evaluateSqlNode(p, pTableMeta->tableInfo.precision, &buf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - } - - for(int32_t i = 0; i < len; ++i) { - SSqlNode* p = taosArrayGetP(pInfo->sub.node, i); - validateSqlNode(p, pQueryInfo, &buf); - } - - taosArrayDestroy(data.pTableMeta); - taosArrayDestroy(req.pUdf); - taosArrayDestroy(req.pTableName); - - return code; -} diff --git a/source/libs/parser/src/dCDAstProcess.c b/source/libs/parser/src/dCDAstProcess.c deleted file mode 100644 index b4125b0a24..0000000000 --- a/source/libs/parser/src/dCDAstProcess.c +++ /dev/null @@ -1,901 +0,0 @@ -#include "tmsg.h" -#include "tglobal.h" -#include "parserInt.h" -#include "ttime.h" -#include "astToMsg.h" -#include "astGenerator.h" -#include "parserUtil.h" -#include "queryInfoUtil.h" - -/* is contained in pFieldList or not */ -static bool has(SArray* pFieldList, int32_t startIndex, const char* name) { - size_t numOfCols = taosArrayGetSize(pFieldList); - for (int32_t j = startIndex; j < numOfCols; ++j) { - TAOS_FIELD* field = taosArrayGet(pFieldList, j); - if (strncasecmp(name, field->name, sizeof(field->name) - 1) == 0) return true; - } - - return false; -} - -static int32_t setShowInfo(SShowInfo* pShowInfo, SParseContext* pCtx, void** output, int32_t* outputLen, - SEpSet* pEpSet, void** pExtension, SMsgBuf* pMsgBuf) { - const char* msg1 = "invalid name"; - const char* msg2 = "wildcard string should be less than %d characters"; - const char* msg3 = "database name too long"; - const char* msg4 = "pattern is invalid"; - const char* msg5 = "database name is empty"; - const char* msg6 = "pattern string is empty"; - const char* msg7 = "database not specified"; - /* - * database prefix in pInfo->pMiscInfo->a[0] - * wildcard in like clause in pInfo->pMiscInfo->a[1] - */ - int16_t showType = pShowInfo->showType; - if (showType == TSDB_MGMT_TABLE_TABLE) { - SArray* array = NULL; - SName name = {0}; - - if (pCtx->db == NULL && pShowInfo->prefix.n == 0) { - return buildInvalidOperationMsg(pMsgBuf, msg7); - } - - SVShowTablesReq* pShowReq = calloc(1, sizeof(SVShowTablesReq)); - if (pShowInfo->prefix.n > 0) { - tNameSetDbName(&name, pCtx->acctId, pShowInfo->prefix.z, pShowInfo->prefix.n); - } else { - tNameSetDbName(&name, pCtx->acctId, pCtx->db, strlen(pCtx->db)); - } - - char dbFname[TSDB_DB_FNAME_LEN] = {0}; - tNameGetFullDbName(&name, dbFname); - - int32_t code = catalogGetDBVgInfo(pCtx->pCatalog, pCtx->pTransporter, &pCtx->mgmtEpSet, dbFname, false, &array); - if (code != TSDB_CODE_SUCCESS) { - terrno = code; - return code; - } - - SVgroupInfo* info = taosArrayGet(array, 0); - pShowReq->head.vgId = htonl(info->vgId); - *pEpSet = info->epset; - - *outputLen = sizeof(SVShowTablesReq); - *output = pShowReq; - *pExtension = array; - } else { - if (showType == TSDB_MGMT_TABLE_STB || showType == TSDB_MGMT_TABLE_VGROUP) { - SToken* pDbPrefixToken = &pShowInfo->prefix; - if (pDbPrefixToken->type != 0) { - if (pDbPrefixToken->n >= TSDB_DB_NAME_LEN) { // db name is too long - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - if (pDbPrefixToken->n <= 0) { - return buildInvalidOperationMsg(pMsgBuf, msg5); - } - - if (parserValidateIdToken(pDbPrefixToken) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - // int32_t ret = tNameSetDbName(&pTableMetaInfo->name, getAccountId(pRequest->pTsc), pDbPrefixToken); - // if (ret != TSDB_CODE_SUCCESS) { - // return buildInvalidOperationMsg(pMsgBuf, msg1); - // } - } - - // show table/stable like 'xxxx', set the like pattern for show tables - SToken* pPattern = &pShowInfo->pattern; - if (pPattern->type != 0) { - if (pPattern->type == TK_ID && pPattern->z[0] == TS_ESCAPE_CHAR) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - pPattern->n = strdequote(pPattern->z); - if (pPattern->n <= 0) { - return buildInvalidOperationMsg(pMsgBuf, msg6); - } - - if (pPattern->n > tsMaxWildCardsLen) { - char tmp[64] = {0}; - sprintf(tmp, msg2, tsMaxWildCardsLen); - return buildInvalidOperationMsg(pMsgBuf, tmp); - } - } - } else if (showType == TSDB_MGMT_TABLE_VNODES) { - if (pShowInfo->prefix.type == 0) { - return buildInvalidOperationMsg(pMsgBuf, "No specified dnode ep"); - } - - if (pShowInfo->prefix.type == TK_STRING) { - pShowInfo->prefix.n = strdequote(pShowInfo->prefix.z); - } - } - - *pEpSet = pCtx->mgmtEpSet; - *output = buildShowMsg(pShowInfo, outputLen, pCtx, pMsgBuf); - if (*output == NULL) { - return terrno; - } - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t validateTableColumns(SArray* pFieldList, int32_t maxRowLength, int32_t maxColumns, SMsgBuf* pMsgBuf) { - const char* msg2 = "row length exceeds max length"; - const char* msg3 = "duplicated column names"; - const char* msg4 = "invalid data type"; - const char* msg5 = "invalid binary/nchar column length"; - const char* msg6 = "invalid column name"; - const char* msg7 = "too many columns"; - const char* msg8 = "illegal number of columns"; - - size_t numOfCols = taosArrayGetSize(pFieldList); - if (numOfCols > maxColumns) { - return buildInvalidOperationMsg(pMsgBuf, msg7); - } - - int32_t rowLen = 0; - for (int32_t i = 0; i < numOfCols; ++i) { - TAOS_FIELD* pField = taosArrayGet(pFieldList, i); - if (!isValidDataType(pField->type)) { - return buildInvalidOperationMsg(pMsgBuf, msg4); - } - - if (pField->bytes == 0) { - return buildInvalidOperationMsg(pMsgBuf, msg5); - } - - if ((pField->type == TSDB_DATA_TYPE_BINARY && (pField->bytes <= 0 || pField->bytes > TSDB_MAX_BINARY_LEN)) || - (pField->type == TSDB_DATA_TYPE_NCHAR && (pField->bytes <= 0 || pField->bytes > TSDB_MAX_NCHAR_LEN))) { - return buildInvalidOperationMsg(pMsgBuf, msg5); - } - - SToken nameToken = {.z = pField->name, .n = strlen(pField->name), .type = TK_ID}; - if (parserValidateNameToken(&nameToken) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg6); - } - - // field name must be unique - if (has(pFieldList, i + 1, pField->name) == true) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - rowLen += pField->bytes; - } - - // max row length must be less than TSDB_MAX_BYTES_PER_ROW - if (rowLen > maxRowLength) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t validateTableColumnInfo(SArray* pFieldList, SMsgBuf* pMsgBuf) { - assert(pFieldList != NULL && pMsgBuf != NULL); - - const char* msg1 = "first column must be timestamp"; - const char* msg2 = "illegal number of columns"; - - // first column must be timestamp - SField* pField = taosArrayGet(pFieldList, 0); - if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - // number of fields no less than 2 - size_t numOfCols = taosArrayGetSize(pFieldList); - if (numOfCols <= 1) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - return validateTableColumns(pFieldList, TSDB_MAX_BYTES_PER_ROW, TSDB_MAX_COLUMNS, pMsgBuf); -} - -static int32_t validateTagParams(SArray* pTagsList, SArray* pFieldList, SMsgBuf* pMsgBuf) { - assert(pTagsList != NULL); - - const char* msg1 = "invalid number of tag columns"; - const char* msg3 = "duplicated column names"; - - // number of fields at least 1 - size_t numOfTags = taosArrayGetSize(pTagsList); - if (numOfTags < 1) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - // field name must be unique - for (int32_t i = 0; i < numOfTags; ++i) { - SField* p = taosArrayGet(pTagsList, i); - if (has(pFieldList, 0, p->name) == true) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - } - - return validateTableColumns(pFieldList, TSDB_MAX_TAGS_LEN, TSDB_MAX_TAGS, pMsgBuf); -} - -int32_t doCheckForCreateTable(SCreateTableSql* pCreateTable, SMsgBuf* pMsgBuf) { - const char* msg1 = "invalid table name"; - - SArray* pFieldList = pCreateTable->colInfo.pColumns; - SArray* pTagList = pCreateTable->colInfo.pTagColumns; - assert(pFieldList != NULL); - - // if sql specifies db, use it, otherwise use default db - SToken* pNameToken = &(pCreateTable->name); - - if (parserValidateIdToken(pNameToken) != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - if (validateTableColumnInfo(pFieldList, pMsgBuf) != TSDB_CODE_SUCCESS || - (pTagList != NULL && validateTagParams(pTagList, pFieldList, pMsgBuf) != TSDB_CODE_SUCCESS)) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - return TSDB_CODE_SUCCESS; -} - -typedef struct SVgroupTablesBatch { - SVCreateTbBatchReq req; - SVgroupInfo info; -} SVgroupTablesBatch; - -static SArray* doSerializeVgroupCreateTableInfo(SHashObj* pVgroupHashmap); - -static int32_t doParseSerializeTagValue(SSchema* pTagSchema, int32_t numOfInputTag, SKVRowBuilder* pKvRowBuilder, - SArray* pTagValList, int32_t tsPrecision, SMsgBuf* pMsgBuf) { - const char* msg1 = "illegal value or data overflow"; - int32_t code = TSDB_CODE_SUCCESS; - - for (int32_t i = 0; i < numOfInputTag; ++i) { - SSchema* pSchema = &pTagSchema[i]; - - char* endPtr = NULL; - char tmpTokenBuf[TSDB_MAX_TAGS_LEN] = {0}; - SKvParam param = {.builder = pKvRowBuilder, .schema = pSchema}; - - SToken* pItem = taosArrayGet(pTagValList, i); - code = parseValueToken(&endPtr, pItem, pSchema, tsPrecision, tmpTokenBuf, KvRowAppend, ¶m, pMsgBuf); - - if (code != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - } - - return code; -} - -static void addCreateTbReqIntoVgroup(SHashObj* pVgroupHashmap, const SName* pTableName, SKVRow row, uint64_t suid, SVgroupInfo* pVgInfo) { - struct SVCreateTbReq req = {0}; - req.type = TD_CHILD_TABLE; - req.name = strdup(tNameGetTableName(pTableName)); - req.ctbCfg.suid = suid; - req.ctbCfg.pTag = row; - - SVgroupTablesBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pVgInfo->vgId, sizeof(pVgInfo->vgId)); - if (pTableBatch == NULL) { - SVgroupTablesBatch tBatch = {0}; - tBatch.info = *pVgInfo; - - tBatch.req.pArray = taosArrayInit(4, sizeof(struct SVCreateTbReq)); - taosArrayPush(tBatch.req.pArray, &req); - - taosHashPut(pVgroupHashmap, &pVgInfo->vgId, sizeof(pVgInfo->vgId), &tBatch, sizeof(tBatch)); - } else { // add to the correct vgroup - assert(pVgInfo->vgId == pTableBatch->info.vgId); - taosArrayPush(pTableBatch->req.pArray, &req); - } -} - -static void destroyCreateTbReqBatch(SVgroupTablesBatch* pTbBatch) { - size_t size = taosArrayGetSize(pTbBatch->req.pArray); - for(int32_t i = 0; i < size; ++i) { - SVCreateTbReq* pTableReq = taosArrayGet(pTbBatch->req.pArray, i); - tfree(pTableReq->name); - - if (pTableReq->type == TSDB_NORMAL_TABLE) { - tfree(pTableReq->ntbCfg.pSchema); - } else if (pTableReq->type == TSDB_CHILD_TABLE) { - tfree(pTableReq->ctbCfg.pTag); - } else { - assert(0); - } - } - - taosArrayDestroy(pTbBatch->req.pArray); -} - -static int32_t doCheckAndBuildCreateCTableReq(SCreateTableSql* pCreateTable, SParseContext* pCtx, SMsgBuf* pMsgBuf, SArray** pBufArray) { - const char* msg1 = "invalid table name"; - const char* msg2 = "tags number not matched"; - const char* msg3 = "tag value too long"; - const char* msg4 = "illegal value or data overflow"; - - int32_t code = 0; - STableMeta* pSuperTableMeta = NULL; - - SHashObj* pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK); - - // super table name, create table by using dst - size_t numOfTables = taosArrayGetSize(pCreateTable->childTableInfo); - for (int32_t j = 0; j < numOfTables; ++j) { - SCreatedTableInfo* pCreateTableInfo = taosArrayGet(pCreateTable->childTableInfo, j); - - SToken* pSTableNameToken = &pCreateTableInfo->stbName; - code = parserValidateNameToken(pSTableNameToken); - if (code != TSDB_CODE_SUCCESS) { - code = buildInvalidOperationMsg(pMsgBuf, msg1); - goto _error; - } - - SName name = {0}; - code = createSName(&name, pSTableNameToken, pCtx, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - goto _error; - } - - SKVRowBuilder kvRowBuilder = {0}; - if (tdInitKVRowBuilder(&kvRowBuilder) < 0) { - code = TSDB_CODE_TSC_OUT_OF_MEMORY; - goto _error; - } - - SArray* pValList = pCreateTableInfo->pTagVals; - size_t numOfInputTag = taosArrayGetSize(pValList); - - code = catalogGetTableMeta(pCtx->pCatalog, pCtx->pTransporter, &pCtx->mgmtEpSet, &name, &pSuperTableMeta); - if (code != TSDB_CODE_SUCCESS) { - goto _error; - } - - assert(pSuperTableMeta != NULL); - - // too long tag values will return invalid sql, not be truncated automatically - SSchema* pTagSchema = getTableTagSchema(pSuperTableMeta); - STableComInfo tinfo = getTableInfo(pSuperTableMeta); - - SArray* pNameList = NULL; - size_t numOfBoundTags = 0; - int32_t schemaSize = getNumOfTags(pSuperTableMeta); - - if (pCreateTableInfo->pTagNames) { - pNameList = pCreateTableInfo->pTagNames; - numOfBoundTags = taosArrayGetSize(pNameList); - - if (numOfInputTag != numOfBoundTags || schemaSize < numOfInputTag) { - tdDestroyKVRowBuilder(&kvRowBuilder); - code = buildInvalidOperationMsg(pMsgBuf, msg2); - goto _error; - } - - bool findColumnIndex = false; - for (int32_t i = 0; i < numOfBoundTags; ++i) { - SToken* sToken = taosArrayGet(pNameList, i); - - char tmpTokenBuf[TSDB_MAX_BYTES_PER_ROW] = {0}; // create tmp buf to avoid alter orginal sqlstr - strncpy(tmpTokenBuf, sToken->z, sToken->n); - sToken->z = tmpTokenBuf; - - // if (TK_STRING == sToken->type) { - // tscDequoteAndTrimToken(sToken); - // } - - // if (TK_ID == sToken->type) { - // tscRmEscapeAndTrimToken(sToken); - // } - - SListItem* pItem = taosArrayGet(pValList, i); - - findColumnIndex = false; - - // todo speedup by using hash list - for (int32_t t = 0; t < schemaSize; ++t) { - if (strncmp(sToken->z, pTagSchema[t].name, sToken->n) == 0 && strlen(pTagSchema[t].name) == sToken->n) { - SSchema* pSchema = &pTagSchema[t]; - - char tagVal[TSDB_MAX_TAGS_LEN] = {0}; - if (pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) { - if (pItem->pVar.nLen > pSchema->bytes) { - tdDestroyKVRowBuilder(&kvRowBuilder); - code = buildInvalidOperationMsg(pMsgBuf, msg3); - goto _error; - } - } else if (pSchema->type == TSDB_DATA_TYPE_TIMESTAMP) { - if (pItem->pVar.nType == TSDB_DATA_TYPE_BINARY) { - // code = convertTimestampStrToInt64(&(pItem->pVar), tinfo.precision); - // if (code != TSDB_CODE_SUCCESS) { - // return buildInvalidOperationMsg(pMsgBuf, msg4); - // } - } else if (pItem->pVar.nType == TSDB_DATA_TYPE_TIMESTAMP) { - pItem->pVar.i = convertTimePrecision(pItem->pVar.i, TSDB_TIME_PRECISION_NANO, tinfo.precision); - } - } - - code = taosVariantDump(&(pItem->pVar), tagVal, pSchema->type, true); - - // check again after the convert since it may be converted from binary to nchar. - if (IS_VAR_DATA_TYPE(pSchema->type)) { - int16_t len = varDataTLen(tagVal); - if (len > pSchema->bytes) { - tdDestroyKVRowBuilder(&kvRowBuilder); - code = buildInvalidOperationMsg(pMsgBuf, msg3); - goto _error; - } - } - - if (code != TSDB_CODE_SUCCESS) { - tdDestroyKVRowBuilder(&kvRowBuilder); - code = buildInvalidOperationMsg(pMsgBuf, msg4); - goto _error; - } - - tdAddColToKVRow(&kvRowBuilder, pSchema->colId, pSchema->type, tagVal); - - findColumnIndex = true; - break; - } - } - - if (!findColumnIndex) { - tdDestroyKVRowBuilder(&kvRowBuilder); - // return buildInvalidOperationMsg(pMsgBuf, "invalid tag name", sToken->z); - } - } - } else { - if (schemaSize != numOfInputTag) { - tdDestroyKVRowBuilder(&kvRowBuilder); - code = buildInvalidOperationMsg(pMsgBuf, msg2); - goto _error; - } - - code = doParseSerializeTagValue(pTagSchema, numOfInputTag, &kvRowBuilder, pValList, tinfo.precision, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - tdDestroyKVRowBuilder(&kvRowBuilder); - goto _error; - } - } - - SKVRow row = tdGetKVRowFromBuilder(&kvRowBuilder); - tdDestroyKVRowBuilder(&kvRowBuilder); - if (row == NULL) { - code = TSDB_CODE_QRY_OUT_OF_MEMORY; - goto _error; - } - - tdSortKVRowByColIdx(row); - - SName tableName = {0}; - code = createSName(&tableName, &pCreateTableInfo->name, pCtx, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - goto _error; - } - - // Find a appropriate vgroup to accommodate this table , according to the table name - SVgroupInfo info = {0}; - code = catalogGetTableHashVgroup(pCtx->pCatalog, pCtx->pTransporter, &pCtx->mgmtEpSet, &tableName, &info); - if (code != TSDB_CODE_SUCCESS) { - goto _error; - } - - addCreateTbReqIntoVgroup(pVgroupHashmap, &tableName, row, pSuperTableMeta->uid, &info); - tfree(pSuperTableMeta); - } - - *pBufArray = doSerializeVgroupCreateTableInfo(pVgroupHashmap); - if (*pBufArray == NULL) { - code = terrno; - goto _error; - } - - taosHashCleanup(pVgroupHashmap); - return TSDB_CODE_SUCCESS; - - _error: - taosHashCleanup(pVgroupHashmap); - tfree(pSuperTableMeta); - terrno = code; - return code; -} - -static int32_t serializeVgroupTablesBatchImpl(SVgroupTablesBatch* pTbBatch, SArray* pBufArray) { - int tlen = sizeof(SMsgHead) + tSerializeSVCreateTbBatchReq(NULL, &(pTbBatch->req)); - void* buf = malloc(tlen); - if (buf == NULL) { - // TODO: handle error - } - - ((SMsgHead*)buf)->vgId = htonl(pTbBatch->info.vgId); - ((SMsgHead*)buf)->contLen = htonl(tlen); - - void* pBuf = POINTER_SHIFT(buf, sizeof(SMsgHead)); - tSerializeSVCreateTbBatchReq(&pBuf, &(pTbBatch->req)); - - SVgDataBlocks* pVgData = calloc(1, sizeof(SVgDataBlocks)); - pVgData->vg = pTbBatch->info; - pVgData->pData = buf; - pVgData->size = tlen; - pVgData->numOfTables = (int32_t) taosArrayGetSize(pTbBatch->req.pArray); - - taosArrayPush(pBufArray, &pVgData); -} - -static int32_t doBuildSingleTableBatchReq(SName* pTableName, SArray* pColumns, SVgroupInfo* pVgroupInfo, SVgroupTablesBatch* pBatch) { - struct SVCreateTbReq req = {0}; - req.type = TD_NORMAL_TABLE; - req.name = strdup(tNameGetTableName(pTableName)); - - req.ntbCfg.nCols = taosArrayGetSize(pColumns); - int32_t num = req.ntbCfg.nCols; - - req.ntbCfg.pSchema = calloc(num, sizeof(SSchema)); - for(int32_t i = 0; i < num; ++i) { - SSchema* pSchema = taosArrayGet(pColumns, i); - memcpy(&req.ntbCfg.pSchema[i], pSchema, sizeof(SSchema)); - } - - pBatch->info = *pVgroupInfo; - pBatch->req.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq)); - if (pBatch->req.pArray == NULL) { - return TSDB_CODE_QRY_OUT_OF_MEMORY; - } - - taosArrayPush(pBatch->req.pArray, &req); - return TSDB_CODE_SUCCESS; -} - -int32_t doCheckAndBuildCreateTableReq(SCreateTableSql* pCreateTable, SParseContext* pCtx, SMsgBuf* pMsgBuf, char** pOutput, int32_t* len) { - SArray* pBufArray = NULL; - int32_t code = 0; - - // it is a sql statement to create a normal table - if (pCreateTable->childTableInfo == NULL) { - assert(taosArrayGetSize(pCreateTable->colInfo.pColumns) > 0 && pCreateTable->colInfo.pTagColumns == NULL); - code = doCheckForCreateTable(pCreateTable, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - SName tableName = {0}; - code = createSName(&tableName, &pCreateTable->name, pCtx, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - SVgroupInfo info = {0}; - catalogGetTableHashVgroup(pCtx->pCatalog, pCtx->pTransporter, &pCtx->mgmtEpSet, &tableName, &info); - - SVgroupTablesBatch tbatch = {0}; - code = doBuildSingleTableBatchReq(&tableName, pCreateTable->colInfo.pColumns, &info, &tbatch); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - pBufArray = taosArrayInit(1, POINTER_BYTES); - if (pBufArray == NULL) { - return TSDB_CODE_QRY_OUT_OF_MEMORY; - } - - serializeVgroupTablesBatchImpl(&tbatch, pBufArray); - destroyCreateTbReqBatch(&tbatch); - } else { // it is a child table, created according to a super table - code = doCheckAndBuildCreateCTableReq(pCreateTable, pCtx, pMsgBuf, &pBufArray); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - } - - SVnodeModifOpStmtInfo* pStmtInfo = calloc(1, sizeof(SVnodeModifOpStmtInfo)); - pStmtInfo->nodeType = TSDB_SQL_CREATE_TABLE; - pStmtInfo->pDataBlocks = pBufArray; - - *pOutput = (char*) pStmtInfo; - *len = sizeof(SVnodeModifOpStmtInfo); - - return TSDB_CODE_SUCCESS; -} - -SArray* doSerializeVgroupCreateTableInfo(SHashObj* pVgroupHashmap) { - SArray* pBufArray = taosArrayInit(taosHashGetSize(pVgroupHashmap), sizeof(void*)); - - SVgroupTablesBatch* pTbBatch = NULL; - do { - pTbBatch = taosHashIterate(pVgroupHashmap, pTbBatch); - if (pTbBatch == NULL) { - break; - } - - /*int32_t code = */serializeVgroupTablesBatchImpl(pTbBatch, pBufArray); - destroyCreateTbReqBatch(pTbBatch); - } while (true); - - return pBufArray; -} - -SDclStmtInfo* qParserValidateDclSqlNode(SSqlInfo* pInfo, SParseContext* pCtx, char* msgBuf, int32_t msgBufLen) { - int32_t code = 0; - - SDclStmtInfo* pDcl = calloc(1, sizeof(SDclStmtInfo)); - - SMsgBuf m = {.buf = msgBuf, .len = msgBufLen}; - SMsgBuf* pMsgBuf = &m; - - pDcl->epSet = pCtx->mgmtEpSet; - - switch (pInfo->type) { - case TSDB_SQL_CREATE_USER: - case TSDB_SQL_ALTER_USER: { - const char* msg1 = "not support options"; - const char* msg2 = "invalid user/account name"; - const char* msg3 = "name too long"; - const char* msg4 = "invalid user rights"; - - SUserInfo* pUser = &pInfo->pMiscInfo->user; - SToken* pName = &pUser->user; - SToken* pPwd = &pUser->passwd; - - if (pName->n >= TSDB_USER_LEN) { - code = buildInvalidOperationMsg(pMsgBuf, msg3); - goto _error; - } - - if (parserValidateIdToken(pName) != TSDB_CODE_SUCCESS) { - code = buildInvalidOperationMsg(pMsgBuf, msg2); - goto _error; - } - - if (pInfo->type == TSDB_SQL_CREATE_USER) { - if (parserValidatePassword(pPwd, pMsgBuf) != TSDB_CODE_SUCCESS) { - code = TSDB_CODE_TSC_INVALID_OPERATION; - goto _error; - } - } else { - if (pUser->type == TSDB_ALTER_USER_PASSWD) { - if (parserValidatePassword(pPwd, pMsgBuf) != TSDB_CODE_SUCCESS) { - code = TSDB_CODE_TSC_INVALID_OPERATION; - goto _error; - } - } else if (pUser->type == TSDB_ALTER_USER_PRIVILEGES) { - assert(pPwd->type == TSDB_DATA_TYPE_NULL); - - SToken* pPrivilege = &pUser->privilege; - if (strncasecmp(pPrivilege->z, "super", 5) == 0 && pPrivilege->n == 5) { - // pCmd->count = 1; - } else if (strncasecmp(pPrivilege->z, "normal", 4) == 0 && pPrivilege->n == 4) { - // pCmd->count = 2; - } else { - code = buildInvalidOperationMsg(pMsgBuf, msg4); - goto _error; - } - } else { - code = buildInvalidOperationMsg(pMsgBuf, msg1); - goto _error; - } - } - - pDcl->pMsg = (char*)buildUserManipulationMsg(pInfo, &pDcl->msgLen, pCtx->requestId, msgBuf, msgBufLen); - pDcl->msgType = (pInfo->type == TSDB_SQL_CREATE_USER) ? TDMT_MND_CREATE_USER : TDMT_MND_ALTER_USER; - break; - } - - case TSDB_SQL_CREATE_ACCT: - case TSDB_SQL_ALTER_ACCT: { - const char* msg1 = "invalid state option, available options[no, r, w, all]"; - const char* msg2 = "invalid user/account name"; - const char* msg3 = "name too long"; - - SToken* pName = &pInfo->pMiscInfo->user.user; - SToken* pPwd = &pInfo->pMiscInfo->user.passwd; - - if (parserValidatePassword(pPwd, pMsgBuf) != TSDB_CODE_SUCCESS) { - code = TSDB_CODE_TSC_INVALID_OPERATION; - goto _error; - } - - if (pName->n >= TSDB_USER_LEN) { - code = buildInvalidOperationMsg(pMsgBuf, msg3); - goto _error; - } - - if (parserValidateNameToken(pName) != TSDB_CODE_SUCCESS) { - code = buildInvalidOperationMsg(pMsgBuf, msg2); - goto _error; - } - - SCreateAcctInfo* pAcctOpt = &pInfo->pMiscInfo->acctOpt; - if (pAcctOpt->stat.n > 0) { - if (pAcctOpt->stat.z[0] == 'r' && pAcctOpt->stat.n == 1) { - } else if (pAcctOpt->stat.z[0] == 'w' && pAcctOpt->stat.n == 1) { - } else if (strncmp(pAcctOpt->stat.z, "all", 3) == 0 && pAcctOpt->stat.n == 3) { - } else if (strncmp(pAcctOpt->stat.z, "no", 2) == 0 && pAcctOpt->stat.n == 2) { - } else { - code = buildInvalidOperationMsg(pMsgBuf, msg1); - goto _error; - } - } - - pDcl->pMsg = (char*)buildAcctManipulationMsg(pInfo, &pDcl->msgLen, pCtx->requestId, msgBuf, msgBufLen); - pDcl->msgType = (pInfo->type == TSDB_SQL_CREATE_ACCT) ? TDMT_MND_CREATE_ACCT : TDMT_MND_ALTER_ACCT; - break; - } - - case TSDB_SQL_DROP_ACCT: - case TSDB_SQL_DROP_USER: { - pDcl->pMsg = (char*)buildDropUserMsg(pInfo, &pDcl->msgLen, pCtx->requestId, msgBuf, msgBufLen); - pDcl->msgType = (pInfo->type == TSDB_SQL_DROP_ACCT) ? TDMT_MND_DROP_ACCT : TDMT_MND_DROP_USER; - break; - } - - case TSDB_SQL_SHOW: { - SShowInfo* pShowInfo = &pInfo->pMiscInfo->showOpt; - code = setShowInfo(pShowInfo, pCtx, (void**)&pDcl->pMsg, &pDcl->msgLen, &pDcl->epSet, &pDcl->pExtension, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - goto _error; - } - - pDcl->msgType = (pShowInfo->showType == TSDB_MGMT_TABLE_TABLE) ? TDMT_VND_SHOW_TABLES : TDMT_MND_SHOW; - break; - } - - case TSDB_SQL_USE_DB: { - const char* msg = "invalid db name"; - - SToken* pToken = taosArrayGet(pInfo->pMiscInfo->a, 0); - if (parserValidateNameToken(pToken) != TSDB_CODE_SUCCESS) { - code = buildInvalidOperationMsg(pMsgBuf, msg); - goto _error; - } - - SName n = {0}; - int32_t ret = tNameSetDbName(&n, pCtx->acctId, pToken->z, pToken->n); - if (ret != TSDB_CODE_SUCCESS) { - code = buildInvalidOperationMsg(pMsgBuf, msg); - goto _error; - } - - SUseDbReq usedbReq = {0}; - tNameExtractFullName(&n, usedbReq.db); - - int32_t bufLen = tSerializeSUseDbReq(NULL, 0, &usedbReq); - void* pBuf = malloc(bufLen); - tSerializeSUseDbReq(pBuf, bufLen, &usedbReq); - - pDcl->pMsg = pBuf; - pDcl->msgLen = bufLen; - pDcl->msgType = TDMT_MND_USE_DB; - break; - } - - case TSDB_SQL_ALTER_DB: - case TSDB_SQL_CREATE_DB: { - const char* msg1 = "invalid db name"; - const char* msg2 = "name too long"; - - SCreateDbInfo* pCreateDB = &(pInfo->pMiscInfo->dbOpt); - if (pCreateDB->dbname.n >= TSDB_DB_NAME_LEN) { - code = buildInvalidOperationMsg(pMsgBuf, msg2); - goto _error; - } - - char buf[TSDB_DB_NAME_LEN] = {0}; - SToken token = taosTokenDup(&pCreateDB->dbname, buf, tListLen(buf)); - - if (parserValidateNameToken(&token) != TSDB_CODE_SUCCESS) { - code = buildInvalidOperationMsg(pMsgBuf, msg1); - goto _error; - } - - int32_t bufLen = 0; - char* pBuf = buildCreateDbMsg(pCreateDB, &bufLen, pCtx, pMsgBuf); - - pDcl->pMsg = pBuf; - pDcl->msgLen = bufLen; - pDcl->msgType = (pInfo->type == TSDB_SQL_CREATE_DB) ? TDMT_MND_CREATE_DB : TDMT_MND_ALTER_DB; - break; - } - - case TSDB_SQL_DROP_DB: { - const char* msg1 = "invalid database name"; - - assert(taosArrayGetSize(pInfo->pMiscInfo->a) == 1); - SToken* dbName = taosArrayGet(pInfo->pMiscInfo->a, 0); - - SName name = {0}; - code = tNameSetDbName(&name, pCtx->acctId, dbName->z, dbName->n); - if (code != TSDB_CODE_SUCCESS) { - code = buildInvalidOperationMsg(pMsgBuf, msg1); - goto _error; - } - - SDropDbReq dropdbReq = {0}; - code = tNameExtractFullName(&name, dropdbReq.db); - dropdbReq.ignoreNotExists = pInfo->pMiscInfo->existsCheck ? 1 : 0; - assert(code == TSDB_CODE_SUCCESS && name.type == TSDB_DB_NAME_T); - - int32_t bufLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq); - void* pBuf = malloc(bufLen); - tSerializeSDropDbReq(pBuf, bufLen, &dropdbReq); - - pDcl->msgType = TDMT_MND_DROP_DB; - pDcl->msgLen = bufLen; - pDcl->pMsg = pBuf; - break; - } - - case TSDB_SQL_CREATE_STABLE: { - SCreateTableSql* pCreateTable = pInfo->pCreateTableInfo; - if ((code = doCheckForCreateTable(pCreateTable, pMsgBuf)) != TSDB_CODE_SUCCESS) { - terrno = code; - goto _error; - } - - pDcl->pMsg = buildCreateStbReq(pCreateTable, &pDcl->msgLen, pCtx, pMsgBuf); - pDcl->msgType = TDMT_MND_CREATE_STB; - break; - } - - case TSDB_SQL_DROP_TABLE: { - pDcl->pMsg = buildDropStableReq(pInfo, &pDcl->msgLen, pCtx, pMsgBuf); - if (pDcl->pMsg == NULL) { - goto _error; - } - - pDcl->msgType = TDMT_MND_DROP_STB; - break; - } - - case TSDB_SQL_CREATE_DNODE: { - pDcl->pMsg = (char*)buildCreateDnodeMsg(pInfo, &pDcl->msgLen, pMsgBuf); - if (pDcl->pMsg == NULL) { - goto _error; - } - - pDcl->msgType = TDMT_MND_CREATE_DNODE; - break; - } - - case TSDB_SQL_DROP_DNODE: { - pDcl->pMsg = (char*)buildDropDnodeMsg(pInfo, &pDcl->msgLen, pMsgBuf); - if (pDcl->pMsg == NULL) { - goto _error; - } - - pDcl->msgType = TDMT_MND_DROP_DNODE; - break; - } - - default: - break; - } - - return pDcl; - - _error: - terrno = code; - tfree(pDcl); - return NULL; -} - -SVnodeModifOpStmtInfo* qParserValidateCreateTbSqlNode(SSqlInfo* pInfo, SParseContext* pCtx, char* msgBuf, int32_t msgBufLen) { - SCreateTableSql* pCreateTable = pInfo->pCreateTableInfo; - assert(pCreateTable->type == TSDB_SQL_CREATE_TABLE); - - SMsgBuf m = {.buf = msgBuf, .len = msgBufLen}; - SMsgBuf* pMsgBuf = &m; - - SVnodeModifOpStmtInfo* pModifSqlStmt = NULL; - - int32_t msgLen = 0; - int32_t code = doCheckAndBuildCreateTableReq(pCreateTable, pCtx, pMsgBuf, (char**) &pModifSqlStmt, &msgLen); - if (code != TSDB_CODE_SUCCESS) { - terrno = code; - tfree(pModifSqlStmt); - return NULL; - } - - return pModifSqlStmt; -} \ No newline at end of file diff --git a/source/libs/parser/src/dataBlockMgt.c b/source/libs/parser/src/dataBlockMgt.c index 381dec4f15..34e440dd19 100644 --- a/source/libs/parser/src/dataBlockMgt.c +++ b/source/libs/parser/src/dataBlockMgt.c @@ -17,8 +17,7 @@ #include "catalog.h" #include "parserUtil.h" -#include "queryInfoUtil.h" -#include "tmsg.h" +#include "querynodes.h" #define IS_RAW_PAYLOAD(t) \ (((int)(t)) == PAYLOAD_TYPE_RAW) // 0: K-V payload for non-prepare insert, 1: rawPayload for prepare insert diff --git a/source/libs/parser/src/insertParser.c b/source/libs/parser/src/insertParser.c index 745982e869..0045953557 100644 --- a/source/libs/parser/src/insertParser.c +++ b/source/libs/parser/src/insertParser.c @@ -16,9 +16,7 @@ #include "insertParser.h" #include "dataBlockMgt.h" -#include "parserInt.h" #include "parserUtil.h" -#include "queryInfoUtil.h" #include "tglobal.h" #include "ttime.h" #include "ttoken.h" @@ -61,9 +59,14 @@ typedef struct SInsertParseContext { SArray* pTableDataBlocks; // global SArray* pVgDataBlocks; // global int32_t totalNum; - SVnodeModifOpStmtInfo* pOutput; + SVnodeModifOpStmt* pOutput; } SInsertParseContext; +typedef int32_t (*_row_append_fn_t)(const void *value, int32_t len, void *param); + +static uint8_t TRUE_VALUE = (uint8_t)TSDB_TRUE; +static uint8_t FALSE_VALUE = (uint8_t)TSDB_FALSE; + static int32_t skipInsertInto(SInsertParseContext* pCxt) { SToken sToken; NEXT_TOKEN(pCxt->pSql, sToken); @@ -95,6 +98,66 @@ static int32_t buildName(SInsertParseContext* pCxt, SToken* pStname, char* fullD return TSDB_CODE_SUCCESS; } +static int32_t createSName(SName* pName, SToken* pTableName, SParseContext* pParseCtx, SMsgBuf* pMsgBuf) { + const char* msg1 = "name too long"; + const char* msg2 = "invalid database name"; + const char* msg3 = "db is not specified"; + + int32_t code = TSDB_CODE_SUCCESS; + char* p = strnchr(pTableName->z, TS_PATH_DELIMITER[0], pTableName->n, true); + + if (p != NULL) { // db has been specified in sql string so we ignore current db path + assert(*p == TS_PATH_DELIMITER[0]); + + int32_t dbLen = p - pTableName->z; + char name[TSDB_DB_FNAME_LEN] = {0}; + strncpy(name, pTableName->z, dbLen); + dbLen = strdequote(name); + + code = tNameSetDbName(pName, pParseCtx->acctId, name, dbLen); + if (code != TSDB_CODE_SUCCESS) { + return buildInvalidOperationMsg(pMsgBuf, msg1); + } + + int32_t tbLen = pTableName->n - dbLen - 1; + char tbname[TSDB_TABLE_FNAME_LEN] = {0}; + strncpy(tbname, p + 1, tbLen); + /*tbLen = */strdequote(tbname); + + code = tNameFromString(pName, tbname, T_NAME_TABLE); + if (code != 0) { + return buildInvalidOperationMsg(pMsgBuf, msg1); + } + } else { // get current DB name first, and then set it into path + if (pTableName->n >= TSDB_TABLE_NAME_LEN) { + return buildInvalidOperationMsg(pMsgBuf, msg1); + } + + assert(pTableName->n < TSDB_TABLE_FNAME_LEN); + + char name[TSDB_TABLE_FNAME_LEN] = {0}; + strncpy(name, pTableName->z, pTableName->n); + strdequote(name); + + if (pParseCtx->db == NULL) { + return buildInvalidOperationMsg(pMsgBuf, msg3); + } + + code = tNameSetDbName(pName, pParseCtx->acctId, pParseCtx->db, strlen(pParseCtx->db)); + if (code != TSDB_CODE_SUCCESS) { + code = buildInvalidOperationMsg(pMsgBuf, msg2); + return code; + } + + code = tNameFromString(pName, name, T_NAME_TABLE); + if (code != 0) { + code = buildInvalidOperationMsg(pMsgBuf, msg1); + } + } + + return code; +} + static int32_t getTableMeta(SInsertParseContext* pCxt, SToken* pTname) { SName name = {0}; createSName(&name, pTname, pCxt->pComCxt, &pCxt->msg); @@ -258,6 +321,231 @@ static int parseTime(char **end, SToken *pToken, int16_t timePrec, int64_t *time return TSDB_CODE_SUCCESS; } +static FORCE_INLINE int32_t checkAndTrimValue(SToken* pToken, uint32_t type, char* tmpTokenBuf, SMsgBuf* pMsgBuf) { + if ((pToken->type != TK_NOW && pToken->type != TK_INTEGER && pToken->type != TK_STRING && pToken->type != TK_FLOAT && pToken->type != TK_BOOL && + pToken->type != TK_NULL && pToken->type != TK_HEX && pToken->type != TK_OCT && pToken->type != TK_BIN) || + (pToken->n == 0) || (pToken->type == TK_RP)) { + return buildSyntaxErrMsg(pMsgBuf, "invalid data or symbol", pToken->z); + } + + if (IS_NUMERIC_TYPE(type) && pToken->n == 0) { + return buildSyntaxErrMsg(pMsgBuf, "invalid numeric data", pToken->z); + } + + // Remove quotation marks + if (TSDB_DATA_TYPE_BINARY == type) { + if (pToken->n >= TSDB_MAX_BYTES_PER_ROW) { + return buildSyntaxErrMsg(pMsgBuf, "too long string", pToken->z); + } + + // delete escape character: \\, \', \" + char delim = pToken->z[0]; + int32_t cnt = 0; + int32_t j = 0; + for (uint32_t k = 1; k < pToken->n - 1; ++k) { + if (pToken->z[k] == '\\' || (pToken->z[k] == delim && pToken->z[k + 1] == delim)) { + tmpTokenBuf[j] = pToken->z[k + 1]; + cnt++; + j++; + k++; + continue; + } + tmpTokenBuf[j] = pToken->z[k]; + j++; + } + + tmpTokenBuf[j] = 0; + pToken->z = tmpTokenBuf; + pToken->n -= 2 + cnt; + } + + return TSDB_CODE_SUCCESS; +} + +static bool isNullStr(SToken *pToken) { + return (pToken->type == TK_NULL) || ((pToken->type == TK_STRING) && (pToken->n != 0) && + (strncasecmp(TSDB_DATA_NULL_STR_L, pToken->z, pToken->n) == 0)); +} + +static FORCE_INLINE int32_t toDouble(SToken *pToken, double *value, char **endPtr) { + errno = 0; + *value = strtold(pToken->z, endPtr); + + // not a valid integer number, return error + if ((*endPtr - pToken->z) != pToken->n) { + return TK_ILLEGAL; + } + + return pToken->type; +} + +static int32_t parseValueToken(char** end, SToken* pToken, SSchema* pSchema, int16_t timePrec, char* tmpTokenBuf, _row_append_fn_t func, void* param, SMsgBuf* pMsgBuf) { + int64_t iv; + char *endptr = NULL; + bool isSigned = false; + + int32_t code = checkAndTrimValue(pToken, pSchema->type, tmpTokenBuf, pMsgBuf); + if (code != TSDB_CODE_SUCCESS) { + return code; + } + + if (isNullStr(pToken)) { + if (TSDB_DATA_TYPE_TIMESTAMP == pSchema->type && PRIMARYKEY_TIMESTAMP_COL_ID == pSchema->colId) { + int64_t tmpVal = 0; + return func(&tmpVal, pSchema->bytes, param); + } + + return func(getNullValue(pSchema->type), 0, param); + } + + switch (pSchema->type) { + case TSDB_DATA_TYPE_BOOL: { + if ((pToken->type == TK_BOOL || pToken->type == TK_STRING) && (pToken->n != 0)) { + if (strncmp(pToken->z, "true", pToken->n) == 0) { + return func(&TRUE_VALUE, pSchema->bytes, param); + } else if (strncmp(pToken->z, "false", pToken->n) == 0) { + return func(&FALSE_VALUE, pSchema->bytes, param); + } else { + return buildSyntaxErrMsg(pMsgBuf, "invalid bool data", pToken->z); + } + } else if (pToken->type == TK_INTEGER) { + return func(((strtoll(pToken->z, NULL, 10) == 0) ? &FALSE_VALUE : &TRUE_VALUE), pSchema->bytes, param); + } else if (pToken->type == TK_FLOAT) { + return func(((strtod(pToken->z, NULL) == 0) ? &FALSE_VALUE : &TRUE_VALUE), pSchema->bytes, param); + } else { + return buildSyntaxErrMsg(pMsgBuf, "invalid bool data", pToken->z); + } + } + + case TSDB_DATA_TYPE_TINYINT: { + if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { + return buildSyntaxErrMsg(pMsgBuf, "invalid tinyint data", pToken->z); + } else if (!IS_VALID_TINYINT(iv)) { + return buildSyntaxErrMsg(pMsgBuf, "tinyint data overflow", pToken->z); + } + + uint8_t tmpVal = (uint8_t)iv; + return func(&tmpVal, pSchema->bytes, param); + } + + case TSDB_DATA_TYPE_UTINYINT:{ + if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { + return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned tinyint data", pToken->z); + } else if (!IS_VALID_UTINYINT(iv)) { + return buildSyntaxErrMsg(pMsgBuf, "unsigned tinyint data overflow", pToken->z); + } + uint8_t tmpVal = (uint8_t)iv; + return func(&tmpVal, pSchema->bytes, param); + } + + case TSDB_DATA_TYPE_SMALLINT: { + if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { + return buildSyntaxErrMsg(pMsgBuf, "invalid smallint data", pToken->z); + } else if (!IS_VALID_SMALLINT(iv)) { + return buildSyntaxErrMsg(pMsgBuf, "smallint data overflow", pToken->z); + } + int16_t tmpVal = (int16_t)iv; + return func(&tmpVal, pSchema->bytes, param); + } + + case TSDB_DATA_TYPE_USMALLINT: { + if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { + return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned smallint data", pToken->z); + } else if (!IS_VALID_USMALLINT(iv)) { + return buildSyntaxErrMsg(pMsgBuf, "unsigned smallint data overflow", pToken->z); + } + uint16_t tmpVal = (uint16_t)iv; + return func(&tmpVal, pSchema->bytes, param); + } + + case TSDB_DATA_TYPE_INT: { + if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { + return buildSyntaxErrMsg(pMsgBuf, "invalid int data", pToken->z); + } else if (!IS_VALID_INT(iv)) { + return buildSyntaxErrMsg(pMsgBuf, "int data overflow", pToken->z); + } + int32_t tmpVal = (int32_t)iv; + return func(&tmpVal, pSchema->bytes, param); + } + + case TSDB_DATA_TYPE_UINT: { + if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { + return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned int data", pToken->z); + } else if (!IS_VALID_UINT(iv)) { + return buildSyntaxErrMsg(pMsgBuf, "unsigned int data overflow", pToken->z); + } + uint32_t tmpVal = (uint32_t)iv; + return func(&tmpVal, pSchema->bytes, param); + } + + case TSDB_DATA_TYPE_BIGINT: { + if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { + return buildSyntaxErrMsg(pMsgBuf, "invalid bigint data", pToken->z); + } else if (!IS_VALID_BIGINT(iv)) { + return buildSyntaxErrMsg(pMsgBuf, "bigint data overflow", pToken->z); + } + return func(&iv, pSchema->bytes, param); + } + + case TSDB_DATA_TYPE_UBIGINT: { + if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { + return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned bigint data", pToken->z); + } else if (!IS_VALID_UBIGINT((uint64_t)iv)) { + return buildSyntaxErrMsg(pMsgBuf, "unsigned bigint data overflow", pToken->z); + } + uint64_t tmpVal = (uint64_t)iv; + return func(&tmpVal, pSchema->bytes, param); + } + + case TSDB_DATA_TYPE_FLOAT: { + double dv; + if (TK_ILLEGAL == toDouble(pToken, &dv, &endptr)) { + return buildSyntaxErrMsg(pMsgBuf, "illegal float data", pToken->z); + } + if (((dv == HUGE_VAL || dv == -HUGE_VAL) && errno == ERANGE) || dv > FLT_MAX || dv < -FLT_MAX || isinf(dv) || isnan(dv)) { + return buildSyntaxErrMsg(pMsgBuf, "illegal float data", pToken->z); + } + float tmpVal = (float)dv; + return func(&tmpVal, pSchema->bytes, param); + } + + case TSDB_DATA_TYPE_DOUBLE: { + double dv; + if (TK_ILLEGAL == toDouble(pToken, &dv, &endptr)) { + return buildSyntaxErrMsg(pMsgBuf, "illegal double data", pToken->z); + } + if (((dv == HUGE_VAL || dv == -HUGE_VAL) && errno == ERANGE) || isinf(dv) || isnan(dv)) { + return buildSyntaxErrMsg(pMsgBuf, "illegal double data", pToken->z); + } + return func(&dv, pSchema->bytes, param); + } + + case TSDB_DATA_TYPE_BINARY: { + // Too long values will raise the invalid sql error message + if (pToken->n + VARSTR_HEADER_SIZE > pSchema->bytes) { + return buildSyntaxErrMsg(pMsgBuf, "string data overflow", pToken->z); + } + + return func(pToken->z, pToken->n, param); + } + + case TSDB_DATA_TYPE_NCHAR: { + return func(pToken->z, pToken->n, param); + } + + case TSDB_DATA_TYPE_TIMESTAMP: { + int64_t tmpVal; + if (parseTime(end, pToken, timePrec, &tmpVal, pMsgBuf) != TSDB_CODE_SUCCESS) { + return buildSyntaxErrMsg(pMsgBuf, "invalid timestamp", pToken->z); + } + + return func(&tmpVal, pSchema->bytes, param); + } + } + + return TSDB_CODE_FAILED; +} + typedef struct SMemParam { SRowBuilder* rb; SSchema* schema; @@ -634,7 +922,7 @@ static int32_t parseInsertBody(SInsertParseContext* pCxt) { // [(field1_name, ...)] // VALUES (field1_value, ...) [(field1_value2, ...) ...] | FILE csv_file_path // [...]; -int32_t parseInsertSql(SParseContext* pContext, SVnodeModifOpStmtInfo** pInfo) { +int32_t parseInsertSql(SParseContext* pContext, SQuery** pQuery) { SInsertParseContext context = { .pComCxt = pContext, .pSql = (char*) pContext->pSql, @@ -643,7 +931,7 @@ int32_t parseInsertSql(SParseContext* pContext, SVnodeModifOpStmtInfo** pInfo) { .pVgroupsHashObj = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, false), .pTableBlockHashObj = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, false), .totalNum = 0, - .pOutput = calloc(1, sizeof(SVnodeModifOpStmtInfo)) + .pOutput = (SVnodeModifOpStmt*)nodesMakeNode(QUERY_NODE_VNODE_MODIF_STMT) }; if (NULL == context.pVgroupsHashObj || NULL == context.pTableBlockHashObj || NULL == context.pOutput) { @@ -651,8 +939,7 @@ int32_t parseInsertSql(SParseContext* pContext, SVnodeModifOpStmtInfo** pInfo) { return TSDB_CODE_TSC_OUT_OF_MEMORY; } - *pInfo = context.pOutput; - context.pOutput->nodeType = TSDB_SQL_INSERT; + (*pQuery)->pRoot = (SNode*)context.pOutput; context.pOutput->payloadType = PAYLOAD_TYPE_KV; int32_t code = skipInsertInto(&context); diff --git a/source/libs/parser/src/parser.c b/source/libs/parser/src/parser.c index 3b79a2de92..599924ab17 100644 --- a/source/libs/parser/src/parser.c +++ b/source/libs/parser/src/parser.c @@ -13,6 +13,7 @@ * along with this program. If not, see . */ +#if 0 #include "astGenerator.h" #include "parserInt.h" #include "parserUtil.h" @@ -260,3 +261,36 @@ void qDestroyQuery(SQueryNode* pQueryNode) { destroyQueryInfo(pQueryStmtInfo); } } + +#else + +#include "parser.h" + +#include "insertParser.h" +#include "parserImpl.h" +#include "ttoken.h" + +static bool isInsertSql(const char* pStr, size_t length) { + int32_t index = 0; + + do { + SToken t0 = tStrGetToken((char*) pStr, &index, false); + if (t0.type != TK_LP) { + return t0.type == TK_INSERT || t0.type == TK_IMPORT; + } + } while (1); +} + +int32_t qParseQuerySql(SParseContext* pCxt, SQuery** pQuery) { + if (isInsertSql(pCxt->pSql, pCxt->sqlLen)) { + return parseInsertSql(pCxt, pQuery); + } else { + return parseQuerySql(pCxt, pQuery); + } +} + +void qDestroyQuery(SQuery* pQueryNode) { + // todo +} + +#endif diff --git a/source/libs/parser/src/parserImpl.c b/source/libs/parser/src/parserImpl.c index ef040fdff4..8312a1b0b3 100644 --- a/source/libs/parser/src/parserImpl.c +++ b/source/libs/parser/src/parserImpl.c @@ -16,9 +16,11 @@ #include "parserImpl.h" #include "astCreateContext.h" +#include "catalog.h" #include "functionMgt.h" -#include "parserInt.h" +#include "parserUtil.h" #include "tglobal.h" +#include "tname.h" #include "ttime.h" #include "ttoken.h" @@ -174,20 +176,20 @@ static uint32_t getToken(const char* z, uint32_t* tokenId) { return n; } -static EStmtType getStmtType(const SNode* pRootNode) { +static bool isCmd(const SNode* pRootNode) { if (NULL == pRootNode) { - return STMT_TYPE_CMD; + return true; } switch (nodeType(pRootNode)) { case QUERY_NODE_SELECT_STMT: - return STMT_TYPE_QUERY; + return false; default: break; } - return STMT_TYPE_CMD; + return true; } -int32_t doParse(SParseContext* pParseCxt, SQuery* pQuery) { +int32_t doParse(SParseContext* pParseCxt, SQuery** pQuery) { SAstCreateContext cxt; createAstCreateContext(pParseCxt, &cxt); void *pParser = NewParseAlloc(malloc); @@ -236,8 +238,11 @@ int32_t doParse(SParseContext* pParseCxt, SQuery* pQuery) { abort_parse: NewParseFree(pParser, free); destroyAstCreateContext(&cxt); - pQuery->stmtType = getStmtType(cxt.pRootNode); - pQuery->pRoot = cxt.pRootNode; + if (cxt.valid) { + *pQuery = calloc(1, sizeof(SQuery)); + (*pQuery)->isCmd = isCmd(cxt.pRootNode); + (*pQuery)->pRoot = cxt.pRootNode; + } return cxt.valid ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED; } @@ -1054,16 +1059,16 @@ int32_t doTranslate(SParseContext* pParseCxt, SQuery* pQuery) { if (TSDB_CODE_SUCCESS == code) { code = translateQuery(&cxt, pQuery->pRoot); } - if (TSDB_CODE_SUCCESS == code && STMT_TYPE_QUERY == pQuery->stmtType) { + if (TSDB_CODE_SUCCESS == code && !pQuery->isCmd) { code = setReslutSchema(&cxt, pQuery); } return code; } -int32_t parser(SParseContext* pParseCxt, SQuery* pQuery) { - int32_t code = doParse(pParseCxt, pQuery); +int32_t parseQuerySql(SParseContext* pCxt, SQuery** pQuery) { + int32_t code = doParse(pCxt, pQuery); if (TSDB_CODE_SUCCESS == code) { - code = doTranslate(pParseCxt, pQuery); + code = doTranslate(pCxt, *pQuery); } return code; } diff --git a/source/libs/parser/src/parserUtil.c b/source/libs/parser/src/parserUtil.c index ec68980c44..da9da0f009 100644 --- a/source/libs/parser/src/parserUtil.c +++ b/source/libs/parser/src/parserUtil.c @@ -14,29 +14,36 @@ */ #include "parserUtil.h" -#include -#include -#include "function.h" -#include "parser.h" -#include "parserInt.h" -#include "queryInfoUtil.h" -#include "taoserror.h" -#include "tbuffer.h" -#include "thash.h" -#include "tmsg.h" -#include "tmsgtype.h" -#include "ttypes.h" -#include "tutil.h" -typedef struct STableFilterCond { - uint64_t uid; - int16_t idx; //table index - int32_t len; // length of tag query condition data - char * cond; -} STableFilterCond; +int32_t buildInvalidOperationMsg(SMsgBuf* pBuf, const char* msg) { + strncpy(pBuf->buf, msg, pBuf->len); + return TSDB_CODE_TSC_INVALID_OPERATION; +} + +int32_t buildSyntaxErrMsg(SMsgBuf* pBuf, const char* additionalInfo, const char* sourceStr) { + const char* msgFormat1 = "syntax error near \'%s\'"; + const char* msgFormat2 = "syntax error near \'%s\' (%s)"; + const char* msgFormat3 = "%s"; + + const char* prefix = "syntax error"; + if (sourceStr == NULL) { + assert(additionalInfo != NULL); + snprintf(pBuf->buf, pBuf->len, msgFormat1, additionalInfo); + return TSDB_CODE_TSC_SQL_SYNTAX_ERROR; + } -static STableMetaInfo* addTableMetaInfo(SQueryStmtInfo* pQueryInfo, SName* name, STableMeta* pTableMeta, - SVgroupsInfo* vgroupList, SArray* pTagCols, SArray* pVgroupTables); + char buf[64] = {0}; // only extract part of sql string + strncpy(buf, sourceStr, tListLen(buf) - 1); + + if (additionalInfo != NULL) { + snprintf(pBuf->buf, pBuf->len, msgFormat2, buf, additionalInfo); + } else { + const char* msgFormat = (0 == strncmp(sourceStr, prefix, strlen(prefix))) ? msgFormat3 : msgFormat1; + snprintf(pBuf->buf, pBuf->len, msgFormat, buf); + } + + return TSDB_CODE_TSC_SQL_SYNTAX_ERROR; +} int32_t parserValidateIdToken(SToken* pToken) { if (pToken == NULL || pToken->z == NULL || pToken->type != TK_ID) { @@ -100,1847 +107,80 @@ int32_t parserValidateIdToken(SToken* pToken) { return TSDB_CODE_SUCCESS; } -int32_t parserValidatePassword(SToken* pToken, SMsgBuf* pMsgBuf) { - const char* msg1 = "password can not be empty"; - const char* msg2 = "name or password too long"; - const char* msg3 = "password needs single quote marks enclosed"; - - if (pToken->type != TK_STRING) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - strdequote(pToken->z); - - pToken->n = (uint32_t)strtrim(pToken->z); // trim space before and after passwords - if (pToken->n <= 0) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - if (pToken->n >= TSDB_USET_PASSWORD_LEN) { - return buildInvalidOperationMsg(pMsgBuf, msg2); - } - - return TSDB_CODE_SUCCESS; -} - -int32_t parserValidateNameToken(SToken* pToken) { - if (pToken == NULL || pToken->z == NULL || pToken->type != TK_ID || pToken->n == 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // it is a token quoted with escape char '`' - if (pToken->z[0] == TS_ESCAPE_CHAR && pToken->z[pToken->n - 1] == TS_ESCAPE_CHAR) { - pToken->n = strdequote(pToken->z); - return TSDB_CODE_SUCCESS; - } - - char* sep = strnchr(pToken->z, TS_PATH_DELIMITER[0], pToken->n, true); - if (sep != NULL) { // It is a complex type, not allow - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - strntolower(pToken->z, pToken->z, pToken->n); - return TSDB_CODE_SUCCESS; -} - -int32_t buildInvalidOperationMsg(SMsgBuf* pBuf, const char* msg) { - strncpy(pBuf->buf, msg, pBuf->len); - return TSDB_CODE_TSC_INVALID_OPERATION; -} - -int32_t buildSyntaxErrMsg(SMsgBuf* pBuf, const char* additionalInfo, const char* sourceStr) { - const char* msgFormat1 = "syntax error near \'%s\'"; - const char* msgFormat2 = "syntax error near \'%s\' (%s)"; - const char* msgFormat3 = "%s"; - - const char* prefix = "syntax error"; - if (sourceStr == NULL) { - assert(additionalInfo != NULL); - snprintf(pBuf->buf, pBuf->len, msgFormat1, additionalInfo); - return TSDB_CODE_TSC_SQL_SYNTAX_ERROR; - } - - char buf[64] = {0}; // only extract part of sql string - strncpy(buf, sourceStr, tListLen(buf) - 1); - - if (additionalInfo != NULL) { - snprintf(pBuf->buf, pBuf->len, msgFormat2, buf, additionalInfo); - } else { - const char* msgFormat = (0 == strncmp(sourceStr, prefix, strlen(prefix))) ? msgFormat3 : msgFormat1; - snprintf(pBuf->buf, pBuf->len, msgFormat, buf); - } - - return TSDB_CODE_TSC_SQL_SYNTAX_ERROR; -} - -SCond* getSTableQueryCond(STagCond* pTagCond, uint64_t uid) { - if (pTagCond->pCond == NULL) { - return NULL; - } - - size_t size = taosArrayGetSize(pTagCond->pCond); - for (int32_t i = 0; i < size; ++i) { - SCond* pCond = taosArrayGet(pTagCond->pCond, i); - - if (uid == pCond->uid) { - return pCond; - } - } - - return NULL; -} - -STableFilterCond* tsGetTableFilter(SArray* filters, uint64_t uid, int16_t idx) { - if (filters == NULL) { - return NULL; - } - - size_t size = taosArrayGetSize(filters); - for (int32_t i = 0; i < size; ++i) { - STableFilterCond* cond = taosArrayGet(filters, i); - - if (uid == cond->uid && (idx >= 0 && cond->idx == idx)) { - return cond; - } - } - - return NULL; -} - -void setSTableQueryCond(STagCond* pTagCond, uint64_t uid, SBufferWriter* bw) { - if (tbufTell(bw) == 0) { - return; - } - - SCond cond = { - .uid = uid, - .len = (int32_t)(tbufTell(bw)), - .cond = NULL, - }; - - cond.cond = tbufGetData(bw, true); - - if (pTagCond->pCond == NULL) { - pTagCond->pCond = taosArrayInit(3, sizeof(SCond)); - } - - taosArrayPush(pTagCond->pCond, &cond); -} - -//typedef struct SJoinStatus { -// SSDataBlock* pBlock; // point to the upstream block -// int32_t index; -// bool completed;// current upstream is completed or not -//} SJoinStatus; - -/* -static void createInputDataFilterInfo(SQueryStmtInfo* px, int32_t numOfCol1, int32_t* numOfFilterCols, SSingleColumnFilterInfo** pFilterInfo) { - SColumnInfo* tableCols = calloc(numOfCol1, sizeof(SColumnInfo)); - for(int32_t i = 0; i < numOfCol1; ++i) { - SColumn* pCol = taosArrayGetP(px->colList, i); - if (pCol->info.flist.numOfFilters > 0) { - (*numOfFilterCols) += 1; - } - - tableCols[i] = pCol->info; - } - - if ((*numOfFilterCols) > 0) { - doCreateFilterInfo(tableCols, numOfCol1, (*numOfFilterCols), pFilterInfo, 0); - } - - tfree(tableCols); -} -*/ - -//void destroyTableNameList(SInsertStatementParam* pInsertParam) { -// if (pInsertParam->numOfTables == 0) { -// assert(pInsertParam->pTableNameList == NULL); -// return; -// } -// -// for(int32_t i = 0; i < pInsertParam->numOfTables; ++i) { -// tfree(pInsertParam->pTableNameList[i]); -// } -// -// pInsertParam->numOfTables = 0; -// tfree(pInsertParam->pTableNameList); -//} - -//void tscDestroyBoundColumnInfo(SParsedDataColInfo* pColInfo) { -// tfree(pColInfo->boundedColumns); -// tfree(pColInfo->cols); -// tfree(pColInfo->colIdxInfo); -//} -// -//void tscDestroyDataBlock(STableDataBlocks* pDataBlock, bool removeMeta) { -// if (pDataBlock == NULL) { -// return; -// } -// -// tfree(pDataBlock->pData); -// -// if (removeMeta) { -// char name[TSDB_TABLE_FNAME_LEN] = {0}; -// tNameExtractFullName(&pDataBlock->tableName, name); -// -// taosHashRemove(tscTableMetaMap, name, strnlen(name, TSDB_TABLE_FNAME_LEN)); -// } -// -// if (!pDataBlock->cloned) { -// tfree(pDataBlock->params); -// -// // free the refcount for metermeta -// if (pDataBlock->pTableMeta != NULL) { -// tfree(pDataBlock->pTableMeta); -// } -// -// tscDestroyBoundColumnInfo(&pDataBlock->boundColumnInfo); -// } -// -// tfree(pDataBlock); -//} - -//SParamInfo* tscAddParamToDataBlock(STableDataBlocks* pDataBlock, char type, uint8_t timePrec, int16_t bytes, -// uint32_t offset) { -// uint32_t needed = pDataBlock->numOfParams + 1; -// if (needed > pDataBlock->numOfAllocedParams) { -// needed *= 2; -// void* tmp = realloc(pDataBlock->params, needed * sizeof(SParamInfo)); -// if (tmp == NULL) { -// return NULL; -// } -// pDataBlock->params = (SParamInfo*)tmp; -// pDataBlock->numOfAllocedParams = needed; -// } -// -// SParamInfo* param = pDataBlock->params + pDataBlock->numOfParams; -// param->idx = -1; -// param->type = type; -// param->timePrec = timePrec; -// param->bytes = bytes; -// param->offset = offset; -// -// ++pDataBlock->numOfParams; -// return param; -//} - -//void* tscDestroyBlockArrayList(SArray* pDataBlockList) { -// if (pDataBlockList == NULL) { -// return NULL; -// } -// -// size_t size = taosArrayGetSize(pDataBlockList); -// for (int32_t i = 0; i < size; i++) { -// void* d = taosArrayGetP(pDataBlockList, i); -// tscDestroyDataBlock(d, false); -// } -// -// taosArrayDestroy(pDataBlockList); -// return NULL; -//} - - -//void freeUdfInfo(SUdfInfo* pUdfInfo) { -// if (pUdfInfo == NULL) { -// return; -// } -// -// if (pUdfInfo->funcs[TSDB_UDF_FUNC_DESTROY]) { -// (*(udfDestroyFunc)pUdfInfo->funcs[TSDB_UDF_FUNC_DESTROY])(&pUdfInfo->init); -// } -// -// tfree(pUdfInfo->name); -// -// if (pUdfInfo->path) { -// unlink(pUdfInfo->path); -// } -// -// tfree(pUdfInfo->path); -// -// tfree(pUdfInfo->content); -// -// taosCloseDll(pUdfInfo->handle); -//} - -//void* tscDestroyUdfArrayList(SArray* pUdfList) { -// if (pUdfList == NULL) { -// return NULL; -// } -// -// size_t size = taosArrayGetSize(pUdfList); -// for (int32_t i = 0; i < size; i++) { -// SUdfInfo* udf = taosArrayGet(pUdfList, i); -// freeUdfInfo(udf); -// } -// -// taosArrayDestroy(pUdfList); -// return NULL; -//} - -//void* tscDestroyBlockHashTable(SHashObj* pBlockHashTable, bool removeMeta) { -// if (pBlockHashTable == NULL) { -// return NULL; -// } -// -// STableDataBlocks** p = taosHashIterate(pBlockHashTable, NULL); -// while(p) { -// tscDestroyDataBlock(*p, removeMeta); -// p = taosHashIterate(pBlockHashTable, p); -// } -// -// taosHashCleanup(pBlockHashTable); -// return NULL; -//} - -/** - * create the in-memory buffer for each table to keep the submitted data block - * @param initialSize - * @param rowSize - * @param startOffset - * @param name - * @param dataBlocks - * @return - */ -// int32_t tscCreateDataBlock(size_t defaultSize, int32_t rowSize, int32_t startOffset, SName* name, -// STableMeta* pTableMeta, STableDataBlocks** dataBlocks) { -// STableDataBlocks* dataBuf = (STableDataBlocks*)calloc(1, sizeof(STableDataBlocks)); -// if (dataBuf == NULL) { -// tscError("failed to allocated memory, reason:%s", strerror(errno)); -// return TSDB_CODE_TSC_OUT_OF_MEMORY; -// } -// -// dataBuf->nAllocSize = (uint32_t)defaultSize; -// dataBuf->headerSize = startOffset; -// -// // the header size will always be the startOffset value, reserved for the subumit block header -// if (dataBuf->nAllocSize <= dataBuf->headerSize) { -// dataBuf->nAllocSize = dataBuf->headerSize * 2; -// } -// -// //dataBuf->pData = calloc(1, dataBuf->nAllocSize); -// dataBuf->pData = malloc(dataBuf->nAllocSize); -// if (dataBuf->pData == NULL) { -// tscError("failed to allocated memory, reason:%s", strerror(errno)); -// tfree(dataBuf); -// return TSDB_CODE_TSC_OUT_OF_MEMORY; -// } -// memset(dataBuf->pData, 0, sizeof(SSubmitBlk)); -// -// //Here we keep the tableMeta to avoid it to be remove by other threads. -// dataBuf->pTableMeta = tscTableMetaDup(pTableMeta); -// -// SParsedDataColInfo* pColInfo = &dataBuf->boundColumnInfo; -// SSchema* pSchema = getTableColumnSchema(dataBuf->pTableMeta); -// tscSetBoundColumnInfo(pColInfo, pSchema, dataBuf->pTableMeta->tableInfo.numOfColumns); -// -// dataBuf->ordered = true; -// dataBuf->prevTS = INT64_MIN; -// dataBuf->rowSize = rowSize; -// dataBuf->size = startOffset; -// dataBuf->tsSource = -1; -// dataBuf->vgId = dataBuf->pTableMeta->vgId; -// -// tNameAssign(&dataBuf->tableName, name); -// -// assert(defaultSize > 0 && pTableMeta != NULL && dataBuf->pTableMeta != NULL); -// -// *dataBlocks = dataBuf; -// return TSDB_CODE_SUCCESS; -//} -// -// int32_t tscGetDataBlockFromList(SHashObj* pHashList, int64_t id, int32_t size, int32_t startOffset, int32_t rowSize, -// SName* name, STableMeta* pTableMeta, STableDataBlocks** dataBlocks, -// SArray* pBlockList) { -// *dataBlocks = NULL; -// STableDataBlocks** t1 = (STableDataBlocks**)taosHashGet(pHashList, (const char*)&id, sizeof(id)); -// if (t1 != NULL) { -// *dataBlocks = *t1; -// } -// -// if (*dataBlocks == NULL) { -// int32_t ret = tscCreateDataBlock((size_t)size, rowSize, startOffset, name, pTableMeta, dataBlocks); -// if (ret != TSDB_CODE_SUCCESS) { -// return ret; -// } -// -// taosHashPut(pHashList, (const char*)&id, sizeof(int64_t), (char*)dataBlocks, POINTER_BYTES); -// if (pBlockList) { -// taosArrayPush(pBlockList, dataBlocks); -// } -// } -// -// return TSDB_CODE_SUCCESS; -//} -// -//// Erase the empty space reserved for binary data -// static int trimDataBlock(void* pDataBlock, STableDataBlocks* pTableDataBlock, SInsertStatementParam* insertParam, -// SBlockKeyTuple* blkKeyTuple) { -// // TODO: optimize this function, handle the case while binary is not presented -// STableMeta* pTableMeta = pTableDataBlock->pTableMeta; -// STableComInfo tinfo = tscGetTableInfo(pTableMeta); -// SSchema* pSchema = getTableColumnSchema(pTableMeta); -// -// SSubmitBlk* pBlock = pDataBlock; -// memcpy(pDataBlock, pTableDataBlock->pData, sizeof(SSubmitBlk)); -// pDataBlock = (char*)pDataBlock + sizeof(SSubmitBlk); -// -// int32_t flen = 0; // original total length of row -// -// // schema needs to be included into the submit data block -// if (insertParam->schemaAttached) { -// int32_t numOfCols = tscGetNumOfColumns(pTableDataBlock->pTableMeta); -// for(int32_t j = 0; j < numOfCols; ++j) { -// STColumn* pCol = (STColumn*) pDataBlock; -// pCol->colId = htons(pSchema[j].colId); -// pCol->type = pSchema[j].type; -// pCol->bytes = htons(pSchema[j].bytes); -// pCol->offset = 0; -// -// pDataBlock = (char*)pDataBlock + sizeof(STColumn); -// flen += TYPE_BYTES[pSchema[j].type]; -// } -// -// int32_t schemaSize = sizeof(STColumn) * numOfCols; -// pBlock->schemaLen = schemaSize; -// } else { -// if (IS_RAW_PAYLOAD(insertParam->payloadType)) { -// for (int32_t j = 0; j < tinfo.numOfColumns; ++j) { -// flen += TYPE_BYTES[pSchema[j].type]; -// } -// } -// pBlock->schemaLen = 0; -// } -// -// char* p = pTableDataBlock->pData + sizeof(SSubmitBlk); -// pBlock->dataLen = 0; -// int32_t numOfRows = htons(pBlock->numOfRows); -// -// if (IS_RAW_PAYLOAD(insertParam->payloadType)) { -// for (int32_t i = 0; i < numOfRows; ++i) { -// STSRow* memRow = (STSRow*)pDataBlock; -// memRowSetType(memRow, SMEM_ROW_DATA); -// SDataRow trow = memRowDataBody(memRow); -// dataRowSetLen(trow, (uint16_t)(TD_DATA_ROW_HEAD_SIZE + flen)); -// dataRowSetVersion(trow, pTableMeta->sversion); -// -// int toffset = 0; -// for (int32_t j = 0; j < tinfo.numOfColumns; j++) { -// tdAppendColVal(trow, p, pSchema[j].type, toffset); -// toffset += TYPE_BYTES[pSchema[j].type]; -// p += pSchema[j].bytes; -// } -// -// pDataBlock = (char*)pDataBlock + TD_ROW_LEN(memRow); -// pBlock->dataLen += TD_ROW_LEN(memRow); -// } -// } else { -// for (int32_t i = 0; i < numOfRows; ++i) { -// char* payload = (blkKeyTuple + i)->payloadAddr; -// TDRowLenT rowTLen = TD_ROW_LEN(payload); -// memcpy(pDataBlock, payload, rowTLen); -// pDataBlock = POINTER_SHIFT(pDataBlock, rowTLen); -// pBlock->dataLen += rowTLen; -// } -// } - -// int32_t len = pBlock->dataLen + pBlock->schemaLen; -// pBlock->dataLen = htonl(pBlock->dataLen); -// pBlock->schemaLen = htonl(pBlock->schemaLen); - -// return len; -// } - -TAOS_FIELD createField(const SSchema* pSchema) { - TAOS_FIELD f = { .type = pSchema->type, .bytes = pSchema->bytes, }; - tstrncpy(f.name, pSchema->name, sizeof(f.name)); - return f; -} - -void setColumn(SColumn* pColumn, uint64_t uid, const char* tableName, int8_t flag, const SSchema* pSchema) { - pColumn->uid = uid; - pColumn->flag = flag; - pColumn->info.colId = pSchema->colId; - pColumn->info.bytes = pSchema->bytes; - pColumn->info.type = pSchema->type; - - if (tableName != NULL) { - char n[TSDB_COL_NAME_LEN + 1 + TSDB_TABLE_NAME_LEN] = {0}; - snprintf(n, tListLen(n), "%s.%s", tableName, pSchema->name); - tstrncpy(pColumn->name, n, tListLen(pColumn->name)); - } else { - tstrncpy(pColumn->name, pSchema->name, tListLen(pColumn->name)); - } -} - -SColumn createColumn(uint64_t uid, const char* tableName, int8_t flag, const SSchema* pSchema) { - SColumn c; - c.uid = uid; - c.flag = flag; - c.info.colId = pSchema->colId; - c.info.bytes = pSchema->bytes; - c.info.type = pSchema->type; - - if (tableName != NULL) { - char n[TSDB_COL_NAME_LEN + 1 + TSDB_TABLE_NAME_LEN] = {0}; - snprintf(n, tListLen(n), "%s.%s", tableName, pSchema->name); - - tstrncpy(c.name, n, tListLen(c.name)); - } else { - tstrncpy(c.name, pSchema->name, tListLen(c.name)); - } - - return c; -} +int32_t KvRowAppend(const void *value, int32_t len, void *param) { + SKvParam* pa = (SKvParam*) param; -void addIntoSourceParam(SSourceParam* pSourceParam, tExprNode* pNode, SColumn* pColumn) { - assert(pSourceParam != NULL); - pSourceParam->num += 1; + int32_t type = pa->schema->type; + int32_t colId = pa->schema->colId; - if (pSourceParam->pExprNodeList != NULL) { - assert(pNode != NULL && pColumn == NULL); - if (pSourceParam->pExprNodeList == NULL) { - pSourceParam->pExprNodeList = taosArrayInit(4, POINTER_BYTES); + if (TSDB_DATA_TYPE_BINARY == type) { + STR_WITH_SIZE_TO_VARSTR(pa->buf, value, len); + tdAddColToKVRow(pa->builder, colId, type, pa->buf); + } else if (TSDB_DATA_TYPE_NCHAR == type) { + // if the converted output len is over than pColumnModel->bytes, return error: 'Argument list too long' + int32_t output = 0; + if (!taosMbsToUcs4(value, len, varDataVal(pa->buf), pa->schema->bytes - VARSTR_HEADER_SIZE, &output)) { + return TSDB_CODE_TSC_SQL_SYNTAX_ERROR; } - taosArrayPush(pSourceParam->pExprNodeList, &pNode); + varDataSetLen(pa->buf, output); + tdAddColToKVRow(pa->builder, colId, type, pa->buf); } else { - assert(pColumn != NULL); - if (pSourceParam->pColumnList == NULL) { - pSourceParam->pColumnList = taosArrayInit(4, POINTER_BYTES); - } - - taosArrayPush(pSourceParam->pColumnList, &pColumn); - } -} - -int32_t getNumOfFields(SFieldInfo* pFieldInfo) { - return pFieldInfo->numOfOutput; -} - -SInternalField* appendFieldInfo(SFieldInfo* pFieldInfo, TAOS_FIELD* pField) { - assert(pFieldInfo != NULL); - pFieldInfo->numOfOutput++; - - struct SInternalField info = { .pExpr = NULL, .visible = true }; - - info.field = *pField; - return taosArrayPush(pFieldInfo->internalField, &info); -} - -SInternalField* insertFieldInfo(SFieldInfo* pFieldInfo, int32_t index, SSchema* pSchema) { - pFieldInfo->numOfOutput++; - struct SInternalField info = { .pExpr = NULL, .visible = true }; - - info.field.type = pSchema->type; - info.field.bytes = pSchema->bytes; - tstrncpy(info.field.name, pSchema->name, tListLen(pSchema->name)); - - return taosArrayInsert(pFieldInfo->internalField, index, &info); -} - -void fieldInfoUpdateOffset(SQueryStmtInfo* pQueryInfo) { - int32_t offset = 0; - size_t numOfExprs = getNumOfExprs(pQueryInfo); - - SArray* pList = getCurrentExprList(pQueryInfo); - for (int32_t i = 0; i < numOfExprs; ++i) { - SExprInfo* p = taosArrayGetP(pList, i); - -// p->base.offset = offset; - offset += p->base.resSchema.bytes; - } -} - -SInternalField* getInternalField(SFieldInfo* pFieldInfo, int32_t index) { - assert(index < pFieldInfo->numOfOutput); - return TARRAY_GET_ELEM(pFieldInfo->internalField, index); -} - -TAOS_FIELD* getFieldInfo(SFieldInfo* pFieldInfo, int32_t index) { - assert(index < pFieldInfo->numOfOutput); - return &((SInternalField*)TARRAY_GET_ELEM(pFieldInfo->internalField, index))->field; -} - -int32_t fieldInfoCompare(const SFieldInfo* pFieldInfo1, const SFieldInfo* pFieldInfo2, int32_t *diffSize) { - assert(pFieldInfo1 != NULL && pFieldInfo2 != NULL); - - if (pFieldInfo1->numOfOutput != pFieldInfo2->numOfOutput) { - return pFieldInfo1->numOfOutput - pFieldInfo2->numOfOutput; - } - - for (int32_t i = 0; i < pFieldInfo1->numOfOutput; ++i) { - TAOS_FIELD* pField1 = getFieldInfo((SFieldInfo*) pFieldInfo1, i); - TAOS_FIELD* pField2 = getFieldInfo((SFieldInfo*) pFieldInfo2, i); - - if (pField1->type != pField2->type || - strcasecmp(pField1->name, pField2->name) != 0) { - return 1; - } - - if (pField1->bytes != pField2->bytes) { - *diffSize = 1; - - if (pField2->bytes > pField1->bytes) { - assert(IS_VAR_DATA_TYPE(pField1->type)); - pField1->bytes = pField2->bytes; - } - } - } - - return 0; -} - -int32_t getFieldInfoSize(const SFieldInfo* pFieldInfo1, const SFieldInfo* pFieldInfo2) { - assert(pFieldInfo1 != NULL && pFieldInfo2 != NULL); - - for (int32_t i = 0; i < pFieldInfo1->numOfOutput; ++i) { - TAOS_FIELD* pField1 = getFieldInfo((SFieldInfo*) pFieldInfo1, i); - TAOS_FIELD* pField2 = getFieldInfo((SFieldInfo*) pFieldInfo2, i); - - pField2->bytes = pField1->bytes; + tdAddColToKVRow(pa->builder, colId, type, value); } - return 0; + return TSDB_CODE_SUCCESS; } -static void destroyFilterInfo(SColumnFilterList* pFilterList) { - if (pFilterList->filterInfo == NULL) { - pFilterList->numOfFilters = 0; - return; - } - - for(int32_t i = 0; i < pFilterList->numOfFilters; ++i) { - if (pFilterList->filterInfo[i].filterstr) { - tfree(pFilterList->filterInfo[i].pz); - } - } - - tfree(pFilterList->filterInfo); - pFilterList->numOfFilters = 0; -} +static uint32_t getTableMetaSize(const STableMeta* pTableMeta) { + assert(pTableMeta != NULL); -void cleanupFieldInfo(SFieldInfo* pFieldInfo) { - if (pFieldInfo == NULL) { - return; + int32_t totalCols = 0; + if (pTableMeta->tableInfo.numOfColumns >= 0) { + totalCols = pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags; } - taosArrayDestroy(pFieldInfo->internalField); - tfree(pFieldInfo->final); - - memset(pFieldInfo, 0, sizeof(SFieldInfo)); + return sizeof(STableMeta) + totalCols * sizeof(SSchema); } -void copyFieldInfo(SFieldInfo* pFieldInfo, const SFieldInfo* pSrc, const SArray* pExprList) { - assert(pFieldInfo != NULL && pSrc != NULL && pExprList != NULL); - pFieldInfo->numOfOutput = pSrc->numOfOutput; - - if (pSrc->final != NULL) { - pFieldInfo->final = calloc(pSrc->numOfOutput, sizeof(TAOS_FIELD)); - memcpy(pFieldInfo->final, pSrc->final, sizeof(TAOS_FIELD) * pSrc->numOfOutput); - } - - if (pSrc->internalField != NULL) { - size_t num = taosArrayGetSize(pSrc->internalField); - size_t numOfExpr = taosArrayGetSize(pExprList); - - for (int32_t i = 0; i < num; ++i) { - SInternalField* pfield = taosArrayGet(pSrc->internalField, i); - - SInternalField p = {.visible = pfield->visible, .field = pfield->field}; - - bool found = false; - int32_t resColId = pfield->pExpr->base.resSchema.colId; - for(int32_t j = 0; j < numOfExpr; ++j) { - SExprInfo* pExpr = taosArrayGetP(pExprList, j); - if (pExpr->base.resSchema.colId == resColId) { - p.pExpr = pExpr; - found = true; - break; - } - } - - if (!found) { - assert(pfield->pExpr->pExpr != NULL); - p.pExpr = calloc(1, sizeof(SExprInfo)); - assignExprInfo(p.pExpr, pfield->pExpr); - } +STableMeta* tableMetaDup(const STableMeta* pTableMeta) { + assert(pTableMeta != NULL); + size_t size = getTableMetaSize(pTableMeta); - taosArrayPush(pFieldInfo->internalField, &p); - } - } + STableMeta* p = malloc(size); + memcpy(p, pTableMeta, size); + return p; } -// ignore the tbname columnIndex to be inserted into source list -int32_t columnExists(SArray* pColumnList, int32_t columnId, uint64_t uid) { - size_t numOfCols = taosArrayGetSize(pColumnList); - - int32_t i = 0; - while (i < numOfCols) { - SColumn* pCol = taosArrayGetP(pColumnList, i); - if ((pCol->info.colId != columnId) || (pCol->uid != uid)) { - ++i; - continue; - } else { - break; - } - } - - if (i >= numOfCols || numOfCols == 0) { - return -1; - } - - return i; +SSchema *getTableColumnSchema(const STableMeta *pTableMeta) { + assert(pTableMeta != NULL); + return (SSchema*) pTableMeta->schema; } -static int32_t doFindPosition(const SArray* pColumnList, uint64_t uid, const SSchema* pSchema) { - int32_t i = 0; +static SSchema* getOneColumnSchema(const STableMeta* pTableMeta, int32_t colIndex) { + assert(pTableMeta != NULL && pTableMeta->schema != NULL && colIndex >= 0 && colIndex < (getNumOfColumns(pTableMeta) + getNumOfTags(pTableMeta))); - size_t numOfCols = taosArrayGetSize(pColumnList); - while (i < numOfCols) { - SColumn* pCol = taosArrayGetP(pColumnList, i); - if (pCol->uid < uid) { - i++; - continue; - } - - if (pCol->info.colId < pSchema->colId) { - i++; - continue; - } - - break; - } - - return i; + SSchema* pSchema = (SSchema*) pTableMeta->schema; + return &pSchema[colIndex]; } -SColumn* columnListInsert(SArray* pColumnList, uint64_t uid, SSchema* pSchema, int32_t flag) { - // ignore the tbname columnIndex to be inserted into source list - assert(pSchema != NULL && pColumnList != NULL); - - int32_t i = doFindPosition(pColumnList, uid, pSchema); - size_t size = taosArrayGetSize(pColumnList); - if (size > 0 && i < size) { - SColumn* pCol = taosArrayGetP(pColumnList, i); - if (pCol->uid == uid && pCol->info.colId == pSchema->colId) { - return pCol; - } - } - - SColumn* b = calloc(1, sizeof(SColumn)); - if (b == NULL) { - return NULL; - } - - b->uid = uid; - b->flag = flag; - b->info.colId = pSchema->colId; - b->info.bytes = pSchema->bytes; - b->info.type = pSchema->type; - tstrncpy(b->name, pSchema->name, tListLen(b->name)); - taosArrayInsert(pColumnList, i, &b); - - return b; +SSchema* getTableTagSchema(const STableMeta* pTableMeta) { + assert(pTableMeta != NULL && (pTableMeta->tableType == TSDB_SUPER_TABLE || pTableMeta->tableType == TSDB_CHILD_TABLE)); + return getOneColumnSchema(pTableMeta, getTableInfo(pTableMeta).numOfColumns); } -SColumn* insertPrimaryTsColumn(SArray* pColumnList, const char* colName, uint64_t tableUid) { - SSchema s = {.type = TSDB_DATA_TYPE_TIMESTAMP, .bytes = TSDB_KEYSIZE, .colId = PRIMARYKEY_TIMESTAMP_COL_ID}; - strncpy(s.name, colName, tListLen(s.name)); - - return columnListInsert(pColumnList, tableUid, &s, TSDB_COL_NORMAL); +int32_t getNumOfColumns(const STableMeta* pTableMeta) { + assert(pTableMeta != NULL); + // table created according to super table, use data from super table + return getTableInfo(pTableMeta).numOfColumns; } -void columnCopy(SColumn* pDest, const SColumn* pSrc); - -SColumn* columnClone(const SColumn* src) { - assert(src != NULL); - - SColumn* dst = calloc(1, sizeof(SColumn)); - if (dst == NULL) { - return NULL; - } - - columnCopy(dst, src); - return dst; +int32_t getNumOfTags(const STableMeta* pTableMeta) { + assert(pTableMeta != NULL); + return getTableInfo(pTableMeta).numOfTags; } -SColumnFilterInfo* tFilterInfoDup(const SColumnFilterInfo* src, int32_t numOfFilters) { - if (numOfFilters == 0 || src == NULL) { - assert(src == NULL); - return NULL; - } - - SColumnFilterInfo* pFilter = calloc(1, numOfFilters * sizeof(SColumnFilterInfo)); - - memcpy(pFilter, src, sizeof(SColumnFilterInfo) * numOfFilters); - for (int32_t j = 0; j < numOfFilters; ++j) { - if (pFilter[j].filterstr) { - size_t len = (size_t) pFilter[j].len + 1 * TSDB_NCHAR_SIZE; - pFilter[j].pz = (int64_t) calloc(1, len); - - memcpy((char*)pFilter[j].pz, (char*)src[j].pz, (size_t) pFilter[j].len); - } - } - - assert(src->filterstr == 0 || src->filterstr == 1); - assert(!(src->lowerRelOptr == 0 && src->upperRelOptr == 0)); - - return pFilter; -} - -void columnCopy(SColumn* pDest, const SColumn* pSrc) { - destroyFilterInfo(&pDest->info.flist); - - pDest->uid = pSrc->uid; - pDest->info.flist.numOfFilters = pSrc->info.flist.numOfFilters; - pDest->info.flist.filterInfo = tFilterInfoDup(pSrc->info.flist.filterInfo, pSrc->info.flist.numOfFilters); - pDest->info.type = pSrc->info.type; - pDest->info.colId = pSrc->info.colId; - pDest->info.bytes = pSrc->info.bytes; -} - -void columnListCopyAll(SArray* dst, const SArray* src) { - assert(src != NULL && dst != NULL); - - size_t num = taosArrayGetSize(src); - for (int32_t i = 0; i < num; ++i) { - SColumn* pCol = taosArrayGetP(src, i); - SColumn* p = columnClone(pCol); - taosArrayPush(dst, &p); - } -} - -void columnListCopy(SArray* dst, const SArray* src, uint64_t uid) { - assert(src != NULL && dst != NULL); - - size_t num = taosArrayGetSize(src); - for (int32_t i = 0; i < num; ++i) { - SColumn* pCol = taosArrayGetP(src, i); - - if (pCol->uid == uid) { - SColumn* p = columnClone(pCol); - taosArrayPush(dst, &p); - } - } -} - -static void columnDestroy(SColumn* pCol) { - destroyFilterInfo(&pCol->info.flist); - free(pCol); -} - -void columnListDestroy(SArray* pColumnList) { - if (pColumnList == NULL) { - return; - } - - size_t num = taosArrayGetSize(pColumnList); - for (int32_t i = 0; i < num; ++i) { - SColumn* pCol = taosArrayGetP(pColumnList, i); - columnDestroy(pCol); - } - - taosArrayDestroy(pColumnList); -} - -bool validateColumnId(STableMetaInfo* pTableMetaInfo, int32_t colId, int32_t numOfParams) { - if (pTableMetaInfo->pTableMeta == NULL) { - return false; - } - - if (colId == TSDB_TBNAME_COLUMN_INDEX || (colId <= TSDB_UD_COLUMN_INDEX && numOfParams == 2)) { - return true; - } - - SSchema* pSchema = getTableColumnSchema(pTableMetaInfo->pTableMeta); - STableComInfo tinfo = getTableInfo(pTableMetaInfo->pTableMeta); - - int32_t numOfTotal = tinfo.numOfTags + tinfo.numOfColumns; - - for (int32_t i = 0; i < numOfTotal; ++i) { - if (pSchema[i].colId == colId) { - return true; - } - } - - return false; -} - -int32_t tscTagCondCopy(STagCond* dest, const STagCond* src) { - memset(dest, 0, sizeof(STagCond)); - - if (src->tbnameCond.cond != NULL) { - dest->tbnameCond.cond = strdup(src->tbnameCond.cond); - if (dest->tbnameCond.cond == NULL) { - return -1; - } - } - - dest->tbnameCond.uid = src->tbnameCond.uid; - dest->tbnameCond.len = src->tbnameCond.len; - - dest->joinInfo.hasJoin = src->joinInfo.hasJoin; - - for (int32_t i = 0; i < TSDB_MAX_JOIN_TABLE_NUM; ++i) { - if (src->joinInfo.joinTables[i]) { - dest->joinInfo.joinTables[i] = calloc(1, sizeof(SJoinNode)); - - memcpy(dest->joinInfo.joinTables[i], src->joinInfo.joinTables[i], sizeof(SJoinNode)); - - if (src->joinInfo.joinTables[i]->tsJoin) { - dest->joinInfo.joinTables[i]->tsJoin = taosArrayDup(src->joinInfo.joinTables[i]->tsJoin); - } - - if (src->joinInfo.joinTables[i]->tagJoin) { - dest->joinInfo.joinTables[i]->tagJoin = taosArrayDup(src->joinInfo.joinTables[i]->tagJoin); - } - } - } - - - dest->relType = src->relType; - - if (src->pCond == NULL) { - return 0; - } - - size_t s = taosArrayGetSize(src->pCond); - dest->pCond = taosArrayInit(s, sizeof(SCond)); - - for (int32_t i = 0; i < s; ++i) { - SCond* pCond = taosArrayGet(src->pCond, i); - - SCond c = {0}; - c.len = pCond->len; - c.uid = pCond->uid; - - if (pCond->len > 0) { - assert(pCond->cond != NULL); - c.cond = malloc(c.len); - if (c.cond == NULL) { - return -1; - } - - memcpy(c.cond, pCond->cond, c.len); - } - - taosArrayPush(dest->pCond, &c); - } - - return 0; -} - -int32_t tscColCondCopy(SArray** dest, const SArray* src, uint64_t uid, int16_t tidx) { - if (src == NULL) { - return 0; - } - - size_t s = taosArrayGetSize(src); - *dest = taosArrayInit(s, sizeof(SCond)); - - for (int32_t i = 0; i < s; ++i) { - STableFilterCond* pCond = taosArrayGet(src, i); - STableFilterCond c = {0}; - - if (tidx > 0) { - if (!(pCond->uid == uid && pCond->idx == tidx)) { - continue; - } - - c.idx = 0; - } else { - c.idx = pCond->idx; - } - - c.len = pCond->len; - c.uid = pCond->uid; - - if (pCond->len > 0) { - assert(pCond->cond != NULL); - c.cond = malloc(c.len); - if (c.cond == NULL) { - return -1; - } - - memcpy(c.cond, pCond->cond, c.len); - } - - taosArrayPush(*dest, &c); - } - - return 0; -} - -void cleanupColumnCond(SArray** pCond) { - if (*pCond == NULL) { - return; - } - - size_t s = taosArrayGetSize(*pCond); - for (int32_t i = 0; i < s; ++i) { - STableFilterCond* p = taosArrayGet(*pCond, i); - tfree(p->cond); - } - - taosArrayDestroy(*pCond); - - *pCond = NULL; -} - -void cleanupTagCond(STagCond* pTagCond) { - free(pTagCond->tbnameCond.cond); - - if (pTagCond->pCond != NULL) { - size_t s = taosArrayGetSize(pTagCond->pCond); - for (int32_t i = 0; i < s; ++i) { - SCond* p = taosArrayGet(pTagCond->pCond, i); - tfree(p->cond); - } - - taosArrayDestroy(pTagCond->pCond); - } - - for (int32_t i = 0; i < TSDB_MAX_JOIN_TABLE_NUM; ++i) { - SJoinNode *node = pTagCond->joinInfo.joinTables[i]; - if (node == NULL) { - continue; - } - - if (node->tsJoin != NULL) { - taosArrayDestroy(node->tsJoin); - } - - if (node->tagJoin != NULL) { - taosArrayDestroy(node->tagJoin); - } - - tfree(node); - } - - memset(pTagCond, 0, sizeof(STagCond)); -} - -//void tscGetSrcColumnInfo(SSrcColumnInfo* pColInfo, SQueryStmtInfo* pQueryInfo) { -// STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); -// SSchema* pSchema = getTableColumnSchema(pTableMetaInfo->pTableMeta); -// -// size_t numOfExprs = getNumOfExprs(pQueryInfo); -// for (int32_t i = 0; i < numOfExprs; ++i) { -// SExprInfo* pExpr = getExprInfo(pQueryInfo, i); -// pColInfo[i].functionId = pExpr->base.functionId; -// -// if (TSDB_COL_IS_TAG(pExpr->base.colInfo.flag)) { -// SSchema* pTagSchema = tscGetTableTagSchema(pTableMetaInfo->pTableMeta); -// -// int16_t index = pExpr->base.colInfo.colIndex; -// pColInfo[i].type = (index != -1) ? pTagSchema[index].type : TSDB_DATA_TYPE_BINARY; -// } else { -// pColInfo[i].type = pSchema[pExpr->base.colInfo.colIndex].type; -// } -// } -//} - -/** - * - * @param clauseIndex denote the index of the union sub clause, usually are 0, if no union query exists. - * @param tableIndex denote the table index for join query, where more than one table exists - * @return - */ -STableMetaInfo* getMetaInfo(const SQueryStmtInfo* pQueryInfo, int32_t tableIndex) { - assert(pQueryInfo != NULL); - if (pQueryInfo->pTableMetaInfo == NULL) { - assert(pQueryInfo->numOfTables == 0); - return NULL; - } - - assert(tableIndex >= 0 && tableIndex <= pQueryInfo->numOfTables && pQueryInfo->pTableMetaInfo != NULL); - return pQueryInfo->pTableMetaInfo[tableIndex]; -} - -STableMetaInfo* getTableMetaInfoByUid(SQueryStmtInfo* pQueryInfo, uint64_t uid, int32_t* index) { - int32_t k = -1; - - for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { - if (pQueryInfo->pTableMetaInfo[i]->pTableMeta->uid == uid) { - k = i; - break; - } - } - - if (index != NULL) { - *index = k; - } - - assert(k != -1); - return getMetaInfo(pQueryInfo, k); -} - -int32_t queryInfoCopy(SQueryStmtInfo* pQueryInfo, const SQueryStmtInfo* pSrc) { - assert(pQueryInfo != NULL && pSrc != NULL); - int32_t code = TSDB_CODE_SUCCESS; - - memcpy(&pQueryInfo->interval, &pSrc->interval, sizeof(pQueryInfo->interval)); - - pQueryInfo->command = pSrc->command; - pQueryInfo->type = pSrc->type; - pQueryInfo->window = pSrc->window; - pQueryInfo->limit = pSrc->limit; - pQueryInfo->slimit = pSrc->slimit; - pQueryInfo->order = pSrc->order; - pQueryInfo->vgroupLimit = pSrc->vgroupLimit; - pQueryInfo->tsBuf = NULL; - pQueryInfo->fillType = pSrc->fillType; - pQueryInfo->fillVal = NULL; - pQueryInfo->numOfFillVal = 0;; - pQueryInfo->clauseLimit = pSrc->clauseLimit; - pQueryInfo->prjOffset = pSrc->prjOffset; - pQueryInfo->numOfTables = 0; - pQueryInfo->window = pSrc->window; - pQueryInfo->sessionWindow = pSrc->sessionWindow; - pQueryInfo->pTableMetaInfo = NULL; - - pQueryInfo->bufLen = pSrc->bufLen; -// pQueryInfo->orderProjectQuery = pSrc->orderProjectQuery; -// pQueryInfo->arithmeticOnAgg = pSrc->arithmeticOnAgg; - pQueryInfo->buf = malloc(pSrc->bufLen); - if (pQueryInfo->buf == NULL) { - code = TSDB_CODE_TSC_OUT_OF_MEMORY; - goto _error; - } - - if (pSrc->bufLen > 0) { - memcpy(pQueryInfo->buf, pSrc->buf, pSrc->bufLen); - } - - pQueryInfo->groupbyExpr = pSrc->groupbyExpr; - if (pSrc->groupbyExpr.columnInfo != NULL) { - pQueryInfo->groupbyExpr.columnInfo = taosArrayDup(pSrc->groupbyExpr.columnInfo); - if (pQueryInfo->groupbyExpr.columnInfo == NULL) { - code = TSDB_CODE_TSC_OUT_OF_MEMORY; - goto _error; - } - } - - if (tscTagCondCopy(&pQueryInfo->tagCond, &pSrc->tagCond) != 0) { - code = TSDB_CODE_TSC_OUT_OF_MEMORY; - goto _error; - } - - if (tscColCondCopy(&pQueryInfo->colCond, pSrc->colCond, 0, -1) != 0) { - code = TSDB_CODE_TSC_OUT_OF_MEMORY; - goto _error; - } - - if (pSrc->fillType != TSDB_FILL_NONE) { - pQueryInfo->fillVal = calloc(1, pSrc->fieldsInfo.numOfOutput * sizeof(int64_t)); - if (pQueryInfo->fillVal == NULL) { - code = TSDB_CODE_TSC_OUT_OF_MEMORY; - goto _error; - } - pQueryInfo->numOfFillVal = pSrc->fieldsInfo.numOfOutput; - - memcpy(pQueryInfo->fillVal, pSrc->fillVal, pSrc->fieldsInfo.numOfOutput * sizeof(int64_t)); - } - - if (copyAllExprInfo(pQueryInfo->exprList[0], pSrc->exprList[0], true) != 0) { - code = TSDB_CODE_TSC_OUT_OF_MEMORY; - goto _error; - } - - columnListCopyAll(pQueryInfo->colList, pSrc->colList); - copyFieldInfo(&pQueryInfo->fieldsInfo, &pSrc->fieldsInfo, pQueryInfo->exprList[0]); - - for(int32_t i = 0; i < pSrc->numOfTables; ++i) { - STableMetaInfo* p1 = getMetaInfo((SQueryStmtInfo*) pSrc, i); - - STableMeta* pMeta = tableMetaDup(p1->pTableMeta); - if (pMeta == NULL) { - // todo handle the error - } - - addTableMetaInfo(pQueryInfo, &p1->name, pMeta, p1->vgroupList, p1->tagColList, NULL); - } - - SArray *pUdfInfo = NULL; - if (pSrc->pUdfInfo) { - pUdfInfo = taosArrayDup(pSrc->pUdfInfo); - } - - pQueryInfo->pUdfInfo = pUdfInfo; - - _error: - return code; -} - -void clearAllTableMetaInfo(SQueryStmtInfo* pQueryInfo, bool removeMeta, uint64_t id) { - for(int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { - STableMetaInfo* pTableMetaInfo = getMetaInfo(pQueryInfo, i); - clearTableMetaInfo(pTableMetaInfo); - } - - tfree(pQueryInfo->pTableMetaInfo); -} - -STableMetaInfo* addTableMetaInfo(SQueryStmtInfo* pQueryInfo, SName* name, STableMeta* pTableMeta, - SVgroupsInfo* vgroupList, SArray* pTagCols, SArray* pVgroupTables) { - void* tmp = realloc(pQueryInfo->pTableMetaInfo, (pQueryInfo->numOfTables + 1) * POINTER_BYTES); - if (tmp == NULL) { - terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; - return NULL; - } - - pQueryInfo->pTableMetaInfo = tmp; - STableMetaInfo* pTableMetaInfo = calloc(1, sizeof(STableMetaInfo)); - - if (pTableMetaInfo == NULL) { - terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; - return NULL; - } - - pQueryInfo->pTableMetaInfo[pQueryInfo->numOfTables] = pTableMetaInfo; - - if (name != NULL) { - tNameAssign(&pTableMetaInfo->name, name); - } - - pTableMetaInfo->pTableMeta = pTableMeta; - - if (vgroupList != NULL) { -// pTableMetaInfo->vgroupList = vgroupInfoClone(vgroupList); - } - - // TODO handle malloc failure - pTableMetaInfo->tagColList = taosArrayInit(4, POINTER_BYTES); - if (pTableMetaInfo->tagColList == NULL) { - return NULL; - } - - if (pTagCols != NULL && pTableMetaInfo->pTableMeta != NULL) { - columnListCopy(pTableMetaInfo->tagColList, pTagCols, pTableMetaInfo->pTableMeta->uid); - } - - pQueryInfo->numOfTables += 1; - return pTableMetaInfo; -} - -STableMetaInfo* addEmptyMetaInfo(SQueryStmtInfo* pQueryInfo) { - return addTableMetaInfo(pQueryInfo, NULL, NULL, NULL, NULL, NULL); -} - -SInternalField* getInternalFieldInfo(SFieldInfo* pFieldInfo, int32_t index) { - assert(index < pFieldInfo->numOfOutput); - return TARRAY_GET_ELEM(pFieldInfo->internalField, index); -} - -int32_t getNumOfInternalField(SFieldInfo* pFieldInfo) { - return (int32_t) taosArrayGetSize(pFieldInfo->internalField); -} - -static void doSetSqlExprAndResultFieldInfo(SQueryStmtInfo* pNewQueryInfo, int64_t uid) { - int32_t numOfOutput = (int32_t)getNumOfExprs(pNewQueryInfo); - if (numOfOutput == 0) { - return; - } - - // set the field info in pNewQueryInfo object according to sqlExpr information - for (int32_t i = 0; i < numOfOutput; ++i) { - SExprInfo* pExpr = getExprInfo(pNewQueryInfo, i); - - TAOS_FIELD f = createField(&pExpr->base.resSchema); - SInternalField* pInfo1 = appendFieldInfo(&pNewQueryInfo->fieldsInfo, &f); - pInfo1->pExpr = pExpr; - } - - // update the pSqlExpr pointer in SInternalField according the field name - // make sure the pSqlExpr point to the correct SqlExpr in pNewQueryInfo, not SqlExpr in pQueryInfo - for (int32_t f = 0; f < pNewQueryInfo->fieldsInfo.numOfOutput; ++f) { - TAOS_FIELD* field = getFieldInfo(&pNewQueryInfo->fieldsInfo, f); - - bool matched = false; - for (int32_t k1 = 0; k1 < numOfOutput; ++k1) { - SExprInfo* pExpr1 = getExprInfo(pNewQueryInfo, k1); - - if (strcmp(field->name, pExpr1->base.resSchema.name) == 0) { // establish link according to the result field name - SInternalField* pInfo = getInternalFieldInfo(&pNewQueryInfo->fieldsInfo, f); - pInfo->pExpr = pExpr1; - - matched = true; - break; - } - } - - assert(matched); - (void)matched; - } - -// updateFieldInfoOffset(pNewQueryInfo); -} - -int16_t getJoinTagColIdByUid(STagCond* pTagCond, uint64_t uid) { - int32_t i = 0; - while (i < TSDB_MAX_JOIN_TABLE_NUM) { - SJoinNode* node = pTagCond->joinInfo.joinTables[i]; - if (node && node->uid == uid) { - return node->tagColId; - } - - i++; - } - - assert(0); - return -1; -} - -int16_t getTagColIndexById(STableMeta* pTableMeta, int16_t colId) { - int32_t numOfTags = getNumOfTags(pTableMeta); - - SSchema* pSchema = getTableTagSchema(pTableMeta); - for(int32_t i = 0; i < numOfTags; ++i) { - if (pSchema[i].colId == colId) { - return i; - } - } - - // can not reach here - assert(0); - return INT16_MIN; -} - -bool isQueryWithLimit(SQueryStmtInfo* pQueryInfo) { - while(pQueryInfo != NULL) { - if (pQueryInfo->limit.limit > 0) { - return true; - } - - pQueryInfo = pQueryInfo->sibling; - } - - return false; -} - -void* vgroupInfoClear(SVgroupsInfo *vgroupList) { - if (vgroupList == NULL) { - return NULL; - } - - tfree(vgroupList); - return NULL; -} - -int32_t copyTagData(STagData* dst, const STagData* src) { - dst->dataLen = src->dataLen; - tstrncpy(dst->name, src->name, tListLen(dst->name)); - - if (dst->dataLen > 0) { - dst->data = malloc(dst->dataLen); - if (dst->data == NULL) { - return -1; - } - - memcpy(dst->data, src->data, dst->dataLen); - } - - return 0; -} - - -uint32_t getTableMetaSize(const STableMeta* pTableMeta) { +STableComInfo getTableInfo(const STableMeta* pTableMeta) { assert(pTableMeta != NULL); - - int32_t totalCols = 0; - if (pTableMeta->tableInfo.numOfColumns >= 0) { - totalCols = pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags; - } - - return sizeof(STableMeta) + totalCols * sizeof(SSchema); -} - -uint32_t getTableMetaMaxSize() { - return sizeof(STableMeta) + TSDB_MAX_COLUMNS * sizeof(SSchema); -} - -STableMeta* tableMetaDup(const STableMeta* pTableMeta) { - assert(pTableMeta != NULL); - size_t size = getTableMetaSize(pTableMeta); - - STableMeta* p = malloc(size); - memcpy(p, pTableMeta, size); - return p; -} - -int32_t getNumOfOutput(SFieldInfo* pFieldInfo) { - return pFieldInfo->numOfOutput; -} - -int32_t getColFilterSerializeLen(SQueryStmtInfo* pQueryInfo) { - int16_t numOfCols = (int16_t)taosArrayGetSize(pQueryInfo->colList); - int32_t len = 0; - - for(int32_t i = 0; i < numOfCols; ++i) { - SColumn* pCol = taosArrayGetP(pQueryInfo->colList, i); - for (int32_t j = 0; j < pCol->info.flist.numOfFilters; ++j) { - len += sizeof(SColumnFilterInfo); - if (pCol->info.flist.filterInfo[j].filterstr) { - len += (int32_t)pCol->info.flist.filterInfo[j].len + 1 * TSDB_NCHAR_SIZE; - } - } - } - return len; -} - -int32_t getTagFilterSerializeLen(SQueryStmtInfo* pQueryInfo) { - // serialize tag column query condition - if (pQueryInfo->tagCond.pCond != NULL && taosArrayGetSize(pQueryInfo->tagCond.pCond) > 0) { - STagCond* pTagCond = &pQueryInfo->tagCond; - - STableMetaInfo *pTableMetaInfo = getMetaInfo(pQueryInfo, 0); - STableMeta * pTableMeta = pTableMetaInfo->pTableMeta; - SCond *pCond = getSTableQueryCond(pTagCond, pTableMeta->uid); - if (pCond != NULL && pCond->cond != NULL) { - return pCond->len; - } - } - return 0; -} - -uint32_t convertRelationalOperator(SToken *pToken) { - switch (pToken->type) { - case TK_LT: - return OP_TYPE_LOWER_THAN; - case TK_LE: - return OP_TYPE_LOWER_EQUAL; - case TK_GT: - return OP_TYPE_GREATER_THAN; - case TK_GE: - return OP_TYPE_GREATER_EQUAL; - case TK_NE: - return OP_TYPE_NOT_EQUAL; - case TK_AND: - return LOGIC_COND_TYPE_AND; - case TK_OR: - return LOGIC_COND_TYPE_OR; - case TK_EQ: - return OP_TYPE_EQUAL; - - case TK_PLUS: - return OP_TYPE_ADD; - case TK_MINUS: - return OP_TYPE_SUB; - case TK_STAR: - return OP_TYPE_MULTI; - case TK_SLASH: - case TK_DIVIDE: - return OP_TYPE_DIV; - case TK_REM: - return OP_TYPE_MOD; - case TK_LIKE: - return OP_TYPE_LIKE; - case TK_MATCH: - return OP_TYPE_MATCH; - case TK_NMATCH: - return OP_TYPE_NMATCH; - case TK_ISNULL: - return OP_TYPE_IS_NULL; - case TK_NOTNULL: - return OP_TYPE_IS_NOT_NULL; - case TK_IN: - return OP_TYPE_IN; - default: { return 0; } - } -} - -bool isDclSqlStatement(SSqlInfo* pSqlInfo) { - int32_t type = pSqlInfo->type; - return (type == TSDB_SQL_CREATE_USER || type == TSDB_SQL_CREATE_ACCT || type == TSDB_SQL_DROP_USER || - type == TSDB_SQL_DROP_ACCT || type == TSDB_SQL_SHOW); -} - -bool isDdlSqlStatement(SSqlInfo* pSqlInfo) { - int32_t type = pSqlInfo->type; - return (type == TSDB_SQL_CREATE_TABLE || type == TSDB_SQL_CREATE_DB || type == TSDB_SQL_DROP_DB); -} - -bool isDqlSqlStatement(SSqlInfo* pSqlInfo) { - return pSqlInfo->type == TSDB_SQL_SELECT; -} - -static uint8_t TRUE_VALUE = (uint8_t)TSDB_TRUE; -static uint8_t FALSE_VALUE = (uint8_t)TSDB_FALSE; - -static FORCE_INLINE int32_t toDouble(SToken *pToken, double *value, char **endPtr) { - errno = 0; - *value = strtold(pToken->z, endPtr); - - // not a valid integer number, return error - if ((*endPtr - pToken->z) != pToken->n) { - return TK_ILLEGAL; - } - - return pToken->type; -} - -static bool isNullStr(SToken *pToken) { - return (pToken->type == TK_NULL) || ((pToken->type == TK_STRING) && (pToken->n != 0) && - (strncasecmp(TSDB_DATA_NULL_STR_L, pToken->z, pToken->n) == 0)); -} - -static FORCE_INLINE int32_t checkAndTrimValue(SToken* pToken, uint32_t type, char* tmpTokenBuf, SMsgBuf* pMsgBuf) { - if ((pToken->type != TK_NOW && pToken->type != TK_INTEGER && pToken->type != TK_STRING && pToken->type != TK_FLOAT && pToken->type != TK_BOOL && - pToken->type != TK_NULL && pToken->type != TK_HEX && pToken->type != TK_OCT && pToken->type != TK_BIN) || - (pToken->n == 0) || (pToken->type == TK_RP)) { - return buildSyntaxErrMsg(pMsgBuf, "invalid data or symbol", pToken->z); - } - - if (IS_NUMERIC_TYPE(type) && pToken->n == 0) { - return buildSyntaxErrMsg(pMsgBuf, "invalid numeric data", pToken->z); - } - - // Remove quotation marks - if (TSDB_DATA_TYPE_BINARY == type) { - if (pToken->n >= TSDB_MAX_BYTES_PER_ROW) { - return buildSyntaxErrMsg(pMsgBuf, "too long string", pToken->z); - } - - // delete escape character: \\, \', \" - char delim = pToken->z[0]; - int32_t cnt = 0; - int32_t j = 0; - for (uint32_t k = 1; k < pToken->n - 1; ++k) { - if (pToken->z[k] == '\\' || (pToken->z[k] == delim && pToken->z[k + 1] == delim)) { - tmpTokenBuf[j] = pToken->z[k + 1]; - cnt++; - j++; - k++; - continue; - } - tmpTokenBuf[j] = pToken->z[k]; - j++; - } - - tmpTokenBuf[j] = 0; - pToken->z = tmpTokenBuf; - pToken->n -= 2 + cnt; - } - - return TSDB_CODE_SUCCESS; -} - -static int parseTime(char **end, SToken *pToken, int16_t timePrec, int64_t *time, SMsgBuf* pMsgBuf) { - int32_t index = 0; - SToken sToken; - int64_t interval; - int64_t ts = 0; - char* pTokenEnd = *end; - - if (pToken->type == TK_NOW) { - ts = taosGetTimestamp(timePrec); - } else if (pToken->type == TK_INTEGER) { - bool isSigned = false; - toInteger(pToken->z, pToken->n, 10, &ts, &isSigned); - } else { // parse the RFC-3339/ISO-8601 timestamp format string - if (taosParseTime(pToken->z, time, pToken->n, timePrec, tsDaylight) != TSDB_CODE_SUCCESS) { - return buildSyntaxErrMsg(pMsgBuf, "invalid timestamp format", pToken->z); - } - - return TSDB_CODE_SUCCESS; - } - - for (int k = pToken->n; pToken->z[k] != '\0'; k++) { - if (pToken->z[k] == ' ' || pToken->z[k] == '\t') continue; - if (pToken->z[k] == ',') { - *end = pTokenEnd; - *time = ts; - return 0; - } - - break; - } - - /* - * time expression: - * e.g., now+12a, now-5h - */ - SToken valueToken; - index = 0; - sToken = tStrGetToken(pTokenEnd, &index, false); - pTokenEnd += index; - - if (sToken.type == TK_MINUS || sToken.type == TK_PLUS) { - index = 0; - valueToken = tStrGetToken(pTokenEnd, &index, false); - pTokenEnd += index; - - if (valueToken.n < 2) { - return buildSyntaxErrMsg(pMsgBuf, "value expected in timestamp", sToken.z); - } - - char unit = 0; - if (parseAbsoluteDuration(valueToken.z, valueToken.n, &interval, &unit, timePrec) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if (sToken.type == TK_PLUS) { - ts += interval; - } else { - ts = ts - interval; - } - - *end = pTokenEnd; - } - - *time = ts; - return TSDB_CODE_SUCCESS; -} - -int32_t parseValueToken(char** end, SToken* pToken, SSchema* pSchema, int16_t timePrec, char* tmpTokenBuf, _row_append_fn_t func, void* param, SMsgBuf* pMsgBuf) { - int64_t iv; - char *endptr = NULL; - bool isSigned = false; - - int32_t code = checkAndTrimValue(pToken, pSchema->type, tmpTokenBuf, pMsgBuf); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - if (isNullStr(pToken)) { - if (TSDB_DATA_TYPE_TIMESTAMP == pSchema->type && PRIMARYKEY_TIMESTAMP_COL_ID == pSchema->colId) { - int64_t tmpVal = 0; - return func(&tmpVal, pSchema->bytes, param); - } - - return func(getNullValue(pSchema->type), 0, param); - } - - switch (pSchema->type) { - case TSDB_DATA_TYPE_BOOL: { - if ((pToken->type == TK_BOOL || pToken->type == TK_STRING) && (pToken->n != 0)) { - if (strncmp(pToken->z, "true", pToken->n) == 0) { - return func(&TRUE_VALUE, pSchema->bytes, param); - } else if (strncmp(pToken->z, "false", pToken->n) == 0) { - return func(&FALSE_VALUE, pSchema->bytes, param); - } else { - return buildSyntaxErrMsg(pMsgBuf, "invalid bool data", pToken->z); - } - } else if (pToken->type == TK_INTEGER) { - return func(((strtoll(pToken->z, NULL, 10) == 0) ? &FALSE_VALUE : &TRUE_VALUE), pSchema->bytes, param); - } else if (pToken->type == TK_FLOAT) { - return func(((strtod(pToken->z, NULL) == 0) ? &FALSE_VALUE : &TRUE_VALUE), pSchema->bytes, param); - } else { - return buildSyntaxErrMsg(pMsgBuf, "invalid bool data", pToken->z); - } - } - - case TSDB_DATA_TYPE_TINYINT: { - if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { - return buildSyntaxErrMsg(pMsgBuf, "invalid tinyint data", pToken->z); - } else if (!IS_VALID_TINYINT(iv)) { - return buildSyntaxErrMsg(pMsgBuf, "tinyint data overflow", pToken->z); - } - - uint8_t tmpVal = (uint8_t)iv; - return func(&tmpVal, pSchema->bytes, param); - } - - case TSDB_DATA_TYPE_UTINYINT:{ - if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { - return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned tinyint data", pToken->z); - } else if (!IS_VALID_UTINYINT(iv)) { - return buildSyntaxErrMsg(pMsgBuf, "unsigned tinyint data overflow", pToken->z); - } - uint8_t tmpVal = (uint8_t)iv; - return func(&tmpVal, pSchema->bytes, param); - } - - case TSDB_DATA_TYPE_SMALLINT: { - if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { - return buildSyntaxErrMsg(pMsgBuf, "invalid smallint data", pToken->z); - } else if (!IS_VALID_SMALLINT(iv)) { - return buildSyntaxErrMsg(pMsgBuf, "smallint data overflow", pToken->z); - } - int16_t tmpVal = (int16_t)iv; - return func(&tmpVal, pSchema->bytes, param); - } - - case TSDB_DATA_TYPE_USMALLINT: { - if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { - return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned smallint data", pToken->z); - } else if (!IS_VALID_USMALLINT(iv)) { - return buildSyntaxErrMsg(pMsgBuf, "unsigned smallint data overflow", pToken->z); - } - uint16_t tmpVal = (uint16_t)iv; - return func(&tmpVal, pSchema->bytes, param); - } - - case TSDB_DATA_TYPE_INT: { - if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { - return buildSyntaxErrMsg(pMsgBuf, "invalid int data", pToken->z); - } else if (!IS_VALID_INT(iv)) { - return buildSyntaxErrMsg(pMsgBuf, "int data overflow", pToken->z); - } - int32_t tmpVal = (int32_t)iv; - return func(&tmpVal, pSchema->bytes, param); - } - - case TSDB_DATA_TYPE_UINT: { - if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { - return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned int data", pToken->z); - } else if (!IS_VALID_UINT(iv)) { - return buildSyntaxErrMsg(pMsgBuf, "unsigned int data overflow", pToken->z); - } - uint32_t tmpVal = (uint32_t)iv; - return func(&tmpVal, pSchema->bytes, param); - } - - case TSDB_DATA_TYPE_BIGINT: { - if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { - return buildSyntaxErrMsg(pMsgBuf, "invalid bigint data", pToken->z); - } else if (!IS_VALID_BIGINT(iv)) { - return buildSyntaxErrMsg(pMsgBuf, "bigint data overflow", pToken->z); - } - return func(&iv, pSchema->bytes, param); - } - - case TSDB_DATA_TYPE_UBIGINT: { - if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv, &isSigned)) { - return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned bigint data", pToken->z); - } else if (!IS_VALID_UBIGINT((uint64_t)iv)) { - return buildSyntaxErrMsg(pMsgBuf, "unsigned bigint data overflow", pToken->z); - } - uint64_t tmpVal = (uint64_t)iv; - return func(&tmpVal, pSchema->bytes, param); - } - - case TSDB_DATA_TYPE_FLOAT: { - double dv; - if (TK_ILLEGAL == toDouble(pToken, &dv, &endptr)) { - return buildSyntaxErrMsg(pMsgBuf, "illegal float data", pToken->z); - } - if (((dv == HUGE_VAL || dv == -HUGE_VAL) && errno == ERANGE) || dv > FLT_MAX || dv < -FLT_MAX || isinf(dv) || isnan(dv)) { - return buildSyntaxErrMsg(pMsgBuf, "illegal float data", pToken->z); - } - float tmpVal = (float)dv; - return func(&tmpVal, pSchema->bytes, param); - } - - case TSDB_DATA_TYPE_DOUBLE: { - double dv; - if (TK_ILLEGAL == toDouble(pToken, &dv, &endptr)) { - return buildSyntaxErrMsg(pMsgBuf, "illegal double data", pToken->z); - } - if (((dv == HUGE_VAL || dv == -HUGE_VAL) && errno == ERANGE) || isinf(dv) || isnan(dv)) { - return buildSyntaxErrMsg(pMsgBuf, "illegal double data", pToken->z); - } - return func(&dv, pSchema->bytes, param); - } - - case TSDB_DATA_TYPE_BINARY: { - // Too long values will raise the invalid sql error message - if (pToken->n + VARSTR_HEADER_SIZE > pSchema->bytes) { - return buildSyntaxErrMsg(pMsgBuf, "string data overflow", pToken->z); - } - - return func(pToken->z, pToken->n, param); - } - - case TSDB_DATA_TYPE_NCHAR: { - return func(pToken->z, pToken->n, param); - } - - case TSDB_DATA_TYPE_TIMESTAMP: { - int64_t tmpVal; - if (parseTime(end, pToken, timePrec, &tmpVal, pMsgBuf) != TSDB_CODE_SUCCESS) { - return buildSyntaxErrMsg(pMsgBuf, "invalid timestamp", pToken->z); - } - - return func(&tmpVal, pSchema->bytes, param); - } - } - - return TSDB_CODE_FAILED; -} - -int32_t KvRowAppend(const void *value, int32_t len, void *param) { - SKvParam* pa = (SKvParam*) param; - - int32_t type = pa->schema->type; - int32_t colId = pa->schema->colId; - - if (TSDB_DATA_TYPE_BINARY == type) { - STR_WITH_SIZE_TO_VARSTR(pa->buf, value, len); - tdAddColToKVRow(pa->builder, colId, type, pa->buf); - } else if (TSDB_DATA_TYPE_NCHAR == type) { - // if the converted output len is over than pColumnModel->bytes, return error: 'Argument list too long' - int32_t output = 0; - if (!taosMbsToUcs4(value, len, varDataVal(pa->buf), pa->schema->bytes - VARSTR_HEADER_SIZE, &output)) { - return TSDB_CODE_TSC_SQL_SYNTAX_ERROR; - } - - varDataSetLen(pa->buf, output); - tdAddColToKVRow(pa->builder, colId, type, pa->buf); - } else { - tdAddColToKVRow(pa->builder, colId, type, value); - } - - return TSDB_CODE_SUCCESS; -} - -int32_t createSName(SName* pName, SToken* pTableName, SParseContext* pParseCtx, SMsgBuf* pMsgBuf) { - const char* msg1 = "name too long"; - const char* msg2 = "invalid database name"; - const char* msg3 = "db is not specified"; - - int32_t code = TSDB_CODE_SUCCESS; - char* p = strnchr(pTableName->z, TS_PATH_DELIMITER[0], pTableName->n, true); - - if (p != NULL) { // db has been specified in sql string so we ignore current db path - assert(*p == TS_PATH_DELIMITER[0]); - - int32_t dbLen = p - pTableName->z; - char name[TSDB_DB_FNAME_LEN] = {0}; - strncpy(name, pTableName->z, dbLen); - dbLen = strdequote(name); - - code = tNameSetDbName(pName, pParseCtx->acctId, name, dbLen); - if (code != TSDB_CODE_SUCCESS) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - int32_t tbLen = pTableName->n - dbLen - 1; - char tbname[TSDB_TABLE_FNAME_LEN] = {0}; - strncpy(tbname, p + 1, tbLen); - /*tbLen = */strdequote(tbname); - - code = tNameFromString(pName, tbname, T_NAME_TABLE); - if (code != 0) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - } else { // get current DB name first, and then set it into path - if (pTableName->n >= TSDB_TABLE_NAME_LEN) { - return buildInvalidOperationMsg(pMsgBuf, msg1); - } - - assert(pTableName->n < TSDB_TABLE_FNAME_LEN); - - char name[TSDB_TABLE_FNAME_LEN] = {0}; - strncpy(name, pTableName->z, pTableName->n); - strdequote(name); - - if (pParseCtx->db == NULL) { - return buildInvalidOperationMsg(pMsgBuf, msg3); - } - - code = tNameSetDbName(pName, pParseCtx->acctId, pParseCtx->db, strlen(pParseCtx->db)); - if (code != TSDB_CODE_SUCCESS) { - code = buildInvalidOperationMsg(pMsgBuf, msg2); - return code; - } - - code = tNameFromString(pName, name, T_NAME_TABLE); - if (code != 0) { - code = buildInvalidOperationMsg(pMsgBuf, msg1); - } - } - - return code; + return pTableMeta->tableInfo; } diff --git a/source/libs/parser/src/queryInfoUtil.c b/source/libs/parser/src/queryInfoUtil.c deleted file mode 100644 index 9a2ca2da98..0000000000 --- a/source/libs/parser/src/queryInfoUtil.c +++ /dev/null @@ -1,377 +0,0 @@ -#include "os.h" -#include "queryInfoUtil.h" -#include "function.h" -#include "parser.h" -#include "parserUtil.h" - -static struct SSchema _s = { - .colId = TSDB_TBNAME_COLUMN_INDEX, - .type = TSDB_DATA_TYPE_BINARY, - .bytes = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE, - .name = "tbname", -}; - -SSchema* getTbnameColumnSchema() { - return &_s; -} - -SArray* getCurrentExprList(SQueryStmtInfo* pQueryInfo) { - assert(pQueryInfo != NULL && pQueryInfo->exprListLevelIndex >= 0 && pQueryInfo->exprListLevelIndex < 10); - return pQueryInfo->exprList[pQueryInfo->exprListLevelIndex]; -} - -size_t getNumOfExprs(SQueryStmtInfo* pQueryInfo) { - SArray* pExprList = getCurrentExprList(pQueryInfo); - return taosArrayGetSize(pExprList); -} - -SSchema* getOneColumnSchema(const STableMeta* pTableMeta, int32_t colIndex) { - assert(pTableMeta != NULL && pTableMeta->schema != NULL && colIndex >= 0 && colIndex < (getNumOfColumns(pTableMeta) + getNumOfTags(pTableMeta))); - - SSchema* pSchema = (SSchema*) pTableMeta->schema; - return &pSchema[colIndex]; -} - -STableComInfo getTableInfo(const STableMeta* pTableMeta) { - assert(pTableMeta != NULL); - return pTableMeta->tableInfo; -} - -int32_t getNumOfColumns(const STableMeta* pTableMeta) { - assert(pTableMeta != NULL); - // table created according to super table, use data from super table - return getTableInfo(pTableMeta).numOfColumns; -} - -int32_t getNumOfTags(const STableMeta* pTableMeta) { - assert(pTableMeta != NULL); - return getTableInfo(pTableMeta).numOfTags; -} - -SSchema *getTableColumnSchema(const STableMeta *pTableMeta) { - assert(pTableMeta != NULL); - return (SSchema*) pTableMeta->schema; -} - -SSchema* getTableTagSchema(const STableMeta* pTableMeta) { - assert(pTableMeta != NULL && (pTableMeta->tableType == TSDB_SUPER_TABLE || pTableMeta->tableType == TSDB_CHILD_TABLE)); - return getOneColumnSchema(pTableMeta, getTableInfo(pTableMeta).numOfColumns); -} - -static tExprNode* createFunctionExprNode(const char* funcName, struct SSourceParam *pParam) { - tExprNode** p = malloc(pParam->num * POINTER_BYTES); - - if (pParam->pColumnList != NULL) { - for(int32_t i = 0; i < pParam->num; ++i) { - p[i] = calloc(1, sizeof(tExprNode)); - p[i]->nodeType = TEXPR_COL_NODE; - - SColumn* pSrc = taosArrayGetP(pParam->pColumnList, i); - SSchema* pSchema = calloc(1, sizeof(SSchema)); - - tstrncpy(pSchema->name, pSrc->name, tListLen(pSchema->name)); - pSchema->type = pSrc->info.type; - pSchema->bytes = pSrc->info.bytes; - pSchema->colId = pSrc->info.colId; - p[i]->pSchema = pSchema; - } - } else { - assert(pParam->pColumnList == NULL); - for(int32_t i = 0; i < pParam->num; ++i) { - p[i] = taosArrayGetP(pParam->pExprNodeList, i); - } - } - - tExprNode* pNode = calloc(1, sizeof(tExprNode)); - - pNode->nodeType = TEXPR_FUNCTION_NODE; - tstrncpy(pNode->_function.functionName, funcName, tListLen(pNode->_function.functionName)); - pNode->_function.pChild = p; - pNode->_function.num = pParam->num; - - return pNode; -} - -SExprInfo* createBinaryExprInfo(tExprNode* pNode, SSchema* pResSchema) { - assert(pNode != NULL && pResSchema != NULL); - - SExprInfo* pExpr = calloc(1, sizeof(SExprInfo)); - if (pExpr == NULL) { - return NULL; - } - - pExpr->pExpr = pNode; - memcpy(&pExpr->base.resSchema, pResSchema, sizeof(SSchema)); - return pExpr; -} - -SExprInfo* createExprInfo(STableMetaInfo* pTableMetaInfo, const char* funcName, SSourceParam* pSourceParam, SSchema* pResSchema, int16_t interSize) { - SExprInfo* pExpr = calloc(1, sizeof(SExprInfo)); - if (pExpr == NULL) { - return NULL; - } - - uint64_t uid = 0; - if (pTableMetaInfo->pTableMeta) { - uid = pTableMetaInfo->pTableMeta->uid; - } - - SSqlExpr* p = &pExpr->base; - - p->pColumns = calloc(pSourceParam->num, sizeof(SColumn)); - p->numOfCols = pSourceParam->num; - p->interBytes = interSize; - memcpy(&p->resSchema, pResSchema, sizeof(SSchema)); - - if (pSourceParam->pExprNodeList != NULL) { - pExpr->pExpr = createFunctionExprNode(funcName, pSourceParam); - return pExpr; - } - - SColumn* pCol = taosArrayGetP(pSourceParam->pColumnList, 0); - if (pCol->info.colId == TSDB_TBNAME_COLUMN_INDEX) { - assert(pSourceParam->num == 1); - - SSchema* s = getTbnameColumnSchema(); - setColumn(p->pColumns, uid, pTableMetaInfo->aliasName, TSDB_COL_TAG, s); - - pExpr->pExpr = createFunctionExprNode(funcName, pSourceParam); - } else if (TSDB_COL_IS_UD_COL(pCol->flag) || strcmp(funcName, "block_dist") == 0) { - setColumn(p->pColumns, uid, pTableMetaInfo->aliasName, TSDB_COL_UDC, pResSchema); - pExpr->pExpr = createFunctionExprNode(funcName, pSourceParam); - } else { - for(int32_t i = 0; i < pSourceParam->num; ++i) { - SColumn* c = taosArrayGetP(pSourceParam->pColumnList, i); - p->pColumns[i] = *c; - } - pExpr->pExpr = createFunctionExprNode(funcName, pSourceParam); - } - - return pExpr; -} - -void addExprInfo(SArray* pExprList, int32_t index, SExprInfo* pExprInfo, int32_t level) { - assert(pExprList != NULL ); - - int32_t num = (int32_t) taosArrayGetSize(pExprList); - if (index == num) { - taosArrayPush(pExprList, &pExprInfo); - } else { - taosArrayInsert(pExprList, index, &pExprInfo); - } - -#if 0 - if (pExprInfo->pExpr->nodeType == TEXPR_FUNCTION_NODE) { - printf("add function: %s, level:%d, total:%ld\n", pExprInfo->pExpr->_function.functionName, level, taosArrayGetSize(pExprList)); - } else { - printf("add operator: %s, level:%d, total:%ld\n", pExprInfo->base.resSchema.name, level, taosArrayGetSize(pExprList)); - } -#endif - -} - -void updateExprInfo(SExprInfo* pExprInfo, int16_t functionId, int32_t colId, int16_t srcColumnIndex, int16_t resType, int16_t resSize) { - assert(pExprInfo != NULL); - - SSqlExpr* pse = &pExprInfo->base; - assert(0); - - pse->resSchema.type = resType; - pse->resSchema.bytes = resSize; -} - -SExprInfo* getExprInfo(SQueryStmtInfo* pQueryInfo, int32_t index) { - assert(pQueryInfo != NULL && pQueryInfo->exprList && index >= 0); - return taosArrayGetP(getCurrentExprList(pQueryInfo), index); -} - -void destroyExprInfo(SExprInfo* pExprInfo) { - tExprTreeDestroy(pExprInfo->pExpr, NULL); - - for(int32_t i = 0; i < pExprInfo->base.numOfParams; ++i) { - taosVariantDestroy(&pExprInfo->base.param[i]); - } - - tfree(pExprInfo->base.pColumns); - tfree(pExprInfo); -} - -void dropOneLevelExprInfo(SArray* pExprInfo) { - size_t size = taosArrayGetSize(pExprInfo); - - for (int32_t i = 0; i < size; ++i) { - SExprInfo* pExpr = taosArrayGetP(pExprInfo, i); - destroyExprInfo(pExpr); - } - - taosArrayDestroy(pExprInfo); -} - -void dropAllExprInfo(SArray** pExprInfo, int32_t numOfLevel) { - for(int32_t i = 0; i < numOfLevel; ++i) { - dropOneLevelExprInfo(pExprInfo[i]); - } -} - -void addExprInfoParam(SSqlExpr* pExpr, char* argument, int32_t type, int32_t bytes) { - assert (pExpr != NULL || argument != NULL || bytes != 0); - - // set parameter value - // transfer to tVariant from byte data/no ascii data - taosVariantCreateFromBinary(&pExpr->param[pExpr->numOfParams], argument, bytes, type); - pExpr->numOfParams += 1; - - assert(pExpr->numOfParams <= 3); -} - -int32_t getExprFunctionId(SExprInfo *pExprInfo) { - assert(pExprInfo != NULL && pExprInfo->pExpr != NULL && pExprInfo->pExpr->nodeType == TEXPR_FUNCTION_NODE); - return 0; -} - -void assignExprInfo(SExprInfo* dst, const SExprInfo* src) { - assert(dst != NULL && src != NULL); - - *dst = *src; -#if 0 - if (src->base.flist.numOfFilters > 0) { - dst->base.flist.filterInfo = calloc(src->base.flist.numOfFilters, sizeof(SColumnFilterInfo)); - memcpy(dst->base.flist.filterInfo, src->base.flist.filterInfo, sizeof(SColumnFilterInfo) * src->base.flist.numOfFilters); - } -#endif - - dst->pExpr = exprdup(src->pExpr); - dst->base.pColumns = calloc(src->base.numOfCols, sizeof(SColumn)); - memcpy(dst->base.pColumns, src->base.pColumns, sizeof(SColumn) * src->base.numOfCols); - - memset(dst->base.param, 0, sizeof(SVariant) * tListLen(dst->base.param)); - for (int32_t j = 0; j < src->base.numOfParams; ++j) { - taosVariantAssign(&dst->base.param[j], &src->base.param[j]); - } -} - -int32_t copyExprInfoList(SArray* dst, const SArray* src, uint64_t uid, bool deepcopy) { - assert(src != NULL && dst != NULL); - - size_t size = taosArrayGetSize(src); - for (int32_t i = 0; i < size; ++i) { - SExprInfo* pExpr = taosArrayGetP(src, i); - uint64_t exprUid = pExpr->base.pColumns->uid; - - if (exprUid == uid) { - if (deepcopy) { - SExprInfo* p1 = calloc(1, sizeof(SExprInfo)); - assignExprInfo(p1, pExpr); - - taosArrayPush(dst, &p1); - } else { - taosArrayPush(dst, &pExpr); - } - } - } - - return 0; -} - -int32_t copyAllExprInfo(SArray* dst, const SArray* src, bool deepcopy) { - assert(src != NULL && dst != NULL); - - size_t size = taosArrayGetSize(src); - for (int32_t i = 0; i < size; ++i) { - SExprInfo* pExpr = taosArrayGetP(src, i); - - SExprInfo* p1 = calloc(1, sizeof(SExprInfo)); - assignExprInfo(p1, pExpr); - taosArrayPush(dst, &p1); - } - - return 0; -} - -//void* tSqlExprDestroy(SExprInfo* pExpr) { -// if (pExpr == NULL) { -// return NULL; -// } -// -// SSqlExpr* p = &pExpr->base; -// for(int32_t i = 0; i < tListLen(p->param); ++i) { -// taosVariantDestroy(&p->param[i]); -// } -// -// if (p->flist.numOfFilters > 0) { -// tfree(p->flist.filterInfo); -// } -// -// if (pExpr->pExpr != NULL) { -// tExprTreeDestroy(pExpr->pExpr, NULL); -// } -// -// tfree(pExpr); -// return NULL; -//} - -int32_t getResRowLength(SArray* pExprList) { - size_t num = taosArrayGetSize(pExprList); - if (num == 0) { - return 0; - } - - int32_t size = 0; - for(int32_t i = 0; i < num; ++i) { - SExprInfo* pExpr = taosArrayGetP(pExprList, i); - size += pExpr->base.resSchema.bytes; - } - - return size; -} - -SArray* extractFunctionList(SArray* pExprInfoList) { - assert(pExprInfoList != NULL); - - size_t len = taosArrayGetSize(pExprInfoList); - SArray* p = taosArrayInit(len, POINTER_BYTES); - - for(int32_t i = 0; i < len; ++i) { - SExprInfo* pExprInfo = taosArrayGetP(pExprInfoList, i); - if (pExprInfo->pExpr->nodeType == TEXPR_FUNCTION_NODE) { - char* name = strdup(pExprInfo->pExpr->_function.functionName); - taosArrayPush(p, &name); - } else { - char* name = strdup("project"); - taosArrayPush(p, &name); - } - } - - return p; -} - -bool tscHasColumnFilter(SQueryStmtInfo* pQueryInfo) { - // filter on primary timestamp column - if (pQueryInfo->window.skey != INT64_MIN || pQueryInfo->window.ekey != INT64_MAX) { - return true; - } - - size_t size = taosArrayGetSize(pQueryInfo->colList); - for (int32_t i = 0; i < size; ++i) { - SColumn* pCol = taosArrayGetP(pQueryInfo->colList, i); - if (pCol->info.flist.numOfFilters > 0) { - return true; - } - } - - return false; -} - -int32_t getExprFunctionLevel(const SQueryStmtInfo* pQueryInfo) { - int32_t n = 10; - - int32_t level = 0; - for(int32_t i = 0; i < n; ++i) { - SArray* pList = pQueryInfo->exprList[i]; - if (taosArrayGetSize(pList) > 0) { - level += 1; - } - } - - return level; -} \ No newline at end of file diff --git a/source/libs/parser/src/sql.c b/source/libs/parser/src/sql.c deleted file mode 100644 index ed96737837..0000000000 --- a/source/libs/parser/src/sql.c +++ /dev/null @@ -1,3698 +0,0 @@ -/* -** 2000-05-29 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** Driver template for the LEMON parser generator. -** -** The "lemon" program processes an LALR(1) input grammar file, then uses -** this template to construct a parser. The "lemon" program inserts text -** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the -** interstitial "-" characters) contained in this template is changed into -** the value of the %name directive from the grammar. Otherwise, the content -** of this template is copied straight through into the generate parser -** source file. -** -** The following is the concatenation of all %include directives from the -** input grammar file: -*/ -#include -#include -/************ Begin %include sections from the grammar ************************/ - -#include -#include -#include -#include -#include -#include "astGenerator.h" -#include "tmsgtype.h" -#include "ttoken.h" -#include "ttokendef.h" -#include "tvariant.h" -#include "parserInt.h" -/**************** End of %include directives **********************************/ -/* These constants specify the various numeric values for terminal symbols -** in a format understandable to "makeheaders". This section is blank unless -** "lemon" is run with the "-m" command-line option. -***************** Begin makeheaders token definitions *************************/ -/**************** End makeheaders token definitions ***************************/ - -/* The next sections is a series of control #defines. -** various aspects of the generated parser. -** YYCODETYPE is the data type used to store the integer codes -** that represent terminal and non-terminal symbols. -** "unsigned char" is used if there are fewer than -** 256 symbols. Larger types otherwise. -** YYNOCODE is a number of type YYCODETYPE that is not used for -** any terminal or nonterminal symbol. -** YYFALLBACK If defined, this indicates that one or more tokens -** (also known as: "terminal symbols") have fall-back -** values which should be used if the original symbol -** would not parse. This permits keywords to sometimes -** be used as identifiers, for example. -** YYACTIONTYPE is the data type used for "action codes" - numbers -** that indicate what to do in response to the next -** token. -** ParseTOKENTYPE is the data type used for minor type for terminal -** symbols. Background: A "minor type" is a semantic -** value associated with a terminal or non-terminal -** symbols. For example, for an "ID" terminal symbol, -** the minor type might be the name of the identifier. -** Each non-terminal can have a different minor type. -** Terminal symbols all have the same minor type, though. -** This macros defines the minor type for terminal -** symbols. -** YYMINORTYPE is the data type used for all minor types. -** This is typically a union of many types, one of -** which is ParseTOKENTYPE. The entry in the union -** for terminal symbols is called "yy0". -** YYSTACKDEPTH is the maximum depth of the parser's stack. If -** zero the stack is dynamically sized using realloc() -** ParseARG_SDECL A static variable declaration for the %extra_argument -** ParseARG_PDECL A parameter declaration for the %extra_argument -** ParseARG_PARAM Code to pass %extra_argument as a subroutine parameter -** ParseARG_STORE Code to store %extra_argument into yypParser -** ParseARG_FETCH Code to extract %extra_argument from yypParser -** ParseCTX_* As ParseARG_ except for %extra_context -** YYERRORSYMBOL is the code number of the error symbol. If not -** defined, then do no error processing. -** YYNSTATE the combined number of states. -** YYNRULE the number of rules in the grammar -** YYNTOKEN Number of terminal symbols -** YY_MAX_SHIFT Maximum value for shift actions -** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions -** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions -** YY_ERROR_ACTION The yy_action[] code for syntax error -** YY_ACCEPT_ACTION The yy_action[] code for accept -** YY_NO_ACTION The yy_action[] code for no-op -** YY_MIN_REDUCE Minimum value for reduce actions -** YY_MAX_REDUCE Maximum value for reduce actions -*/ -#ifndef INTERFACE -# define INTERFACE 1 -#endif -/************* Begin control #defines *****************************************/ -#define YYCODETYPE unsigned short int -#define YYNOCODE 274 -#define YYACTIONTYPE unsigned short int -#define ParseTOKENTYPE SToken -typedef union { - int yyinit; - ParseTOKENTYPE yy0; - SVariant yy1; - SField yy16; - int yy40; - SIntervalVal yy52; - int64_t yy61; - SSubclause* yy93; - SWindowStateVal yy112; - SRelationInfo* yy160; - SCreatedTableInfo yy184; - SSqlNode* yy185; - SArray* yy225; - tSqlExpr* yy226; - SCreateDbInfo yy326; - int32_t yy460; - SSessionWindowVal yy463; - SCreateTableSql* yy482; - SLimit yy495; - SCreateAcctInfo yy523; -} YYMINORTYPE; -#ifndef YYSTACKDEPTH -#define YYSTACKDEPTH 100 -#endif -#define ParseARG_SDECL SSqlInfo* pInfo; -#define ParseARG_PDECL ,SSqlInfo* pInfo -#define ParseARG_PARAM ,pInfo -#define ParseARG_FETCH SSqlInfo* pInfo=yypParser->pInfo; -#define ParseARG_STORE yypParser->pInfo=pInfo; -#define ParseCTX_SDECL -#define ParseCTX_PDECL -#define ParseCTX_PARAM -#define ParseCTX_FETCH -#define ParseCTX_STORE -#define YYFALLBACK 1 -#define YYNSTATE 368 -#define YYNRULE 304 -#define YYNTOKEN 192 -#define YY_MAX_SHIFT 367 -#define YY_MIN_SHIFTREDUCE 590 -#define YY_MAX_SHIFTREDUCE 893 -#define YY_ERROR_ACTION 894 -#define YY_ACCEPT_ACTION 895 -#define YY_NO_ACTION 896 -#define YY_MIN_REDUCE 897 -#define YY_MAX_REDUCE 1200 -/************* End control #defines *******************************************/ -#define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0]))) - -/* Define the yytestcase() macro to be a no-op if is not already defined -** otherwise. -** -** Applications can choose to define yytestcase() in the %include section -** to a macro that can assist in verifying code coverage. For production -** code the yytestcase() macro should be turned off. But it is useful -** for testing. -*/ -#ifndef yytestcase -# define yytestcase(X) -#endif - - -/* Next are the tables used to determine what action to take based on the -** current state and lookahead token. These tables are used to implement -** functions that take a state number and lookahead value and return an -** action integer. -** -** Suppose the action integer is N. Then the action is determined as -** follows -** -** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead -** token onto the stack and goto state N. -** -** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then -** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE. -** -** N == YY_ERROR_ACTION A syntax error has occurred. -** -** N == YY_ACCEPT_ACTION The parser accepts its input. -** -** N == YY_NO_ACTION No such action. Denotes unused -** slots in the yy_action[] table. -** -** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE -** and YY_MAX_REDUCE -** -** The action table is constructed as a single large table named yy_action[]. -** Given state S and lookahead X, the action is computed as either: -** -** (A) N = yy_action[ yy_shift_ofst[S] + X ] -** (B) N = yy_default[S] -** -** The (A) formula is preferred. The B formula is used instead if -** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X. -** -** The formulas above are for computing the action when the lookahead is -** a terminal symbol. If the lookahead is a non-terminal (as occurs after -** a reduce action) then the yy_reduce_ofst[] array is used in place of -** the yy_shift_ofst[] array. -** -** The following are the tables generated in this section: -** -** yy_action[] A single table containing all actions. -** yy_lookahead[] A table containing the lookahead for each entry in -** yy_action. Used to detect hash collisions. -** yy_shift_ofst[] For each state, the offset into yy_action for -** shifting terminals. -** yy_reduce_ofst[] For each state, the offset into yy_action for -** shifting non-terminals after a reduce. -** yy_default[] Default action for each state. -** -*********** Begin parsing tables **********************************************/ -#define YY_ACTTAB_COUNT (781) -static const YYACTIONTYPE yy_action[] = { - /* 0 */ 91, 641, 242, 1085, 676, 249, 1050, 55, 56, 641, - /* 10 */ 59, 60, 895, 367, 252, 49, 48, 47, 1075, 58, - /* 20 */ 325, 63, 61, 64, 62, 641, 641, 366, 230, 54, - /* 30 */ 53, 206, 248, 52, 51, 50, 233, 55, 56, 246, - /* 40 */ 59, 60, 1176, 1050, 252, 49, 48, 47, 104, 58, - /* 50 */ 325, 63, 61, 64, 62, 1022, 21, 1020, 1021, 54, - /* 60 */ 53, 1075, 1023, 52, 51, 50, 1024, 206, 1025, 1026, - /* 70 */ 280, 279, 1082, 55, 56, 1044, 59, 60, 1177, 274, - /* 80 */ 252, 49, 48, 47, 89, 58, 325, 63, 61, 64, - /* 90 */ 62, 39, 236, 1062, 206, 54, 53, 362, 982, 52, - /* 100 */ 51, 50, 27, 55, 57, 1177, 59, 60, 323, 830, - /* 110 */ 252, 49, 48, 47, 1075, 58, 325, 63, 61, 64, - /* 120 */ 62, 243, 294, 80, 81, 54, 53, 795, 796, 52, - /* 130 */ 51, 50, 234, 116, 56, 232, 59, 60, 311, 1047, - /* 140 */ 252, 49, 48, 47, 104, 58, 325, 63, 61, 64, - /* 150 */ 62, 42, 776, 361, 360, 54, 53, 952, 359, 52, - /* 160 */ 51, 50, 358, 43, 357, 356, 1033, 1034, 30, 1037, - /* 170 */ 253, 42, 319, 361, 360, 318, 317, 316, 359, 315, - /* 180 */ 314, 313, 358, 312, 357, 356, 310, 1014, 1002, 1003, - /* 190 */ 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, - /* 200 */ 1015, 1016, 1017, 1018, 641, 59, 60, 159, 773, 252, - /* 210 */ 49, 48, 47, 113, 58, 325, 63, 61, 64, 62, - /* 220 */ 1124, 355, 292, 355, 54, 53, 836, 839, 52, 51, - /* 230 */ 50, 282, 206, 54, 53, 7, 321, 52, 51, 50, - /* 240 */ 780, 723, 22, 1177, 591, 592, 593, 594, 595, 596, - /* 250 */ 597, 598, 599, 600, 601, 602, 603, 604, 199, 215, - /* 260 */ 231, 251, 845, 834, 837, 840, 216, 345, 344, 198, - /* 270 */ 195, 193, 175, 174, 172, 217, 80, 321, 83, 330, - /* 280 */ 80, 251, 845, 834, 837, 840, 52, 51, 50, 228, - /* 290 */ 229, 121, 78, 326, 63, 61, 64, 62, 759, 756, - /* 300 */ 757, 758, 54, 53, 835, 838, 52, 51, 50, 228, - /* 310 */ 229, 255, 751, 748, 749, 750, 43, 1061, 79, 203, - /* 320 */ 43, 3, 32, 131, 39, 257, 258, 1038, 104, 129, - /* 330 */ 85, 123, 133, 104, 39, 945, 39, 39, 65, 244, - /* 340 */ 245, 158, 273, 39, 86, 39, 843, 746, 747, 305, - /* 350 */ 260, 224, 189, 186, 183, 149, 142, 162, 65, 181, - /* 360 */ 179, 178, 177, 176, 167, 170, 160, 39, 240, 269, - /* 370 */ 39, 88, 1047, 164, 204, 39, 846, 841, 241, 209, - /* 380 */ 334, 335, 1047, 842, 1047, 1047, 812, 336, 39, 337, - /* 390 */ 256, 1047, 254, 1047, 333, 332, 846, 841, 327, 266, - /* 400 */ 12, 261, 39, 842, 1123, 1035, 84, 261, 270, 82, - /* 410 */ 124, 341, 760, 761, 342, 1047, 127, 92, 1047, 343, - /* 420 */ 365, 364, 190, 1047, 844, 93, 752, 753, 71, 262, - /* 430 */ 35, 259, 347, 340, 339, 275, 1047, 119, 955, 261, - /* 440 */ 792, 946, 802, 803, 158, 74, 811, 158, 1048, 832, - /* 450 */ 1046, 40, 733, 297, 744, 745, 97, 70, 66, 24, - /* 460 */ 735, 299, 734, 868, 847, 70, 300, 250, 40, 40, - /* 470 */ 1049, 72, 640, 14, 77, 13, 67, 117, 67, 23, - /* 480 */ 23, 833, 210, 140, 211, 139, 75, 1171, 23, 4, - /* 490 */ 1170, 16, 18, 15, 17, 764, 765, 762, 763, 147, - /* 500 */ 1134, 146, 20, 1169, 19, 849, 169, 168, 226, 722, - /* 510 */ 227, 207, 208, 212, 205, 1196, 213, 214, 219, 220, - /* 520 */ 1188, 221, 1077, 218, 202, 1133, 238, 44, 1130, 1076, - /* 530 */ 277, 1129, 239, 346, 114, 1116, 1115, 324, 196, 271, - /* 540 */ 791, 76, 281, 1045, 235, 276, 73, 87, 1084, 283, - /* 550 */ 285, 1095, 295, 46, 293, 291, 90, 108, 94, 1092, - /* 560 */ 1093, 1097, 95, 1073, 101, 286, 288, 1117, 105, 106, - /* 570 */ 107, 109, 289, 110, 111, 287, 284, 112, 45, 29, - /* 580 */ 306, 115, 225, 958, 1043, 150, 118, 247, 980, 120, - /* 590 */ 301, 959, 302, 303, 304, 348, 307, 308, 200, 349, - /* 600 */ 151, 38, 322, 350, 954, 957, 130, 953, 331, 1195, - /* 610 */ 137, 351, 1194, 352, 1191, 353, 354, 363, 141, 222, - /* 620 */ 338, 223, 1187, 144, 1186, 1183, 893, 148, 979, 265, - /* 630 */ 264, 892, 268, 891, 874, 873, 41, 31, 8, 70, - /* 640 */ 201, 28, 296, 153, 157, 272, 152, 154, 155, 943, - /* 650 */ 156, 163, 941, 165, 166, 939, 938, 263, 1, 981, - /* 660 */ 937, 171, 936, 173, 935, 934, 933, 932, 931, 930, - /* 670 */ 767, 929, 267, 180, 278, 182, 928, 184, 185, 187, - /* 680 */ 927, 926, 924, 188, 922, 919, 793, 96, 98, 920, - /* 690 */ 194, 917, 804, 197, 918, 913, 99, 100, 2, 798, - /* 700 */ 102, 237, 9, 800, 33, 103, 34, 10, 298, 290, - /* 710 */ 11, 25, 26, 119, 122, 126, 654, 693, 309, 692, - /* 720 */ 36, 125, 689, 37, 687, 128, 686, 685, 683, 682, - /* 730 */ 681, 678, 644, 132, 134, 135, 5, 850, 320, 848, - /* 740 */ 6, 329, 328, 68, 69, 136, 138, 143, 725, 40, - /* 750 */ 145, 724, 721, 670, 668, 660, 666, 662, 664, 658, - /* 760 */ 656, 691, 690, 688, 684, 680, 679, 161, 642, 897, - /* 770 */ 896, 608, 896, 896, 896, 896, 896, 896, 896, 191, - /* 780 */ 192, -}; -static const YYCODETYPE yy_lookahead[] = { - /* 0 */ 194, 1, 239, 194, 3, 200, 243, 7, 8, 1, - /* 10 */ 10, 11, 192, 193, 14, 15, 16, 17, 241, 19, - /* 20 */ 20, 21, 22, 23, 24, 1, 1, 194, 195, 29, - /* 30 */ 30, 262, 200, 33, 34, 35, 259, 7, 8, 239, - /* 40 */ 10, 11, 273, 243, 14, 15, 16, 17, 194, 19, - /* 50 */ 20, 21, 22, 23, 24, 216, 262, 218, 219, 29, - /* 60 */ 30, 241, 223, 33, 34, 35, 227, 262, 229, 230, - /* 70 */ 264, 265, 263, 7, 8, 194, 10, 11, 273, 259, - /* 80 */ 14, 15, 16, 17, 84, 19, 20, 21, 22, 23, - /* 90 */ 24, 194, 244, 245, 262, 29, 30, 214, 215, 33, - /* 100 */ 34, 35, 78, 7, 8, 273, 10, 11, 83, 79, - /* 110 */ 14, 15, 16, 17, 241, 19, 20, 21, 22, 23, - /* 120 */ 24, 240, 268, 78, 270, 29, 30, 124, 125, 33, - /* 130 */ 34, 35, 259, 201, 8, 238, 10, 11, 63, 242, - /* 140 */ 14, 15, 16, 17, 194, 19, 20, 21, 22, 23, - /* 150 */ 24, 96, 33, 98, 99, 29, 30, 1, 103, 33, - /* 160 */ 34, 35, 107, 118, 109, 110, 234, 235, 236, 237, - /* 170 */ 200, 96, 97, 98, 99, 100, 101, 102, 103, 104, - /* 180 */ 105, 106, 107, 108, 109, 110, 111, 216, 217, 218, - /* 190 */ 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, - /* 200 */ 229, 230, 231, 232, 1, 10, 11, 74, 95, 14, - /* 210 */ 15, 16, 17, 249, 19, 20, 21, 22, 23, 24, - /* 220 */ 270, 88, 272, 88, 29, 30, 3, 4, 33, 34, - /* 230 */ 35, 267, 262, 29, 30, 122, 80, 33, 34, 35, - /* 240 */ 121, 3, 40, 273, 41, 42, 43, 44, 45, 46, - /* 250 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 57, - /* 260 */ 57, 1, 2, 3, 4, 5, 64, 29, 30, 58, - /* 270 */ 59, 60, 70, 71, 72, 73, 78, 80, 246, 77, - /* 280 */ 78, 1, 2, 3, 4, 5, 33, 34, 35, 29, - /* 290 */ 30, 201, 260, 33, 21, 22, 23, 24, 2, 3, - /* 300 */ 4, 5, 29, 30, 3, 4, 33, 34, 35, 29, - /* 310 */ 30, 64, 2, 3, 4, 5, 118, 245, 120, 262, - /* 320 */ 118, 58, 59, 60, 194, 29, 30, 237, 194, 66, - /* 330 */ 67, 68, 69, 194, 194, 199, 194, 194, 78, 29, - /* 340 */ 30, 205, 140, 194, 142, 194, 123, 3, 4, 86, - /* 350 */ 64, 149, 58, 59, 60, 58, 59, 60, 78, 65, - /* 360 */ 66, 67, 68, 69, 67, 68, 69, 194, 238, 111, - /* 370 */ 194, 201, 242, 76, 262, 194, 116, 117, 238, 262, - /* 380 */ 238, 238, 242, 123, 242, 242, 72, 238, 194, 238, - /* 390 */ 143, 242, 145, 242, 147, 148, 116, 117, 9, 141, - /* 400 */ 78, 194, 194, 123, 270, 235, 84, 194, 150, 270, - /* 410 */ 203, 238, 116, 117, 238, 242, 203, 79, 242, 238, - /* 420 */ 61, 62, 63, 242, 123, 79, 116, 117, 95, 143, - /* 430 */ 78, 145, 238, 147, 148, 79, 242, 115, 199, 194, - /* 440 */ 79, 199, 79, 79, 205, 95, 132, 205, 203, 1, - /* 450 */ 242, 95, 79, 79, 3, 4, 95, 119, 95, 95, - /* 460 */ 79, 79, 79, 79, 79, 119, 114, 56, 95, 95, - /* 470 */ 243, 138, 79, 144, 78, 146, 95, 95, 95, 95, - /* 480 */ 95, 33, 262, 144, 262, 146, 136, 262, 95, 78, - /* 490 */ 262, 144, 144, 146, 146, 3, 4, 3, 4, 144, - /* 500 */ 233, 146, 144, 262, 146, 116, 74, 75, 262, 113, - /* 510 */ 262, 262, 262, 262, 262, 245, 262, 262, 262, 262, - /* 520 */ 245, 262, 241, 262, 262, 233, 233, 261, 233, 241, - /* 530 */ 241, 233, 233, 233, 247, 271, 271, 194, 56, 194, - /* 540 */ 123, 135, 266, 241, 266, 196, 137, 194, 194, 266, - /* 550 */ 266, 194, 130, 134, 133, 128, 196, 254, 196, 194, - /* 560 */ 194, 194, 194, 258, 194, 196, 194, 196, 257, 256, - /* 570 */ 255, 253, 127, 252, 251, 126, 129, 250, 139, 248, - /* 580 */ 87, 194, 196, 204, 194, 94, 194, 196, 213, 194, - /* 590 */ 196, 194, 194, 194, 194, 93, 194, 194, 194, 47, - /* 600 */ 212, 194, 194, 90, 194, 204, 201, 194, 194, 194, - /* 610 */ 194, 92, 194, 51, 194, 91, 89, 80, 194, 196, - /* 620 */ 194, 196, 194, 194, 194, 194, 3, 194, 194, 3, - /* 630 */ 151, 3, 3, 3, 98, 97, 194, 194, 78, 119, - /* 640 */ 194, 78, 114, 207, 206, 141, 211, 210, 208, 194, - /* 650 */ 209, 194, 194, 194, 194, 194, 194, 194, 202, 215, - /* 660 */ 196, 194, 196, 194, 194, 194, 194, 194, 194, 194, - /* 670 */ 79, 194, 151, 197, 95, 197, 196, 194, 197, 194, - /* 680 */ 196, 194, 194, 197, 194, 196, 79, 95, 78, 194, - /* 690 */ 194, 194, 79, 194, 196, 194, 78, 95, 198, 79, - /* 700 */ 78, 1, 131, 79, 95, 78, 95, 131, 114, 78, - /* 710 */ 78, 78, 78, 115, 74, 66, 3, 3, 112, 3, - /* 720 */ 85, 84, 5, 85, 3, 84, 3, 3, 3, 3, - /* 730 */ 3, 3, 81, 74, 82, 82, 78, 116, 9, 79, - /* 740 */ 78, 55, 20, 10, 10, 146, 146, 146, 3, 95, - /* 750 */ 146, 3, 79, 3, 3, 3, 3, 3, 3, 3, - /* 760 */ 3, 3, 3, 3, 3, 3, 3, 95, 81, 0, - /* 770 */ 274, 56, 274, 274, 274, 274, 274, 274, 274, 15, - /* 780 */ 15, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 790 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 800 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 810 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 820 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 830 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 840 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 850 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 860 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 870 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 880 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 890 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 900 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 910 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 920 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 930 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 940 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 950 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 960 */ 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, - /* 970 */ 274, 274, 274, -}; -#define YY_SHIFT_COUNT (367) -#define YY_SHIFT_MIN (0) -#define YY_SHIFT_MAX (769) -static const unsigned short int yy_shift_ofst[] = { - /* 0 */ 202, 75, 55, 197, 260, 280, 280, 24, 8, 8, - /* 10 */ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - /* 20 */ 8, 0, 203, 280, 296, 310, 310, 45, 45, 3, - /* 30 */ 156, 133, 197, 8, 8, 8, 8, 8, 135, 8, - /* 40 */ 8, 135, 1, 781, 280, 280, 280, 280, 280, 280, - /* 50 */ 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, - /* 60 */ 280, 280, 280, 280, 280, 280, 296, 310, 296, 296, - /* 70 */ 198, 238, 238, 238, 238, 238, 238, 238, 119, 45, - /* 80 */ 45, 314, 314, 113, 45, 25, 8, 482, 8, 8, - /* 90 */ 8, 482, 8, 8, 8, 482, 8, 417, 417, 417, - /* 100 */ 417, 482, 8, 8, 482, 406, 409, 422, 419, 421, - /* 110 */ 427, 445, 449, 447, 439, 482, 8, 8, 482, 8, - /* 120 */ 482, 8, 8, 8, 493, 8, 8, 493, 8, 8, - /* 130 */ 8, 197, 8, 8, 8, 8, 8, 8, 8, 8, - /* 140 */ 8, 482, 8, 8, 8, 8, 8, 8, 482, 8, - /* 150 */ 8, 491, 502, 552, 513, 519, 562, 524, 527, 8, - /* 160 */ 8, 1, 8, 8, 8, 8, 8, 8, 8, 8, - /* 170 */ 8, 482, 8, 482, 8, 8, 8, 8, 8, 8, - /* 180 */ 8, 537, 8, 537, 482, 8, 537, 482, 8, 537, - /* 190 */ 8, 8, 8, 8, 482, 8, 8, 482, 8, 8, - /* 200 */ 781, 781, 30, 66, 66, 96, 66, 126, 195, 273, - /* 210 */ 273, 273, 273, 273, 273, 263, 294, 297, 204, 204, - /* 220 */ 204, 204, 247, 286, 258, 322, 253, 253, 223, 301, - /* 230 */ 359, 211, 356, 338, 346, 361, 363, 364, 333, 350, - /* 240 */ 373, 374, 381, 382, 344, 451, 383, 352, 384, 385, - /* 250 */ 448, 411, 389, 393, 329, 339, 347, 492, 494, 348, - /* 260 */ 355, 396, 358, 432, 623, 479, 626, 628, 521, 629, - /* 270 */ 630, 536, 538, 504, 520, 528, 560, 591, 563, 579, - /* 280 */ 592, 607, 610, 613, 618, 620, 602, 622, 624, 627, - /* 290 */ 700, 631, 609, 571, 611, 576, 632, 528, 633, 594, - /* 300 */ 634, 598, 640, 635, 637, 649, 713, 638, 641, 714, - /* 310 */ 606, 716, 717, 721, 723, 724, 725, 726, 727, 728, - /* 320 */ 651, 729, 659, 652, 653, 658, 660, 621, 662, 722, - /* 330 */ 686, 733, 599, 600, 654, 654, 654, 654, 734, 601, - /* 340 */ 604, 654, 654, 654, 745, 748, 673, 654, 750, 751, - /* 350 */ 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, - /* 360 */ 762, 763, 672, 687, 764, 765, 715, 769, -}; -#define YY_REDUCE_COUNT (201) -#define YY_REDUCE_MIN (-237) -#define YY_REDUCE_MAX (501) -static const short yy_reduce_ofst[] = { - /* 0 */ -180, -29, -161, -68, -195, -168, -30, -194, -103, -50, - /* 10 */ -146, 130, 140, 142, 143, 149, 151, 173, 176, 181, - /* 20 */ 194, -191, -167, -231, -152, -237, -200, -223, -127, -36, - /* 30 */ 90, 136, 170, 134, 139, -119, 207, 213, 239, 245, - /* 40 */ 208, 242, -117, 32, -206, 57, 112, 117, 220, 222, - /* 50 */ 225, 228, 241, 246, 248, 249, 250, 251, 252, 254, - /* 60 */ 255, 256, 257, 259, 261, 262, 72, 227, 270, 275, - /* 70 */ 281, 267, 292, 293, 295, 298, 299, 300, 266, 288, - /* 80 */ 289, 264, 265, 287, 302, 343, 345, 349, 353, 354, - /* 90 */ 357, 360, 365, 366, 367, 362, 368, 276, 278, 283, - /* 100 */ 284, 369, 370, 372, 371, 305, 311, 313, 315, 303, - /* 110 */ 318, 321, 323, 327, 331, 386, 387, 390, 391, 392, - /* 120 */ 394, 395, 397, 398, 379, 399, 400, 401, 402, 403, - /* 130 */ 404, 405, 407, 408, 410, 413, 414, 415, 416, 418, - /* 140 */ 420, 423, 424, 426, 428, 429, 430, 431, 425, 433, - /* 150 */ 434, 375, 388, 435, 436, 437, 440, 441, 438, 442, - /* 160 */ 443, 444, 446, 455, 457, 458, 459, 460, 461, 462, - /* 170 */ 463, 464, 467, 466, 469, 470, 471, 472, 473, 474, - /* 180 */ 475, 476, 477, 478, 480, 483, 481, 484, 485, 486, - /* 190 */ 487, 488, 490, 495, 489, 496, 497, 498, 499, 501, - /* 200 */ 456, 500, -}; -static const YYACTIONTYPE yy_default[] = { - /* 0 */ 894, 956, 944, 952, 1179, 1179, 1179, 894, 894, 894, - /* 10 */ 894, 894, 894, 894, 894, 894, 894, 894, 894, 894, - /* 20 */ 894, 1086, 914, 1179, 894, 894, 894, 894, 894, 1101, - /* 30 */ 1036, 962, 952, 894, 894, 894, 894, 894, 962, 894, - /* 40 */ 894, 962, 894, 1081, 894, 894, 894, 894, 894, 894, - /* 50 */ 894, 894, 894, 894, 894, 894, 894, 894, 894, 894, - /* 60 */ 894, 894, 894, 894, 894, 894, 894, 894, 894, 894, - /* 70 */ 894, 894, 894, 894, 894, 894, 894, 894, 1088, 894, - /* 80 */ 894, 1120, 1120, 1079, 894, 894, 894, 916, 894, 894, - /* 90 */ 1094, 916, 1091, 894, 1096, 916, 894, 894, 894, 894, - /* 100 */ 894, 916, 894, 894, 916, 1127, 1131, 1113, 1125, 1121, - /* 110 */ 1108, 1106, 1104, 1112, 1135, 916, 894, 894, 916, 894, - /* 120 */ 916, 894, 894, 894, 960, 894, 894, 960, 894, 894, - /* 130 */ 894, 952, 894, 894, 894, 894, 894, 894, 894, 894, - /* 140 */ 894, 916, 894, 894, 894, 894, 894, 894, 916, 894, - /* 150 */ 894, 978, 976, 974, 966, 972, 968, 970, 964, 894, - /* 160 */ 894, 894, 894, 942, 894, 940, 894, 894, 894, 894, - /* 170 */ 894, 916, 894, 916, 894, 894, 894, 894, 894, 894, - /* 180 */ 894, 950, 894, 950, 916, 894, 950, 916, 894, 950, - /* 190 */ 925, 894, 894, 894, 916, 894, 894, 916, 894, 912, - /* 200 */ 1001, 1019, 894, 1136, 1126, 894, 1178, 1166, 1165, 1174, - /* 210 */ 1173, 1172, 1164, 1163, 1162, 894, 894, 894, 1158, 1161, - /* 220 */ 1160, 1159, 894, 894, 894, 894, 1168, 1167, 894, 894, - /* 230 */ 894, 894, 894, 894, 894, 894, 894, 894, 1132, 1128, - /* 240 */ 894, 894, 894, 894, 894, 894, 894, 894, 894, 894, - /* 250 */ 894, 1138, 894, 894, 894, 894, 894, 894, 894, 894, - /* 260 */ 894, 1027, 894, 894, 894, 894, 894, 894, 894, 894, - /* 270 */ 894, 894, 894, 894, 1078, 894, 894, 894, 894, 1090, - /* 280 */ 1089, 894, 894, 894, 894, 894, 894, 894, 894, 894, - /* 290 */ 894, 894, 1122, 894, 1114, 894, 894, 1039, 894, 894, - /* 300 */ 894, 894, 894, 894, 894, 894, 894, 894, 894, 894, - /* 310 */ 894, 894, 894, 894, 894, 894, 894, 894, 894, 894, - /* 320 */ 894, 894, 894, 894, 894, 894, 894, 894, 894, 894, - /* 330 */ 894, 894, 894, 894, 1197, 1192, 1193, 1190, 894, 894, - /* 340 */ 894, 1189, 1184, 1185, 894, 894, 894, 1182, 894, 894, - /* 350 */ 894, 894, 894, 894, 894, 894, 894, 894, 894, 894, - /* 360 */ 894, 894, 984, 894, 923, 921, 894, 894, -}; -/********** End of lemon-generated parsing tables *****************************/ - -/* The next table maps tokens (terminal symbols) into fallback tokens. -** If a construct like the following: -** -** %fallback ID X Y Z. -** -** appears in the grammar, then ID becomes a fallback token for X, Y, -** and Z. Whenever one of the tokens X, Y, or Z is input to the parser -** but it does not parse, the type of the token is changed to ID and -** the parse is retried before an error is thrown. -** -** This feature can be used, for example, to cause some keywords in a language -** to revert to identifiers if they keyword does not apply in the context where -** it appears. -*/ -#ifdef YYFALLBACK -static const YYCODETYPE yyFallback[] = { - 0, /* $ => nothing */ - 0, /* ID => nothing */ - 1, /* BOOL => ID */ - 1, /* INTEGER => ID */ - 1, /* FLOAT => ID */ - 1, /* STRING => ID */ - 1, /* TIMESTAMP => ID */ - 0, /* OR => nothing */ - 0, /* AND => nothing */ - 0, /* NOT => nothing */ - 0, /* EQ => nothing */ - 0, /* NE => nothing */ - 0, /* ISNULL => nothing */ - 0, /* NOTNULL => nothing */ - 0, /* IS => nothing */ - 1, /* LIKE => ID */ - 1, /* MATCH => ID */ - 1, /* NMATCH => ID */ - 1, /* GLOB => ID */ - 0, /* BETWEEN => nothing */ - 0, /* IN => nothing */ - 0, /* GT => nothing */ - 0, /* GE => nothing */ - 0, /* LT => nothing */ - 0, /* LE => nothing */ - 0, /* BITAND => nothing */ - 0, /* BITOR => nothing */ - 0, /* LSHIFT => nothing */ - 0, /* RSHIFT => nothing */ - 0, /* PLUS => nothing */ - 0, /* MINUS => nothing */ - 0, /* DIVIDE => nothing */ - 0, /* TIMES => nothing */ - 0, /* STAR => nothing */ - 0, /* SLASH => nothing */ - 0, /* REM => nothing */ - 0, /* CONCAT => nothing */ - 0, /* UMINUS => nothing */ - 0, /* UPLUS => nothing */ - 0, /* BITNOT => nothing */ - 0, /* SHOW => nothing */ - 0, /* DATABASES => nothing */ - 0, /* TOPICS => nothing */ - 0, /* FUNCTIONS => nothing */ - 0, /* MNODES => nothing */ - 0, /* DNODES => nothing */ - 0, /* ACCOUNTS => nothing */ - 0, /* USERS => nothing */ - 0, /* MODULES => nothing */ - 0, /* QUERIES => nothing */ - 0, /* CONNECTIONS => nothing */ - 0, /* STREAMS => nothing */ - 0, /* VARIABLES => nothing */ - 0, /* SCORES => nothing */ - 0, /* GRANTS => nothing */ - 0, /* VNODES => nothing */ - 0, /* DOT => nothing */ - 0, /* CREATE => nothing */ - 0, /* TABLE => nothing */ - 1, /* STABLE => ID */ - 1, /* DATABASE => ID */ - 0, /* TABLES => nothing */ - 0, /* STABLES => nothing */ - 0, /* VGROUPS => nothing */ - 0, /* DROP => nothing */ - 0, /* TOPIC => nothing */ - 0, /* FUNCTION => nothing */ - 0, /* DNODE => nothing */ - 0, /* USER => nothing */ - 0, /* ACCOUNT => nothing */ - 0, /* USE => nothing */ - 0, /* DESCRIBE => nothing */ - 1, /* DESC => ID */ - 0, /* ALTER => nothing */ - 0, /* PASS => nothing */ - 0, /* PRIVILEGE => nothing */ - 0, /* LOCAL => nothing */ - 0, /* COMPACT => nothing */ - 0, /* LP => nothing */ - 0, /* RP => nothing */ - 0, /* IF => nothing */ - 0, /* EXISTS => nothing */ - 0, /* PORT => nothing */ - 1, /* IPTOKEN => ID */ - 0, /* AS => nothing */ - 0, /* OUTPUTTYPE => nothing */ - 0, /* AGGREGATE => nothing */ - 0, /* BUFSIZE => nothing */ - 0, /* PPS => nothing */ - 0, /* TSERIES => nothing */ - 0, /* DBS => nothing */ - 0, /* STORAGE => nothing */ - 0, /* QTIME => nothing */ - 0, /* CONNS => nothing */ - 0, /* STATE => nothing */ - 0, /* COMMA => nothing */ - 0, /* KEEP => nothing */ - 0, /* CACHE => nothing */ - 0, /* REPLICA => nothing */ - 0, /* QUORUM => nothing */ - 0, /* DAYS => nothing */ - 0, /* MINROWS => nothing */ - 0, /* MAXROWS => nothing */ - 0, /* BLOCKS => nothing */ - 0, /* CTIME => nothing */ - 0, /* WAL => nothing */ - 0, /* FSYNC => nothing */ - 0, /* COMP => nothing */ - 0, /* PRECISION => nothing */ - 0, /* UPDATE => nothing */ - 0, /* CACHELAST => nothing */ - 0, /* STREAM => nothing */ - 0, /* MODE => nothing */ - 0, /* UNSIGNED => nothing */ - 0, /* TAGS => nothing */ - 0, /* USING => nothing */ - 1, /* NULL => ID */ - 1, /* NOW => ID */ - 0, /* SELECT => nothing */ - 0, /* UNION => nothing */ - 1, /* ALL => ID */ - 0, /* DISTINCT => nothing */ - 0, /* FROM => nothing */ - 0, /* VARIABLE => nothing */ - 0, /* INTERVAL => nothing */ - 0, /* EVERY => nothing */ - 0, /* SESSION => nothing */ - 0, /* STATE_WINDOW => nothing */ - 0, /* FILL => nothing */ - 0, /* SLIDING => nothing */ - 0, /* ORDER => nothing */ - 0, /* BY => nothing */ - 1, /* ASC => ID */ - 0, /* GROUP => nothing */ - 0, /* HAVING => nothing */ - 0, /* LIMIT => nothing */ - 1, /* OFFSET => ID */ - 0, /* SLIMIT => nothing */ - 0, /* SOFFSET => nothing */ - 0, /* WHERE => nothing */ - 0, /* RESET => nothing */ - 0, /* QUERY => nothing */ - 0, /* SYNCDB => nothing */ - 0, /* ADD => nothing */ - 0, /* COLUMN => nothing */ - 0, /* MODIFY => nothing */ - 0, /* TAG => nothing */ - 0, /* CHANGE => nothing */ - 0, /* SET => nothing */ - 0, /* KILL => nothing */ - 0, /* CONNECTION => nothing */ - 0, /* COLON => nothing */ - 1, /* ABORT => ID */ - 1, /* AFTER => ID */ - 1, /* ATTACH => ID */ - 1, /* BEFORE => ID */ - 1, /* BEGIN => ID */ - 1, /* CASCADE => ID */ - 1, /* CLUSTER => ID */ - 1, /* CONFLICT => ID */ - 1, /* COPY => ID */ - 1, /* DEFERRED => ID */ - 1, /* DELIMITERS => ID */ - 1, /* DETACH => ID */ - 1, /* EACH => ID */ - 1, /* END => ID */ - 1, /* EXPLAIN => ID */ - 1, /* FAIL => ID */ - 1, /* FOR => ID */ - 1, /* IGNORE => ID */ - 1, /* IMMEDIATE => ID */ - 1, /* INITIALLY => ID */ - 1, /* INSTEAD => ID */ - 1, /* KEY => ID */ - 1, /* OF => ID */ - 1, /* RAISE => ID */ - 1, /* REPLACE => ID */ - 1, /* RESTRICT => ID */ - 1, /* ROW => ID */ - 1, /* STATEMENT => ID */ - 1, /* TRIGGER => ID */ - 1, /* VIEW => ID */ - 1, /* SEMI => ID */ - 1, /* NONE => ID */ - 1, /* PREV => ID */ - 1, /* LINEAR => ID */ - 1, /* IMPORT => ID */ - 1, /* TBNAME => ID */ - 1, /* JOIN => ID */ - 1, /* INSERT => ID */ - 1, /* INTO => ID */ - 1, /* VALUES => ID */ -}; -#endif /* YYFALLBACK */ - -/* The following structure represents a single element of the -** parser's stack. Information stored includes: -** -** + The state number for the parser at this level of the stack. -** -** + The value of the token stored at this level of the stack. -** (In other words, the "major" token.) -** -** + The semantic value stored at this level of the stack. This is -** the information used by the action routines in the grammar. -** It is sometimes called the "minor" token. -** -** After the "shift" half of a SHIFTREDUCE action, the stateno field -** actually contains the reduce action for the second half of the -** SHIFTREDUCE. -*/ -struct yyStackEntry { - YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */ - YYCODETYPE major; /* The major token value. This is the code - ** number for the token at this stack level */ - YYMINORTYPE minor; /* The user-supplied minor token value. This - ** is the value of the token */ -}; -typedef struct yyStackEntry yyStackEntry; - -/* The state of the parser is completely contained in an instance of -** the following structure */ -struct yyParser { - yyStackEntry *yytos; /* Pointer to top element of the stack */ -#ifdef YYTRACKMAXSTACKDEPTH - int yyhwm; /* High-water mark of the stack */ -#endif -#ifndef YYNOERRORRECOVERY - int yyerrcnt; /* Shifts left before out of the error */ -#endif - ParseARG_SDECL /* A place to hold %extra_argument */ - ParseCTX_SDECL /* A place to hold %extra_context */ -#if YYSTACKDEPTH<=0 - int yystksz; /* Current side of the stack */ - yyStackEntry *yystack; /* The parser's stack */ - yyStackEntry yystk0; /* First stack entry */ -#else - yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */ - yyStackEntry *yystackEnd; /* Last entry in the stack */ -#endif -}; -typedef struct yyParser yyParser; - -#ifndef NDEBUG -#include -static FILE *yyTraceFILE = 0; -static char *yyTracePrompt = 0; -#endif /* NDEBUG */ - -#ifndef NDEBUG -/* -** Turn parser tracing on by giving a stream to which to write the trace -** and a prompt to preface each trace message. Tracing is turned off -** by making either argument NULL -** -** Inputs: -**
    -**
  • A FILE* to which trace output should be written. -** If NULL, then tracing is turned off. -**
  • A prefix string written at the beginning of every -** line of trace output. If NULL, then tracing is -** turned off. -**
-** -** Outputs: -** None. -*/ -void ParseTrace(FILE *TraceFILE, char *zTracePrompt){ - yyTraceFILE = TraceFILE; - yyTracePrompt = zTracePrompt; - if( yyTraceFILE==0 ) yyTracePrompt = 0; - else if( yyTracePrompt==0 ) yyTraceFILE = 0; -} -#endif /* NDEBUG */ - -#if defined(YYCOVERAGE) || !defined(NDEBUG) -/* For tracing shifts, the names of all terminals and nonterminals -** are required. The following table supplies these names */ -static const char *const yyTokenName[] = { - /* 0 */ "$", - /* 1 */ "ID", - /* 2 */ "BOOL", - /* 3 */ "INTEGER", - /* 4 */ "FLOAT", - /* 5 */ "STRING", - /* 6 */ "TIMESTAMP", - /* 7 */ "OR", - /* 8 */ "AND", - /* 9 */ "NOT", - /* 10 */ "EQ", - /* 11 */ "NE", - /* 12 */ "ISNULL", - /* 13 */ "NOTNULL", - /* 14 */ "IS", - /* 15 */ "LIKE", - /* 16 */ "MATCH", - /* 17 */ "NMATCH", - /* 18 */ "GLOB", - /* 19 */ "BETWEEN", - /* 20 */ "IN", - /* 21 */ "GT", - /* 22 */ "GE", - /* 23 */ "LT", - /* 24 */ "LE", - /* 25 */ "BITAND", - /* 26 */ "BITOR", - /* 27 */ "LSHIFT", - /* 28 */ "RSHIFT", - /* 29 */ "PLUS", - /* 30 */ "MINUS", - /* 31 */ "DIVIDE", - /* 32 */ "TIMES", - /* 33 */ "STAR", - /* 34 */ "SLASH", - /* 35 */ "REM", - /* 36 */ "CONCAT", - /* 37 */ "UMINUS", - /* 38 */ "UPLUS", - /* 39 */ "BITNOT", - /* 40 */ "SHOW", - /* 41 */ "DATABASES", - /* 42 */ "TOPICS", - /* 43 */ "FUNCTIONS", - /* 44 */ "MNODES", - /* 45 */ "DNODES", - /* 46 */ "ACCOUNTS", - /* 47 */ "USERS", - /* 48 */ "MODULES", - /* 49 */ "QUERIES", - /* 50 */ "CONNECTIONS", - /* 51 */ "STREAMS", - /* 52 */ "VARIABLES", - /* 53 */ "SCORES", - /* 54 */ "GRANTS", - /* 55 */ "VNODES", - /* 56 */ "DOT", - /* 57 */ "CREATE", - /* 58 */ "TABLE", - /* 59 */ "STABLE", - /* 60 */ "DATABASE", - /* 61 */ "TABLES", - /* 62 */ "STABLES", - /* 63 */ "VGROUPS", - /* 64 */ "DROP", - /* 65 */ "TOPIC", - /* 66 */ "FUNCTION", - /* 67 */ "DNODE", - /* 68 */ "USER", - /* 69 */ "ACCOUNT", - /* 70 */ "USE", - /* 71 */ "DESCRIBE", - /* 72 */ "DESC", - /* 73 */ "ALTER", - /* 74 */ "PASS", - /* 75 */ "PRIVILEGE", - /* 76 */ "LOCAL", - /* 77 */ "COMPACT", - /* 78 */ "LP", - /* 79 */ "RP", - /* 80 */ "IF", - /* 81 */ "EXISTS", - /* 82 */ "PORT", - /* 83 */ "IPTOKEN", - /* 84 */ "AS", - /* 85 */ "OUTPUTTYPE", - /* 86 */ "AGGREGATE", - /* 87 */ "BUFSIZE", - /* 88 */ "PPS", - /* 89 */ "TSERIES", - /* 90 */ "DBS", - /* 91 */ "STORAGE", - /* 92 */ "QTIME", - /* 93 */ "CONNS", - /* 94 */ "STATE", - /* 95 */ "COMMA", - /* 96 */ "KEEP", - /* 97 */ "CACHE", - /* 98 */ "REPLICA", - /* 99 */ "QUORUM", - /* 100 */ "DAYS", - /* 101 */ "MINROWS", - /* 102 */ "MAXROWS", - /* 103 */ "BLOCKS", - /* 104 */ "CTIME", - /* 105 */ "WAL", - /* 106 */ "FSYNC", - /* 107 */ "COMP", - /* 108 */ "PRECISION", - /* 109 */ "UPDATE", - /* 110 */ "CACHELAST", - /* 111 */ "STREAM", - /* 112 */ "MODE", - /* 113 */ "UNSIGNED", - /* 114 */ "TAGS", - /* 115 */ "USING", - /* 116 */ "NULL", - /* 117 */ "NOW", - /* 118 */ "SELECT", - /* 119 */ "UNION", - /* 120 */ "ALL", - /* 121 */ "DISTINCT", - /* 122 */ "FROM", - /* 123 */ "VARIABLE", - /* 124 */ "INTERVAL", - /* 125 */ "EVERY", - /* 126 */ "SESSION", - /* 127 */ "STATE_WINDOW", - /* 128 */ "FILL", - /* 129 */ "SLIDING", - /* 130 */ "ORDER", - /* 131 */ "BY", - /* 132 */ "ASC", - /* 133 */ "GROUP", - /* 134 */ "HAVING", - /* 135 */ "LIMIT", - /* 136 */ "OFFSET", - /* 137 */ "SLIMIT", - /* 138 */ "SOFFSET", - /* 139 */ "WHERE", - /* 140 */ "RESET", - /* 141 */ "QUERY", - /* 142 */ "SYNCDB", - /* 143 */ "ADD", - /* 144 */ "COLUMN", - /* 145 */ "MODIFY", - /* 146 */ "TAG", - /* 147 */ "CHANGE", - /* 148 */ "SET", - /* 149 */ "KILL", - /* 150 */ "CONNECTION", - /* 151 */ "COLON", - /* 152 */ "ABORT", - /* 153 */ "AFTER", - /* 154 */ "ATTACH", - /* 155 */ "BEFORE", - /* 156 */ "BEGIN", - /* 157 */ "CASCADE", - /* 158 */ "CLUSTER", - /* 159 */ "CONFLICT", - /* 160 */ "COPY", - /* 161 */ "DEFERRED", - /* 162 */ "DELIMITERS", - /* 163 */ "DETACH", - /* 164 */ "EACH", - /* 165 */ "END", - /* 166 */ "EXPLAIN", - /* 167 */ "FAIL", - /* 168 */ "FOR", - /* 169 */ "IGNORE", - /* 170 */ "IMMEDIATE", - /* 171 */ "INITIALLY", - /* 172 */ "INSTEAD", - /* 173 */ "KEY", - /* 174 */ "OF", - /* 175 */ "RAISE", - /* 176 */ "REPLACE", - /* 177 */ "RESTRICT", - /* 178 */ "ROW", - /* 179 */ "STATEMENT", - /* 180 */ "TRIGGER", - /* 181 */ "VIEW", - /* 182 */ "SEMI", - /* 183 */ "NONE", - /* 184 */ "PREV", - /* 185 */ "LINEAR", - /* 186 */ "IMPORT", - /* 187 */ "TBNAME", - /* 188 */ "JOIN", - /* 189 */ "INSERT", - /* 190 */ "INTO", - /* 191 */ "VALUES", - /* 192 */ "program", - /* 193 */ "cmd", - /* 194 */ "ids", - /* 195 */ "dbPrefix", - /* 196 */ "cpxName", - /* 197 */ "ifexists", - /* 198 */ "alter_db_optr", - /* 199 */ "acct_optr", - /* 200 */ "exprlist", - /* 201 */ "ifnotexists", - /* 202 */ "db_optr", - /* 203 */ "typename", - /* 204 */ "bufsize", - /* 205 */ "pps", - /* 206 */ "tseries", - /* 207 */ "dbs", - /* 208 */ "streams", - /* 209 */ "storage", - /* 210 */ "qtime", - /* 211 */ "users", - /* 212 */ "conns", - /* 213 */ "state", - /* 214 */ "intitemlist", - /* 215 */ "intitem", - /* 216 */ "keep", - /* 217 */ "cache", - /* 218 */ "replica", - /* 219 */ "quorum", - /* 220 */ "days", - /* 221 */ "minrows", - /* 222 */ "maxrows", - /* 223 */ "blocks", - /* 224 */ "ctime", - /* 225 */ "wal", - /* 226 */ "fsync", - /* 227 */ "comp", - /* 228 */ "prec", - /* 229 */ "update", - /* 230 */ "cachelast", - /* 231 */ "vgroups", - /* 232 */ "stream_mode", - /* 233 */ "signed", - /* 234 */ "create_table_args", - /* 235 */ "create_stable_args", - /* 236 */ "create_table_list", - /* 237 */ "create_from_stable", - /* 238 */ "columnlist", - /* 239 */ "tagitemlist1", - /* 240 */ "tagNamelist", - /* 241 */ "select", - /* 242 */ "column", - /* 243 */ "tagitem1", - /* 244 */ "tagitemlist", - /* 245 */ "tagitem", - /* 246 */ "selcollist", - /* 247 */ "from", - /* 248 */ "where_opt", - /* 249 */ "interval_option", - /* 250 */ "sliding_opt", - /* 251 */ "session_option", - /* 252 */ "windowstate_option", - /* 253 */ "fill_opt", - /* 254 */ "groupby_opt", - /* 255 */ "having_opt", - /* 256 */ "orderby_opt", - /* 257 */ "slimit_opt", - /* 258 */ "limit_opt", - /* 259 */ "union", - /* 260 */ "sclp", - /* 261 */ "distinct", - /* 262 */ "expr", - /* 263 */ "as", - /* 264 */ "tablelist", - /* 265 */ "sub", - /* 266 */ "tmvar", - /* 267 */ "intervalKey", - /* 268 */ "sortlist", - /* 269 */ "sortitem", - /* 270 */ "item", - /* 271 */ "sortorder", - /* 272 */ "grouplist", - /* 273 */ "expritem", -}; -#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */ - -#ifndef NDEBUG -/* For tracing reduce actions, the names of all rules are required. -*/ -static const char *const yyRuleName[] = { - /* 0 */ "program ::= cmd", - /* 1 */ "cmd ::= SHOW DATABASES", - /* 2 */ "cmd ::= SHOW TOPICS", - /* 3 */ "cmd ::= SHOW FUNCTIONS", - /* 4 */ "cmd ::= SHOW MNODES", - /* 5 */ "cmd ::= SHOW DNODES", - /* 6 */ "cmd ::= SHOW ACCOUNTS", - /* 7 */ "cmd ::= SHOW USERS", - /* 8 */ "cmd ::= SHOW MODULES", - /* 9 */ "cmd ::= SHOW QUERIES", - /* 10 */ "cmd ::= SHOW CONNECTIONS", - /* 11 */ "cmd ::= SHOW STREAMS", - /* 12 */ "cmd ::= SHOW VARIABLES", - /* 13 */ "cmd ::= SHOW SCORES", - /* 14 */ "cmd ::= SHOW GRANTS", - /* 15 */ "cmd ::= SHOW VNODES", - /* 16 */ "cmd ::= SHOW VNODES ids", - /* 17 */ "dbPrefix ::=", - /* 18 */ "dbPrefix ::= ids DOT", - /* 19 */ "cpxName ::=", - /* 20 */ "cpxName ::= DOT ids", - /* 21 */ "cmd ::= SHOW CREATE TABLE ids cpxName", - /* 22 */ "cmd ::= SHOW CREATE STABLE ids cpxName", - /* 23 */ "cmd ::= SHOW CREATE DATABASE ids", - /* 24 */ "cmd ::= SHOW dbPrefix TABLES", - /* 25 */ "cmd ::= SHOW dbPrefix TABLES LIKE ids", - /* 26 */ "cmd ::= SHOW dbPrefix STABLES", - /* 27 */ "cmd ::= SHOW dbPrefix STABLES LIKE ids", - /* 28 */ "cmd ::= SHOW dbPrefix VGROUPS", - /* 29 */ "cmd ::= SHOW dbPrefix VGROUPS ids", - /* 30 */ "cmd ::= DROP TABLE ifexists ids cpxName", - /* 31 */ "cmd ::= DROP STABLE ifexists ids cpxName", - /* 32 */ "cmd ::= DROP DATABASE ifexists ids", - /* 33 */ "cmd ::= DROP TOPIC ifexists ids", - /* 34 */ "cmd ::= DROP FUNCTION ids", - /* 35 */ "cmd ::= DROP DNODE ids", - /* 36 */ "cmd ::= DROP USER ids", - /* 37 */ "cmd ::= DROP ACCOUNT ids", - /* 38 */ "cmd ::= USE ids", - /* 39 */ "cmd ::= DESCRIBE ids cpxName", - /* 40 */ "cmd ::= DESC ids cpxName", - /* 41 */ "cmd ::= ALTER USER ids PASS ids", - /* 42 */ "cmd ::= ALTER USER ids PRIVILEGE ids", - /* 43 */ "cmd ::= ALTER DNODE ids ids", - /* 44 */ "cmd ::= ALTER DNODE ids ids ids", - /* 45 */ "cmd ::= ALTER LOCAL ids", - /* 46 */ "cmd ::= ALTER LOCAL ids ids", - /* 47 */ "cmd ::= ALTER DATABASE ids alter_db_optr", - /* 48 */ "cmd ::= ALTER ACCOUNT ids acct_optr", - /* 49 */ "cmd ::= ALTER ACCOUNT ids PASS ids acct_optr", - /* 50 */ "cmd ::= COMPACT VNODES IN LP exprlist RP", - /* 51 */ "ids ::= ID", - /* 52 */ "ifexists ::= IF EXISTS", - /* 53 */ "ifexists ::=", - /* 54 */ "ifnotexists ::= IF NOT EXISTS", - /* 55 */ "ifnotexists ::=", - /* 56 */ "cmd ::= CREATE DNODE ids PORT ids", - /* 57 */ "cmd ::= CREATE DNODE IPTOKEN PORT ids", - /* 58 */ "cmd ::= CREATE ACCOUNT ids PASS ids acct_optr", - /* 59 */ "cmd ::= CREATE DATABASE ifnotexists ids db_optr", - /* 60 */ "cmd ::= CREATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize", - /* 61 */ "cmd ::= CREATE AGGREGATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize", - /* 62 */ "cmd ::= CREATE USER ids PASS ids", - /* 63 */ "bufsize ::=", - /* 64 */ "bufsize ::= BUFSIZE INTEGER", - /* 65 */ "pps ::=", - /* 66 */ "pps ::= PPS INTEGER", - /* 67 */ "tseries ::=", - /* 68 */ "tseries ::= TSERIES INTEGER", - /* 69 */ "dbs ::=", - /* 70 */ "dbs ::= DBS INTEGER", - /* 71 */ "streams ::=", - /* 72 */ "streams ::= STREAMS INTEGER", - /* 73 */ "storage ::=", - /* 74 */ "storage ::= STORAGE INTEGER", - /* 75 */ "qtime ::=", - /* 76 */ "qtime ::= QTIME INTEGER", - /* 77 */ "users ::=", - /* 78 */ "users ::= USERS INTEGER", - /* 79 */ "conns ::=", - /* 80 */ "conns ::= CONNS INTEGER", - /* 81 */ "state ::=", - /* 82 */ "state ::= STATE ids", - /* 83 */ "acct_optr ::= pps tseries storage streams qtime dbs users conns state", - /* 84 */ "intitemlist ::= intitemlist COMMA intitem", - /* 85 */ "intitemlist ::= intitem", - /* 86 */ "intitem ::= INTEGER", - /* 87 */ "keep ::= KEEP intitemlist", - /* 88 */ "cache ::= CACHE INTEGER", - /* 89 */ "replica ::= REPLICA INTEGER", - /* 90 */ "quorum ::= QUORUM INTEGER", - /* 91 */ "days ::= DAYS INTEGER", - /* 92 */ "minrows ::= MINROWS INTEGER", - /* 93 */ "maxrows ::= MAXROWS INTEGER", - /* 94 */ "blocks ::= BLOCKS INTEGER", - /* 95 */ "ctime ::= CTIME INTEGER", - /* 96 */ "wal ::= WAL INTEGER", - /* 97 */ "fsync ::= FSYNC INTEGER", - /* 98 */ "comp ::= COMP INTEGER", - /* 99 */ "prec ::= PRECISION STRING", - /* 100 */ "update ::= UPDATE INTEGER", - /* 101 */ "cachelast ::= CACHELAST INTEGER", - /* 102 */ "vgroups ::= VGROUPS INTEGER", - /* 103 */ "stream_mode ::= STREAM MODE INTEGER", - /* 104 */ "db_optr ::=", - /* 105 */ "db_optr ::= db_optr cache", - /* 106 */ "db_optr ::= db_optr replica", - /* 107 */ "db_optr ::= db_optr quorum", - /* 108 */ "db_optr ::= db_optr days", - /* 109 */ "db_optr ::= db_optr minrows", - /* 110 */ "db_optr ::= db_optr maxrows", - /* 111 */ "db_optr ::= db_optr blocks", - /* 112 */ "db_optr ::= db_optr ctime", - /* 113 */ "db_optr ::= db_optr wal", - /* 114 */ "db_optr ::= db_optr fsync", - /* 115 */ "db_optr ::= db_optr comp", - /* 116 */ "db_optr ::= db_optr prec", - /* 117 */ "db_optr ::= db_optr keep", - /* 118 */ "db_optr ::= db_optr update", - /* 119 */ "db_optr ::= db_optr cachelast", - /* 120 */ "db_optr ::= db_optr vgroups", - /* 121 */ "db_optr ::= db_optr stream_mode", - /* 122 */ "alter_db_optr ::=", - /* 123 */ "alter_db_optr ::= alter_db_optr replica", - /* 124 */ "alter_db_optr ::= alter_db_optr quorum", - /* 125 */ "alter_db_optr ::= alter_db_optr keep", - /* 126 */ "alter_db_optr ::= alter_db_optr blocks", - /* 127 */ "alter_db_optr ::= alter_db_optr comp", - /* 128 */ "alter_db_optr ::= alter_db_optr update", - /* 129 */ "alter_db_optr ::= alter_db_optr cachelast", - /* 130 */ "typename ::= ids", - /* 131 */ "typename ::= ids LP signed RP", - /* 132 */ "typename ::= ids UNSIGNED", - /* 133 */ "signed ::= INTEGER", - /* 134 */ "signed ::= PLUS INTEGER", - /* 135 */ "signed ::= MINUS INTEGER", - /* 136 */ "cmd ::= CREATE TABLE create_table_args", - /* 137 */ "cmd ::= CREATE TABLE create_stable_args", - /* 138 */ "cmd ::= CREATE STABLE create_stable_args", - /* 139 */ "cmd ::= CREATE TABLE create_table_list", - /* 140 */ "create_table_list ::= create_from_stable", - /* 141 */ "create_table_list ::= create_table_list create_from_stable", - /* 142 */ "create_table_args ::= ifnotexists ids cpxName LP columnlist RP", - /* 143 */ "create_stable_args ::= ifnotexists ids cpxName LP columnlist RP TAGS LP columnlist RP", - /* 144 */ "create_from_stable ::= ifnotexists ids cpxName USING ids cpxName TAGS LP tagitemlist1 RP", - /* 145 */ "create_from_stable ::= ifnotexists ids cpxName USING ids cpxName LP tagNamelist RP TAGS LP tagitemlist1 RP", - /* 146 */ "tagNamelist ::= tagNamelist COMMA ids", - /* 147 */ "tagNamelist ::= ids", - /* 148 */ "create_table_args ::= ifnotexists ids cpxName AS select", - /* 149 */ "columnlist ::= columnlist COMMA column", - /* 150 */ "columnlist ::= column", - /* 151 */ "column ::= ids typename", - /* 152 */ "tagitemlist1 ::= tagitemlist1 COMMA tagitem1", - /* 153 */ "tagitemlist1 ::= tagitem1", - /* 154 */ "tagitem1 ::= MINUS INTEGER", - /* 155 */ "tagitem1 ::= MINUS FLOAT", - /* 156 */ "tagitem1 ::= PLUS INTEGER", - /* 157 */ "tagitem1 ::= PLUS FLOAT", - /* 158 */ "tagitem1 ::= INTEGER", - /* 159 */ "tagitem1 ::= FLOAT", - /* 160 */ "tagitem1 ::= STRING", - /* 161 */ "tagitem1 ::= BOOL", - /* 162 */ "tagitem1 ::= NULL", - /* 163 */ "tagitem1 ::= NOW", - /* 164 */ "tagitemlist ::= tagitemlist COMMA tagitem", - /* 165 */ "tagitemlist ::= tagitem", - /* 166 */ "tagitem ::= INTEGER", - /* 167 */ "tagitem ::= FLOAT", - /* 168 */ "tagitem ::= STRING", - /* 169 */ "tagitem ::= BOOL", - /* 170 */ "tagitem ::= NULL", - /* 171 */ "tagitem ::= NOW", - /* 172 */ "tagitem ::= MINUS INTEGER", - /* 173 */ "tagitem ::= MINUS FLOAT", - /* 174 */ "tagitem ::= PLUS INTEGER", - /* 175 */ "tagitem ::= PLUS FLOAT", - /* 176 */ "select ::= SELECT selcollist from where_opt interval_option sliding_opt session_option windowstate_option fill_opt groupby_opt having_opt orderby_opt slimit_opt limit_opt", - /* 177 */ "select ::= LP select RP", - /* 178 */ "union ::= select", - /* 179 */ "union ::= union UNION ALL select", - /* 180 */ "union ::= union UNION select", - /* 181 */ "cmd ::= union", - /* 182 */ "select ::= SELECT selcollist", - /* 183 */ "sclp ::= selcollist COMMA", - /* 184 */ "sclp ::=", - /* 185 */ "selcollist ::= sclp distinct expr as", - /* 186 */ "selcollist ::= sclp STAR", - /* 187 */ "as ::= AS ids", - /* 188 */ "as ::= ids", - /* 189 */ "as ::=", - /* 190 */ "distinct ::= DISTINCT", - /* 191 */ "distinct ::=", - /* 192 */ "from ::= FROM tablelist", - /* 193 */ "from ::= FROM sub", - /* 194 */ "sub ::= LP union RP", - /* 195 */ "sub ::= LP union RP ids", - /* 196 */ "sub ::= sub COMMA LP union RP ids", - /* 197 */ "tablelist ::= ids cpxName", - /* 198 */ "tablelist ::= ids cpxName ids", - /* 199 */ "tablelist ::= tablelist COMMA ids cpxName", - /* 200 */ "tablelist ::= tablelist COMMA ids cpxName ids", - /* 201 */ "tmvar ::= VARIABLE", - /* 202 */ "interval_option ::= intervalKey LP tmvar RP", - /* 203 */ "interval_option ::= intervalKey LP tmvar COMMA tmvar RP", - /* 204 */ "interval_option ::=", - /* 205 */ "intervalKey ::= INTERVAL", - /* 206 */ "intervalKey ::= EVERY", - /* 207 */ "session_option ::=", - /* 208 */ "session_option ::= SESSION LP ids cpxName COMMA tmvar RP", - /* 209 */ "windowstate_option ::=", - /* 210 */ "windowstate_option ::= STATE_WINDOW LP ids RP", - /* 211 */ "fill_opt ::=", - /* 212 */ "fill_opt ::= FILL LP ID COMMA tagitemlist RP", - /* 213 */ "fill_opt ::= FILL LP ID RP", - /* 214 */ "sliding_opt ::= SLIDING LP tmvar RP", - /* 215 */ "sliding_opt ::=", - /* 216 */ "orderby_opt ::=", - /* 217 */ "orderby_opt ::= ORDER BY sortlist", - /* 218 */ "sortlist ::= sortlist COMMA item sortorder", - /* 219 */ "sortlist ::= item sortorder", - /* 220 */ "item ::= ids cpxName", - /* 221 */ "sortorder ::= ASC", - /* 222 */ "sortorder ::= DESC", - /* 223 */ "sortorder ::=", - /* 224 */ "groupby_opt ::=", - /* 225 */ "groupby_opt ::= GROUP BY grouplist", - /* 226 */ "grouplist ::= grouplist COMMA item", - /* 227 */ "grouplist ::= item", - /* 228 */ "having_opt ::=", - /* 229 */ "having_opt ::= HAVING expr", - /* 230 */ "limit_opt ::=", - /* 231 */ "limit_opt ::= LIMIT signed", - /* 232 */ "limit_opt ::= LIMIT signed OFFSET signed", - /* 233 */ "limit_opt ::= LIMIT signed COMMA signed", - /* 234 */ "slimit_opt ::=", - /* 235 */ "slimit_opt ::= SLIMIT signed", - /* 236 */ "slimit_opt ::= SLIMIT signed SOFFSET signed", - /* 237 */ "slimit_opt ::= SLIMIT signed COMMA signed", - /* 238 */ "where_opt ::=", - /* 239 */ "where_opt ::= WHERE expr", - /* 240 */ "expr ::= LP expr RP", - /* 241 */ "expr ::= ID", - /* 242 */ "expr ::= ID DOT ID", - /* 243 */ "expr ::= ID DOT STAR", - /* 244 */ "expr ::= INTEGER", - /* 245 */ "expr ::= MINUS INTEGER", - /* 246 */ "expr ::= PLUS INTEGER", - /* 247 */ "expr ::= FLOAT", - /* 248 */ "expr ::= MINUS FLOAT", - /* 249 */ "expr ::= PLUS FLOAT", - /* 250 */ "expr ::= STRING", - /* 251 */ "expr ::= NOW", - /* 252 */ "expr ::= VARIABLE", - /* 253 */ "expr ::= PLUS VARIABLE", - /* 254 */ "expr ::= MINUS VARIABLE", - /* 255 */ "expr ::= BOOL", - /* 256 */ "expr ::= NULL", - /* 257 */ "expr ::= ID LP exprlist RP", - /* 258 */ "expr ::= ID LP STAR RP", - /* 259 */ "expr ::= expr IS NULL", - /* 260 */ "expr ::= expr IS NOT NULL", - /* 261 */ "expr ::= expr LT expr", - /* 262 */ "expr ::= expr GT expr", - /* 263 */ "expr ::= expr LE expr", - /* 264 */ "expr ::= expr GE expr", - /* 265 */ "expr ::= expr NE expr", - /* 266 */ "expr ::= expr EQ expr", - /* 267 */ "expr ::= expr BETWEEN expr AND expr", - /* 268 */ "expr ::= expr AND expr", - /* 269 */ "expr ::= expr OR expr", - /* 270 */ "expr ::= expr PLUS expr", - /* 271 */ "expr ::= expr MINUS expr", - /* 272 */ "expr ::= expr STAR expr", - /* 273 */ "expr ::= expr SLASH expr", - /* 274 */ "expr ::= expr REM expr", - /* 275 */ "expr ::= expr LIKE expr", - /* 276 */ "expr ::= expr MATCH expr", - /* 277 */ "expr ::= expr NMATCH expr", - /* 278 */ "expr ::= expr IN LP exprlist RP", - /* 279 */ "exprlist ::= exprlist COMMA expritem", - /* 280 */ "exprlist ::= expritem", - /* 281 */ "expritem ::= expr", - /* 282 */ "expritem ::=", - /* 283 */ "cmd ::= RESET QUERY CACHE", - /* 284 */ "cmd ::= SYNCDB ids REPLICA", - /* 285 */ "cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist", - /* 286 */ "cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids", - /* 287 */ "cmd ::= ALTER TABLE ids cpxName MODIFY COLUMN columnlist", - /* 288 */ "cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist", - /* 289 */ "cmd ::= ALTER TABLE ids cpxName DROP TAG ids", - /* 290 */ "cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids", - /* 291 */ "cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem", - /* 292 */ "cmd ::= ALTER TABLE ids cpxName MODIFY TAG columnlist", - /* 293 */ "cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist", - /* 294 */ "cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids", - /* 295 */ "cmd ::= ALTER STABLE ids cpxName MODIFY COLUMN columnlist", - /* 296 */ "cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist", - /* 297 */ "cmd ::= ALTER STABLE ids cpxName DROP TAG ids", - /* 298 */ "cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids", - /* 299 */ "cmd ::= ALTER STABLE ids cpxName SET TAG ids EQ tagitem", - /* 300 */ "cmd ::= ALTER STABLE ids cpxName MODIFY TAG columnlist", - /* 301 */ "cmd ::= KILL CONNECTION INTEGER", - /* 302 */ "cmd ::= KILL STREAM INTEGER COLON INTEGER", - /* 303 */ "cmd ::= KILL QUERY INTEGER COLON INTEGER", -}; -#endif /* NDEBUG */ - - -#if YYSTACKDEPTH<=0 -/* -** Try to increase the size of the parser stack. Return the number -** of errors. Return 0 on success. -*/ -static int yyGrowStack(yyParser *p){ - int newSize; - int idx; - yyStackEntry *pNew; - - newSize = p->yystksz*2 + 100; - idx = p->yytos ? (int)(p->yytos - p->yystack) : 0; - if( p->yystack==&p->yystk0 ){ - pNew = malloc(newSize*sizeof(pNew[0])); - if( pNew ) pNew[0] = p->yystk0; - }else{ - pNew = realloc(p->yystack, newSize*sizeof(pNew[0])); - } - if( pNew ){ - p->yystack = pNew; - p->yytos = &p->yystack[idx]; -#ifndef NDEBUG - if( yyTraceFILE ){ - fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n", - yyTracePrompt, p->yystksz, newSize); - } -#endif - p->yystksz = newSize; - } - return pNew==0; -} -#endif - -/* Datatype of the argument to the memory allocated passed as the -** second argument to ParseAlloc() below. This can be changed by -** putting an appropriate #define in the %include section of the input -** grammar. -*/ -#ifndef YYMALLOCARGTYPE -# define YYMALLOCARGTYPE size_t -#endif - -/* Initialize a new parser that has already been allocated. -*/ -void ParseInit(void *yypRawParser ParseCTX_PDECL){ - yyParser *yypParser = (yyParser*)yypRawParser; - ParseCTX_STORE -#ifdef YYTRACKMAXSTACKDEPTH - yypParser->yyhwm = 0; -#endif -#if YYSTACKDEPTH<=0 - yypParser->yytos = NULL; - yypParser->yystack = NULL; - yypParser->yystksz = 0; - if( yyGrowStack(yypParser) ){ - yypParser->yystack = &yypParser->yystk0; - yypParser->yystksz = 1; - } -#endif -#ifndef YYNOERRORRECOVERY - yypParser->yyerrcnt = -1; -#endif - yypParser->yytos = yypParser->yystack; - yypParser->yystack[0].stateno = 0; - yypParser->yystack[0].major = 0; -#if YYSTACKDEPTH>0 - yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1]; -#endif -} - -#ifndef Parse_ENGINEALWAYSONSTACK -/* -** This function allocates a new parser. -** The only argument is a pointer to a function which works like -** malloc. -** -** Inputs: -** A pointer to the function used to allocate memory. -** -** Outputs: -** A pointer to a parser. This pointer is used in subsequent calls -** to Parse and ParseFree. -*/ -void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) ParseCTX_PDECL){ - yyParser *yypParser; - yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) ); - if( yypParser ){ - ParseCTX_STORE - ParseInit(yypParser ParseCTX_PARAM); - } - return (void*)yypParser; -} -#endif /* Parse_ENGINEALWAYSONSTACK */ - - -/* The following function deletes the "minor type" or semantic value -** associated with a symbol. The symbol can be either a terminal -** or nonterminal. "yymajor" is the symbol code, and "yypminor" is -** a pointer to the value to be deleted. The code used to do the -** deletions is derived from the %destructor and/or %token_destructor -** directives of the input grammar. -*/ -static void yy_destructor( - yyParser *yypParser, /* The parser */ - YYCODETYPE yymajor, /* Type code for object to destroy */ - YYMINORTYPE *yypminor /* The object to be destroyed */ -){ - ParseARG_FETCH - ParseCTX_FETCH - switch( yymajor ){ - /* Here is inserted the actions which take place when a - ** terminal or non-terminal is destroyed. This can happen - ** when the symbol is popped from the stack during a - ** reduce or during error processing or when a parser is - ** being destroyed before it is finished parsing. - ** - ** Note: during a reduce, the only symbols destroyed are those - ** which appear on the RHS of the rule, but which are *not* used - ** inside the C code. - */ -/********* Begin destructor definitions ***************************************/ - case 200: /* exprlist */ - case 246: /* selcollist */ - case 260: /* sclp */ -{ -tSqlExprListDestroy((yypminor->yy225)); -} - break; - case 214: /* intitemlist */ - case 216: /* keep */ - case 238: /* columnlist */ - case 239: /* tagitemlist1 */ - case 240: /* tagNamelist */ - case 244: /* tagitemlist */ - case 253: /* fill_opt */ - case 254: /* groupby_opt */ - case 256: /* orderby_opt */ - case 268: /* sortlist */ - case 272: /* grouplist */ -{ -taosArrayDestroy((yypminor->yy225)); -} - break; - case 236: /* create_table_list */ -{ -destroyCreateTableSql((yypminor->yy482)); -} - break; - case 241: /* select */ -{ -destroySqlNode((yypminor->yy185)); -} - break; - case 247: /* from */ - case 264: /* tablelist */ - case 265: /* sub */ -{ -destroyRelationInfo((yypminor->yy160)); -} - break; - case 248: /* where_opt */ - case 255: /* having_opt */ - case 262: /* expr */ - case 273: /* expritem */ -{ -tSqlExprDestroy((yypminor->yy226)); -} - break; - case 259: /* union */ -{ -destroyAllSqlNode((yypminor->yy93)); -} - break; - case 269: /* sortitem */ -{ -taosVariantDestroy(&(yypminor->yy1)); -} - break; -/********* End destructor definitions *****************************************/ - default: break; /* If no destructor action specified: do nothing */ - } -} - -/* -** Pop the parser's stack once. -** -** If there is a destructor routine associated with the token which -** is popped from the stack, then call it. -*/ -static void yy_pop_parser_stack(yyParser *pParser){ - yyStackEntry *yytos; - assert( pParser->yytos!=0 ); - assert( pParser->yytos > pParser->yystack ); - yytos = pParser->yytos--; -#ifndef NDEBUG - if( yyTraceFILE ){ - fprintf(yyTraceFILE,"%sPopping %s\n", - yyTracePrompt, - yyTokenName[yytos->major]); - } -#endif - yy_destructor(pParser, yytos->major, &yytos->minor); -} - -/* -** Clear all secondary memory allocations from the parser -*/ -void ParseFinalize(void *p){ - yyParser *pParser = (yyParser*)p; - while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser); -#if YYSTACKDEPTH<=0 - if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack); -#endif -} - -#ifndef Parse_ENGINEALWAYSONSTACK -/* -** Deallocate and destroy a parser. Destructors are called for -** all stack elements before shutting the parser down. -** -** If the YYPARSEFREENEVERNULL macro exists (for example because it -** is defined in a %include section of the input grammar) then it is -** assumed that the input pointer is never NULL. -*/ -void ParseFree( - void *p, /* The parser to be deleted */ - void (*freeProc)(void*) /* Function used to reclaim memory */ -){ -#ifndef YYPARSEFREENEVERNULL - if( p==0 ) return; -#endif - ParseFinalize(p); - (*freeProc)(p); -} -#endif /* Parse_ENGINEALWAYSONSTACK */ - -/* -** Return the peak depth of the stack for a parser. -*/ -#ifdef YYTRACKMAXSTACKDEPTH -int ParseStackPeak(void *p){ - yyParser *pParser = (yyParser*)p; - return pParser->yyhwm; -} -#endif - -/* This array of booleans keeps track of the parser statement -** coverage. The element yycoverage[X][Y] is set when the parser -** is in state X and has a lookahead token Y. In a well-tested -** systems, every element of this matrix should end up being set. -*/ -#if defined(YYCOVERAGE) -static unsigned char yycoverage[YYNSTATE][YYNTOKEN]; -#endif - -/* -** Write into out a description of every state/lookahead combination that -** -** (1) has not been used by the parser, and -** (2) is not a syntax error. -** -** Return the number of missed state/lookahead combinations. -*/ -#if defined(YYCOVERAGE) -int ParseCoverage(FILE *out){ - int stateno, iLookAhead, i; - int nMissed = 0; - for(stateno=0; statenoYY_MAX_SHIFT ) return stateno; - assert( stateno <= YY_SHIFT_COUNT ); -#if defined(YYCOVERAGE) - yycoverage[stateno][iLookAhead] = 1; -#endif - do{ - i = yy_shift_ofst[stateno]; - assert( i>=0 ); - /* assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); */ - assert( iLookAhead!=YYNOCODE ); - assert( iLookAhead < YYNTOKEN ); - i += iLookAhead; - if( i>=YY_NLOOKAHEAD || yy_lookahead[i]!=iLookAhead ){ -#ifdef YYFALLBACK - YYCODETYPE iFallback; /* Fallback token */ - if( iLookAhead %s\n", - yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); - } -#endif - assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */ - iLookAhead = iFallback; - continue; - } -#endif -#ifdef YYWILDCARD - { - int j = i - iLookAhead + YYWILDCARD; - if( -#if YY_SHIFT_MIN+YYWILDCARD<0 - j>=0 && -#endif -#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT - j0 - ){ -#ifndef NDEBUG - if( yyTraceFILE ){ - fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", - yyTracePrompt, yyTokenName[iLookAhead], - yyTokenName[YYWILDCARD]); - } -#endif /* NDEBUG */ - return yy_action[j]; - } - } -#endif /* YYWILDCARD */ - return yy_default[stateno]; - }else{ - return yy_action[i]; - } - }while(1); -} - -/* -** Find the appropriate action for a parser given the non-terminal -** look-ahead token iLookAhead. -*/ -static YYACTIONTYPE yy_find_reduce_action( - YYACTIONTYPE stateno, /* Current state number */ - YYCODETYPE iLookAhead /* The look-ahead token */ -){ - int i; -#ifdef YYERRORSYMBOL - if( stateno>YY_REDUCE_COUNT ){ - return yy_default[stateno]; - } -#else - assert( stateno<=YY_REDUCE_COUNT ); -#endif - i = yy_reduce_ofst[stateno]; - assert( iLookAhead!=YYNOCODE ); - i += iLookAhead; -#ifdef YYERRORSYMBOL - if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ - return yy_default[stateno]; - } -#else - assert( i>=0 && iyytos>yypParser->yystack ) yy_pop_parser_stack(yypParser); - /* Here code is inserted which will execute if the parser - ** stack every overflows */ -/******** Begin %stack_overflow code ******************************************/ -/******** End %stack_overflow code ********************************************/ - ParseARG_STORE /* Suppress warning about unused %extra_argument var */ - ParseCTX_STORE -} - -/* -** Print tracing information for a SHIFT action -*/ -#ifndef NDEBUG -static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){ - if( yyTraceFILE ){ - if( yyNewStateyytos->major], - yyNewState); - }else{ - fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n", - yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major], - yyNewState - YY_MIN_REDUCE); - } - } -} -#else -# define yyTraceShift(X,Y,Z) -#endif - -/* -** Perform a shift action. -*/ -static void yy_shift( - yyParser *yypParser, /* The parser to be shifted */ - YYACTIONTYPE yyNewState, /* The new state to shift in */ - YYCODETYPE yyMajor, /* The major token to shift in */ - ParseTOKENTYPE yyMinor /* The minor token to shift in */ -){ - yyStackEntry *yytos; - yypParser->yytos++; -#ifdef YYTRACKMAXSTACKDEPTH - if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){ - yypParser->yyhwm++; - assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) ); - } -#endif -#if YYSTACKDEPTH>0 - if( yypParser->yytos>yypParser->yystackEnd ){ - yypParser->yytos--; - yyStackOverflow(yypParser); - return; - } -#else - if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){ - if( yyGrowStack(yypParser) ){ - yypParser->yytos--; - yyStackOverflow(yypParser); - return; - } - } -#endif - if( yyNewState > YY_MAX_SHIFT ){ - yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; - } - yytos = yypParser->yytos; - yytos->stateno = yyNewState; - yytos->major = yyMajor; - yytos->minor.yy0 = yyMinor; - yyTraceShift(yypParser, yyNewState, "Shift"); -} - -/* The following table contains information about every rule that -** is used during the reduce. -*/ -static const struct { - YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */ - signed char nrhs; /* Negative of the number of RHS symbols in the rule */ -} yyRuleInfo[] = { - { 192, -1 }, /* (0) program ::= cmd */ - { 193, -2 }, /* (1) cmd ::= SHOW DATABASES */ - { 193, -2 }, /* (2) cmd ::= SHOW TOPICS */ - { 193, -2 }, /* (3) cmd ::= SHOW FUNCTIONS */ - { 193, -2 }, /* (4) cmd ::= SHOW MNODES */ - { 193, -2 }, /* (5) cmd ::= SHOW DNODES */ - { 193, -2 }, /* (6) cmd ::= SHOW ACCOUNTS */ - { 193, -2 }, /* (7) cmd ::= SHOW USERS */ - { 193, -2 }, /* (8) cmd ::= SHOW MODULES */ - { 193, -2 }, /* (9) cmd ::= SHOW QUERIES */ - { 193, -2 }, /* (10) cmd ::= SHOW CONNECTIONS */ - { 193, -2 }, /* (11) cmd ::= SHOW STREAMS */ - { 193, -2 }, /* (12) cmd ::= SHOW VARIABLES */ - { 193, -2 }, /* (13) cmd ::= SHOW SCORES */ - { 193, -2 }, /* (14) cmd ::= SHOW GRANTS */ - { 193, -2 }, /* (15) cmd ::= SHOW VNODES */ - { 193, -3 }, /* (16) cmd ::= SHOW VNODES ids */ - { 195, 0 }, /* (17) dbPrefix ::= */ - { 195, -2 }, /* (18) dbPrefix ::= ids DOT */ - { 196, 0 }, /* (19) cpxName ::= */ - { 196, -2 }, /* (20) cpxName ::= DOT ids */ - { 193, -5 }, /* (21) cmd ::= SHOW CREATE TABLE ids cpxName */ - { 193, -5 }, /* (22) cmd ::= SHOW CREATE STABLE ids cpxName */ - { 193, -4 }, /* (23) cmd ::= SHOW CREATE DATABASE ids */ - { 193, -3 }, /* (24) cmd ::= SHOW dbPrefix TABLES */ - { 193, -5 }, /* (25) cmd ::= SHOW dbPrefix TABLES LIKE ids */ - { 193, -3 }, /* (26) cmd ::= SHOW dbPrefix STABLES */ - { 193, -5 }, /* (27) cmd ::= SHOW dbPrefix STABLES LIKE ids */ - { 193, -3 }, /* (28) cmd ::= SHOW dbPrefix VGROUPS */ - { 193, -4 }, /* (29) cmd ::= SHOW dbPrefix VGROUPS ids */ - { 193, -5 }, /* (30) cmd ::= DROP TABLE ifexists ids cpxName */ - { 193, -5 }, /* (31) cmd ::= DROP STABLE ifexists ids cpxName */ - { 193, -4 }, /* (32) cmd ::= DROP DATABASE ifexists ids */ - { 193, -4 }, /* (33) cmd ::= DROP TOPIC ifexists ids */ - { 193, -3 }, /* (34) cmd ::= DROP FUNCTION ids */ - { 193, -3 }, /* (35) cmd ::= DROP DNODE ids */ - { 193, -3 }, /* (36) cmd ::= DROP USER ids */ - { 193, -3 }, /* (37) cmd ::= DROP ACCOUNT ids */ - { 193, -2 }, /* (38) cmd ::= USE ids */ - { 193, -3 }, /* (39) cmd ::= DESCRIBE ids cpxName */ - { 193, -3 }, /* (40) cmd ::= DESC ids cpxName */ - { 193, -5 }, /* (41) cmd ::= ALTER USER ids PASS ids */ - { 193, -5 }, /* (42) cmd ::= ALTER USER ids PRIVILEGE ids */ - { 193, -4 }, /* (43) cmd ::= ALTER DNODE ids ids */ - { 193, -5 }, /* (44) cmd ::= ALTER DNODE ids ids ids */ - { 193, -3 }, /* (45) cmd ::= ALTER LOCAL ids */ - { 193, -4 }, /* (46) cmd ::= ALTER LOCAL ids ids */ - { 193, -4 }, /* (47) cmd ::= ALTER DATABASE ids alter_db_optr */ - { 193, -4 }, /* (48) cmd ::= ALTER ACCOUNT ids acct_optr */ - { 193, -6 }, /* (49) cmd ::= ALTER ACCOUNT ids PASS ids acct_optr */ - { 193, -6 }, /* (50) cmd ::= COMPACT VNODES IN LP exprlist RP */ - { 194, -1 }, /* (51) ids ::= ID */ - { 197, -2 }, /* (52) ifexists ::= IF EXISTS */ - { 197, 0 }, /* (53) ifexists ::= */ - { 201, -3 }, /* (54) ifnotexists ::= IF NOT EXISTS */ - { 201, 0 }, /* (55) ifnotexists ::= */ - { 193, -5 }, /* (56) cmd ::= CREATE DNODE ids PORT ids */ - { 193, -5 }, /* (57) cmd ::= CREATE DNODE IPTOKEN PORT ids */ - { 193, -6 }, /* (58) cmd ::= CREATE ACCOUNT ids PASS ids acct_optr */ - { 193, -5 }, /* (59) cmd ::= CREATE DATABASE ifnotexists ids db_optr */ - { 193, -8 }, /* (60) cmd ::= CREATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize */ - { 193, -9 }, /* (61) cmd ::= CREATE AGGREGATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize */ - { 193, -5 }, /* (62) cmd ::= CREATE USER ids PASS ids */ - { 204, 0 }, /* (63) bufsize ::= */ - { 204, -2 }, /* (64) bufsize ::= BUFSIZE INTEGER */ - { 205, 0 }, /* (65) pps ::= */ - { 205, -2 }, /* (66) pps ::= PPS INTEGER */ - { 206, 0 }, /* (67) tseries ::= */ - { 206, -2 }, /* (68) tseries ::= TSERIES INTEGER */ - { 207, 0 }, /* (69) dbs ::= */ - { 207, -2 }, /* (70) dbs ::= DBS INTEGER */ - { 208, 0 }, /* (71) streams ::= */ - { 208, -2 }, /* (72) streams ::= STREAMS INTEGER */ - { 209, 0 }, /* (73) storage ::= */ - { 209, -2 }, /* (74) storage ::= STORAGE INTEGER */ - { 210, 0 }, /* (75) qtime ::= */ - { 210, -2 }, /* (76) qtime ::= QTIME INTEGER */ - { 211, 0 }, /* (77) users ::= */ - { 211, -2 }, /* (78) users ::= USERS INTEGER */ - { 212, 0 }, /* (79) conns ::= */ - { 212, -2 }, /* (80) conns ::= CONNS INTEGER */ - { 213, 0 }, /* (81) state ::= */ - { 213, -2 }, /* (82) state ::= STATE ids */ - { 199, -9 }, /* (83) acct_optr ::= pps tseries storage streams qtime dbs users conns state */ - { 214, -3 }, /* (84) intitemlist ::= intitemlist COMMA intitem */ - { 214, -1 }, /* (85) intitemlist ::= intitem */ - { 215, -1 }, /* (86) intitem ::= INTEGER */ - { 216, -2 }, /* (87) keep ::= KEEP intitemlist */ - { 217, -2 }, /* (88) cache ::= CACHE INTEGER */ - { 218, -2 }, /* (89) replica ::= REPLICA INTEGER */ - { 219, -2 }, /* (90) quorum ::= QUORUM INTEGER */ - { 220, -2 }, /* (91) days ::= DAYS INTEGER */ - { 221, -2 }, /* (92) minrows ::= MINROWS INTEGER */ - { 222, -2 }, /* (93) maxrows ::= MAXROWS INTEGER */ - { 223, -2 }, /* (94) blocks ::= BLOCKS INTEGER */ - { 224, -2 }, /* (95) ctime ::= CTIME INTEGER */ - { 225, -2 }, /* (96) wal ::= WAL INTEGER */ - { 226, -2 }, /* (97) fsync ::= FSYNC INTEGER */ - { 227, -2 }, /* (98) comp ::= COMP INTEGER */ - { 228, -2 }, /* (99) prec ::= PRECISION STRING */ - { 229, -2 }, /* (100) update ::= UPDATE INTEGER */ - { 230, -2 }, /* (101) cachelast ::= CACHELAST INTEGER */ - { 231, -2 }, /* (102) vgroups ::= VGROUPS INTEGER */ - { 232, -3 }, /* (103) stream_mode ::= STREAM MODE INTEGER */ - { 202, 0 }, /* (104) db_optr ::= */ - { 202, -2 }, /* (105) db_optr ::= db_optr cache */ - { 202, -2 }, /* (106) db_optr ::= db_optr replica */ - { 202, -2 }, /* (107) db_optr ::= db_optr quorum */ - { 202, -2 }, /* (108) db_optr ::= db_optr days */ - { 202, -2 }, /* (109) db_optr ::= db_optr minrows */ - { 202, -2 }, /* (110) db_optr ::= db_optr maxrows */ - { 202, -2 }, /* (111) db_optr ::= db_optr blocks */ - { 202, -2 }, /* (112) db_optr ::= db_optr ctime */ - { 202, -2 }, /* (113) db_optr ::= db_optr wal */ - { 202, -2 }, /* (114) db_optr ::= db_optr fsync */ - { 202, -2 }, /* (115) db_optr ::= db_optr comp */ - { 202, -2 }, /* (116) db_optr ::= db_optr prec */ - { 202, -2 }, /* (117) db_optr ::= db_optr keep */ - { 202, -2 }, /* (118) db_optr ::= db_optr update */ - { 202, -2 }, /* (119) db_optr ::= db_optr cachelast */ - { 202, -2 }, /* (120) db_optr ::= db_optr vgroups */ - { 202, -2 }, /* (121) db_optr ::= db_optr stream_mode */ - { 198, 0 }, /* (122) alter_db_optr ::= */ - { 198, -2 }, /* (123) alter_db_optr ::= alter_db_optr replica */ - { 198, -2 }, /* (124) alter_db_optr ::= alter_db_optr quorum */ - { 198, -2 }, /* (125) alter_db_optr ::= alter_db_optr keep */ - { 198, -2 }, /* (126) alter_db_optr ::= alter_db_optr blocks */ - { 198, -2 }, /* (127) alter_db_optr ::= alter_db_optr comp */ - { 198, -2 }, /* (128) alter_db_optr ::= alter_db_optr update */ - { 198, -2 }, /* (129) alter_db_optr ::= alter_db_optr cachelast */ - { 203, -1 }, /* (130) typename ::= ids */ - { 203, -4 }, /* (131) typename ::= ids LP signed RP */ - { 203, -2 }, /* (132) typename ::= ids UNSIGNED */ - { 233, -1 }, /* (133) signed ::= INTEGER */ - { 233, -2 }, /* (134) signed ::= PLUS INTEGER */ - { 233, -2 }, /* (135) signed ::= MINUS INTEGER */ - { 193, -3 }, /* (136) cmd ::= CREATE TABLE create_table_args */ - { 193, -3 }, /* (137) cmd ::= CREATE TABLE create_stable_args */ - { 193, -3 }, /* (138) cmd ::= CREATE STABLE create_stable_args */ - { 193, -3 }, /* (139) cmd ::= CREATE TABLE create_table_list */ - { 236, -1 }, /* (140) create_table_list ::= create_from_stable */ - { 236, -2 }, /* (141) create_table_list ::= create_table_list create_from_stable */ - { 234, -6 }, /* (142) create_table_args ::= ifnotexists ids cpxName LP columnlist RP */ - { 235, -10 }, /* (143) create_stable_args ::= ifnotexists ids cpxName LP columnlist RP TAGS LP columnlist RP */ - { 237, -10 }, /* (144) create_from_stable ::= ifnotexists ids cpxName USING ids cpxName TAGS LP tagitemlist1 RP */ - { 237, -13 }, /* (145) create_from_stable ::= ifnotexists ids cpxName USING ids cpxName LP tagNamelist RP TAGS LP tagitemlist1 RP */ - { 240, -3 }, /* (146) tagNamelist ::= tagNamelist COMMA ids */ - { 240, -1 }, /* (147) tagNamelist ::= ids */ - { 234, -5 }, /* (148) create_table_args ::= ifnotexists ids cpxName AS select */ - { 238, -3 }, /* (149) columnlist ::= columnlist COMMA column */ - { 238, -1 }, /* (150) columnlist ::= column */ - { 242, -2 }, /* (151) column ::= ids typename */ - { 239, -3 }, /* (152) tagitemlist1 ::= tagitemlist1 COMMA tagitem1 */ - { 239, -1 }, /* (153) tagitemlist1 ::= tagitem1 */ - { 243, -2 }, /* (154) tagitem1 ::= MINUS INTEGER */ - { 243, -2 }, /* (155) tagitem1 ::= MINUS FLOAT */ - { 243, -2 }, /* (156) tagitem1 ::= PLUS INTEGER */ - { 243, -2 }, /* (157) tagitem1 ::= PLUS FLOAT */ - { 243, -1 }, /* (158) tagitem1 ::= INTEGER */ - { 243, -1 }, /* (159) tagitem1 ::= FLOAT */ - { 243, -1 }, /* (160) tagitem1 ::= STRING */ - { 243, -1 }, /* (161) tagitem1 ::= BOOL */ - { 243, -1 }, /* (162) tagitem1 ::= NULL */ - { 243, -1 }, /* (163) tagitem1 ::= NOW */ - { 244, -3 }, /* (164) tagitemlist ::= tagitemlist COMMA tagitem */ - { 244, -1 }, /* (165) tagitemlist ::= tagitem */ - { 245, -1 }, /* (166) tagitem ::= INTEGER */ - { 245, -1 }, /* (167) tagitem ::= FLOAT */ - { 245, -1 }, /* (168) tagitem ::= STRING */ - { 245, -1 }, /* (169) tagitem ::= BOOL */ - { 245, -1 }, /* (170) tagitem ::= NULL */ - { 245, -1 }, /* (171) tagitem ::= NOW */ - { 245, -2 }, /* (172) tagitem ::= MINUS INTEGER */ - { 245, -2 }, /* (173) tagitem ::= MINUS FLOAT */ - { 245, -2 }, /* (174) tagitem ::= PLUS INTEGER */ - { 245, -2 }, /* (175) tagitem ::= PLUS FLOAT */ - { 241, -14 }, /* (176) select ::= SELECT selcollist from where_opt interval_option sliding_opt session_option windowstate_option fill_opt groupby_opt having_opt orderby_opt slimit_opt limit_opt */ - { 241, -3 }, /* (177) select ::= LP select RP */ - { 259, -1 }, /* (178) union ::= select */ - { 259, -4 }, /* (179) union ::= union UNION ALL select */ - { 259, -3 }, /* (180) union ::= union UNION select */ - { 193, -1 }, /* (181) cmd ::= union */ - { 241, -2 }, /* (182) select ::= SELECT selcollist */ - { 260, -2 }, /* (183) sclp ::= selcollist COMMA */ - { 260, 0 }, /* (184) sclp ::= */ - { 246, -4 }, /* (185) selcollist ::= sclp distinct expr as */ - { 246, -2 }, /* (186) selcollist ::= sclp STAR */ - { 263, -2 }, /* (187) as ::= AS ids */ - { 263, -1 }, /* (188) as ::= ids */ - { 263, 0 }, /* (189) as ::= */ - { 261, -1 }, /* (190) distinct ::= DISTINCT */ - { 261, 0 }, /* (191) distinct ::= */ - { 247, -2 }, /* (192) from ::= FROM tablelist */ - { 247, -2 }, /* (193) from ::= FROM sub */ - { 265, -3 }, /* (194) sub ::= LP union RP */ - { 265, -4 }, /* (195) sub ::= LP union RP ids */ - { 265, -6 }, /* (196) sub ::= sub COMMA LP union RP ids */ - { 264, -2 }, /* (197) tablelist ::= ids cpxName */ - { 264, -3 }, /* (198) tablelist ::= ids cpxName ids */ - { 264, -4 }, /* (199) tablelist ::= tablelist COMMA ids cpxName */ - { 264, -5 }, /* (200) tablelist ::= tablelist COMMA ids cpxName ids */ - { 266, -1 }, /* (201) tmvar ::= VARIABLE */ - { 249, -4 }, /* (202) interval_option ::= intervalKey LP tmvar RP */ - { 249, -6 }, /* (203) interval_option ::= intervalKey LP tmvar COMMA tmvar RP */ - { 249, 0 }, /* (204) interval_option ::= */ - { 267, -1 }, /* (205) intervalKey ::= INTERVAL */ - { 267, -1 }, /* (206) intervalKey ::= EVERY */ - { 251, 0 }, /* (207) session_option ::= */ - { 251, -7 }, /* (208) session_option ::= SESSION LP ids cpxName COMMA tmvar RP */ - { 252, 0 }, /* (209) windowstate_option ::= */ - { 252, -4 }, /* (210) windowstate_option ::= STATE_WINDOW LP ids RP */ - { 253, 0 }, /* (211) fill_opt ::= */ - { 253, -6 }, /* (212) fill_opt ::= FILL LP ID COMMA tagitemlist RP */ - { 253, -4 }, /* (213) fill_opt ::= FILL LP ID RP */ - { 250, -4 }, /* (214) sliding_opt ::= SLIDING LP tmvar RP */ - { 250, 0 }, /* (215) sliding_opt ::= */ - { 256, 0 }, /* (216) orderby_opt ::= */ - { 256, -3 }, /* (217) orderby_opt ::= ORDER BY sortlist */ - { 268, -4 }, /* (218) sortlist ::= sortlist COMMA item sortorder */ - { 268, -2 }, /* (219) sortlist ::= item sortorder */ - { 270, -2 }, /* (220) item ::= ids cpxName */ - { 271, -1 }, /* (221) sortorder ::= ASC */ - { 271, -1 }, /* (222) sortorder ::= DESC */ - { 271, 0 }, /* (223) sortorder ::= */ - { 254, 0 }, /* (224) groupby_opt ::= */ - { 254, -3 }, /* (225) groupby_opt ::= GROUP BY grouplist */ - { 272, -3 }, /* (226) grouplist ::= grouplist COMMA item */ - { 272, -1 }, /* (227) grouplist ::= item */ - { 255, 0 }, /* (228) having_opt ::= */ - { 255, -2 }, /* (229) having_opt ::= HAVING expr */ - { 258, 0 }, /* (230) limit_opt ::= */ - { 258, -2 }, /* (231) limit_opt ::= LIMIT signed */ - { 258, -4 }, /* (232) limit_opt ::= LIMIT signed OFFSET signed */ - { 258, -4 }, /* (233) limit_opt ::= LIMIT signed COMMA signed */ - { 257, 0 }, /* (234) slimit_opt ::= */ - { 257, -2 }, /* (235) slimit_opt ::= SLIMIT signed */ - { 257, -4 }, /* (236) slimit_opt ::= SLIMIT signed SOFFSET signed */ - { 257, -4 }, /* (237) slimit_opt ::= SLIMIT signed COMMA signed */ - { 248, 0 }, /* (238) where_opt ::= */ - { 248, -2 }, /* (239) where_opt ::= WHERE expr */ - { 262, -3 }, /* (240) expr ::= LP expr RP */ - { 262, -1 }, /* (241) expr ::= ID */ - { 262, -3 }, /* (242) expr ::= ID DOT ID */ - { 262, -3 }, /* (243) expr ::= ID DOT STAR */ - { 262, -1 }, /* (244) expr ::= INTEGER */ - { 262, -2 }, /* (245) expr ::= MINUS INTEGER */ - { 262, -2 }, /* (246) expr ::= PLUS INTEGER */ - { 262, -1 }, /* (247) expr ::= FLOAT */ - { 262, -2 }, /* (248) expr ::= MINUS FLOAT */ - { 262, -2 }, /* (249) expr ::= PLUS FLOAT */ - { 262, -1 }, /* (250) expr ::= STRING */ - { 262, -1 }, /* (251) expr ::= NOW */ - { 262, -1 }, /* (252) expr ::= VARIABLE */ - { 262, -2 }, /* (253) expr ::= PLUS VARIABLE */ - { 262, -2 }, /* (254) expr ::= MINUS VARIABLE */ - { 262, -1 }, /* (255) expr ::= BOOL */ - { 262, -1 }, /* (256) expr ::= NULL */ - { 262, -4 }, /* (257) expr ::= ID LP exprlist RP */ - { 262, -4 }, /* (258) expr ::= ID LP STAR RP */ - { 262, -3 }, /* (259) expr ::= expr IS NULL */ - { 262, -4 }, /* (260) expr ::= expr IS NOT NULL */ - { 262, -3 }, /* (261) expr ::= expr LT expr */ - { 262, -3 }, /* (262) expr ::= expr GT expr */ - { 262, -3 }, /* (263) expr ::= expr LE expr */ - { 262, -3 }, /* (264) expr ::= expr GE expr */ - { 262, -3 }, /* (265) expr ::= expr NE expr */ - { 262, -3 }, /* (266) expr ::= expr EQ expr */ - { 262, -5 }, /* (267) expr ::= expr BETWEEN expr AND expr */ - { 262, -3 }, /* (268) expr ::= expr AND expr */ - { 262, -3 }, /* (269) expr ::= expr OR expr */ - { 262, -3 }, /* (270) expr ::= expr PLUS expr */ - { 262, -3 }, /* (271) expr ::= expr MINUS expr */ - { 262, -3 }, /* (272) expr ::= expr STAR expr */ - { 262, -3 }, /* (273) expr ::= expr SLASH expr */ - { 262, -3 }, /* (274) expr ::= expr REM expr */ - { 262, -3 }, /* (275) expr ::= expr LIKE expr */ - { 262, -3 }, /* (276) expr ::= expr MATCH expr */ - { 262, -3 }, /* (277) expr ::= expr NMATCH expr */ - { 262, -5 }, /* (278) expr ::= expr IN LP exprlist RP */ - { 200, -3 }, /* (279) exprlist ::= exprlist COMMA expritem */ - { 200, -1 }, /* (280) exprlist ::= expritem */ - { 273, -1 }, /* (281) expritem ::= expr */ - { 273, 0 }, /* (282) expritem ::= */ - { 193, -3 }, /* (283) cmd ::= RESET QUERY CACHE */ - { 193, -3 }, /* (284) cmd ::= SYNCDB ids REPLICA */ - { 193, -7 }, /* (285) cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ - { 193, -7 }, /* (286) cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ - { 193, -7 }, /* (287) cmd ::= ALTER TABLE ids cpxName MODIFY COLUMN columnlist */ - { 193, -7 }, /* (288) cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ - { 193, -7 }, /* (289) cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ - { 193, -8 }, /* (290) cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ - { 193, -9 }, /* (291) cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ - { 193, -7 }, /* (292) cmd ::= ALTER TABLE ids cpxName MODIFY TAG columnlist */ - { 193, -7 }, /* (293) cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist */ - { 193, -7 }, /* (294) cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids */ - { 193, -7 }, /* (295) cmd ::= ALTER STABLE ids cpxName MODIFY COLUMN columnlist */ - { 193, -7 }, /* (296) cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist */ - { 193, -7 }, /* (297) cmd ::= ALTER STABLE ids cpxName DROP TAG ids */ - { 193, -8 }, /* (298) cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids */ - { 193, -9 }, /* (299) cmd ::= ALTER STABLE ids cpxName SET TAG ids EQ tagitem */ - { 193, -7 }, /* (300) cmd ::= ALTER STABLE ids cpxName MODIFY TAG columnlist */ - { 193, -3 }, /* (301) cmd ::= KILL CONNECTION INTEGER */ - { 193, -5 }, /* (302) cmd ::= KILL STREAM INTEGER COLON INTEGER */ - { 193, -5 }, /* (303) cmd ::= KILL QUERY INTEGER COLON INTEGER */ -}; - -static void yy_accept(yyParser*); /* Forward Declaration */ - -/* -** Perform a reduce action and the shift that must immediately -** follow the reduce. -** -** The yyLookahead and yyLookaheadToken parameters provide reduce actions -** access to the lookahead token (if any). The yyLookahead will be YYNOCODE -** if the lookahead token has already been consumed. As this procedure is -** only called from one place, optimizing compilers will in-line it, which -** means that the extra parameters have no performance impact. -*/ -static YYACTIONTYPE yy_reduce( - yyParser *yypParser, /* The parser */ - unsigned int yyruleno, /* Number of the rule by which to reduce */ - int yyLookahead, /* Lookahead token, or YYNOCODE if none */ - ParseTOKENTYPE yyLookaheadToken /* Value of the lookahead token */ - ParseCTX_PDECL /* %extra_context */ -){ - int yygoto; /* The next state */ - YYACTIONTYPE yyact; /* The next action */ - yyStackEntry *yymsp; /* The top of the parser's stack */ - int yysize; /* Amount to pop the stack */ - ParseARG_FETCH - (void)yyLookahead; - (void)yyLookaheadToken; - yymsp = yypParser->yytos; -#ifndef NDEBUG - if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ - yysize = yyRuleInfo[yyruleno].nrhs; - if( yysize ){ - fprintf(yyTraceFILE, "%sReduce %d [%s], go to state %d.\n", - yyTracePrompt, - yyruleno, yyRuleName[yyruleno], yymsp[yysize].stateno); - }else{ - fprintf(yyTraceFILE, "%sReduce %d [%s].\n", - yyTracePrompt, yyruleno, yyRuleName[yyruleno]); - } - } -#endif /* NDEBUG */ - - /* Check that the stack is large enough to grow by a single entry - ** if the RHS of the rule is empty. This ensures that there is room - ** enough on the stack to push the LHS value */ - if( yyRuleInfo[yyruleno].nrhs==0 ){ -#ifdef YYTRACKMAXSTACKDEPTH - if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){ - yypParser->yyhwm++; - assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack)); - } -#endif -#if YYSTACKDEPTH>0 - if( yypParser->yytos>=yypParser->yystackEnd ){ - yyStackOverflow(yypParser); - /* The call to yyStackOverflow() above pops the stack until it is - ** empty, causing the main parser loop to exit. So the return value - ** is never used and does not matter. */ - return 0; - } -#else - if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){ - if( yyGrowStack(yypParser) ){ - yyStackOverflow(yypParser); - /* The call to yyStackOverflow() above pops the stack until it is - ** empty, causing the main parser loop to exit. So the return value - ** is never used and does not matter. */ - return 0; - } - yymsp = yypParser->yytos; - } -#endif - } - - switch( yyruleno ){ - /* Beginning here are the reduction cases. A typical example - ** follows: - ** case 0: - ** #line - ** { ... } // User supplied code - ** #line - ** break; - */ -/********** Begin reduce actions **********************************************/ - YYMINORTYPE yylhsminor; - case 0: /* program ::= cmd */ - case 136: /* cmd ::= CREATE TABLE create_table_args */ yytestcase(yyruleno==136); - case 137: /* cmd ::= CREATE TABLE create_stable_args */ yytestcase(yyruleno==137); - case 138: /* cmd ::= CREATE STABLE create_stable_args */ yytestcase(yyruleno==138); -{} - break; - case 1: /* cmd ::= SHOW DATABASES */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_DB, 0, 0);} - break; - case 2: /* cmd ::= SHOW TOPICS */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_TP, 0, 0);} - break; - case 3: /* cmd ::= SHOW FUNCTIONS */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_FUNC, 0, 0);} - break; - case 4: /* cmd ::= SHOW MNODES */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_MNODE, 0, 0);} - break; - case 5: /* cmd ::= SHOW DNODES */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_DNODE, 0, 0);} - break; - case 6: /* cmd ::= SHOW ACCOUNTS */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_ACCT, 0, 0);} - break; - case 7: /* cmd ::= SHOW USERS */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_USER, 0, 0);} - break; - case 8: /* cmd ::= SHOW MODULES */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_MODULE, 0, 0); } - break; - case 9: /* cmd ::= SHOW QUERIES */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_QUERIES, 0, 0); } - break; - case 10: /* cmd ::= SHOW CONNECTIONS */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_CONNS, 0, 0);} - break; - case 11: /* cmd ::= SHOW STREAMS */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_STREAMS, 0, 0); } - break; - case 12: /* cmd ::= SHOW VARIABLES */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_VARIABLES, 0, 0); } - break; - case 13: /* cmd ::= SHOW SCORES */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_TRANS, 0, 0); } - break; - case 14: /* cmd ::= SHOW GRANTS */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_GRANTS, 0, 0); } - break; - case 15: /* cmd ::= SHOW VNODES */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_VNODES, 0, 0); } - break; - case 16: /* cmd ::= SHOW VNODES ids */ -{ setShowOptions(pInfo, TSDB_MGMT_TABLE_VNODES, &yymsp[0].minor.yy0, 0); } - break; - case 17: /* dbPrefix ::= */ -{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.type = 0;} - break; - case 18: /* dbPrefix ::= ids DOT */ -{yylhsminor.yy0 = yymsp[-1].minor.yy0; } - yymsp[-1].minor.yy0 = yylhsminor.yy0; - break; - case 19: /* cpxName ::= */ -{yymsp[1].minor.yy0.n = 0; } - break; - case 20: /* cpxName ::= DOT ids */ -{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n += 1; } - break; - case 21: /* cmd ::= SHOW CREATE TABLE ids cpxName */ -{ - yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_TABLE, 1, &yymsp[-1].minor.yy0); -} - break; - case 22: /* cmd ::= SHOW CREATE STABLE ids cpxName */ -{ - yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_STABLE, 1, &yymsp[-1].minor.yy0); -} - break; - case 23: /* cmd ::= SHOW CREATE DATABASE ids */ -{ - setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_DATABASE, 1, &yymsp[0].minor.yy0); -} - break; - case 24: /* cmd ::= SHOW dbPrefix TABLES */ -{ - setShowOptions(pInfo, TSDB_MGMT_TABLE_TABLE, &yymsp[-1].minor.yy0, 0); -} - break; - case 25: /* cmd ::= SHOW dbPrefix TABLES LIKE ids */ -{ - setShowOptions(pInfo, TSDB_MGMT_TABLE_TABLE, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0); -} - break; - case 26: /* cmd ::= SHOW dbPrefix STABLES */ -{ - setShowOptions(pInfo, TSDB_MGMT_TABLE_STB, &yymsp[-1].minor.yy0, 0); -} - break; - case 27: /* cmd ::= SHOW dbPrefix STABLES LIKE ids */ -{ - SToken token; - tSetDbName(&token, &yymsp[-3].minor.yy0); - setShowOptions(pInfo, TSDB_MGMT_TABLE_STB, &token, &yymsp[0].minor.yy0); -} - break; - case 28: /* cmd ::= SHOW dbPrefix VGROUPS */ -{ - SToken token; - tSetDbName(&token, &yymsp[-1].minor.yy0); - setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, 0); -} - break; - case 29: /* cmd ::= SHOW dbPrefix VGROUPS ids */ -{ - SToken token; - tSetDbName(&token, &yymsp[-2].minor.yy0); - setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, &yymsp[0].minor.yy0); -} - break; - case 30: /* cmd ::= DROP TABLE ifexists ids cpxName */ -{ - yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - setDropDbTableInfo(pInfo, TSDB_SQL_DROP_TABLE, &yymsp[-1].minor.yy0, &yymsp[-2].minor.yy0, -1, -1); -} - break; - case 31: /* cmd ::= DROP STABLE ifexists ids cpxName */ -{ - yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - setDropDbTableInfo(pInfo, TSDB_SQL_DROP_TABLE, &yymsp[-1].minor.yy0, &yymsp[-2].minor.yy0, -1, TSDB_SUPER_TABLE); -} - break; - case 32: /* cmd ::= DROP DATABASE ifexists ids */ -{ setDropDbTableInfo(pInfo, TSDB_SQL_DROP_DB, &yymsp[0].minor.yy0, &yymsp[-1].minor.yy0, TSDB_DB_TYPE_DEFAULT, -1); } - break; - case 33: /* cmd ::= DROP TOPIC ifexists ids */ -{ setDropDbTableInfo(pInfo, TSDB_SQL_DROP_DB, &yymsp[0].minor.yy0, &yymsp[-1].minor.yy0, TSDB_DB_TYPE_TOPIC, -1); } - break; - case 34: /* cmd ::= DROP FUNCTION ids */ -{ setDropFuncInfo(pInfo, TSDB_SQL_DROP_FUNCTION, &yymsp[0].minor.yy0); } - break; - case 35: /* cmd ::= DROP DNODE ids */ -{ setDCLSqlElems(pInfo, TSDB_SQL_DROP_DNODE, 1, &yymsp[0].minor.yy0); } - break; - case 36: /* cmd ::= DROP USER ids */ -{ setDCLSqlElems(pInfo, TSDB_SQL_DROP_USER, 1, &yymsp[0].minor.yy0); } - break; - case 37: /* cmd ::= DROP ACCOUNT ids */ -{ setDCLSqlElems(pInfo, TSDB_SQL_DROP_ACCT, 1, &yymsp[0].minor.yy0); } - break; - case 38: /* cmd ::= USE ids */ -{ setDCLSqlElems(pInfo, TSDB_SQL_USE_DB, 1, &yymsp[0].minor.yy0);} - break; - case 39: /* cmd ::= DESCRIBE ids cpxName */ - case 40: /* cmd ::= DESC ids cpxName */ yytestcase(yyruleno==40); -{ - yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - setDCLSqlElems(pInfo, TSDB_SQL_DESCRIBE_TABLE, 1, &yymsp[-1].minor.yy0); -} - break; - case 41: /* cmd ::= ALTER USER ids PASS ids */ -{ setAlterUserSql(pInfo, TSDB_ALTER_USER_PASSWD, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, NULL); } - break; - case 42: /* cmd ::= ALTER USER ids PRIVILEGE ids */ -{ setAlterUserSql(pInfo, TSDB_ALTER_USER_PRIVILEGES, &yymsp[-2].minor.yy0, NULL, &yymsp[0].minor.yy0);} - break; - case 43: /* cmd ::= ALTER DNODE ids ids */ -{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_DNODE, 2, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } - break; - case 44: /* cmd ::= ALTER DNODE ids ids ids */ -{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_DNODE, 3, &yymsp[-2].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } - break; - case 45: /* cmd ::= ALTER LOCAL ids */ -{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_LOCAL, 1, &yymsp[0].minor.yy0); } - break; - case 46: /* cmd ::= ALTER LOCAL ids ids */ -{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_LOCAL, 2, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } - break; - case 47: /* cmd ::= ALTER DATABASE ids alter_db_optr */ -{ SToken t = {0}; setCreateDbInfo(pInfo, TSDB_SQL_ALTER_DB, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy326, &t);} - break; - case 48: /* cmd ::= ALTER ACCOUNT ids acct_optr */ -{ setCreateAcctSql(pInfo, TSDB_SQL_ALTER_ACCT, &yymsp[-1].minor.yy0, NULL, &yymsp[0].minor.yy523);} - break; - case 49: /* cmd ::= ALTER ACCOUNT ids PASS ids acct_optr */ -{ setCreateAcctSql(pInfo, TSDB_SQL_ALTER_ACCT, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy523);} - break; - case 50: /* cmd ::= COMPACT VNODES IN LP exprlist RP */ -{ setCompactVnodeSql(pInfo, TSDB_SQL_COMPACT_VNODE, yymsp[-1].minor.yy225);} - break; - case 51: /* ids ::= ID */ -{yylhsminor.yy0 = yymsp[0].minor.yy0; } - yymsp[0].minor.yy0 = yylhsminor.yy0; - break; - case 52: /* ifexists ::= IF EXISTS */ -{ yymsp[-1].minor.yy0.n = 1;} - break; - case 53: /* ifexists ::= */ - case 55: /* ifnotexists ::= */ yytestcase(yyruleno==55); - case 191: /* distinct ::= */ yytestcase(yyruleno==191); -{ yymsp[1].minor.yy0.n = 0;} - break; - case 54: /* ifnotexists ::= IF NOT EXISTS */ -{ yymsp[-2].minor.yy0.n = 1;} - break; - case 56: /* cmd ::= CREATE DNODE ids PORT ids */ - case 57: /* cmd ::= CREATE DNODE IPTOKEN PORT ids */ yytestcase(yyruleno==57); -{ setDCLSqlElems(pInfo, TSDB_SQL_CREATE_DNODE, 2, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);} - break; - case 58: /* cmd ::= CREATE ACCOUNT ids PASS ids acct_optr */ -{ setCreateAcctSql(pInfo, TSDB_SQL_CREATE_ACCT, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy523);} - break; - case 59: /* cmd ::= CREATE DATABASE ifnotexists ids db_optr */ -{ setCreateDbInfo(pInfo, TSDB_SQL_CREATE_DB, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy326, &yymsp[-2].minor.yy0);} - break; - case 60: /* cmd ::= CREATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize */ -{ setCreateFuncInfo(pInfo, TSDB_SQL_CREATE_FUNCTION, &yymsp[-5].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy16, &yymsp[0].minor.yy0, 1);} - break; - case 61: /* cmd ::= CREATE AGGREGATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize */ -{ setCreateFuncInfo(pInfo, TSDB_SQL_CREATE_FUNCTION, &yymsp[-5].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy16, &yymsp[0].minor.yy0, 2);} - break; - case 62: /* cmd ::= CREATE USER ids PASS ids */ -{ setCreateUserSql(pInfo, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);} - break; - case 63: /* bufsize ::= */ - case 65: /* pps ::= */ yytestcase(yyruleno==65); - case 67: /* tseries ::= */ yytestcase(yyruleno==67); - case 69: /* dbs ::= */ yytestcase(yyruleno==69); - case 71: /* streams ::= */ yytestcase(yyruleno==71); - case 73: /* storage ::= */ yytestcase(yyruleno==73); - case 75: /* qtime ::= */ yytestcase(yyruleno==75); - case 77: /* users ::= */ yytestcase(yyruleno==77); - case 79: /* conns ::= */ yytestcase(yyruleno==79); - case 81: /* state ::= */ yytestcase(yyruleno==81); -{ yymsp[1].minor.yy0.n = 0; } - break; - case 64: /* bufsize ::= BUFSIZE INTEGER */ - case 66: /* pps ::= PPS INTEGER */ yytestcase(yyruleno==66); - case 68: /* tseries ::= TSERIES INTEGER */ yytestcase(yyruleno==68); - case 70: /* dbs ::= DBS INTEGER */ yytestcase(yyruleno==70); - case 72: /* streams ::= STREAMS INTEGER */ yytestcase(yyruleno==72); - case 74: /* storage ::= STORAGE INTEGER */ yytestcase(yyruleno==74); - case 76: /* qtime ::= QTIME INTEGER */ yytestcase(yyruleno==76); - case 78: /* users ::= USERS INTEGER */ yytestcase(yyruleno==78); - case 80: /* conns ::= CONNS INTEGER */ yytestcase(yyruleno==80); - case 82: /* state ::= STATE ids */ yytestcase(yyruleno==82); -{ yymsp[-1].minor.yy0 = yymsp[0].minor.yy0; } - break; - case 83: /* acct_optr ::= pps tseries storage streams qtime dbs users conns state */ -{ - yylhsminor.yy523.maxUsers = (yymsp[-2].minor.yy0.n>0)?atoi(yymsp[-2].minor.yy0.z):-1; - yylhsminor.yy523.maxDbs = (yymsp[-3].minor.yy0.n>0)?atoi(yymsp[-3].minor.yy0.z):-1; - yylhsminor.yy523.maxTimeSeries = (yymsp[-7].minor.yy0.n>0)?atoi(yymsp[-7].minor.yy0.z):-1; - yylhsminor.yy523.maxStreams = (yymsp[-5].minor.yy0.n>0)?atoi(yymsp[-5].minor.yy0.z):-1; - yylhsminor.yy523.maxPointsPerSecond = (yymsp[-8].minor.yy0.n>0)?atoi(yymsp[-8].minor.yy0.z):-1; - yylhsminor.yy523.maxStorage = (yymsp[-6].minor.yy0.n>0)?strtoll(yymsp[-6].minor.yy0.z, NULL, 10):-1; - yylhsminor.yy523.maxQueryTime = (yymsp[-4].minor.yy0.n>0)?strtoll(yymsp[-4].minor.yy0.z, NULL, 10):-1; - yylhsminor.yy523.maxConnections = (yymsp[-1].minor.yy0.n>0)?atoi(yymsp[-1].minor.yy0.z):-1; - yylhsminor.yy523.stat = yymsp[0].minor.yy0; -} - yymsp[-8].minor.yy523 = yylhsminor.yy523; - break; - case 84: /* intitemlist ::= intitemlist COMMA intitem */ - case 164: /* tagitemlist ::= tagitemlist COMMA tagitem */ yytestcase(yyruleno==164); -{ yylhsminor.yy225 = tListItemAppend(yymsp[-2].minor.yy225, &yymsp[0].minor.yy1, -1); } - yymsp[-2].minor.yy225 = yylhsminor.yy225; - break; - case 85: /* intitemlist ::= intitem */ - case 165: /* tagitemlist ::= tagitem */ yytestcase(yyruleno==165); -{ yylhsminor.yy225 = tListItemAppend(NULL, &yymsp[0].minor.yy1, -1); } - yymsp[0].minor.yy225 = yylhsminor.yy225; - break; - case 86: /* intitem ::= INTEGER */ - case 166: /* tagitem ::= INTEGER */ yytestcase(yyruleno==166); - case 167: /* tagitem ::= FLOAT */ yytestcase(yyruleno==167); - case 168: /* tagitem ::= STRING */ yytestcase(yyruleno==168); - case 169: /* tagitem ::= BOOL */ yytestcase(yyruleno==169); -{ toTSDBType(yymsp[0].minor.yy0.type); taosVariantCreate(&yylhsminor.yy1, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.type); } - yymsp[0].minor.yy1 = yylhsminor.yy1; - break; - case 87: /* keep ::= KEEP intitemlist */ -{ yymsp[-1].minor.yy225 = yymsp[0].minor.yy225; } - break; - case 88: /* cache ::= CACHE INTEGER */ - case 89: /* replica ::= REPLICA INTEGER */ yytestcase(yyruleno==89); - case 90: /* quorum ::= QUORUM INTEGER */ yytestcase(yyruleno==90); - case 91: /* days ::= DAYS INTEGER */ yytestcase(yyruleno==91); - case 92: /* minrows ::= MINROWS INTEGER */ yytestcase(yyruleno==92); - case 93: /* maxrows ::= MAXROWS INTEGER */ yytestcase(yyruleno==93); - case 94: /* blocks ::= BLOCKS INTEGER */ yytestcase(yyruleno==94); - case 95: /* ctime ::= CTIME INTEGER */ yytestcase(yyruleno==95); - case 96: /* wal ::= WAL INTEGER */ yytestcase(yyruleno==96); - case 97: /* fsync ::= FSYNC INTEGER */ yytestcase(yyruleno==97); - case 98: /* comp ::= COMP INTEGER */ yytestcase(yyruleno==98); - case 99: /* prec ::= PRECISION STRING */ yytestcase(yyruleno==99); - case 100: /* update ::= UPDATE INTEGER */ yytestcase(yyruleno==100); - case 101: /* cachelast ::= CACHELAST INTEGER */ yytestcase(yyruleno==101); - case 102: /* vgroups ::= VGROUPS INTEGER */ yytestcase(yyruleno==102); -{ yymsp[-1].minor.yy0 = yymsp[0].minor.yy0; } - break; - case 103: /* stream_mode ::= STREAM MODE INTEGER */ -{ yymsp[-2].minor.yy0 = yymsp[0].minor.yy0; } - break; - case 104: /* db_optr ::= */ -{setDefaultCreateDbOption(&yymsp[1].minor.yy326);} - break; - case 105: /* db_optr ::= db_optr cache */ -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.cacheBlockSize = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 106: /* db_optr ::= db_optr replica */ - case 123: /* alter_db_optr ::= alter_db_optr replica */ yytestcase(yyruleno==123); -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.replica = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 107: /* db_optr ::= db_optr quorum */ - case 124: /* alter_db_optr ::= alter_db_optr quorum */ yytestcase(yyruleno==124); -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.quorum = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 108: /* db_optr ::= db_optr days */ -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.daysPerFile = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 109: /* db_optr ::= db_optr minrows */ -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.minRowsPerBlock = strtod(yymsp[0].minor.yy0.z, NULL); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 110: /* db_optr ::= db_optr maxrows */ -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.maxRowsPerBlock = strtod(yymsp[0].minor.yy0.z, NULL); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 111: /* db_optr ::= db_optr blocks */ - case 126: /* alter_db_optr ::= alter_db_optr blocks */ yytestcase(yyruleno==126); -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.numOfBlocks = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 112: /* db_optr ::= db_optr ctime */ -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.commitTime = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 113: /* db_optr ::= db_optr wal */ -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.walLevel = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 114: /* db_optr ::= db_optr fsync */ -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.fsyncPeriod = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 115: /* db_optr ::= db_optr comp */ - case 127: /* alter_db_optr ::= alter_db_optr comp */ yytestcase(yyruleno==127); -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.compressionLevel = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 116: /* db_optr ::= db_optr prec */ -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.precision = yymsp[0].minor.yy0; } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 117: /* db_optr ::= db_optr keep */ - case 125: /* alter_db_optr ::= alter_db_optr keep */ yytestcase(yyruleno==125); -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.keep = yymsp[0].minor.yy225; } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 118: /* db_optr ::= db_optr update */ - case 128: /* alter_db_optr ::= alter_db_optr update */ yytestcase(yyruleno==128); -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.update = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 119: /* db_optr ::= db_optr cachelast */ - case 129: /* alter_db_optr ::= alter_db_optr cachelast */ yytestcase(yyruleno==129); -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.cachelast = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 120: /* db_optr ::= db_optr vgroups */ -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.numOfVgroups = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 121: /* db_optr ::= db_optr stream_mode */ -{ yylhsminor.yy326 = yymsp[-1].minor.yy326; yylhsminor.yy326.streamMode = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy326 = yylhsminor.yy326; - break; - case 122: /* alter_db_optr ::= */ -{ setDefaultCreateDbOption(&yymsp[1].minor.yy326);} - break; - case 130: /* typename ::= ids */ -{ - yymsp[0].minor.yy0.type = 0; - tSetColumnType (&yylhsminor.yy16, &yymsp[0].minor.yy0); -} - yymsp[0].minor.yy16 = yylhsminor.yy16; - break; - case 131: /* typename ::= ids LP signed RP */ -{ - if (yymsp[-1].minor.yy61 <= 0) { - yymsp[-3].minor.yy0.type = 0; - tSetColumnType(&yylhsminor.yy16, &yymsp[-3].minor.yy0); - } else { - yymsp[-3].minor.yy0.type = -yymsp[-1].minor.yy61; // negative value of name length - tSetColumnType(&yylhsminor.yy16, &yymsp[-3].minor.yy0); - } -} - yymsp[-3].minor.yy16 = yylhsminor.yy16; - break; - case 132: /* typename ::= ids UNSIGNED */ -{ - yymsp[-1].minor.yy0.type = 0; - yymsp[-1].minor.yy0.n = ((yymsp[0].minor.yy0.z + yymsp[0].minor.yy0.n) - yymsp[-1].minor.yy0.z); - tSetColumnType (&yylhsminor.yy16, &yymsp[-1].minor.yy0); -} - yymsp[-1].minor.yy16 = yylhsminor.yy16; - break; - case 133: /* signed ::= INTEGER */ -{ yylhsminor.yy61 = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[0].minor.yy61 = yylhsminor.yy61; - break; - case 134: /* signed ::= PLUS INTEGER */ -{ yymsp[-1].minor.yy61 = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - break; - case 135: /* signed ::= MINUS INTEGER */ -{ yymsp[-1].minor.yy61 = -strtol(yymsp[0].minor.yy0.z, NULL, 10);} - break; - case 139: /* cmd ::= CREATE TABLE create_table_list */ -{ pInfo->type = TSDB_SQL_CREATE_TABLE; pInfo->pCreateTableInfo = yymsp[0].minor.yy482;} - break; - case 140: /* create_table_list ::= create_from_stable */ -{ - SCreateTableSql* pCreateTable = calloc(1, sizeof(SCreateTableSql)); - pCreateTable->childTableInfo = taosArrayInit(4, sizeof(SCreatedTableInfo)); - - taosArrayPush(pCreateTable->childTableInfo, &yymsp[0].minor.yy184); - pCreateTable->type = TSDB_SQL_CREATE_TABLE; - yylhsminor.yy482 = pCreateTable; -} - yymsp[0].minor.yy482 = yylhsminor.yy482; - break; - case 141: /* create_table_list ::= create_table_list create_from_stable */ -{ - taosArrayPush(yymsp[-1].minor.yy482->childTableInfo, &yymsp[0].minor.yy184); - yylhsminor.yy482 = yymsp[-1].minor.yy482; -} - yymsp[-1].minor.yy482 = yylhsminor.yy482; - break; - case 142: /* create_table_args ::= ifnotexists ids cpxName LP columnlist RP */ -{ - yylhsminor.yy482 = tSetCreateTableInfo(yymsp[-1].minor.yy225, NULL, NULL, TSDB_SQL_CREATE_TABLE); - setSqlInfo(pInfo, yylhsminor.yy482, NULL, TSDB_SQL_CREATE_TABLE); - - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - setCreatedTableName(pInfo, &yymsp[-4].minor.yy0, &yymsp[-5].minor.yy0); -} - yymsp[-5].minor.yy482 = yylhsminor.yy482; - break; - case 143: /* create_stable_args ::= ifnotexists ids cpxName LP columnlist RP TAGS LP columnlist RP */ -{ - yylhsminor.yy482 = tSetCreateTableInfo(yymsp[-5].minor.yy225, yymsp[-1].minor.yy225, NULL, TSDB_SQL_CREATE_STABLE); - setSqlInfo(pInfo, yylhsminor.yy482, NULL, TSDB_SQL_CREATE_STABLE); - - yymsp[-8].minor.yy0.n += yymsp[-7].minor.yy0.n; - setCreatedTableName(pInfo, &yymsp[-8].minor.yy0, &yymsp[-9].minor.yy0); -} - yymsp[-9].minor.yy482 = yylhsminor.yy482; - break; - case 144: /* create_from_stable ::= ifnotexists ids cpxName USING ids cpxName TAGS LP tagitemlist1 RP */ -{ - yymsp[-5].minor.yy0.n += yymsp[-4].minor.yy0.n; - yymsp[-8].minor.yy0.n += yymsp[-7].minor.yy0.n; - yylhsminor.yy184 = createNewChildTableInfo(&yymsp[-5].minor.yy0, NULL, yymsp[-1].minor.yy225, &yymsp[-8].minor.yy0, &yymsp[-9].minor.yy0); -} - yymsp[-9].minor.yy184 = yylhsminor.yy184; - break; - case 145: /* create_from_stable ::= ifnotexists ids cpxName USING ids cpxName LP tagNamelist RP TAGS LP tagitemlist1 RP */ -{ - yymsp[-8].minor.yy0.n += yymsp[-7].minor.yy0.n; - yymsp[-11].minor.yy0.n += yymsp[-10].minor.yy0.n; - yylhsminor.yy184 = createNewChildTableInfo(&yymsp[-8].minor.yy0, yymsp[-5].minor.yy225, yymsp[-1].minor.yy225, &yymsp[-11].minor.yy0, &yymsp[-12].minor.yy0); -} - yymsp[-12].minor.yy184 = yylhsminor.yy184; - break; - case 146: /* tagNamelist ::= tagNamelist COMMA ids */ -{taosArrayPush(yymsp[-2].minor.yy225, &yymsp[0].minor.yy0); yylhsminor.yy225 = yymsp[-2].minor.yy225; } - yymsp[-2].minor.yy225 = yylhsminor.yy225; - break; - case 147: /* tagNamelist ::= ids */ -{yylhsminor.yy225 = taosArrayInit(4, sizeof(SToken)); taosArrayPush(yylhsminor.yy225, &yymsp[0].minor.yy0);} - yymsp[0].minor.yy225 = yylhsminor.yy225; - break; - case 148: /* create_table_args ::= ifnotexists ids cpxName AS select */ -{ -// yylhsminor.yy482 = tSetCreateTableInfo(NULL, NULL, yymsp[0].minor.yy185, TSQL_CREATE_STREAM); -// setSqlInfo(pInfo, yylhsminor.yy482, NULL, TSDB_SQL_CREATE_TABLE); -// -// yymsp[-3].minor.yy0.n += yymsp[-2].minor.yy0.n; -// setCreatedTableName(pInfo, &yymsp[-3].minor.yy0, &yymsp[-4].minor.yy0); -} - yymsp[-4].minor.yy482 = yylhsminor.yy482; - break; - case 149: /* columnlist ::= columnlist COMMA column */ -{taosArrayPush(yymsp[-2].minor.yy225, &yymsp[0].minor.yy16); yylhsminor.yy225 = yymsp[-2].minor.yy225; } - yymsp[-2].minor.yy225 = yylhsminor.yy225; - break; - case 150: /* columnlist ::= column */ -{yylhsminor.yy225 = taosArrayInit(4, sizeof(SField)); taosArrayPush(yylhsminor.yy225, &yymsp[0].minor.yy16);} - yymsp[0].minor.yy225 = yylhsminor.yy225; - break; - case 151: /* column ::= ids typename */ -{ - tSetColumnInfo(&yylhsminor.yy16, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy16); -} - yymsp[-1].minor.yy16 = yylhsminor.yy16; - break; - case 152: /* tagitemlist1 ::= tagitemlist1 COMMA tagitem1 */ -{ taosArrayPush(yymsp[-2].minor.yy225, &yymsp[0].minor.yy0); yylhsminor.yy225 = yymsp[-2].minor.yy225;} - yymsp[-2].minor.yy225 = yylhsminor.yy225; - break; - case 153: /* tagitemlist1 ::= tagitem1 */ -{ yylhsminor.yy225 = taosArrayInit(4, sizeof(SToken)); taosArrayPush(yylhsminor.yy225, &yymsp[0].minor.yy0); } - yymsp[0].minor.yy225 = yylhsminor.yy225; - break; - case 154: /* tagitem1 ::= MINUS INTEGER */ - case 155: /* tagitem1 ::= MINUS FLOAT */ yytestcase(yyruleno==155); - case 156: /* tagitem1 ::= PLUS INTEGER */ yytestcase(yyruleno==156); - case 157: /* tagitem1 ::= PLUS FLOAT */ yytestcase(yyruleno==157); -{ yylhsminor.yy0.n = yymsp[-1].minor.yy0.n + yymsp[0].minor.yy0.n; yylhsminor.yy0.type = yymsp[0].minor.yy0.type; } - yymsp[-1].minor.yy0 = yylhsminor.yy0; - break; - case 158: /* tagitem1 ::= INTEGER */ - case 159: /* tagitem1 ::= FLOAT */ yytestcase(yyruleno==159); - case 160: /* tagitem1 ::= STRING */ yytestcase(yyruleno==160); - case 161: /* tagitem1 ::= BOOL */ yytestcase(yyruleno==161); - case 162: /* tagitem1 ::= NULL */ yytestcase(yyruleno==162); - case 163: /* tagitem1 ::= NOW */ yytestcase(yyruleno==163); -{ yylhsminor.yy0 = yymsp[0].minor.yy0; } - yymsp[0].minor.yy0 = yylhsminor.yy0; - break; - case 170: /* tagitem ::= NULL */ -{ yymsp[0].minor.yy0.type = 0; taosVariantCreate(&yylhsminor.yy1, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.type); } - yymsp[0].minor.yy1 = yylhsminor.yy1; - break; - case 171: /* tagitem ::= NOW */ -{ yymsp[0].minor.yy0.type = TSDB_DATA_TYPE_TIMESTAMP; taosVariantCreate(&yylhsminor.yy1, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.type);} - yymsp[0].minor.yy1 = yylhsminor.yy1; - break; - case 172: /* tagitem ::= MINUS INTEGER */ - case 173: /* tagitem ::= MINUS FLOAT */ yytestcase(yyruleno==173); - case 174: /* tagitem ::= PLUS INTEGER */ yytestcase(yyruleno==174); - case 175: /* tagitem ::= PLUS FLOAT */ yytestcase(yyruleno==175); -{ - yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - yymsp[-1].minor.yy0.type = yymsp[0].minor.yy0.type; - toTSDBType(yymsp[-1].minor.yy0.type); - taosVariantCreate(&yylhsminor.yy1, yymsp[-1].minor.yy0.z, yymsp[-1].minor.yy0.n, yymsp[-1].minor.yy0.type); -} - yymsp[-1].minor.yy1 = yylhsminor.yy1; - break; - case 176: /* select ::= SELECT selcollist from where_opt interval_option sliding_opt session_option windowstate_option fill_opt groupby_opt having_opt orderby_opt slimit_opt limit_opt */ -{ - yylhsminor.yy185 = tSetQuerySqlNode(&yymsp[-13].minor.yy0, yymsp[-12].minor.yy225, yymsp[-11].minor.yy160, yymsp[-10].minor.yy226, yymsp[-4].minor.yy225, yymsp[-2].minor.yy225, &yymsp[-9].minor.yy52, &yymsp[-7].minor.yy463, &yymsp[-6].minor.yy112, &yymsp[-8].minor.yy0, yymsp[-5].minor.yy225, &yymsp[0].minor.yy495, &yymsp[-1].minor.yy495, yymsp[-3].minor.yy226); -} - yymsp[-13].minor.yy185 = yylhsminor.yy185; - break; - case 177: /* select ::= LP select RP */ -{yymsp[-2].minor.yy185 = yymsp[-1].minor.yy185;} - break; - case 178: /* union ::= select */ -{ yylhsminor.yy93 = setSubclause(NULL, yymsp[0].minor.yy185); } - yymsp[0].minor.yy93 = yylhsminor.yy93; - break; - case 179: /* union ::= union UNION ALL select */ -{ yylhsminor.yy93 = appendSelectClause(yymsp[-3].minor.yy93, SQL_TYPE_UNIONALL, yymsp[0].minor.yy185); } - yymsp[-3].minor.yy93 = yylhsminor.yy93; - break; - case 180: /* union ::= union UNION select */ -{ yylhsminor.yy93 = appendSelectClause(yymsp[-2].minor.yy93, SQL_TYPE_UNION, yymsp[0].minor.yy185); } - yymsp[-2].minor.yy93 = yylhsminor.yy93; - break; - case 181: /* cmd ::= union */ -{ setSqlInfo(pInfo, yymsp[0].minor.yy93, NULL, TSDB_SQL_SELECT); } - break; - case 182: /* select ::= SELECT selcollist */ -{ - yylhsminor.yy185 = tSetQuerySqlNode(&yymsp[-1].minor.yy0, yymsp[0].minor.yy225, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); -} - yymsp[-1].minor.yy185 = yylhsminor.yy185; - break; - case 183: /* sclp ::= selcollist COMMA */ -{yylhsminor.yy225 = yymsp[-1].minor.yy225;} - yymsp[-1].minor.yy225 = yylhsminor.yy225; - break; - case 184: /* sclp ::= */ - case 216: /* orderby_opt ::= */ yytestcase(yyruleno==216); -{yymsp[1].minor.yy225 = 0;} - break; - case 185: /* selcollist ::= sclp distinct expr as */ -{ - yylhsminor.yy225 = tSqlExprListAppend(yymsp[-3].minor.yy225, yymsp[-1].minor.yy226, yymsp[-2].minor.yy0.n? &yymsp[-2].minor.yy0:0, yymsp[0].minor.yy0.n?&yymsp[0].minor.yy0:0); -} - yymsp[-3].minor.yy225 = yylhsminor.yy225; - break; - case 186: /* selcollist ::= sclp STAR */ -{ - tSqlExpr *pNode = tSqlExprCreateIdValue(NULL, TK_ALL); - yylhsminor.yy225 = tSqlExprListAppend(yymsp[-1].minor.yy225, pNode, 0, 0); -} - yymsp[-1].minor.yy225 = yylhsminor.yy225; - break; - case 187: /* as ::= AS ids */ -{ yymsp[-1].minor.yy0 = yymsp[0].minor.yy0; } - break; - case 188: /* as ::= ids */ -{ yylhsminor.yy0 = yymsp[0].minor.yy0; } - yymsp[0].minor.yy0 = yylhsminor.yy0; - break; - case 189: /* as ::= */ -{ yymsp[1].minor.yy0.n = 0; } - break; - case 190: /* distinct ::= DISTINCT */ -{ yylhsminor.yy0 = yymsp[0].minor.yy0; } - yymsp[0].minor.yy0 = yylhsminor.yy0; - break; - case 192: /* from ::= FROM tablelist */ - case 193: /* from ::= FROM sub */ yytestcase(yyruleno==193); -{yymsp[-1].minor.yy160 = yymsp[0].minor.yy160;} - break; - case 194: /* sub ::= LP union RP */ -{yymsp[-2].minor.yy160 = addSubquery(NULL, yymsp[-1].minor.yy93, NULL);} - break; - case 195: /* sub ::= LP union RP ids */ -{yymsp[-3].minor.yy160 = addSubquery(NULL, yymsp[-2].minor.yy93, &yymsp[0].minor.yy0);} - break; - case 196: /* sub ::= sub COMMA LP union RP ids */ -{yylhsminor.yy160 = addSubquery(yymsp[-5].minor.yy160, yymsp[-2].minor.yy93, &yymsp[0].minor.yy0);} - yymsp[-5].minor.yy160 = yylhsminor.yy160; - break; - case 197: /* tablelist ::= ids cpxName */ -{ - yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - yylhsminor.yy160 = setTableNameList(NULL, &yymsp[-1].minor.yy0, NULL); -} - yymsp[-1].minor.yy160 = yylhsminor.yy160; - break; - case 198: /* tablelist ::= ids cpxName ids */ -{ - yymsp[-2].minor.yy0.n += yymsp[-1].minor.yy0.n; - yylhsminor.yy160 = setTableNameList(NULL, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0); -} - yymsp[-2].minor.yy160 = yylhsminor.yy160; - break; - case 199: /* tablelist ::= tablelist COMMA ids cpxName */ -{ - yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - yylhsminor.yy160 = setTableNameList(yymsp[-3].minor.yy160, &yymsp[-1].minor.yy0, NULL); -} - yymsp[-3].minor.yy160 = yylhsminor.yy160; - break; - case 200: /* tablelist ::= tablelist COMMA ids cpxName ids */ -{ - yymsp[-2].minor.yy0.n += yymsp[-1].minor.yy0.n; - yylhsminor.yy160 = setTableNameList(yymsp[-4].minor.yy160, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0); -} - yymsp[-4].minor.yy160 = yylhsminor.yy160; - break; - case 201: /* tmvar ::= VARIABLE */ -{yylhsminor.yy0 = yymsp[0].minor.yy0;} - yymsp[0].minor.yy0 = yylhsminor.yy0; - break; - case 202: /* interval_option ::= intervalKey LP tmvar RP */ -{yylhsminor.yy52.interval = yymsp[-1].minor.yy0; yylhsminor.yy52.offset.n = 0; yylhsminor.yy52.token = yymsp[-3].minor.yy460;} - yymsp[-3].minor.yy52 = yylhsminor.yy52; - break; - case 203: /* interval_option ::= intervalKey LP tmvar COMMA tmvar RP */ -{yylhsminor.yy52.interval = yymsp[-3].minor.yy0; yylhsminor.yy52.offset = yymsp[-1].minor.yy0; yylhsminor.yy52.token = yymsp[-5].minor.yy460;} - yymsp[-5].minor.yy52 = yylhsminor.yy52; - break; - case 204: /* interval_option ::= */ -{memset(&yymsp[1].minor.yy52, 0, sizeof(yymsp[1].minor.yy52));} - break; - case 205: /* intervalKey ::= INTERVAL */ -{yymsp[0].minor.yy460 = TK_INTERVAL;} - break; - case 206: /* intervalKey ::= EVERY */ -{yymsp[0].minor.yy460 = TK_EVERY; } - break; - case 207: /* session_option ::= */ -{yymsp[1].minor.yy463.col.n = 0; yymsp[1].minor.yy463.gap.n = 0;} - break; - case 208: /* session_option ::= SESSION LP ids cpxName COMMA tmvar RP */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - yymsp[-6].minor.yy463.col = yymsp[-4].minor.yy0; - yymsp[-6].minor.yy463.gap = yymsp[-1].minor.yy0; -} - break; - case 209: /* windowstate_option ::= */ -{ yymsp[1].minor.yy112.col.n = 0; yymsp[1].minor.yy112.col.z = NULL;} - break; - case 210: /* windowstate_option ::= STATE_WINDOW LP ids RP */ -{ yymsp[-3].minor.yy112.col = yymsp[-1].minor.yy0; } - break; - case 211: /* fill_opt ::= */ -{ yymsp[1].minor.yy225 = 0; } - break; - case 212: /* fill_opt ::= FILL LP ID COMMA tagitemlist RP */ -{ - SVariant A = {0}; - toTSDBType(yymsp[-3].minor.yy0.type); - taosVariantCreate(&A, yymsp[-3].minor.yy0.z, yymsp[-3].minor.yy0.n, yymsp[-3].minor.yy0.type); - - tListItemInsert(yymsp[-1].minor.yy225, &A, -1, 0); - yymsp[-5].minor.yy225 = yymsp[-1].minor.yy225; -} - break; - case 213: /* fill_opt ::= FILL LP ID RP */ -{ - toTSDBType(yymsp[-1].minor.yy0.type); - yymsp[-3].minor.yy225 = tListItemAppendToken(NULL, &yymsp[-1].minor.yy0, -1); -} - break; - case 214: /* sliding_opt ::= SLIDING LP tmvar RP */ -{yymsp[-3].minor.yy0 = yymsp[-1].minor.yy0; } - break; - case 215: /* sliding_opt ::= */ -{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = NULL; yymsp[1].minor.yy0.type = 0; } - break; - case 217: /* orderby_opt ::= ORDER BY sortlist */ -{yymsp[-2].minor.yy225 = yymsp[0].minor.yy225;} - break; - case 218: /* sortlist ::= sortlist COMMA item sortorder */ -{ - yylhsminor.yy225 = tListItemAppend(yymsp[-3].minor.yy225, &yymsp[-1].minor.yy1, yymsp[0].minor.yy40); -} - yymsp[-3].minor.yy225 = yylhsminor.yy225; - break; - case 219: /* sortlist ::= item sortorder */ -{ - yylhsminor.yy225 = tListItemAppend(NULL, &yymsp[-1].minor.yy1, yymsp[0].minor.yy40); -} - yymsp[-1].minor.yy225 = yylhsminor.yy225; - break; - case 220: /* item ::= ids cpxName */ -{ - toTSDBType(yymsp[-1].minor.yy0.type); - yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - - taosVariantCreate(&yylhsminor.yy1, yymsp[-1].minor.yy0.z, yymsp[-1].minor.yy0.n, yymsp[-1].minor.yy0.type); -} - yymsp[-1].minor.yy1 = yylhsminor.yy1; - break; - case 221: /* sortorder ::= ASC */ -{ yymsp[0].minor.yy40 = TSDB_ORDER_ASC; } - break; - case 222: /* sortorder ::= DESC */ -{ yymsp[0].minor.yy40 = TSDB_ORDER_DESC;} - break; - case 223: /* sortorder ::= */ -{ yymsp[1].minor.yy40 = TSDB_ORDER_ASC; } - break; - case 224: /* groupby_opt ::= */ -{ yymsp[1].minor.yy225 = 0;} - break; - case 225: /* groupby_opt ::= GROUP BY grouplist */ -{ yymsp[-2].minor.yy225 = yymsp[0].minor.yy225;} - break; - case 226: /* grouplist ::= grouplist COMMA item */ -{ - yylhsminor.yy225 = tListItemAppend(yymsp[-2].minor.yy225, &yymsp[0].minor.yy1, -1); -} - yymsp[-2].minor.yy225 = yylhsminor.yy225; - break; - case 227: /* grouplist ::= item */ -{ - yylhsminor.yy225 = tListItemAppend(NULL, &yymsp[0].minor.yy1, -1); -} - yymsp[0].minor.yy225 = yylhsminor.yy225; - break; - case 228: /* having_opt ::= */ - case 238: /* where_opt ::= */ yytestcase(yyruleno==238); - case 282: /* expritem ::= */ yytestcase(yyruleno==282); -{yymsp[1].minor.yy226 = 0;} - break; - case 229: /* having_opt ::= HAVING expr */ - case 239: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==239); -{yymsp[-1].minor.yy226 = yymsp[0].minor.yy226;} - break; - case 230: /* limit_opt ::= */ - case 234: /* slimit_opt ::= */ yytestcase(yyruleno==234); -{yymsp[1].minor.yy495.limit = -1; yymsp[1].minor.yy495.offset = 0;} - break; - case 231: /* limit_opt ::= LIMIT signed */ - case 235: /* slimit_opt ::= SLIMIT signed */ yytestcase(yyruleno==235); -{yymsp[-1].minor.yy495.limit = yymsp[0].minor.yy61; yymsp[-1].minor.yy495.offset = 0;} - break; - case 232: /* limit_opt ::= LIMIT signed OFFSET signed */ -{ yymsp[-3].minor.yy495.limit = yymsp[-2].minor.yy61; yymsp[-3].minor.yy495.offset = yymsp[0].minor.yy61;} - break; - case 233: /* limit_opt ::= LIMIT signed COMMA signed */ -{ yymsp[-3].minor.yy495.limit = yymsp[0].minor.yy61; yymsp[-3].minor.yy495.offset = yymsp[-2].minor.yy61;} - break; - case 236: /* slimit_opt ::= SLIMIT signed SOFFSET signed */ -{yymsp[-3].minor.yy495.limit = yymsp[-2].minor.yy61; yymsp[-3].minor.yy495.offset = yymsp[0].minor.yy61;} - break; - case 237: /* slimit_opt ::= SLIMIT signed COMMA signed */ -{yymsp[-3].minor.yy495.limit = yymsp[0].minor.yy61; yymsp[-3].minor.yy495.offset = yymsp[-2].minor.yy61;} - break; - case 240: /* expr ::= LP expr RP */ -{yylhsminor.yy226 = yymsp[-1].minor.yy226; yylhsminor.yy226->exprToken.z = yymsp[-2].minor.yy0.z; yylhsminor.yy226->exprToken.n = (yymsp[0].minor.yy0.z - yymsp[-2].minor.yy0.z + 1);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 241: /* expr ::= ID */ -{ yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_ID);} - yymsp[0].minor.yy226 = yylhsminor.yy226; - break; - case 242: /* expr ::= ID DOT ID */ -{ yymsp[-2].minor.yy0.n += (1+yymsp[0].minor.yy0.n); yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[-2].minor.yy0, TK_ID);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 243: /* expr ::= ID DOT STAR */ -{ yymsp[-2].minor.yy0.n += (1+yymsp[0].minor.yy0.n); yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[-2].minor.yy0, TK_ALL);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 244: /* expr ::= INTEGER */ -{ yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_INTEGER);} - yymsp[0].minor.yy226 = yylhsminor.yy226; - break; - case 245: /* expr ::= MINUS INTEGER */ - case 246: /* expr ::= PLUS INTEGER */ yytestcase(yyruleno==246); -{ yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_INTEGER; yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[-1].minor.yy0, TK_INTEGER);} - yymsp[-1].minor.yy226 = yylhsminor.yy226; - break; - case 247: /* expr ::= FLOAT */ -{ yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_FLOAT);} - yymsp[0].minor.yy226 = yylhsminor.yy226; - break; - case 248: /* expr ::= MINUS FLOAT */ - case 249: /* expr ::= PLUS FLOAT */ yytestcase(yyruleno==249); -{ yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_FLOAT; yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[-1].minor.yy0, TK_FLOAT);} - yymsp[-1].minor.yy226 = yylhsminor.yy226; - break; - case 250: /* expr ::= STRING */ -{ yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_STRING);} - yymsp[0].minor.yy226 = yylhsminor.yy226; - break; - case 251: /* expr ::= NOW */ -{ yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_NOW); } - yymsp[0].minor.yy226 = yylhsminor.yy226; - break; - case 252: /* expr ::= VARIABLE */ -{ yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_VARIABLE);} - yymsp[0].minor.yy226 = yylhsminor.yy226; - break; - case 253: /* expr ::= PLUS VARIABLE */ - case 254: /* expr ::= MINUS VARIABLE */ yytestcase(yyruleno==254); -{ yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_VARIABLE; yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[-1].minor.yy0, TK_VARIABLE);} - yymsp[-1].minor.yy226 = yylhsminor.yy226; - break; - case 255: /* expr ::= BOOL */ -{ yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_BOOL);} - yymsp[0].minor.yy226 = yylhsminor.yy226; - break; - case 256: /* expr ::= NULL */ -{ yylhsminor.yy226 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_NULL);} - yymsp[0].minor.yy226 = yylhsminor.yy226; - break; - case 257: /* expr ::= ID LP exprlist RP */ -{ tRecordFuncName(pInfo->funcs, &yymsp[-3].minor.yy0); yylhsminor.yy226 = tSqlExprCreateFunction(yymsp[-1].minor.yy225, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0, yymsp[-3].minor.yy0.type); } - yymsp[-3].minor.yy226 = yylhsminor.yy226; - break; - case 258: /* expr ::= ID LP STAR RP */ -{ tRecordFuncName(pInfo->funcs, &yymsp[-3].minor.yy0); yylhsminor.yy226 = tSqlExprCreateFunction(NULL, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0, yymsp[-3].minor.yy0.type); } - yymsp[-3].minor.yy226 = yylhsminor.yy226; - break; - case 259: /* expr ::= expr IS NULL */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, NULL, TK_ISNULL);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 260: /* expr ::= expr IS NOT NULL */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-3].minor.yy226, NULL, TK_NOTNULL);} - yymsp[-3].minor.yy226 = yylhsminor.yy226; - break; - case 261: /* expr ::= expr LT expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_LT);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 262: /* expr ::= expr GT expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_GT);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 263: /* expr ::= expr LE expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_LE);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 264: /* expr ::= expr GE expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_GE);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 265: /* expr ::= expr NE expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_NE);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 266: /* expr ::= expr EQ expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_EQ);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 267: /* expr ::= expr BETWEEN expr AND expr */ -{ tSqlExpr* X2 = tSqlExprClone(yymsp[-4].minor.yy226); yylhsminor.yy226 = tSqlExprCreate(tSqlExprCreate(yymsp[-4].minor.yy226, yymsp[-2].minor.yy226, TK_GE), tSqlExprCreate(X2, yymsp[0].minor.yy226, TK_LE), TK_AND);} - yymsp[-4].minor.yy226 = yylhsminor.yy226; - break; - case 268: /* expr ::= expr AND expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_AND);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 269: /* expr ::= expr OR expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_OR); } - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 270: /* expr ::= expr PLUS expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_PLUS); } - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 271: /* expr ::= expr MINUS expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_MINUS); } - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 272: /* expr ::= expr STAR expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_STAR); } - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 273: /* expr ::= expr SLASH expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_DIVIDE);} - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 274: /* expr ::= expr REM expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_REM); } - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 275: /* expr ::= expr LIKE expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_LIKE); } - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 276: /* expr ::= expr MATCH expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_MATCH); } - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 277: /* expr ::= expr NMATCH expr */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-2].minor.yy226, yymsp[0].minor.yy226, TK_NMATCH); } - yymsp[-2].minor.yy226 = yylhsminor.yy226; - break; - case 278: /* expr ::= expr IN LP exprlist RP */ -{yylhsminor.yy226 = tSqlExprCreate(yymsp[-4].minor.yy226, (tSqlExpr*)yymsp[-1].minor.yy225, TK_IN); } - yymsp[-4].minor.yy226 = yylhsminor.yy226; - break; - case 279: /* exprlist ::= exprlist COMMA expritem */ -{yylhsminor.yy225 = tSqlExprListAppend(yymsp[-2].minor.yy225,yymsp[0].minor.yy226,0, 0);} - yymsp[-2].minor.yy225 = yylhsminor.yy225; - break; - case 280: /* exprlist ::= expritem */ -{yylhsminor.yy225 = tSqlExprListAppend(0,yymsp[0].minor.yy226,0, 0);} - yymsp[0].minor.yy225 = yylhsminor.yy225; - break; - case 281: /* expritem ::= expr */ -{yylhsminor.yy226 = yymsp[0].minor.yy226;} - yymsp[0].minor.yy226 = yylhsminor.yy226; - break; - case 283: /* cmd ::= RESET QUERY CACHE */ -{ setDCLSqlElems(pInfo, TSDB_SQL_RESET_CACHE, 0);} - break; - case 284: /* cmd ::= SYNCDB ids REPLICA */ -{ setDCLSqlElems(pInfo, TSDB_SQL_SYNC_DB_REPLICA, 1, &yymsp[-1].minor.yy0);} - break; - case 285: /* cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy225, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, -1); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 286: /* cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - toTSDBType(yymsp[0].minor.yy0.type); - SArray* K = tListItemAppendToken(NULL, &yymsp[0].minor.yy0, -1); - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, -1); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 287: /* cmd ::= ALTER TABLE ids cpxName MODIFY COLUMN columnlist */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy225, NULL, TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES, -1); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 288: /* cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy225, NULL, TSDB_ALTER_TABLE_ADD_TAG, -1); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 289: /* cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - - toTSDBType(yymsp[0].minor.yy0.type); - SArray* A = tListItemAppendToken(NULL, &yymsp[0].minor.yy0, -1); - - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, A, TSDB_ALTER_TABLE_DROP_TAG, -1); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 290: /* cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ -{ - yymsp[-5].minor.yy0.n += yymsp[-4].minor.yy0.n; - - toTSDBType(yymsp[-1].minor.yy0.type); - SArray* A = tListItemAppendToken(NULL, &yymsp[-1].minor.yy0, -1); - - toTSDBType(yymsp[0].minor.yy0.type); - A = tListItemAppendToken(A, &yymsp[0].minor.yy0, -1); - - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-5].minor.yy0, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_NAME, -1); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 291: /* cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ -{ - yymsp[-6].minor.yy0.n += yymsp[-5].minor.yy0.n; - - toTSDBType(yymsp[-2].minor.yy0.type); - SArray* A = tListItemAppendToken(NULL, &yymsp[-2].minor.yy0, -1); - A = tListItemAppend(A, &yymsp[0].minor.yy1, -1); - - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-6].minor.yy0, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL, -1); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 292: /* cmd ::= ALTER TABLE ids cpxName MODIFY TAG columnlist */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy225, NULL, TSDB_ALTER_TABLE_UPDATE_TAG_BYTES, -1); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 293: /* cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy225, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, TSDB_SUPER_TABLE); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 294: /* cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - - toTSDBType(yymsp[0].minor.yy0.type); - SArray* K = tListItemAppendToken(NULL, &yymsp[0].minor.yy0, -1); - - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, TSDB_SUPER_TABLE); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 295: /* cmd ::= ALTER STABLE ids cpxName MODIFY COLUMN columnlist */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy225, NULL, TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES, TSDB_SUPER_TABLE); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 296: /* cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy225, NULL, TSDB_ALTER_TABLE_ADD_TAG, TSDB_SUPER_TABLE); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 297: /* cmd ::= ALTER STABLE ids cpxName DROP TAG ids */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - - toTSDBType(yymsp[0].minor.yy0.type); - SArray* A = tListItemAppendToken(NULL, &yymsp[0].minor.yy0, -1); - - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, A, TSDB_ALTER_TABLE_DROP_TAG, TSDB_SUPER_TABLE); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 298: /* cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids */ -{ - yymsp[-5].minor.yy0.n += yymsp[-4].minor.yy0.n; - - toTSDBType(yymsp[-1].minor.yy0.type); - SArray* A = tListItemAppendToken(NULL, &yymsp[-1].minor.yy0, -1); - - toTSDBType(yymsp[0].minor.yy0.type); - A = tListItemAppendToken(A, &yymsp[0].minor.yy0, -1); - - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-5].minor.yy0, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_NAME, TSDB_SUPER_TABLE); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 299: /* cmd ::= ALTER STABLE ids cpxName SET TAG ids EQ tagitem */ -{ - yymsp[-6].minor.yy0.n += yymsp[-5].minor.yy0.n; - - toTSDBType(yymsp[-2].minor.yy0.type); - SArray* A = tListItemAppendToken(NULL, &yymsp[-2].minor.yy0, -1); - A = tListItemAppend(A, &yymsp[0].minor.yy1, -1); - - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-6].minor.yy0, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL, TSDB_SUPER_TABLE); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 300: /* cmd ::= ALTER STABLE ids cpxName MODIFY TAG columnlist */ -{ - yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy225, NULL, TSDB_ALTER_TABLE_UPDATE_TAG_BYTES, TSDB_SUPER_TABLE); - setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); -} - break; - case 301: /* cmd ::= KILL CONNECTION INTEGER */ -{setKillSql(pInfo, TSDB_SQL_KILL_CONNECTION, &yymsp[0].minor.yy0);} - break; - case 302: /* cmd ::= KILL STREAM INTEGER COLON INTEGER */ -{yymsp[-2].minor.yy0.n += (yymsp[-1].minor.yy0.n + yymsp[0].minor.yy0.n); setKillSql(pInfo, TSDB_SQL_KILL_STREAM, &yymsp[-2].minor.yy0);} - break; - case 303: /* cmd ::= KILL QUERY INTEGER COLON INTEGER */ -{yymsp[-2].minor.yy0.n += (yymsp[-1].minor.yy0.n + yymsp[0].minor.yy0.n); setKillSql(pInfo, TSDB_SQL_KILL_QUERY, &yymsp[-2].minor.yy0);} - break; - default: - break; -/********** End reduce actions ************************************************/ - }; - assert( yyrulenoYY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) ); - - /* It is not possible for a REDUCE to be followed by an error */ - assert( yyact!=YY_ERROR_ACTION ); - - yymsp += yysize+1; - yypParser->yytos = yymsp; - yymsp->stateno = (YYACTIONTYPE)yyact; - yymsp->major = (YYCODETYPE)yygoto; - yyTraceShift(yypParser, yyact, "... then shift"); - return yyact; -} - -/* -** The following code executes when the parse fails -*/ -#ifndef YYNOERRORRECOVERY -static void yy_parse_failed( - yyParser *yypParser /* The parser */ -){ - ParseARG_FETCH - ParseCTX_FETCH -#ifndef NDEBUG - if( yyTraceFILE ){ - fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); - } -#endif - while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser); - /* Here code is inserted which will be executed whenever the - ** parser fails */ -/************ Begin %parse_failure code ***************************************/ -/************ End %parse_failure code *****************************************/ - ParseARG_STORE /* Suppress warning about unused %extra_argument variable */ - ParseCTX_STORE -} -#endif /* YYNOERRORRECOVERY */ - -/* -** The following code executes when a syntax error first occurs. -*/ -static void yy_syntax_error( - yyParser *yypParser, /* The parser */ - int yymajor, /* The major type of the error token */ - ParseTOKENTYPE yyminor /* The minor type of the error token */ -){ - ParseARG_FETCH - ParseCTX_FETCH -#define TOKEN yyminor -/************ Begin %syntax_error code ****************************************/ - - pInfo->valid = false; - int32_t outputBufLen = tListLen(pInfo->msg); - int32_t len = 0; - - if(TOKEN.z) { - char msg[] = "syntax error near \"%s\""; - int32_t sqlLen = strlen(&TOKEN.z[0]); - - if (sqlLen + sizeof(msg)/sizeof(msg[0]) + 1 > outputBufLen) { - char tmpstr[128] = {0}; - memcpy(tmpstr, &TOKEN.z[0], sizeof(tmpstr)/sizeof(tmpstr[0]) - 1); - len = sprintf(pInfo->msg, msg, tmpstr); - } else { - len = sprintf(pInfo->msg, msg, &TOKEN.z[0]); - } - - } else { - len = sprintf(pInfo->msg, "Incomplete SQL statement"); - } - - assert(len <= outputBufLen); -/************ End %syntax_error code ******************************************/ - ParseARG_STORE /* Suppress warning about unused %extra_argument variable */ - ParseCTX_STORE -} - -/* -** The following is executed when the parser accepts -*/ -static void yy_accept( - yyParser *yypParser /* The parser */ -){ - ParseARG_FETCH - ParseCTX_FETCH -#ifndef NDEBUG - if( yyTraceFILE ){ - fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); - } -#endif -#ifndef YYNOERRORRECOVERY - yypParser->yyerrcnt = -1; -#endif - assert( yypParser->yytos==yypParser->yystack ); - /* Here code is inserted which will be executed whenever the - ** parser accepts */ -/*********** Begin %parse_accept code *****************************************/ - -/*********** End %parse_accept code *******************************************/ - ParseARG_STORE /* Suppress warning about unused %extra_argument variable */ - ParseCTX_STORE -} - -/* The main parser program. -** The first argument is a pointer to a structure obtained from -** "ParseAlloc" which describes the current state of the parser. -** The second argument is the major token number. The third is -** the minor token. The fourth optional argument is whatever the -** user wants (and specified in the grammar) and is available for -** use by the action routines. -** -** Inputs: -**
    -**
  • A pointer to the parser (an opaque structure.) -**
  • The major token number. -**
  • The minor token number. -**
  • An option argument of a grammar-specified type. -**
-** -** Outputs: -** None. -*/ -void Parse( - void *yyp, /* The parser */ - int yymajor, /* The major token code number */ - ParseTOKENTYPE yyminor /* The value for the token */ - ParseARG_PDECL /* Optional %extra_argument parameter */ -){ - YYMINORTYPE yyminorunion; - YYACTIONTYPE yyact; /* The parser action. */ -#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) - int yyendofinput; /* True if we are at the end of input */ -#endif -#ifdef YYERRORSYMBOL - int yyerrorhit = 0; /* True if yymajor has invoked an error */ -#endif - yyParser *yypParser = (yyParser*)yyp; /* The parser */ - ParseCTX_FETCH - ParseARG_STORE - - assert( yypParser->yytos!=0 ); -#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) - yyendofinput = (yymajor==0); -#endif - - yyact = yypParser->yytos->stateno; -#ifndef NDEBUG - if( yyTraceFILE ){ - if( yyact < YY_MIN_REDUCE ){ - fprintf(yyTraceFILE,"%sInput '%s' in state %d\n", - yyTracePrompt,yyTokenName[yymajor],yyact); - }else{ - fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n", - yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE); - } - } -#endif - - do{ - assert( yyact==yypParser->yytos->stateno ); - yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact); - if( yyact >= YY_MIN_REDUCE ){ - yyact = yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor, - yyminor ParseCTX_PARAM); - }else if( yyact <= YY_MAX_SHIFTREDUCE ){ - yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor); -#ifndef YYNOERRORRECOVERY - yypParser->yyerrcnt--; -#endif - break; - }else if( yyact==YY_ACCEPT_ACTION ){ - yypParser->yytos--; - yy_accept(yypParser); - return; - }else{ - assert( yyact == YY_ERROR_ACTION ); - yyminorunion.yy0 = yyminor; -#ifdef YYERRORSYMBOL - int yymx; -#endif -#ifndef NDEBUG - if( yyTraceFILE ){ - fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt); - } -#endif -#ifdef YYERRORSYMBOL - /* A syntax error has occurred. - ** The response to an error depends upon whether or not the - ** grammar defines an error token "ERROR". - ** - ** This is what we do if the grammar does define ERROR: - ** - ** * Call the %syntax_error function. - ** - ** * Begin popping the stack until we enter a state where - ** it is legal to shift the error symbol, then shift - ** the error symbol. - ** - ** * Set the error count to three. - ** - ** * Begin accepting and shifting new tokens. No new error - ** processing will occur until three tokens have been - ** shifted successfully. - ** - */ - if( yypParser->yyerrcnt<0 ){ - yy_syntax_error(yypParser,yymajor,yyminor); - } - yymx = yypParser->yytos->major; - if( yymx==YYERRORSYMBOL || yyerrorhit ){ -#ifndef NDEBUG - if( yyTraceFILE ){ - fprintf(yyTraceFILE,"%sDiscard input token %s\n", - yyTracePrompt,yyTokenName[yymajor]); - } -#endif - yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion); - yymajor = YYNOCODE; - }else{ - while( yypParser->yytos >= yypParser->yystack - && (yyact = yy_find_reduce_action( - yypParser->yytos->stateno, - YYERRORSYMBOL)) > YY_MAX_SHIFTREDUCE - ){ - yy_pop_parser_stack(yypParser); - } - if( yypParser->yytos < yypParser->yystack || yymajor==0 ){ - yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); - yy_parse_failed(yypParser); -#ifndef YYNOERRORRECOVERY - yypParser->yyerrcnt = -1; -#endif - yymajor = YYNOCODE; - }else if( yymx!=YYERRORSYMBOL ){ - yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor); - } - } - yypParser->yyerrcnt = 3; - yyerrorhit = 1; - if( yymajor==YYNOCODE ) break; - yyact = yypParser->yytos->stateno; -#elif defined(YYNOERRORRECOVERY) - /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to - ** do any kind of error recovery. Instead, simply invoke the syntax - ** error routine and continue going as if nothing had happened. - ** - ** Applications can set this macro (for example inside %include) if - ** they intend to abandon the parse upon the first syntax error seen. - */ - yy_syntax_error(yypParser,yymajor, yyminor); - yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); - break; -#else /* YYERRORSYMBOL is not defined */ - /* This is what we do if the grammar does not define ERROR: - ** - ** * Report an error message, and throw away the input token. - ** - ** * If the input token is $, then fail the parse. - ** - ** As before, subsequent error messages are suppressed until - ** three input tokens have been successfully shifted. - */ - if( yypParser->yyerrcnt<=0 ){ - yy_syntax_error(yypParser,yymajor, yyminor); - } - yypParser->yyerrcnt = 3; - yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); - if( yyendofinput ){ - yy_parse_failed(yypParser); -#ifndef YYNOERRORRECOVERY - yypParser->yyerrcnt = -1; -#endif - } - break; -#endif - } - }while( yypParser->yytos>yypParser->yystack ); -#ifndef NDEBUG - if( yyTraceFILE ){ - yyStackEntry *i; - char cDiv = '['; - fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt); - for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){ - fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]); - cDiv = ' '; - } - fprintf(yyTraceFILE,"]\n"); - } -#endif - return; -} - -/* -** Return the fallback token corresponding to canonical token iToken, or -** 0 if iToken has no fallback. -*/ -int ParseFallback(int iToken){ -#ifdef YYFALLBACK - if( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) ){ - return yyFallback[iToken]; - } -#else - (void)iToken; -#endif - return 0; -} diff --git a/source/libs/parser/test/insertParserTest.cpp b/source/libs/parser/test/insertParserTest.cpp index b6992e5157..7c46981028 100644 --- a/source/libs/parser/test/insertParserTest.cpp +++ b/source/libs/parser/test/insertParserTest.cpp @@ -60,15 +60,12 @@ protected: return code_; } - SVnodeModifOpStmtInfo* reslut() { - return res_; - } - void dumpReslut() { - size_t num = taosArrayGetSize(res_->pDataBlocks); - cout << "schemaAttache:" << (int32_t)res_->schemaAttache << ", payloadType:" << (int32_t)res_->payloadType << ", insertType:" << res_->insertType << ", numOfVgs:" << num << endl; + SVnodeModifOpStmt* pStmt = getVnodeModifStmt(res_); + size_t num = taosArrayGetSize(pStmt->pDataBlocks); + cout << "schemaAttache:" << (int32_t)pStmt->schemaAttache << ", payloadType:" << (int32_t)pStmt->payloadType << ", insertType:" << pStmt->insertType << ", numOfVgs:" << num << endl; for (size_t i = 0; i < num; ++i) { - SVgDataBlocks* vg = (SVgDataBlocks*)taosArrayGetP(res_->pDataBlocks, i); + SVgDataBlocks* vg = (SVgDataBlocks*)taosArrayGetP(pStmt->pDataBlocks, i); cout << "vgId:" << vg->vg.vgId << ", numOfTables:" << vg->numOfTables << ", dataSize:" << vg->size << endl; SSubmitReq* submit = (SSubmitReq*)vg->pData; cout << "length:" << ntohl(submit->length) << ", numOfBlocks:" << ntohl(submit->numOfBlocks) << endl; @@ -84,13 +81,14 @@ protected: } void checkReslut(int32_t numOfTables, int16_t numOfRows1, int16_t numOfRows2 = -1) { - ASSERT_EQ(res_->schemaAttache, 0); - ASSERT_EQ(res_->payloadType, PAYLOAD_TYPE_KV); - ASSERT_EQ(res_->insertType, TSDB_QUERY_TYPE_INSERT); - size_t num = taosArrayGetSize(res_->pDataBlocks); + SVnodeModifOpStmt* pStmt = getVnodeModifStmt(res_); + ASSERT_EQ(pStmt->schemaAttache, 0); + ASSERT_EQ(pStmt->payloadType, PAYLOAD_TYPE_KV); + ASSERT_EQ(pStmt->insertType, TSDB_QUERY_TYPE_INSERT); + size_t num = taosArrayGetSize(pStmt->pDataBlocks); ASSERT_GE(num, 0); for (size_t i = 0; i < num; ++i) { - SVgDataBlocks* vg = (SVgDataBlocks*)taosArrayGetP(res_->pDataBlocks, i); + SVgDataBlocks* vg = (SVgDataBlocks*)taosArrayGetP(pStmt->pDataBlocks, i); ASSERT_EQ(vg->numOfTables, numOfTables); ASSERT_GE(vg->size, 0); SSubmitReq* submit = (SSubmitReq*)vg->pData; @@ -115,7 +113,10 @@ private: cxt_.pMsg = errMagBuf_; cxt_.msgLen = max_err_len; code_ = TSDB_CODE_SUCCESS; - res_ = nullptr; + } + + SVnodeModifOpStmt* getVnodeModifStmt(SQuery* pQuery) { + return (SVnodeModifOpStmt*)pQuery->pRoot; } string acctId_; @@ -124,7 +125,7 @@ private: char sqlBuf_[max_sql_len]; SParseContext cxt_; int32_t code_; - SVnodeModifOpStmtInfo* res_; + SQuery* res_; }; // INSERT INTO tb_name VALUES (field1_value, ...) diff --git a/source/libs/parser/test/newParserTest.cpp b/source/libs/parser/test/newParserTest.cpp index 996e941848..d030931cba 100644 --- a/source/libs/parser/test/newParserTest.cpp +++ b/source/libs/parser/test/newParserTest.cpp @@ -50,19 +50,19 @@ protected: if (TSDB_CODE_SUCCESS != parseCode) { return false; } - code = doTranslate(&cxt_, &query_); + code = doTranslate(&cxt_, query_); // cout << "doTranslate return " << code << endl; if (code != TSDB_CODE_SUCCESS) { cout << "sql:[" << cxt_.pSql << "] code:" << code << ", " << translateCode << ", msg:" << errMagBuf_ << endl; return (code == translateCode); } - if (NULL != query_.pRoot && QUERY_NODE_SELECT_STMT == nodeType(query_.pRoot)) { + if (NULL != query_->pRoot && QUERY_NODE_SELECT_STMT == nodeType(query_->pRoot)) { cout << "input sql : [" << cxt_.pSql << "]" << endl; // string sql; // selectToSql(query_.pRoot, sql); // cout << "output sql : [" << sql << "]" << endl; string str; - selectToStr(query_.pRoot, str); + selectToStr(query_->pRoot, str); cout << "translate str : \n" << str << endl; } return (TSDB_CODE_SUCCESS == translateCode); @@ -507,7 +507,7 @@ private: char errMagBuf_[max_err_len]; string sqlBuf_; SParseContext cxt_; - SQuery query_; + SQuery* query_; }; TEST_F(NewParserTest, selectSimple) { diff --git a/source/libs/parser/test/parserTests.cpp b/source/libs/parser/test/parserTests.cpp deleted file mode 100644 index b971760132..0000000000 --- a/source/libs/parser/test/parserTests.cpp +++ /dev/null @@ -1,787 +0,0 @@ -/* - * 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 . - */ - -#include -#include -#include -#include "tglobal.h" - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wwrite-strings" -#pragma GCC diagnostic ignored "-Wunused-function" -#pragma GCC diagnostic ignored "-Wunused-variable" -#pragma GCC diagnostic ignored "-Wsign-compare" -#include "os.h" - -#include "astGenerator.h" -#include "parserInt.h" -#include "taos.h" -#include "tdef.h" -#include "tvariant.h" -#include "parserUtil.h" - -namespace { -void setSchema(SSchema* p, int32_t type, int32_t bytes, const char* name, int32_t colId) { - p->colId = colId; - p->bytes = bytes; - p->type = type; - strcpy(p->name, name); -} - -void setTableMetaInfo(SQueryStmtInfo* pQueryInfo, SCatalogReq* req) { - pQueryInfo->numOfTables = 1; - - pQueryInfo->pTableMetaInfo = (STableMetaInfo**)calloc(1, POINTER_BYTES); - STableMetaInfo* pTableMetaInfo = (STableMetaInfo*)calloc(1, sizeof(STableMetaInfo)); - pQueryInfo->pTableMetaInfo[0] = pTableMetaInfo; - - SName* name = (SName*)taosArrayGet(req->pTableName, 0); - - memcpy(&pTableMetaInfo->name, taosArrayGet(req->pTableName, 0), sizeof(SName)); - pTableMetaInfo->pTableMeta = (STableMeta*)calloc(1, sizeof(STableMeta) + 6 * sizeof(SSchema)); - strcpy(pTableMetaInfo->aliasName, name->tname); - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - pTableMeta->tableType = TSDB_NORMAL_TABLE; - pTableMeta->tableInfo.numOfColumns = 6; - pTableMeta->tableInfo.rowSize = 28; - pTableMeta->uid = 110; - - pTableMetaInfo->tagColList = (SArray*)taosArrayInit(4, POINTER_BYTES); - - SSchema* pSchema = pTableMetaInfo->pTableMeta->schema; - setSchema(&pSchema[0], TSDB_DATA_TYPE_TIMESTAMP, 8, "ts", 0); - setSchema(&pSchema[1], TSDB_DATA_TYPE_INT, 4, "a", 1); - setSchema(&pSchema[2], TSDB_DATA_TYPE_DOUBLE, 8, "b", 2); - setSchema(&pSchema[3], TSDB_DATA_TYPE_DOUBLE, 8, "col", 3); - setSchema(&pSchema[4], TSDB_DATA_TYPE_BINARY, 12, "c", 4); - setSchema(&pSchema[5], TSDB_DATA_TYPE_BINARY, 44, "d", 5); -} - -void sqlCheck(const char* sql, bool valid) { - SSqlInfo info1 = doGenerateAST(sql); - ASSERT_EQ(info1.valid, true); - - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SParseContext ctx = {0}; - ctx.db = "db1"; - ctx.acctId = 1; - SSqlNode* pNode = (SSqlNode*)taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - - if (valid) { - ASSERT_EQ(ret, 0); - } else { - ASSERT_NE(ret, 0); - } - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - -} // namespace - -TEST(testCase, validateAST_test) { - SSqlInfo info1 = doGenerateAST("select a a1111, a+b + 22, tbname from `t.1abc` where tsexprList[0]; - ASSERT_EQ(taosArrayGetSize(pExprList), 3); - - SExprInfo* p1 = (SExprInfo*)taosArrayGetP(pExprList, 0); - ASSERT_EQ(p1->base.pColumns->uid, 110); - ASSERT_EQ(p1->base.numOfParams, 0); - ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_INT); - ASSERT_STRCASEEQ(p1->base.resSchema.name, "a1111"); - ASSERT_STRCASEEQ(p1->base.pColumns->name, "t.1abc.a"); - ASSERT_EQ(p1->base.pColumns->info.colId, 1); - ASSERT_EQ(p1->base.pColumns->flag, TSDB_COL_NORMAL); - ASSERT_STRCASEEQ(p1->base.token, "a1111"); - - ASSERT_EQ(taosArrayGetSize(pExprList), 3); - - SExprInfo* p2 = (SExprInfo*)taosArrayGetP(pExprList, 1); - ASSERT_EQ(p2->base.pColumns->uid, 110); - ASSERT_EQ(p2->base.numOfParams, 1); // it is the serialized binary string of expression. - ASSERT_EQ(p2->base.resSchema.type, TSDB_DATA_TYPE_DOUBLE); - ASSERT_STRCASEEQ(p2->base.resSchema.name, "a+b + 22"); - - // ASSERT_STRCASEEQ(p2->base.colInfo.name, "t.1abc.a"); - // ASSERT_EQ(p1->base.colInfo.colId, 1); - // ASSERT_EQ(p1->base.colInfo.flag, TSDB_COL_NORMAL); - ASSERT_STRCASEEQ(p2->base.token, "a+b + 22"); - - ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 3); - ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 3); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - -TEST(testCase, function_Test) { - SSqlInfo info1 = doGenerateAST("select count(a) from `t.1abc`"); - ASSERT_EQ(info1.valid, true); - - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SSqlNode* pNode = (SSqlNode*)taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - SParseContext ctx = {0}; - ctx.db = "db1"; - ctx.acctId = 1; - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - - SArray* pExprList = pQueryInfo->exprList[0]; - ASSERT_EQ(taosArrayGetSize(pExprList), 1); - - SExprInfo* p1 = (SExprInfo*)taosArrayGetP(pExprList, 0); - ASSERT_EQ(p1->base.pColumns->uid, 110); - ASSERT_EQ(p1->base.numOfParams, 0); - ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_BIGINT); - ASSERT_STRCASEEQ(p1->base.resSchema.name, "count(a)"); - ASSERT_STRCASEEQ(p1->base.pColumns->name, "t.1abc.a"); - ASSERT_EQ(p1->base.pColumns->info.colId, 1); - ASSERT_EQ(p1->base.pColumns->flag, TSDB_COL_NORMAL); - ASSERT_STRCASEEQ(p1->base.token, "count(a)"); - ASSERT_EQ(p1->base.interBytes, 8); - - ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 2); - ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 1); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - -TEST(testCase, function_Test2) { - SSqlInfo info1 = doGenerateAST("select count(a) abc from `t.1abc`"); - ASSERT_EQ(info1.valid, true); - - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SSqlNode* pNode = (SSqlNode*)taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - SParseContext ctx = {0}; - ctx.db = "db1"; - ctx.acctId = 1; - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - - SArray* pExprList = pQueryInfo->exprList[0]; - ASSERT_EQ(taosArrayGetSize(pExprList), 1); - - SExprInfo* p1 = (SExprInfo*)taosArrayGetP(pExprList, 0); - ASSERT_EQ(p1->base.pColumns->uid, 110); - ASSERT_EQ(p1->base.numOfParams, 0); - ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_BIGINT); - ASSERT_STRCASEEQ(p1->base.resSchema.name, "abc"); - ASSERT_STRCASEEQ(p1->base.pColumns->name, "t.1abc.a"); - ASSERT_EQ(p1->base.pColumns->info.colId, 1); - ASSERT_EQ(p1->base.pColumns->flag, TSDB_COL_NORMAL); - ASSERT_STRCASEEQ(p1->base.token, "count(a)"); - ASSERT_EQ(p1->base.interBytes, 8); - - ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 2); - ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 1); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - -TEST(testCase, function_Test3) { - SSqlInfo info1 = doGenerateAST("select first(*) from `t.1abc`"); - ASSERT_EQ(info1.valid, true); - - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SSqlNode* pNode = (SSqlNode*)taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - SParseContext ctx = {0}; - ctx.db = "db1"; - ctx.acctId = 1; - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - - SArray* pExprList = pQueryInfo->exprList[0]; - ASSERT_EQ(taosArrayGetSize(pExprList), 6); - - SExprInfo* p1 = (SExprInfo*)taosArrayGetP(pExprList, 0); - ASSERT_EQ(p1->base.pColumns->uid, 110); - ASSERT_EQ(p1->base.numOfParams, 0); - ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_TIMESTAMP); - ASSERT_STRCASEEQ(p1->base.resSchema.name, "first(ts)"); - ASSERT_STRCASEEQ(p1->base.pColumns->name, "t.1abc.ts"); - ASSERT_EQ(p1->base.pColumns->info.colId, 0); - ASSERT_EQ(p1->base.pColumns->flag, TSDB_COL_NORMAL); - ASSERT_STRCASEEQ(p1->base.token, "first(ts)"); - ASSERT_EQ(p1->base.interBytes, 24); - - ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 6); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - -TEST(testCase, function_Test4) { - SSqlInfo info1 = doGenerateAST("select block_dist() as a1 from `t.1abc`"); - ASSERT_EQ(info1.valid, true); - - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SSqlNode* pNode = (SSqlNode*)taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - SParseContext ctx = {0}; - ctx.db = "db1"; - ctx.acctId = 1; - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - - SArray* pExprList = pQueryInfo->exprList[0]; - ASSERT_EQ(taosArrayGetSize(pExprList), 1); - - SExprInfo* p1 = (SExprInfo*)taosArrayGetP(pExprList, 0); - ASSERT_EQ(p1->base.pColumns->uid, 110); - ASSERT_EQ(p1->base.numOfParams, 1); - ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_BINARY); - ASSERT_STRCASEEQ(p1->base.resSchema.name, "a1"); - // ASSERT_STRCASEEQ(p1->base.colInfo.name, "t.1abc.ts"); - // ASSERT_EQ(p1->base.colInfo.colId, 0); - ASSERT_EQ(p1->base.pColumns->flag, TSDB_COL_UDC); - ASSERT_STRCASEEQ(p1->base.token, "block_dist()"); - ASSERT_EQ(p1->base.interBytes, 0); - - ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 1); - ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 1); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - -TEST(testCase, function_Test5) { - // todo select concat(concat(a, b), concat(b, a)) from `t.1abc`; - - SSqlInfo info1 = doGenerateAST("select sum(a) + avg(b) as a1 from `t.1abc`"); - ASSERT_EQ(info1.valid, true); - - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SSqlNode* pNode = (SSqlNode*)taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - SParseContext ctx = {0}; - ctx.db = "db1"; - ctx.acctId = 1; - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - ASSERT_EQ(ret, 0); - - SArray* pExprList = pQueryInfo->exprList[0]; - ASSERT_EQ(taosArrayGetSize(pExprList), 1); - - SExprInfo* p1 = (SExprInfo*)taosArrayGetP(pExprList, 0); - ASSERT_EQ(p1->base.numOfCols, 2); - ASSERT_EQ(p1->base.pColumns->uid, 110); - - ASSERT_EQ(p1->base.numOfParams, 1); - ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_DOUBLE); - ASSERT_STRCASEEQ(p1->base.resSchema.name, "a1"); - - ASSERT_EQ(p1->base.pColumns->flag, TSDB_COL_TMP); - ASSERT_STREQ(p1->base.pColumns->name, "sum(a)"); - ASSERT_STRCASEEQ(p1->base.token, "sum(a) + avg(b)"); - ASSERT_EQ(p1->base.interBytes, 0); - - ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 3); - ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 1); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - -TEST(testCase, function_Test10) { - sqlCheck("select c from `t.1abc`", true); - sqlCheck("select length(c) from `t.1abc`", true); - sqlCheck("select length(sum(col)) from `t.1abc`", true); - sqlCheck("select sum(length(a+b)) from `t.1abc`", true); - sqlCheck("select sum(sum(a+b)) from `t.1abc`", false); - sqlCheck("select sum(length(a) + length(b)) from `t.1abc`", true); - sqlCheck("select length(sum(a) + sum(b)) + length(sum(a) + sum(b)) from `t.1abc`", true); - sqlCheck("select sum(length(sum(a))) from `t.1abc`", true); - sqlCheck("select cov(a, b) from `t.1abc`", true); - sqlCheck("select sum(length(a) + count(b)) from `t.1abc`", false); - - sqlCheck("select concat(sum(a), count(b)) from `t.1abc`", true); - - sqlCheck("select concat(concat(a,b), concat(a,b)) from `t.1abc`", true); - sqlCheck("select length(length(length(a))) from `t.1abc`", true); - sqlCheck("select count() from `t.1abc`", false); - sqlCheck("select block_dist() from `t.1abc`", true); - sqlCheck("select block_dist(a) from `t.1abc`", false); - sqlCheck("select count(*) from `t.1abc` interval(1s) group by a", false); - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// - sqlCheck("select length119(a,b) from `t.1abc`", false); - sqlCheck("select length(a, b) from `t.1abc`", false); - sqlCheck("select block_dist() + 20 from `t.1abc`", true); - sqlCheck("select count(b), c from `t.1abc`", false); - sqlCheck("select top(a, 20), count(b) from `t.1abc`", false); - sqlCheck("select top(a, 20), b from `t.1abc`", false); - sqlCheck("select top(a, 20), a+20 from `t.1abc`", true); -// sqlCheck("select top(a, 20), bottom(a, 10) from `t.1abc`", false); -// sqlCheck("select last_row(*), count(b) from `t.1abc`", false); -// sqlCheck("select last_row(a, b) + 20 from `t.1abc`", false); -// sqlCheck("select last_row(count(*)) from `t.1abc`", false); -} - -TEST(testCase, function_Test6) { - SSqlInfo info1 = doGenerateAST( - "select sum(a+b) as a1, first(b*a), count(b+b), count(1), count(42.1) from `t.1abc` interval(10s, 1s)"); - ASSERT_EQ(info1.valid, true); - - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SSqlNode* pNode = (SSqlNode*)taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - SParseContext ctx = {0}; - ctx.db = "db1"; - ctx.acctId = 1; - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - ASSERT_EQ(ret, 0); - - SArray* pExprList = pQueryInfo->exprList[0]; - if (tsCompatibleModel) { - ASSERT_EQ(taosArrayGetSize(pExprList), 6); - } else { - ASSERT_EQ(taosArrayGetSize(pExprList), 5); - } - - int32_t index = tsCompatibleModel? 1:0; - SExprInfo* p1 = (SExprInfo*)taosArrayGetP(pExprList, index); - ASSERT_EQ(p1->base.pColumns->uid, 110); - ASSERT_EQ(p1->base.numOfParams, 0); - ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_DOUBLE); - ASSERT_STRCASEEQ(p1->base.resSchema.name, "a1"); - ASSERT_EQ(p1->base.pColumns->flag, TSDB_COL_TMP); - ASSERT_STRCASEEQ(p1->base.token, "sum(a+b)"); - ASSERT_EQ(p1->base.interBytes, 16); - ASSERT_EQ(p1->pExpr->nodeType, TEXPR_FUNCTION_NODE); - ASSERT_STRCASEEQ(p1->pExpr->_function.functionName, "sum"); - ASSERT_EQ(p1->pExpr->_function.num, 1); - - tExprNode* pParam = p1->pExpr->_function.pChild[0]; - - ASSERT_EQ(pParam->nodeType, TEXPR_COL_NODE); - ASSERT_STREQ(pParam->pSchema->name, "t.1abc.a+b"); - - ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 3); - - int32_t numOfResCol = tsCompatibleModel? 6:5; - ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, numOfResCol); - - index = tsCompatibleModel? 2:1; - SExprInfo* p2 = (SExprInfo*)taosArrayGetP(pExprList, index); - ASSERT_EQ(p2->base.pColumns->uid, 110); - ASSERT_EQ(p2->base.numOfParams, 0); - ASSERT_EQ(p2->base.resSchema.type, TSDB_DATA_TYPE_DOUBLE); - ASSERT_STRCASEEQ(p2->base.resSchema.name, "first(b*a)"); - - ASSERT_EQ(p2->base.pColumns->flag, TSDB_COL_TMP); - ASSERT_STREQ(p2->base.pColumns->name, "t.1abc.b*a"); - - ASSERT_STRCASEEQ(p2->base.token, "first(b*a)"); - ASSERT_EQ(p2->base.interBytes, 24); - ASSERT_EQ(p2->pExpr->nodeType, TEXPR_FUNCTION_NODE); - ASSERT_STRCASEEQ(p2->pExpr->_function.functionName, "first"); - ASSERT_EQ(p2->pExpr->_function.num, 1); - ASSERT_EQ(p2->pExpr->_function.pChild[0]->nodeType, TEXPR_COL_NODE); - ASSERT_STREQ(p2->pExpr->_function.pChild[0]->pSchema->name, "t.1abc.b*a"); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - - TEST(testCase, function_Test7) { - SSqlInfo info1 = doGenerateAST("select count(a+b),count(1) from `t.1abc` interval(10s, 1s)"); - ASSERT_EQ(info1.valid, true); - - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - SParseContext ctx = {0}; - ctx.db = "db1"; - ctx.acctId = 1; - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - ASSERT_EQ(ret, 0); - - SArray* pExprList = pQueryInfo->exprList[0]; - ASSERT_EQ(taosArrayGetSize(pExprList), 3); - - int32_t index = tsCompatibleModel? 1:0; - SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, index); - ASSERT_EQ(p1->base.pColumns->uid, 110); - ASSERT_EQ(p1->base.numOfParams, 0); - ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_BIGINT); - ASSERT_STRCASEEQ(p1->base.resSchema.name, "count(a+b)"); - ASSERT_EQ(p1->base.pColumns->flag, TSDB_COL_TMP); - ASSERT_STRCASEEQ(p1->base.token, "count(a+b)"); - ASSERT_EQ(p1->base.interBytes, 8); - ASSERT_EQ(p1->pExpr->nodeType, TEXPR_FUNCTION_NODE); - ASSERT_STREQ(p1->pExpr->_function.functionName, "count"); - - tExprNode* pParam = p1->pExpr->_function.pChild[0]; - ASSERT_EQ(pParam->nodeType, TEXPR_COL_NODE); - - SExprInfo* p2 = (SExprInfo*) taosArrayGetP(pQueryInfo->exprList[1], 0); - ASSERT_EQ(p2->pExpr->nodeType, TEXPR_BINARYEXPR_NODE); - - ASSERT_EQ(p2->pExpr->_node.optr, OP_TYPE_ADD); - ASSERT_EQ(p2->pExpr->_node.pLeft->nodeType, TEXPR_COL_NODE); - ASSERT_EQ(p2->pExpr->_node.pRight->nodeType, TEXPR_COL_NODE); - - ASSERT_EQ(pParam->pSchema->colId, p2->base.resSchema.colId); - - ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 3); - - int32_t numOfCols = tsCompatibleModel? 3:2; - ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, numOfCols); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - - TEST(testCase, function_Test8) { - SSqlInfo info1 = doGenerateAST("select top(a*b / 99, 20) from `t.1abc` interval(10s, 1s)"); - ASSERT_EQ(info1.valid, true); - - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - SParseContext ctx = {0}; - ctx.db = "db1"; - ctx.acctId = 1; - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - ASSERT_EQ(ret, 0); - - SArray* pExprList = pQueryInfo->exprList[0]; - ASSERT_EQ(taosArrayGetSize(pExprList), 2); - - SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, 1); - ASSERT_EQ(p1->base.pColumns->uid, 110); - ASSERT_EQ(p1->base.numOfParams, 1); - ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_DOUBLE); - ASSERT_STRCASEEQ(p1->base.resSchema.name, "top(a*b / 99, 20)"); - ASSERT_EQ(p1->base.pColumns->flag, TSDB_COL_TMP); - ASSERT_STRCASEEQ(p1->base.token, "top(a*b / 99, 20)"); - ASSERT_EQ(p1->base.interBytes, 16); - - ASSERT_EQ(p1->pExpr->nodeType, TEXPR_FUNCTION_NODE); - ASSERT_STRCASEEQ(p1->pExpr->_function.functionName, "top"); - ASSERT_TRUE(p1->pExpr->_function.num == 1); - - tExprNode* pParam = p1->pExpr->_function.pChild[0]; - - ASSERT_EQ(pParam->nodeType, TSDB_COL_TMP); -// ASSERT_EQ(pParam->.optr, TSDB_BINARY_OP_DIVIDE); -// ASSERT_EQ(pParam->_node.pLeft->nodeType, TEXPR_BINARYEXPR_NODE); -// ASSERT_EQ(pParam->_node.pRight->nodeType, TEXPR_VALUE_NODE); - - ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 3); - ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 2); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); - - ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - info1 = doGenerateAST("select sum(length(a)+length(b)) from `t.1abc` interval(10s, 1s)"); - ASSERT_EQ(info1.valid, true); - - pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.sub.node), 0); - code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - ASSERT_EQ(ret, 0); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - - TEST(testCase, invalid_sql_Test) { - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SSqlInfo info1 = doGenerateAST("select count(k) from `t.1abc` interval(10s, 1s)"); - ASSERT_EQ(info1.valid, true); - - SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - SParseContext ctx = {0}; - ctx.db = "db1"; - ctx.acctId = 1; - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - ASSERT_NE(ret, 0); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -//=============================================================================================================== - info1 = doGenerateAST("select top(a*b, ABC) from `t.1abc` interval(10s, 1s)"); - ASSERT_EQ(info1.valid, true); - - pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.sub.node), 0); - code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - ASSERT_NE(ret, 0); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - -TEST(testCase, show_user_Test) { - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - char sql1[] = "show users"; - SSqlInfo info1 = doGenerateAST(sql1); - ASSERT_EQ(info1.valid, true); - - SParseContext ct= {.requestId = 1, .acctId = 1, .db = "abc", .pTransporter = NULL}; - SDclStmtInfo* output = qParserValidateDclSqlNode(&info1, &ct, msg, buf.len); - ASSERT_NE(output, nullptr); - - // convert the show command to be the select query - // select name, privilege, create_time, account from information_schema.users; -} - -TEST(testCase, create_user_Test) { - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - char sql[] = {"create user abc pass 'abc'"}; - - SSqlInfo info1 = doGenerateAST(sql); - ASSERT_EQ(info1.valid, true); - ASSERT_EQ(isDclSqlStatement(&info1), true); - - SParseContext ct= {.requestId = 1, .acctId = 1, .db = "abc"}; - SDclStmtInfo* output = qParserValidateDclSqlNode(&info1, &ct, msg, buf.len); - ASSERT_NE(output, nullptr); - - destroySqlInfo(&info1); -} - -#pragma GCC diagnostic pop \ No newline at end of file diff --git a/source/libs/parser/test/plannerTest.cpp b/source/libs/parser/test/plannerTest.cpp deleted file mode 100644 index 43daff9fec..0000000000 --- a/source/libs/parser/test/plannerTest.cpp +++ /dev/null @@ -1,209 +0,0 @@ -/* - * 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 . - */ - -#include -#include -#include -#include - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wwrite-strings" -#pragma GCC diagnostic ignored "-Wunused-function" -#pragma GCC diagnostic ignored "-Wunused-variable" -#pragma GCC diagnostic ignored "-Wsign-compare" -#include "os.h" - -#include "astGenerator.h" -#include "parserInt.h" -#include "taos.h" -#include "tdef.h" -#include "tvariant.h" -#include "planner.h" -#include "../../planner/inc/plannerInt.h" - -namespace { -void setSchema(SSchema* p, int32_t type, int32_t bytes, const char* name, int32_t colId) { - p->colId = colId; - p->bytes = bytes; - p->type = type; - strcpy(p->name, name); -} - -void setTableMetaInfo(SQueryStmtInfo* pQueryInfo, SCatalogReq *req) { - pQueryInfo->numOfTables = 1; - - pQueryInfo->pTableMetaInfo = (STableMetaInfo**)calloc(1, POINTER_BYTES); - STableMetaInfo* pTableMetaInfo = (STableMetaInfo*)calloc(1, sizeof(STableMetaInfo)); - pQueryInfo->pTableMetaInfo[0] = pTableMetaInfo; - - SName* name = (SName*)taosArrayGet(req->pTableName, 0); - - memcpy(&pTableMetaInfo->name, taosArrayGet(req->pTableName, 0), sizeof(SName)); - pTableMetaInfo->pTableMeta = (STableMeta*)calloc(1, sizeof(STableMeta) + 4 * sizeof(SSchema)); - strcpy(pTableMetaInfo->aliasName, name->tname); - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - pTableMeta->tableType = TSDB_NORMAL_TABLE; - pTableMeta->tableInfo.numOfColumns = 4; - pTableMeta->tableInfo.rowSize = 28; - pTableMeta->uid = 110; - - pTableMetaInfo->tagColList = (SArray*) taosArrayInit(4, POINTER_BYTES); - - SSchema* pSchema = pTableMetaInfo->pTableMeta->schema; - setSchema(&pSchema[0], TSDB_DATA_TYPE_TIMESTAMP, 8, "ts", 0); - setSchema(&pSchema[1], TSDB_DATA_TYPE_INT, 4, "a", 1); - setSchema(&pSchema[2], TSDB_DATA_TYPE_DOUBLE, 8, "b", 2); - setSchema(&pSchema[3], TSDB_DATA_TYPE_DOUBLE, 8, "col", 3); -} - -void generateLogicplan(const char* sql) { - SSqlInfo info1 = doGenerateAST(sql); - ASSERT_EQ(info1.valid, true); - - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - SParseContext ctx = {0}; - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - ASSERT_EQ(ret, 0); - - struct SQueryPlanNode* n = nullptr; - code = createQueryPlan((const SQueryNode*)pQueryInfo, &n); - - char* str = NULL; - queryPlanToString(n, &str); - - printf("--------SQL:%s\n", sql); - printf("%s\n", str); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} -} - -TEST(testCase, planner_test) { - SSqlInfo info1 = doGenerateAST("select top(a*b / 99, 20) from `t.1abc` interval(10s, 1s)"); - ASSERT_EQ(info1.valid, true); - - char msg[128] = {0}; - SMsgBuf buf; - buf.len = 128; - buf.buf = msg; - - SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.sub.node), 0); - int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf); - ASSERT_EQ(code, 0); - - SCatalogReq req = {0}; - SParseContext ctx = {0}; - - int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128); - ASSERT_EQ(ret, 0); - ASSERT_EQ(taosArrayGetSize(req.pTableName), 1); - - SQueryStmtInfo* pQueryInfo = createQueryInfo(); - setTableMetaInfo(pQueryInfo, &req); - - SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.sub.node, 0); - ret = validateSqlNode(pSqlNode, pQueryInfo, &buf); - ASSERT_EQ(ret, 0); - - SArray* pExprList = pQueryInfo->exprList[0]; - - int32_t num = tsCompatibleModel? 2:1; - ASSERT_EQ(taosArrayGetSize(pExprList), num); - - SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, 1); - ASSERT_EQ(p1->base.pColumns->uid, 110); - ASSERT_EQ(p1->base.numOfParams, 1); - ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_DOUBLE); - ASSERT_STRCASEEQ(p1->base.resSchema.name, "top(a*b / 99, 20)"); - ASSERT_EQ(p1->base.pColumns->flag, TSDB_COL_TMP); - ASSERT_STRCASEEQ(p1->base.token, "top(a*b / 99, 20)"); - ASSERT_EQ(p1->base.interBytes, 16); - - ASSERT_EQ(p1->pExpr->nodeType, TEXPR_FUNCTION_NODE); - ASSERT_STREQ(p1->pExpr->_function.functionName, "top"); - - tExprNode* pParam = p1->pExpr->_function.pChild[0]; - - ASSERT_EQ(pParam->nodeType, TEXPR_COL_NODE); - ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 3); - ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 2); - - struct SQueryPlanNode* n = nullptr; - code = createQueryPlan((const SQueryNode*)pQueryInfo, &n); - - char* str = NULL; - queryPlanToString(n, &str); - printf("%s\n", str); - - destroyQueryInfo(pQueryInfo); - qParserCleanupMetaRequestInfo(&req); - destroySqlInfo(&info1); -} - -TEST(testCase, displayPlan) { - generateLogicplan("select count(*) from `t.1abc`"); - generateLogicplan("select count(*)+ 22 from `t.1abc`"); - generateLogicplan("select count(*)+ 22 from `t.1abc` interval(1h, 20s) sliding(10m) limit 20,30"); - generateLogicplan("select count(*) from `t.1abc` group by a"); - generateLogicplan("select count(A+B) from `t.1abc` group by a"); - generateLogicplan("select count(length(a)+b) from `t.1abc` group by a"); - generateLogicplan("select count(*) from `t.1abc` interval(10s, 5s) sliding(7s)"); - generateLogicplan("select count(*) from `t.1abc` interval(10s, 5s) sliding(7s) order by 1 desc "); - generateLogicplan("select count(*),sum(a),avg(b),min(a+b)+99 from `t.1abc`"); - generateLogicplan("select count(*), min(a) + 99 from `t.1abc`"); - generateLogicplan("select count(length(count(*) + 22)) from `t.1abc`"); - generateLogicplan("select concat(concat(a,b), concat(a,b)) from `t.1abc` limit 20"); - generateLogicplan("select count(*), first(a), last(b) from `t.1abc` state_window(a)"); - generateLogicplan("select count(*), first(a), last(b) from `t.1abc` session(ts, 20s)"); - - // order by + group by column + limit offset - generateLogicplan("select top(a, 20) k from `t.1abc` order by k asc limit 3 offset 1"); - - // fill - generateLogicplan("select min(a) from `t.1abc` where ts>now and ts #include @@ -723,4 +725,6 @@ TEST(testCase, extractMeta_test) { destroySqlInfo(&info1); } -#pragma GCC diagnostic pop \ No newline at end of file +#pragma GCC diagnostic pop + +#endif diff --git a/source/libs/planner/inc/plannerInt.h b/source/libs/planner/inc/plannerInt.h deleted file mode 100644 index 26ae44a08f..0000000000 --- a/source/libs/planner/inc/plannerInt.h +++ /dev/null @@ -1,137 +0,0 @@ -/* - * 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 _TD_PLANNER_INT_H_ -#define _TD_PLANNER_INT_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include "common.h" -#include "tarray.h" -#include "planner.h" -#include "parser.h" -#include "tmsg.h" - -#define QNODE_TAGSCAN 1 -#define QNODE_TABLESCAN 2 -#define QNODE_STREAMSCAN 3 -#define QNODE_PROJECT 4 -#define QNODE_AGGREGATE 5 -#define QNODE_GROUPBY 6 -#define QNODE_LIMIT 7 -#define QNODE_JOIN 8 -#define QNODE_DISTINCT 9 -#define QNODE_SORT 10 -#define QNODE_UNION 11 -#define QNODE_TIMEWINDOW 12 -#define QNODE_SESSIONWINDOW 13 -#define QNODE_STATEWINDOW 14 -#define QNODE_FILL 15 -#define QNODE_MODIFY 16 - -typedef struct SQueryDistPlanNodeInfo { - bool stableQuery; // super table query or not - int32_t phase; // merge|partial - int32_t type; // operator type - char *name; // operator name - SEpSet *sourceEp; // data source epset -} SQueryDistPlanNodeInfo; - -typedef struct SQueryTableInfo { - char *tableName; // to be deleted - uint64_t uid; // to be deleted - STableMetaInfo *pMeta; - STimeWindow window; -} SQueryTableInfo; - -typedef struct SQueryPlanNode { - SQueryNodeBasicInfo info; - SSchema *pSchema; // the schema of the input SSDatablock - int32_t numOfCols; // number of input columns - SArray *pExpr; // the query functions or sql aggregations - int32_t numOfExpr; // number of result columns, which is also the number of pExprs - void *pExtInfo; // additional information - // children operator to generated result for current node to process - // in case of join, multiple prev nodes exist. - SArray *pChildren; // upstream nodes - struct SQueryPlanNode *pParent; -} SQueryPlanNode; - -typedef struct SDataPayloadInfo { - int32_t msgType; - SArray *payload; -} SDataPayloadInfo; - -/** - * Optimize the query execution plan, currently not implement yet. - * @param pQueryNode - * @return - */ -int32_t optimizeQueryPlan(struct SQueryPlanNode* pQueryNode); - -/** - * Create the query plan according to the bound AST, which is in the form of pQueryInfo - * @param pQueryInfo - * @param pQueryNode - * @return - */ -int32_t createQueryPlan(const SQueryNode* pNode, struct SQueryPlanNode** pQueryPlan); - -/** - * Convert the query plan to string, in order to display it in the shell. - * @param pQueryNode - * @return - */ -int32_t queryPlanToString(struct SQueryPlanNode* pQueryNode, char** str); - -/** - * Restore the SQL statement according to the logic query plan. - * @param pQueryNode - * @param sql - * @return - */ -int32_t queryPlanToSql(struct SQueryPlanNode* pQueryNode, char** sql); - -int32_t createDag(SQueryPlanNode* pQueryNode, struct SCatalog* pCatalog, SQueryDag** pDag, SArray* pNodeList, uint64_t requestId); -void setSubplanExecutionNode(SSubplan* subplan, uint64_t templateId, SDownstreamSource* pSource); -int32_t subPlanToString(const SSubplan *pPhyNode, char** str, int32_t* len); -int32_t stringToSubplan(const char* str, SSubplan** subplan); - -/** - * Destroy the query plan object. - * @return - */ -void destroyQueryPlan(struct SQueryPlanNode* pQueryNode); - -/** - * Destroy the physical plan. - * @param pQueryPhyNode - * @return - */ -void* destroyQueryPhyPlan(struct SPhyNode* pQueryPhyNode); - -const char* opTypeToOpName(int32_t type); -int32_t opNameToOpType(const char* name); - -const char* dsinkTypeToDsinkName(int32_t type); -int32_t dsinkNameToDsinkType(const char* name); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_PLANNER_INT_H_*/ \ No newline at end of file diff --git a/source/libs/planner/inc/plannerUtil.h b/source/libs/planner/inc/plannerUtil.h deleted file mode 100644 index 198c883c5a..0000000000 --- a/source/libs/planner/inc/plannerUtil.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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_PLANNERUTIL_H -#define TDENGINE_PLANNERUTIL_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include "parser.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#endif // TDENGINE_PLANNERUTIL_H diff --git a/source/libs/planner/src/logicPlan.c b/source/libs/planner/src/logicPlan.c index a5b1fd6f6b..6ba17f0627 100644 --- a/source/libs/planner/src/logicPlan.c +++ b/source/libs/planner/src/logicPlan.c @@ -13,6 +13,8 @@ * along with this program. If not, see . */ +#if 0 + #include #include "function.h" #include "os.h" @@ -664,3 +666,5 @@ int32_t queryPlanToString(struct SQueryPlanNode* pQueryNode, char** str) { SQueryPlanNode* queryPlanFromString() { return NULL; } + +#endif diff --git a/source/libs/planner/src/physicalPlan.c b/source/libs/planner/src/physicalPlan.c index eadc95b98d..9c927ca8e7 100644 --- a/source/libs/planner/src/physicalPlan.c +++ b/source/libs/planner/src/physicalPlan.c @@ -13,6 +13,8 @@ * along with this program. If not, see . */ +#if 0 + #include "plannerInt.h" #include "exception.h" #include "parser.h" @@ -488,3 +490,5 @@ void qDestroySubplan(SSubplan* pSubplan) { tfree(pSubplan); } + +#endif diff --git a/source/libs/planner/src/physicalPlanJson.c b/source/libs/planner/src/physicalPlanJson.c index b2109c0a4f..1a69a9d84c 100644 --- a/source/libs/planner/src/physicalPlanJson.c +++ b/source/libs/planner/src/physicalPlanJson.c @@ -13,6 +13,8 @@ * along with this program. If not, see . */ +#if 0 + #include "plannerInt.h" #include "parser.h" #include "cJSON.h" @@ -1221,3 +1223,5 @@ SQueryDag* qStringToDag(const char* pStr) { cJSON* pRoot = cJSON_Parse(pStr); return qJsonToDag(pRoot); } + +#endif diff --git a/source/libs/planner/src/planner.c b/source/libs/planner/src/planner.c index e6b7eaca7f..c2882928e0 100644 --- a/source/libs/planner/src/planner.c +++ b/source/libs/planner/src/planner.c @@ -13,90 +13,32 @@ * along with this program. If not, see . */ -#include "parser.h" -#include "plannerInt.h" +#include "planner.h" -static void extractResSchema(struct SQueryDag* const* pDag, SSchema** pResSchema, int32_t* numOfCols); +int32_t qCreateQueryPlan(SPlanContext* pCxt, SQueryPlan** pPlan) { -void qDestroyQueryDag(struct SQueryDag* pDag) { - if (pDag == NULL) { - return; - } - - size_t size = taosArrayGetSize(pDag->pSubplans); - for(size_t i = 0; i < size; ++i) { - SArray* pa = taosArrayGetP(pDag->pSubplans, i); - - size_t t = taosArrayGetSize(pa); - for(int32_t j = 0; j < t; ++j) { - SSubplan* pSubplan = taosArrayGetP(pa, j); - qDestroySubplan(pSubplan); - } - - taosArrayDestroy(pa); - } - - taosArrayDestroy(pDag->pSubplans); - tfree(pDag); } -int32_t qCreateQueryDag(const struct SQueryNode* pNode, struct SQueryDag** pDag, SSchema** pResSchema, int32_t* numOfCols, SArray* pNodeList, - uint64_t requestId) { - SQueryPlanNode* pLogicPlan; - int32_t code = createQueryPlan(pNode, &pLogicPlan); - if (TSDB_CODE_SUCCESS != code) { - destroyQueryPlan(pLogicPlan); - return code; - } +void qSetSubplanExecutionNode(SSubplan* subplan, uint64_t templateId, SDownstreamSource* pSource) { - if (pLogicPlan->info.type != QNODE_MODIFY) { - char* str = NULL; - queryPlanToString(pLogicPlan, &str); - qDebug("reqId:0x%"PRIx64": %s", requestId, str); - tfree(str); - } +} - code = optimizeQueryPlan(pLogicPlan); - if (TSDB_CODE_SUCCESS != code) { - destroyQueryPlan(pLogicPlan); - return code; - } +int32_t qSubPlanToString(const SSubplan* subplan, char** str, int32_t* len) { - code = createDag(pLogicPlan, NULL, pDag, pNodeList, requestId); - if (TSDB_CODE_SUCCESS != code) { - destroyQueryPlan(pLogicPlan); - qDestroyQueryDag(*pDag); - return code; - } +} - extractResSchema(pDag, pResSchema, numOfCols); +int32_t qStringToSubplan(const char* str, SSubplan** subplan) { - destroyQueryPlan(pLogicPlan); - return TSDB_CODE_SUCCESS; } -// extract the final result schema -void extractResSchema(struct SQueryDag* const* pDag, SSchema** pResSchema, int32_t* numOfCols) { - SArray* pTopSubplan = taosArrayGetP((*pDag)->pSubplans, 0); - - SSubplan* pPlan = taosArrayGetP(pTopSubplan, 0); - SDataBlockSchema* pDataBlockSchema = &(pPlan->pDataSink->schema); +char* qQueryPlanToString(const SQueryPlan* pPlan) { - *numOfCols = pDataBlockSchema->numOfCols; - if (*numOfCols > 0) { - *pResSchema = calloc(pDataBlockSchema->numOfCols, sizeof(SSchema)); - memcpy((*pResSchema), pDataBlockSchema->pSchema, pDataBlockSchema->numOfCols * sizeof(SSchema)); - } } -void qSetSubplanExecutionNode(SSubplan* subplan, uint64_t templateId, SDownstreamSource* pSource) { - setSubplanExecutionNode(subplan, templateId, pSource); -} +SQueryPlan* qStringToQueryPlan(const char* pStr) { -int32_t qSubPlanToString(const SSubplan *subplan, char** str, int32_t* len) { - return subPlanToString(subplan, str, len); } -int32_t qStringToSubplan(const char* str, SSubplan** subplan) { - return stringToSubplan(str, subplan); +void qDestroyQueryPlan(SQueryPlan* pPlan) { + } diff --git a/source/libs/planner/src/plannerImpl.c b/source/libs/planner/src/plannerImpl.c index b7e28d70db..8e2e898703 100644 --- a/source/libs/planner/src/plannerImpl.c +++ b/source/libs/planner/src/plannerImpl.c @@ -14,7 +14,9 @@ */ #include "plannerImpl.h" + #include "functionMgt.h" +#include "query.h" #define CHECK_ALLOC(p, res) \ do { \ @@ -33,13 +35,13 @@ } \ } while (0) -typedef struct SPlanContext { +typedef struct SLogicPlanContext { int32_t errCode; int32_t planNodeId; -} SPlanContext; +} SLogicPlanContext; -static SLogicNode* createQueryLogicNode(SPlanContext* pCxt, SNode* pStmt); -static SLogicNode* createLogicNodeByTable(SPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable); +static SLogicNode* createQueryLogicNode(SLogicPlanContext* pCxt, SNode* pStmt); +static SLogicNode* createLogicNodeByTable(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable); typedef struct SRewriteExprCxt { int32_t errCode; @@ -109,7 +111,7 @@ static int32_t rewriteExpr(int32_t planNodeId, int32_t rewriteId, SNodeList* pEx return cxt.errCode; } -static SLogicNode* pushLogicNode(SPlanContext* pCxt, SLogicNode* pRoot, SLogicNode* pNode) { +static SLogicNode* pushLogicNode(SLogicPlanContext* pCxt, SLogicNode* pRoot, SLogicNode* pNode) { if (TSDB_CODE_SUCCESS != pCxt->errCode) { goto error; } @@ -138,7 +140,7 @@ error: return pRoot; } -static SLogicNode* createScanLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect, SRealTableNode* pRealTable) { +static SLogicNode* createScanLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SRealTableNode* pRealTable) { SScanLogicNode* pScan = (SScanLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SCAN); CHECK_ALLOC(pScan, NULL); pScan->node.id = pCxt->planNodeId++; @@ -166,7 +168,7 @@ static SLogicNode* createScanLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect, return (SLogicNode*)pScan; } -static SLogicNode* createSubqueryLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect, STempTableNode* pTable) { +static SLogicNode* createSubqueryLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, STempTableNode* pTable) { SLogicNode* pRoot = createQueryLogicNode(pCxt, pTable->pSubquery); CHECK_ALLOC(pRoot, NULL); SNode* pNode; @@ -176,7 +178,7 @@ static SLogicNode* createSubqueryLogicNode(SPlanContext* pCxt, SSelectStmt* pSel return pRoot; } -static SLogicNode* createJoinLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect, SJoinTableNode* pJoinTable) { +static SLogicNode* createJoinLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SJoinTableNode* pJoinTable) { SJoinLogicNode* pJoin = (SJoinLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_JOIN); CHECK_ALLOC(pJoin, NULL); pJoin->node.id = pCxt->planNodeId++; @@ -209,7 +211,7 @@ static SLogicNode* createJoinLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect, return (SLogicNode*)pJoin; } -static SLogicNode* createLogicNodeByTable(SPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable) { +static SLogicNode* createLogicNodeByTable(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable) { switch (nodeType(pTable)) { case QUERY_NODE_REAL_TABLE: return createScanLogicNode(pCxt, pSelect, (SRealTableNode*)pTable); @@ -255,7 +257,7 @@ static EDealRes doCreateColumn(SNode* pNode, void* pContext) { return DEAL_RES_CONTINUE; } -static SNodeList* createColumnByRewriteExps(SPlanContext* pCxt, SNodeList* pExprs) { +static SNodeList* createColumnByRewriteExps(SLogicPlanContext* pCxt, SNodeList* pExprs) { SCreateColumnCxt cxt = { .errCode = TSDB_CODE_SUCCESS, .pList = nodesMakeList() }; CHECK_ALLOC(cxt.pList, NULL); @@ -267,7 +269,7 @@ static SNodeList* createColumnByRewriteExps(SPlanContext* pCxt, SNodeList* pExpr return cxt.pList; } -static SLogicNode* createAggLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect) { +static SLogicNode* createAggLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect) { SNodeList* pAggFuncs = NULL; CHECK_CODE(nodesCollectFuncs(pSelect, fmIsAggFunc, &pAggFuncs), NULL); if (NULL == pAggFuncs && NULL == pSelect->pGroupByList) { @@ -314,7 +316,7 @@ static SLogicNode* createAggLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect) return (SLogicNode*)pAgg; } -static SNodeList* createColumnByProjections(SPlanContext* pCxt, SNodeList* pExprs) { +static SNodeList* createColumnByProjections(SLogicPlanContext* pCxt, SNodeList* pExprs) { SNodeList* pList = nodesMakeList(); CHECK_ALLOC(pList, NULL); SNode* pNode; @@ -336,7 +338,7 @@ error: return NULL; } -static SLogicNode* createProjectLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect) { +static SLogicNode* createProjectLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect) { SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT); CHECK_ALLOC(pProject, NULL); pProject->node.id = pCxt->planNodeId++; @@ -349,7 +351,7 @@ static SLogicNode* createProjectLogicNode(SPlanContext* pCxt, SSelectStmt* pSele return (SLogicNode*)pProject; } -static SLogicNode* createSelectLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect) { +static SLogicNode* createSelectLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect) { SLogicNode* pRoot = createLogicNodeByTable(pCxt, pSelect, pSelect->pFromTable); if (TSDB_CODE_SUCCESS == pCxt->errCode && NULL != pSelect->pWhere) { pRoot->pConditions = nodesCloneNode(pSelect->pWhere); @@ -364,7 +366,7 @@ static SLogicNode* createSelectLogicNode(SPlanContext* pCxt, SSelectStmt* pSelec return pRoot; } -static SLogicNode* createQueryLogicNode(SPlanContext* pCxt, SNode* pStmt) { +static SLogicNode* createQueryLogicNode(SLogicPlanContext* pCxt, SNode* pStmt) { switch (nodeType(pStmt)) { case QUERY_NODE_SELECT_STMT: return createSelectLogicNode(pCxt, (SSelectStmt*)pStmt); @@ -374,7 +376,7 @@ static SLogicNode* createQueryLogicNode(SPlanContext* pCxt, SNode* pStmt) { } int32_t createLogicPlan(SNode* pNode, SLogicNode** pLogicNode) { - SPlanContext cxt = { .errCode = TSDB_CODE_SUCCESS, .planNodeId = 1 }; + SLogicPlanContext cxt = { .errCode = TSDB_CODE_SUCCESS, .planNodeId = 1 }; SLogicNode* pRoot = createQueryLogicNode(&cxt, pNode); if (TSDB_CODE_SUCCESS != cxt.errCode) { nodesDestroyNode((SNode*)pRoot); diff --git a/source/libs/planner/src/plannerUtil.c b/source/libs/planner/src/plannerUtil.c deleted file mode 100644 index b692fc67c2..0000000000 --- a/source/libs/planner/src/plannerUtil.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "tvariant.h" -#include "plannerUtil.h" - - - - diff --git a/source/libs/planner/test/newPlannerTest.cpp b/source/libs/planner/test/newPlannerTest.cpp index e7c0c24830..3f7f0ca868 100644 --- a/source/libs/planner/test/newPlannerTest.cpp +++ b/source/libs/planner/test/newPlannerTest.cpp @@ -18,7 +18,7 @@ #include #include "plannerImpl.h" -#include "newParser.h" +#include "parser.h" using namespace std; using namespace testing; @@ -46,17 +46,17 @@ protected: } bool run(TestTarget target = TEST_PHYSICAL_PLAN) { - int32_t code = parser(&cxt_, &query_); + int32_t code = qParseQuerySql(&cxt_, &query_); if (code != TSDB_CODE_SUCCESS) { cout << "sql:[" << cxt_.pSql << "] parser code:" << code << ", strerror:" << tstrerror(code) << ", msg:" << errMagBuf_ << endl; return false; } - const string syntaxTreeStr = toString(query_.pRoot, false); + const string syntaxTreeStr = toString(query_->pRoot, false); SLogicNode* pLogicPlan = nullptr; - code = createLogicPlan(query_.pRoot, &pLogicPlan); + code = createLogicPlan(query_->pRoot, &pLogicPlan); if (code != TSDB_CODE_SUCCESS) { cout << "sql:[" << cxt_.pSql << "] logic plan code:" << code << ", strerror:" << tstrerror(code) << endl; return false; @@ -110,7 +110,7 @@ private: char errMagBuf_[max_err_len]; string sqlBuf_; SParseContext cxt_; - SQuery query_; + SQuery* query_; }; TEST_F(NewPlannerTest, simple) { diff --git a/source/libs/planner/test/phyPlanTests.cpp b/source/libs/planner/test/phyPlanTests.cpp index f4bdf57572..b70b021057 100644 --- a/source/libs/planner/test/phyPlanTests.cpp +++ b/source/libs/planner/test/phyPlanTests.cpp @@ -12,7 +12,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - +#if 0 #include #include "plannerInt.h" @@ -205,3 +205,4 @@ TEST_F(PhyPlanTest, insertTest) { SQueryDag* dag = result(); // todo check } +#endif \ No newline at end of file diff --git a/source/libs/planner/test/plannerTests.cpp b/source/libs/planner/test/plannerTests.cpp index 765651a31a..466618da26 100644 --- a/source/libs/planner/test/plannerTests.cpp +++ b/source/libs/planner/test/plannerTests.cpp @@ -53,7 +53,7 @@ TEST(testCase, planner_test) { char msg[128] = {0}; const char* sql = "select top(a*b / 99, 20) from `t.1abc` interval(10s, 1s)"; - SQueryStmtInfo* pQueryInfo = nullptr; + // SQueryStmtInfo* pQueryInfo = nullptr; // int32_t code = qParseQuerySql(sql, strlen(sql), &pQueryInfo, 0, msg, sizeof(msg)); // ASSERT_EQ(code, 0); diff --git a/source/libs/qworker/CMakeLists.txt b/source/libs/qworker/CMakeLists.txt index 9ada451c61..89700e8939 100644 --- a/source/libs/qworker/CMakeLists.txt +++ b/source/libs/qworker/CMakeLists.txt @@ -8,7 +8,7 @@ target_include_directories( ) target_link_libraries(qworker - PRIVATE os util transport planner qcom executor + PRIVATE os util transport nodes planner qcom executor ) if(${BUILD_TEST}) diff --git a/source/libs/qworker/test/CMakeLists.txt b/source/libs/qworker/test/CMakeLists.txt index a464486546..6de71a4530 100644 --- a/source/libs/qworker/test/CMakeLists.txt +++ b/source/libs/qworker/test/CMakeLists.txt @@ -8,7 +8,7 @@ AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} SOURCE_LIST) ADD_EXECUTABLE(qworkerTest ${SOURCE_LIST}) TARGET_LINK_LIBRARIES( qworkerTest - PUBLIC os util common transport gtest qcom planner qworker executor + PUBLIC os util common transport gtest qcom nodes planner qworker executor ) TARGET_INCLUDE_DIRECTORIES( diff --git a/source/libs/scheduler/CMakeLists.txt b/source/libs/scheduler/CMakeLists.txt index 1b4aee3ccf..cfeb9eadde 100644 --- a/source/libs/scheduler/CMakeLists.txt +++ b/source/libs/scheduler/CMakeLists.txt @@ -9,7 +9,7 @@ target_include_directories( target_link_libraries( scheduler - PUBLIC os util planner qcom common catalog transport + PUBLIC os util nodes planner qcom common catalog transport ) if(${BUILD_TEST}) diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 45e125608a..2831a83905 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -303,7 +303,7 @@ int32_t schRecordTaskExecNode(SSchJob *pJob, SSchTask *pTask, SQueryNodeAddr *ad } -int32_t schValidateAndBuildJob(SQueryDag *pDag, SSchJob *pJob) { +int32_t schValidateAndBuildJob(SQueryPlan *pDag, SSchJob *pJob) { int32_t code = 0; pJob->queryId = pDag->queryId; @@ -1283,7 +1283,7 @@ void schDropJobAllTasks(SSchJob *pJob) { schDropTaskInHashList(pJob, pJob->failTasks); } -static int32_t schExecJobImpl(void *transport, SArray *pNodeList, SQueryDag* pDag, struct SSchJob** job, const char* sql, bool syncSchedule) { +static int32_t schExecJobImpl(void *transport, SArray *pNodeList, SQueryPlan* pDag, struct SSchJob** job, const char* sql, bool syncSchedule) { qDebug("QID:0x%"PRIx64" job started", pDag->queryId); if (pNodeList == NULL || (pNodeList && taosArrayGetSize(pNodeList) <= 0)) { @@ -1398,7 +1398,7 @@ int32_t schedulerInit(SSchedulerCfg *cfg) { return TSDB_CODE_SUCCESS; } -int32_t schedulerExecJob(void *transport, SArray *nodeList, SQueryDag* pDag, struct SSchJob** pJob, const char* sql, SQueryResult *pRes) { +int32_t schedulerExecJob(void *transport, SArray *nodeList, SQueryPlan* pDag, struct SSchJob** pJob, const char* sql, SQueryResult *pRes) { if (NULL == transport || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob || NULL == pRes) { SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } @@ -1410,7 +1410,7 @@ int32_t schedulerExecJob(void *transport, SArray *nodeList, SQueryDag* pDag, str return TSDB_CODE_SUCCESS; } -int32_t schedulerAsyncExecJob(void *transport, SArray *pNodeList, SQueryDag* pDag, const char* sql, struct SSchJob** pJob) { +int32_t schedulerAsyncExecJob(void *transport, SArray *pNodeList, SQueryPlan* pDag, const char* sql, struct SSchJob** pJob) { if (NULL == transport || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob) { SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } @@ -1419,7 +1419,7 @@ int32_t schedulerAsyncExecJob(void *transport, SArray *pNodeList, SQueryDag* pDa return TSDB_CODE_SUCCESS; } -int32_t schedulerConvertDagToTaskList(SQueryDag* pDag, SArray **pTasks) { +int32_t schedulerConvertDagToTaskList(SQueryPlan* pDag, SArray **pTasks) { if (NULL == pDag || pDag->numOfSubplans <= 0 || taosArrayGetSize(pDag->pSubplans) == 0) { SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } diff --git a/source/libs/scheduler/test/schedulerTests.cpp b/source/libs/scheduler/test/schedulerTests.cpp index 89d365a7e7..d05dd731a8 100644 --- a/source/libs/scheduler/test/schedulerTests.cpp +++ b/source/libs/scheduler/test/schedulerTests.cpp @@ -73,7 +73,7 @@ void schtInitLogFile() { } -void schtBuildQueryDag(SQueryDag *dag) { +void schtBuildQueryDag(SQueryPlan *dag) { uint64_t qId = schtQueryId; dag->queryId = qId; @@ -97,7 +97,7 @@ void schtBuildQueryDag(SQueryDag *dag) { scanPlan->pChildren = NULL; scanPlan->level = 1; scanPlan->pParents = taosArrayInit(1, POINTER_BYTES); - scanPlan->pNode = (SPhyNode*)calloc(1, sizeof(SPhyNode)); + scanPlan->pNode = (SPhysiNode*)calloc(1, sizeof(SPhysiNode)); scanPlan->msgType = TDMT_VND_QUERY; mergePlan->id.queryId = qId; @@ -109,7 +109,7 @@ void schtBuildQueryDag(SQueryDag *dag) { mergePlan->pChildren = taosArrayInit(1, POINTER_BYTES); mergePlan->pParents = NULL; - mergePlan->pNode = (SPhyNode*)calloc(1, sizeof(SPhyNode)); + mergePlan->pNode = (SPhysiNode*)calloc(1, sizeof(SPhysiNode)); mergePlan->msgType = TDMT_VND_QUERY; SSubplan *mergePointer = (SSubplan *)taosArrayPush(merge, &mergePlan); @@ -122,12 +122,12 @@ void schtBuildQueryDag(SQueryDag *dag) { taosArrayPush(dag->pSubplans, &scan); } -void schtFreeQueryDag(SQueryDag *dag) { +void schtFreeQueryDag(SQueryPlan *dag) { } -void schtBuildInsertDag(SQueryDag *dag) { +void schtBuildInsertDag(SQueryPlan *dag) { uint64_t qId = 0x0000000000000002; dag->queryId = qId; @@ -150,7 +150,7 @@ void schtBuildInsertDag(SQueryDag *dag) { insertPlan[0].pChildren = NULL; insertPlan[0].pParents = NULL; insertPlan[0].pNode = NULL; - insertPlan[0].pDataSink = (SDataSink*)calloc(1, sizeof(SDataSink)); + insertPlan[0].pDataSink = (SDataSinkNode*)calloc(1, sizeof(SDataSinkNode)); insertPlan[0].msgType = TDMT_VND_SUBMIT; insertPlan[1].id.queryId = qId; @@ -166,7 +166,7 @@ void schtBuildInsertDag(SQueryDag *dag) { insertPlan[1].pChildren = NULL; insertPlan[1].pParents = NULL; insertPlan[1].pNode = NULL; - insertPlan[1].pDataSink = (SDataSink*)calloc(1, sizeof(SDataSink)); + insertPlan[1].pDataSink = (SDataSinkNode*)calloc(1, sizeof(SDataSinkNode)); insertPlan[1].msgType = TDMT_VND_SUBMIT; taosArrayPush(inserta, &insertPlan); @@ -347,7 +347,7 @@ void* schtRunJobThread(void *aa) { char *dbname = "1.db1"; char *tablename = "table1"; SVgroupInfo vgInfo = {0}; - SQueryDag dag = {0}; + SQueryPlan dag = {0}; schtInitLogFile(); @@ -517,7 +517,7 @@ TEST(queryTest, normalCase) { char *tablename = "table1"; SVgroupInfo vgInfo = {0}; SSchJob *pJob = NULL; - SQueryDag dag = {0}; + SQueryPlan dag = {0}; schtInitLogFile(); @@ -620,7 +620,7 @@ TEST(insertTest, normalCase) { char *dbname = "1.db1"; char *tablename = "table1"; SVgroupInfo vgInfo = {0}; - SQueryDag dag = {0}; + SQueryPlan dag = {0}; uint64_t numOfRows = 0; schtInitLogFile(); -- GitLab