From c35c114fc245e50c5f055dd85b439387e4c6352d Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 15 Mar 2021 10:27:26 +0800 Subject: [PATCH] [td-225]refactor. --- src/client/src/tscSQLParser.c | 23 ++-- src/query/inc/qSqlparser.h | 205 +++++++++++++++++----------------- src/query/inc/sql.y | 2 +- src/query/src/qSqlParser.c | 50 ++++----- src/query/src/sql.c | 2 +- 5 files changed, 136 insertions(+), 146 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index a50c3dd4b3..2e95026068 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -780,8 +780,8 @@ int32_t parseIntervalClause(SSqlObj* pSql, SQueryInfo* pQueryInfo, SQuerySqlNode STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); - if (pQuerySqlNode->interval == NULL) { - if (pQuerySqlNode->sliding != NULL) { + if (!TPARSER_HAS_TOKEN(pQuerySqlNode->interval.interval)) { + if (TPARSER_HAS_TOKEN(pQuerySqlNode->sliding)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } @@ -794,7 +794,7 @@ int32_t parseIntervalClause(SSqlObj* pSql, SQueryInfo* pQueryInfo, SQuerySqlNode } // interval is not null - SStrToken *t = &pQuerySqlNode->interval->interval; + SStrToken *t = &pQuerySqlNode->interval.interval; if (parseNatualDuration(t->z, t->n, &pQueryInfo->interval.interval, &pQueryInfo->interval.intervalUnit) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } @@ -811,11 +811,11 @@ int32_t parseIntervalClause(SSqlObj* pSql, SQueryInfo* pQueryInfo, SQuerySqlNode } } - if (parseIntervalOffset(pCmd, pQueryInfo, &pQuerySqlNode->interval->offset) != TSDB_CODE_SUCCESS) { + if (parseIntervalOffset(pCmd, pQueryInfo, &pQuerySqlNode->interval.offset) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } - if (parseSlidingClause(pCmd, pQueryInfo, pQuerySqlNode->sliding) != TSDB_CODE_SUCCESS) { + if (parseSlidingClause(pCmd, pQueryInfo, &pQuerySqlNode->sliding) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } @@ -830,12 +830,12 @@ int32_t parseSessionClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySqlNode const char* msg4 = "invalid time window"; // no session window - if (pQuerySqlNode->sessionVal == NULL) { + if (!TPARSER_HAS_TOKEN(pQuerySqlNode->sessionVal.gap)) { return TSDB_CODE_SUCCESS; } - SStrToken* col = &pQuerySqlNode->sessionVal->col; - SStrToken* gap = &pQuerySqlNode->sessionVal->gap; + SStrToken* col = &pQuerySqlNode->sessionVal.col; + SStrToken* gap = &pQuerySqlNode->sessionVal.gap; char timeUnit = 0; if (parseNatualDuration(gap->z, gap->n, &pQueryInfo->sessionWindow.gap, &timeUnit) != TSDB_CODE_SUCCESS) { @@ -5439,9 +5439,9 @@ int32_t parseLimitClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t clauseIn const char* msg2 = "slimit/soffset can not apply to projection query"; // handle the limit offset value, validate the limit - pQueryInfo->limit = *pQuerySqlNode->limit; + pQueryInfo->limit = pQuerySqlNode->limit; pQueryInfo->clauseLimit = pQueryInfo->limit.limit; - pQueryInfo->slimit = *pQuerySqlNode->slimit; + pQueryInfo->slimit = pQuerySqlNode->slimit; tscDebug("%p limit:%" PRId64 ", offset:%" PRId64 " slimit:%" PRId64 ", soffset:%" PRId64, pSql, pQueryInfo->limit.limit, pQueryInfo->limit.offset, pQueryInfo->slimit.limit, pQueryInfo->slimit.offset); @@ -6840,7 +6840,8 @@ int32_t doValidateSqlNode(SSqlObj* pSql, SQuerySqlNode* pQuerySqlNode, int32_t i } int32_t joinQuery = (pQuerySqlNode->from != NULL && taosArrayGetSize(pQuerySqlNode->from) > 2); - int32_t timeWindowQuery = !(pQuerySqlNode->interval == NULL || pQuerySqlNode->sessionVal == NULL); + int32_t timeWindowQuery = + (TPARSER_HAS_TOKEN(pQuerySqlNode->interval.interval) || TPARSER_HAS_TOKEN(pQuerySqlNode->sessionVal.gap)); if (parseSelectClause(pCmd, index, pQuerySqlNode->pSelectList, isSTable, joinQuery, timeWindowQuery) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; diff --git a/src/query/inc/qSqlparser.h b/src/query/inc/qSqlparser.h index dcfc0aeec3..8164546cb5 100644 --- a/src/query/inc/qSqlparser.h +++ b/src/query/inc/qSqlparser.h @@ -50,29 +50,31 @@ extern char tTokenTypeSwitcher[13]; } \ } while (0) +#define TPARSER_HAS_TOKEN(_t) ((_t).n > 0) + typedef struct SLimitVal { - int64_t limit; - int64_t offset; + int64_t limit; + int64_t offset; } SLimitVal; typedef struct SOrderVal { - uint32_t order; - int32_t orderColId; + uint32_t order; + int32_t orderColId; } SOrderVal; typedef struct tVariantListItem { - tVariant pVar; - uint8_t sortOrder; + tVariant pVar; + uint8_t sortOrder; } tVariantListItem; typedef struct SIntervalVal { - SStrToken interval; - SStrToken offset; + SStrToken interval; + SStrToken offset; } SIntervalVal; typedef struct SSessionWindowVal { - SStrToken col; - SStrToken gap; + SStrToken col; + SStrToken gap; } SSessionWindowVal; typedef struct SQuerySqlNode { @@ -81,113 +83,113 @@ typedef struct SQuerySqlNode { struct tSqlExpr *pWhere; // where clause [optional] SArray *pGroupby; // groupby clause, only for tags[optional], SArray SArray *pSortOrder; // orderby [optional], SArray - SIntervalVal *interval; // (interval, interval_offset) [optional] - SSessionWindowVal *sessionVal; // session window [optional] - SStrToken *sliding; // sliding window [optional] - SLimitVal *limit; // limit offset [optional] - SLimitVal *slimit; // group limit offset [optional] SArray *fillType; // fill type[optional], SArray + SIntervalVal interval; // (interval, interval_offset) [optional] + SSessionWindowVal sessionVal; // session window [optional] + SStrToken sliding; // sliding window [optional] + SLimitVal limit; // limit offset [optional] + SLimitVal slimit; // group limit offset [optional] SStrToken sqlstr; // sql string in select clause } SQuerySqlNode; typedef struct SCreatedTableInfo { - SStrToken name; // table name token - SStrToken stableName; // 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 - char *fullname; // table full name - STagData tagdata; // true tag data, super table full name is in STagData - int8_t igExist; // ignore if exists + SStrToken name; // table name token + SStrToken stableName; // 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 + char *fullname; // table full name + STagData tagdata; // true tag data, super table full name is in STagData + int8_t igExist; // ignore if exists } SCreatedTableInfo; typedef struct SCreateTableSql { - SStrToken name; // table name, create table [name] xxx - int8_t type; // create normal table/from super table/ stream - bool existCheck; + SStrToken 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 + SArray *pTagColumns; // SArray + SArray *pColumns; // SArray } colInfo; - SArray *childTableInfo; // SArray - SQuerySqlNode *pSelect; + SArray *childTableInfo; // SArray + SQuerySqlNode *pSelect; } SCreateTableSql; typedef struct SAlterTableInfo { - SStrToken name; - int16_t tableType; - int16_t type; - STagData tagData; - SArray *pAddColumns; // SArray - SArray *varList; // set t=val or: change src dst, SArray + SStrToken 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 { - SStrToken dbname; - int32_t replica; - int32_t cacheBlockSize; - int32_t maxTablesPerVnode; - 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; - SStrToken precision; - bool ignoreExists; - int8_t update; - int8_t cachelast; - SArray *keep; - int8_t dbType; - int16_t partitions; + SStrToken dbname; + int32_t replica; + int32_t cacheBlockSize; + int32_t maxTablesPerVnode; + 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; + SStrToken precision; + bool ignoreExists; + int8_t update; + int8_t cachelast; + SArray *keep; + int8_t dbType; + int16_t partitions; } SCreateDbInfo; 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; - SStrToken stat; + int32_t maxUsers; + int32_t maxDbs; + int32_t maxTimeSeries; + int32_t maxStreams; + int32_t maxPointsPerSecond; + int64_t maxStorage; + int64_t maxQueryTime; + int32_t maxConnections; + SStrToken stat; } SCreateAcctInfo; typedef struct SShowInfo { - uint8_t showType; - SStrToken prefix; - SStrToken pattern; + uint8_t showType; + SStrToken prefix; + SStrToken pattern; } SShowInfo; typedef struct SUserInfo { - SStrToken user; - SStrToken passwd; - SStrToken privilege; - int16_t type; + SStrToken user; + SStrToken passwd; + SStrToken privilege; + int16_t type; } SUserInfo; typedef struct SMiscInfo { - SArray *a; // SArray - bool existsCheck; - int16_t dbType; - int16_t tableType; - SUserInfo user; + SArray *a; // SArray + bool existsCheck; + int16_t dbType; + int16_t tableType; + SUserInfo user; union { - SCreateDbInfo dbOpt; - SCreateAcctInfo acctOpt; - SShowInfo showOpt; - SStrToken id; + SCreateDbInfo dbOpt; + SCreateAcctInfo acctOpt; + SShowInfo showOpt; + SStrToken id; }; } SMiscInfo; typedef struct SSubclauseInfo { // "UNION" multiple select sub-clause - SQuerySqlNode **pClause; - int32_t numOfClause; + SQuerySqlNode **pClause; + int32_t numOfClause; } SSubclauseInfo; typedef struct SSqlInfo { @@ -196,34 +198,34 @@ typedef struct SSqlInfo { SSubclauseInfo subclauseInfo; char msg[256]; union { - SCreateTableSql *pCreateTableInfo; - SAlterTableInfo *pAlterInfo; - SMiscInfo *pMiscInfo; + 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) + uint16_t type; // sql node type + uint32_t tokenId; // TK_LE: less than(binary expr) // the whole string of the function(col, param), while the function name is kept in token - SStrToken operand; - uint32_t functionId; // function id + SStrToken operand; + uint32_t functionId; // function id - SStrToken colInfo; // table column info - tVariant value; // the use input value - SStrToken token; // original sql expr string + SStrToken colInfo; // table column info + tVariant value; // the use input value + SStrToken token; // original sql expr string - struct tSqlExpr *pLeft; // left child - struct tSqlExpr *pRight; // right child - struct SArray *pParam; // function parameters list + struct tSqlExpr *pLeft; // left child + struct tSqlExpr *pRight; // right child + struct SArray *pParam; // function parameters list } tSqlExpr; // used in select clause. select from xxx typedef struct tSqlExprItem { - tSqlExpr *pNode; // The list of expressions - char * aliasName; // alias name, null-terminated string - bool distinct; + tSqlExpr *pNode; // The list of expressions + char *aliasName; // alias name, null-terminated string + bool distinct; } tSqlExprItem; SArray *tVariantListAppend(SArray *pList, tVariant *pVar, uint8_t sortOrder); @@ -241,9 +243,8 @@ SArray *tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SStrToken *pDistinc void tSqlExprListDestroy(SArray *pList); SQuerySqlNode *tSetQuerySqlNode(SStrToken *pSelectToken, SArray *pSelectList, SArray *pFrom, tSqlExpr *pWhere, - SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, - SSessionWindowVal *pSession, SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, - SLimitVal *pGLimit); + SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, SSessionWindowVal *ps, + SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *pgLimit); SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SQuerySqlNode *pSelect, int32_t type); @@ -251,7 +252,7 @@ SAlterTableInfo *tSetAlterTableInfo(SStrToken *pTableName, SArray *pCols, SArray SCreatedTableInfo createNewChildTableInfo(SStrToken *pTableName, SArray *pTagNames, SArray *pTagVals, SStrToken *pToken, SStrToken* igExists); void destroyAllSelectClause(SSubclauseInfo *pSql); -void doDestroyQuerySql(SQuerySqlNode *pSql); +void destroyQuerySqlNode(SQuerySqlNode *pSql); void freeCreateTableInfo(void* p); SSqlInfo *setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type); diff --git a/src/query/inc/sql.y b/src/query/inc/sql.y index 0959db0efa..0d7f1e9755 100644 --- a/src/query/inc/sql.y +++ b/src/query/inc/sql.y @@ -451,7 +451,7 @@ tagitem(A) ::= PLUS(X) FLOAT(Y). { //////////////////////// The SELECT statement ///////////////////////////////// %type select {SQuerySqlNode*} -%destructor select {doDestroyQuerySql($$);} +%destructor select {destroyQuerySqlNode($$);} select(A) ::= SELECT(T) selcollist(W) from(X) where_opt(Y) interval_opt(K) session_option(H) fill_opt(F) sliding_opt(S) groupby_opt(P) orderby_opt(Z) having_opt(N) slimit_opt(G) limit_opt(L). { A = tSetQuerySqlNode(&T, W, X, Y, P, Z, &K, &H, &S, F, &L, &G); } diff --git a/src/query/src/qSqlParser.c b/src/query/src/qSqlParser.c index c81e9339a1..b2316d0396 100644 --- a/src/query/src/qSqlParser.c +++ b/src/query/src/qSqlParser.c @@ -547,38 +547,26 @@ void tSetColumnType(TAOS_FIELD *pField, SStrToken *type) { */ SQuerySqlNode *tSetQuerySqlNode(SStrToken *pSelectToken, SArray *pSelectList, SArray *pFrom, tSqlExpr *pWhere, SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, SSessionWindowVal *pSession, - SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *pGLimit) { - assert(pSelectList != NULL); + SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *psLimit) { + assert(pSelectList != NULL && pLimit != NULL && psLimit != NULL && pInterval != NULL && pSliding != NULL && + pSession != NULL); SQuerySqlNode *pQuery = calloc(1, sizeof(SQuerySqlNode)); - pQuery->sqlstr = *pSelectToken; - pQuery->sqlstr.n = (uint32_t)strlen(pQuery->sqlstr.z); // all later sql string are belonged to the stream sql - pQuery->pSelectList = pSelectList; - pQuery->from = pFrom; - pQuery->pGroupby = pGroupby; - pQuery->pSortOrder = pSortOrder; - pQuery->pWhere = pWhere; - - if (pLimit != NULL) { - pQuery->limit = pLimit; - } + // all later sql string are belonged to the stream sql + pQuery->sqlstr = *pSelectToken; + pQuery->sqlstr.n = (uint32_t)strlen(pQuery->sqlstr.z); - if (pGLimit != NULL) { - pQuery->slimit = pGLimit; - } - - if (pInterval != NULL) { - pQuery->interval = pInterval; - } - - if (pSliding != NULL) { - pQuery->sliding = pSliding; - } - - if (pSession != NULL) { - pQuery->sessionVal = pSession; - } + pQuery->pSelectList = pSelectList; + pQuery->from = pFrom; + pQuery->pGroupby = pGroupby; + pQuery->pSortOrder = pSortOrder; + pQuery->pWhere = pWhere; + pQuery->limit = *pLimit; + pQuery->slimit = *psLimit; + pQuery->interval = *pInterval; + pQuery->sliding = *pSliding; + pQuery->sessionVal = *pSession; pQuery->fillType = pFill; return pQuery; @@ -597,7 +585,7 @@ void freeCreateTableInfo(void* p) { tfree(pInfo->tagdata.data); } -void doDestroyQuerySql(SQuerySqlNode *pQuerySql) { +void destroyQuerySqlNode(SQuerySqlNode *pQuerySql) { if (pQuerySql == NULL) { return; } @@ -631,7 +619,7 @@ void destroyAllSelectClause(SSubclauseInfo *pClause) { for(int32_t i = 0; i < pClause->numOfClause; ++i) { SQuerySqlNode *pQuerySql = pClause->pClause[i]; - doDestroyQuerySql(pQuerySql); + destroyQuerySqlNode(pQuerySql); } tfree(pClause->pClause); @@ -705,7 +693,7 @@ SAlterTableInfo *tSetAlterTableInfo(SStrToken *pTableName, SArray *pCols, SArray } void* destroyCreateTableSql(SCreateTableSql* pCreate) { - doDestroyQuerySql(pCreate->pSelect); + destroyQuerySqlNode(pCreate->pSelect); taosArrayDestroy(pCreate->colInfo.pColumns); taosArrayDestroy(pCreate->colInfo.pTagColumns); diff --git a/src/query/src/sql.c b/src/query/src/sql.c index 505c96436f..c6d6d0c931 100644 --- a/src/query/src/sql.c +++ b/src/query/src/sql.c @@ -1425,7 +1425,7 @@ destroyCreateTableSql((yypminor->yy230)); break; case 235: /* select */ { -doDestroyQuerySql((yypminor->yy342)); +destroyQuerySqlNode((yypminor->yy342)); } break; case 238: /* selcollist */ -- GitLab