diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 891879e6b2d81a1ca0eaefe1182df201dfacdd20..c8115d9340760c175a21f7d306d7b73c2fbac8b6 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -18,15 +18,18 @@ #define _XOPEN_SOURCE 500 #define _DEFAULT_SOURCE #define _GNU_SOURCE -#endif // __APPLE__ +#endif // __APPLE__ #include +#include "cJSON.h" #include "os.h" -#include "regex.h" +#include "qFilter.h" #include "qPlan.h" +#include "qScript.h" #include "qSqlparser.h" #include "qTableMeta.h" #include "qUtil.h" +#include "regex.h" #include "taos.h" #include "taosmsg.h" #include "tcompare.h" @@ -38,10 +41,7 @@ #include "tstrbuild.h" #include "ttoken.h" #include "ttokendef.h" -#include "qScript.h" #include "ttype.h" -#include "qFilter.h" -#include "cJSON.h" #define DEFAULT_PRIMARY_TIMESTAMP_COL_NAME "_c0" @@ -69,24 +69,25 @@ static SExprInfo* doAddProjectCol(SQueryInfo* pQueryInfo, int32_t colIndex, int3 static int32_t setShowInfo(SSqlObj* pSql, SSqlInfo* pInfo); static char* getAccountId(SSqlObj* pSql); -static int convertTimestampStrToInt64(tVariant *pVar, int32_t precision); -static bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType, uint8_t precision); +static int convertTimestampStrToInt64(tVariant* pVar, int32_t precision); +static bool serializeExprListToVariant(SArray* pList, tVariant** dst, int16_t colType, uint8_t precision); -static bool has(SArray* pFieldList, int32_t startIdx, const char* name); +static bool has(SArray* pFieldList, int32_t startIdx, const char* name); static int32_t getDelimiterIndex(SStrToken* pTableName); -static bool validateTableColumnInfo(SArray* pFieldList, SSqlCmd* pCmd); -static bool validateTagParams(SArray* pTagsList, SArray* pFieldList, SSqlCmd* pCmd); +static bool validateTableColumnInfo(SArray* pFieldList, SSqlCmd* pCmd); +static bool validateTagParams(SArray* pTagsList, SArray* pFieldList, SSqlCmd* pCmd); static void getColumnName(tSqlExprItem* pItem, char* resultFieldName, char* rawName, int32_t nameLength); static int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t colIndex, tSqlExprItem* pItem, - bool finalResult, SUdfInfo* pUdfInfo); + bool finalResult, SUdfInfo* pUdfInfo); static int32_t insertResultField(SQueryInfo* pQueryInfo, int32_t outputIndex, SColumnList* pColList, int16_t bytes, - int8_t type, char* fieldName, SExprInfo* pSqlExpr); + int8_t type, char* fieldName, SExprInfo* pSqlExpr); -static uint8_t convertRelationalOperator(SStrToken *pToken); +static uint8_t convertRelationalOperator(SStrToken* pToken); -static int32_t validateSelectNodeList(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelNodeList, bool isSTable, bool joinQuery, bool timeWindowQuery); +static int32_t validateSelectNodeList(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelNodeList, bool isSTable, + bool joinQuery, bool timeWindowQuery); static bool hasUnsupportFunctionsForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo); static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool twQuery); @@ -98,7 +99,8 @@ static int32_t parseIntervalOffset(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrTo static int32_t parseSlidingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* pSliding); static int32_t validateStateWindowNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNode, bool isStable); -static int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExprItem* pItem, bool outerQuery, bool timeWindowQuery); +static int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExprItem* pItem, + bool outerQuery, bool timeWindowQuery); static int32_t validateWhereNode(SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SSqlObj* pSql, bool joinQuery); static int32_t validateFillNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNode); @@ -109,8 +111,8 @@ static int32_t tsRewriteFieldNameIfNecessary(SSqlCmd* pCmd, SQueryInfo* pQueryIn static int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo); static int32_t validateSqlFunctionInStreamSql(SSqlCmd* pCmd, SQueryInfo* pQueryInfo); static int32_t validateFunctionsInIntervalOrGroupbyQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo); -static int32_t validateSQLExprItem(SSqlCmd* pCmd, tSqlExpr* pExpr, - SQueryInfo* pQueryInfo, SColumnList* pList, int32_t* type, uint64_t* uid, int32_t* height); +static int32_t validateSQLExprItem(SSqlCmd* pCmd, tSqlExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList, + int32_t* type, uint64_t* uid, int32_t* height); static int32_t validateEp(char* ep); static int32_t validateDNodeConfig(SMiscInfo* pOptions); static int32_t validateLocalConfig(SMiscInfo* pOptions); @@ -119,7 +121,7 @@ static int32_t setKillInfo(SSqlObj* pSql, struct SSqlInfo* pInfo, int32_t killTy static int32_t setCompactVnodeInfo(SSqlObj* pSql, struct SSqlInfo* pInfo); static int32_t validateOneTag(SSqlCmd* pCmd, TAOS_FIELD* pTagField); -static bool hasNormalColumnFilter(SQueryInfo* pQueryInfo); +static bool hasNormalColumnFilter(SQueryInfo* pQueryInfo); static int32_t validateLimitNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNode, SSqlObj* pSql); static int32_t parseCreateDBOptions(SSqlCmd* pCmd, SCreateDbInfo* pCreateDbSql); @@ -138,37 +140,35 @@ static int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo); static int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo); static int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInfo); -static int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pSqlExpr, SQueryInfo* pQueryInfo, SArray* pCols, uint64_t *uid); -static bool validateDebugFlag(int32_t v); -static int32_t checkQueryRangeForFill(SSqlCmd* pCmd, SQueryInfo* pQueryInfo); -static int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo); +static int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode** pExpr, const tSqlExpr* pSqlExpr, SQueryInfo* pQueryInfo, + SArray* pCols, uint64_t* uid); +static bool validateDebugFlag(int32_t v); +static int32_t checkQueryRangeForFill(SSqlCmd* pCmd, SQueryInfo* pQueryInfo); +static int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo); static tSqlExpr* extractExprForSTable(SSqlCmd* pCmd, tSqlExpr** pExpr, SQueryInfo* pQueryInfo, int32_t tableIndex); -static void convertWhereStringCharset(tSqlExpr* pRight); +static void convertWhereStringCharset(tSqlExpr* pRight); -int validateTableName(char *tblName, int len, SStrToken* psTblToken, bool *dbIncluded); +int validateTableName(char* tblName, int len, SStrToken* psTblToken, bool* dbIncluded); static bool isTimeWindowQuery(SQueryInfo* pQueryInfo) { return pQueryInfo->interval.interval > 0 || pQueryInfo->sessionWindow.gap > 0; } - -int16_t getNewResColId(SSqlCmd* pCmd) { - return pCmd->resColumnId--; -} +int16_t getNewResColId(SSqlCmd* pCmd) { return pCmd->resColumnId--; } // serialize expr in exprlist to binary // format "type | size | value" -bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType, uint8_t precision) { +bool serializeExprListToVariant(SArray* pList, tVariant** dst, int16_t colType, uint8_t precision) { bool ret = false; if (!pList || pList->size <= 0 || colType < 0) { return ret; } tSqlExpr* item = ((tSqlExprItem*)(taosArrayGet(pList, 0)))->pNode; - int32_t firstVarType = item->value.nType; - if(colType == TSDB_DATA_TYPE_JSON) colType = firstVarType; + int32_t firstVarType = item->value.nType; + if (colType == TSDB_DATA_TYPE_JSON) colType = firstVarType; - SBufferWriter bw = tbufInitWriter( NULL, false); + SBufferWriter bw = tbufInitWriter(NULL, false); tbufEnsureCapacity(&bw, 512); if (colType == TSDB_DATA_TYPE_TIMESTAMP) { tbufWriteUint32(&bw, TSDB_DATA_TYPE_BIGINT); @@ -180,7 +180,7 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType, for (int32_t i = 0; i < (int32_t)pList->size; i++) { tSqlExpr* pSub = ((tSqlExprItem*)(taosArrayGet(pList, i)))->pNode; - tVariant* var = &pSub->value; + tVariant* var = &pSub->value; // check all the exprToken type in expr list same or not if (firstVarType != var->nType) { @@ -190,7 +190,7 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType, if (var->nType != TSDB_DATA_TYPE_BOOL && !IS_SIGNED_NUMERIC_TYPE(var->nType)) { break; } - if (colType == TSDB_DATA_TYPE_BOOL && (var->i64 > 1 ||var->i64 < 0)) { + if (colType == TSDB_DATA_TYPE_BOOL && (var->i64 > 1 || var->i64 < 0)) { break; } tbufWriteInt64(&bw, var->i64); @@ -203,7 +203,7 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType, } else if (colType == TSDB_DATA_TYPE_DOUBLE || colType == TSDB_DATA_TYPE_FLOAT) { if (IS_SIGNED_NUMERIC_TYPE(var->nType) || IS_UNSIGNED_NUMERIC_TYPE(var->nType)) { tbufWriteDouble(&bw, (double)(var->i64)); - } else if (var->nType == TSDB_DATA_TYPE_DOUBLE || var->nType == TSDB_DATA_TYPE_FLOAT){ + } else if (var->nType == TSDB_DATA_TYPE_DOUBLE || var->nType == TSDB_DATA_TYPE_FLOAT) { tbufWriteDouble(&bw, var->dKey); } else { break; @@ -217,28 +217,30 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType, if (var->nType != TSDB_DATA_TYPE_BINARY) { break; } - char *buf = (char *)calloc(1, (var->nLen + 1)*TSDB_NCHAR_SIZE); + char* buf = (char*)calloc(1, (var->nLen + 1) * TSDB_NCHAR_SIZE); if (tVariantDump(var, buf, colType, false) != TSDB_CODE_SUCCESS) { free(buf); break; } - tbufWriteBinary(&bw, buf, twcslen((wchar_t *)buf) * TSDB_NCHAR_SIZE); + tbufWriteBinary(&bw, buf, twcslen((wchar_t*)buf) * TSDB_NCHAR_SIZE); free(buf); } else if (colType == TSDB_DATA_TYPE_TIMESTAMP) { if (var->nType == TSDB_DATA_TYPE_BINARY) { - if (convertTimestampStrToInt64(var, precision) < 0) { - break; - } - tbufWriteInt64(&bw, var->i64); - } else if (var->nType == TSDB_DATA_TYPE_BIGINT) { - tbufWriteInt64(&bw, var->i64); - } else { - break; - } + if (convertTimestampStrToInt64(var, precision) < 0) { + break; + } + tbufWriteInt64(&bw, var->i64); + } else if (var->nType == TSDB_DATA_TYPE_BIGINT) { + tbufWriteInt64(&bw, var->i64); + } else { + break; + } } else { break; } - if (i == (int32_t)(pList->size - 1)) { ret = true;} + if (i == (int32_t)(pList->size - 1)) { + ret = true; + } } if (ret == true) { if ((*dst = calloc(1, sizeof(tVariant))) != NULL) { @@ -251,8 +253,7 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType, return ret; } - -static uint8_t convertRelationalOperator(SStrToken *pToken) { +static uint8_t convertRelationalOperator(SStrToken* pToken) { switch (pToken->type) { case TK_LT: return TSDB_RELATION_LESS; @@ -300,7 +301,9 @@ static uint8_t convertRelationalOperator(SStrToken *pToken) { return TSDB_RELATION_NOTNULL; case TK_IN: return TSDB_RELATION_IN; - default: { return 0; } + default: { + return 0; + } } } @@ -309,7 +312,7 @@ static bool validateDebugFlag(int32_t v) { for (int i = 0; i < tListLen(validFlag); i++) { if (v == validFlag[i]) { - return true; + return true; } } return false; @@ -322,11 +325,11 @@ static int32_t invalidOperationMsg(char* dstBuffer, const char* errMsg) { return tscInvalidOperationMsg(dstBuffer, errMsg, NULL); } -static int convertTimestampStrToInt64(tVariant *pVar, int32_t precision) { - int64_t time = 0; +static int convertTimestampStrToInt64(tVariant* pVar, int32_t precision) { + int64_t time = 0; stringProcess(pVar->pz, pVar->nLen); - char* seg = strnchr(pVar->pz, '-', pVar->nLen, false); + char* seg = strnchr(pVar->pz, '-', pVar->nLen, false); if (seg != NULL) { if (taosParseTime(pVar->pz, &time, pVar->nLen, precision, tsDaylight) != TSDB_CODE_SUCCESS) { return -1; @@ -347,7 +350,7 @@ static int setColumnFilterInfoForTimestamp(SSqlCmd* pCmd, SQueryInfo* pQueryInfo STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); if (convertTimestampStrToInt64(pVar, tinfo.precision) < 0) { - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } return TSDB_CODE_SUCCESS; } @@ -390,7 +393,7 @@ static int32_t normalizeVarDataTypeLength(SSqlCmd* pCmd) { // pQueryInfo1 itself ++siblings; - while(pSibling != NULL) { + while (pSibling != NULL) { int32_t ret = tscFieldInfoCompare(&pQueryInfo1->fieldsInfo, &pSibling->fieldsInfo, &diffSize); if (ret != 0) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); @@ -407,7 +410,7 @@ static int32_t normalizeVarDataTypeLength(SSqlCmd* pCmd) { pQueryInfo1 = pCmd->pQueryInfo; pSibling = pQueryInfo1->sibling; - while(pSibling->sibling != NULL) { + while (pSibling->sibling != NULL) { tscFieldInfoSetSize(&pQueryInfo1->fieldsInfo, &pSibling->fieldsInfo); pSibling = pSibling->sibling; } @@ -416,7 +419,7 @@ static int32_t normalizeVarDataTypeLength(SSqlCmd* pCmd) { return TSDB_CODE_SUCCESS; } -int32_t readFromFile(char *name, uint32_t *len, void **buf) { +int32_t readFromFile(char* name, uint32_t* len, void** buf) { struct stat fileStat; if (stat(name, &fileStat) < 0) { tscError("stat file %s failed, error:%s", name, strerror(errno)); @@ -453,22 +456,21 @@ int32_t readFromFile(char *name, uint32_t *len, void **buf) { return TSDB_CODE_SUCCESS; } - int32_t handleUserDefinedFunc(SSqlObj* pSql, struct SSqlInfo* pInfo) { - const char *msg1 = "invalid function name or length"; - const char *msg2 = "path is too long"; - const char *msg3 = "invalid outputtype"; - #ifdef LUA_EMBEDDED - const char *msg4 = "invalid script"; - #endif - const char *msg5 = "invalid dyn lib"; - SSqlCmd *pCmd = &pSql->cmd; + const char* msg1 = "invalid function name or length"; + const char* msg2 = "path is too long"; + const char* msg3 = "invalid outputtype"; +#ifdef LUA_EMBEDDED + const char* msg4 = "invalid script"; +#endif + const char* msg5 = "invalid dyn lib"; + SSqlCmd* pCmd = &pSql->cmd; switch (pInfo->type) { case TSDB_SQL_CREATE_FUNCTION: { - SCreateFuncInfo *createInfo = &pInfo->pMiscInfo->funcOpt; - uint32_t len = 0; - void *buf = NULL; + SCreateFuncInfo* createInfo = &pInfo->pMiscInfo->funcOpt; + uint32_t len = 0; + void* buf = NULL; if (createInfo->output.type == (uint8_t)-1 || createInfo->output.bytes < 0) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); @@ -477,7 +479,7 @@ int32_t handleUserDefinedFunc(SSqlObj* pSql, struct SSqlInfo* pInfo) { createInfo->name.z[createInfo->name.n] = 0; // funcname's naming rule is same to column if (validateColumnName(createInfo->name.z) != TSDB_CODE_SUCCESS) { - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } stringProcess(createInfo->name.z, createInfo->name.n); @@ -497,22 +499,22 @@ int32_t handleUserDefinedFunc(SSqlObj* pSql, struct SSqlInfo* pInfo) { if (ret) { return ret; } - //validate *.lua or .so + // validate *.lua or .so int32_t pathLen = (int32_t)strlen(createInfo->path.z); #ifdef LUA_EMBEDDED if ((pathLen > 4) && (0 == strncmp(createInfo->path.z + pathLen - 4, ".lua", 4)) && !isValidScript(buf, len)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } else #endif - if (pathLen > 3 && (0 == strncmp(createInfo->path.z + pathLen - 3, ".so", 3))) { - void *handle = taosLoadDll(createInfo->path.z); + if (pathLen > 3 && (0 == strncmp(createInfo->path.z + pathLen - 3, ".so", 3))) { + void* handle = taosLoadDll(createInfo->path.z); taosCloseDll(handle); if (handle == NULL) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg5); } } - //TODO CHECK CODE + // TODO CHECK CODE if (len + sizeof(SCreateFuncMsg) > pSql->cmd.allocSize) { ret = tscAllocPayload(&pSql->cmd, len + sizeof(SCreateFuncMsg)); if (ret) { @@ -521,7 +523,7 @@ int32_t handleUserDefinedFunc(SSqlObj* pSql, struct SSqlInfo* pInfo) { } } - SCreateFuncMsg *pMsg = (SCreateFuncMsg *)pSql->cmd.payload; + SCreateFuncMsg* pMsg = (SCreateFuncMsg*)pSql->cmd.payload; strcpy(pMsg->name, createInfo->name.z); strcpy(pMsg->path, createInfo->path.z); @@ -541,7 +543,7 @@ int32_t handleUserDefinedFunc(SSqlObj* pSql, struct SSqlInfo* pInfo) { case TSDB_SQL_DROP_FUNCTION: { SStrToken* t0 = taosArrayGet(pInfo->pMiscInfo->a, 0); - SDropFuncMsg *pMsg = (SDropFuncMsg *)pSql->cmd.payload; + SDropFuncMsg* pMsg = (SDropFuncMsg*)pSql->cmd.payload; t0->z[t0->n] = 0; @@ -582,7 +584,8 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { return pRes->code; } - STableMetaInfo* pTableMetaInfo = (pQueryInfo->numOfTables == 0)? tscAddEmptyMetaInfo(pQueryInfo) : pQueryInfo->pTableMetaInfo[0]; + STableMetaInfo* pTableMetaInfo = + (pQueryInfo->numOfTables == 0) ? tscAddEmptyMetaInfo(pQueryInfo) : pQueryInfo->pTableMetaInfo[0]; if (pTableMetaInfo == NULL) { pRes->code = TSDB_CODE_TSC_OUT_OF_MEMORY; return pRes->code; @@ -600,16 +603,17 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { const char* msg3 = "param name too long"; SStrToken* pzName = taosArrayGet(pInfo->pMiscInfo->a, 0); - bool escapeEnabled = (pInfo->type == TSDB_SQL_DROP_TABLE) ? true: false; + bool escapeEnabled = (pInfo->type == TSDB_SQL_DROP_TABLE) ? true : false; - bool dbIncluded = false; + bool dbIncluded = false; char buf[TSDB_TABLE_FNAME_LEN]; SStrToken sTblToken; sTblToken.z = buf; if (pInfo->type != TSDB_SQL_DROP_DNODE) { - if ((escapeEnabled && (validateTableName(pzName->z, pzName->n, &sTblToken, &dbIncluded) != TSDB_CODE_SUCCESS)) || - ((!escapeEnabled) && (tscValidateName(pzName, escapeEnabled, &dbIncluded) != TSDB_CODE_SUCCESS))){ + if ((escapeEnabled && + (validateTableName(pzName->z, pzName->n, &sTblToken, &dbIncluded) != TSDB_CODE_SUCCESS)) || + ((!escapeEnabled) && (tscValidateName(pzName, escapeEnabled, &dbIncluded) != TSDB_CODE_SUCCESS))) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } } @@ -625,7 +629,7 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { assert(taosArrayGetSize(pInfo->pMiscInfo->a) == 1); code = tscSetTableFullName(&pTableMetaInfo->name, &sTblToken, pSql, dbIncluded); - if(code != TSDB_CODE_SUCCESS) { + if (code != TSDB_CODE_SUCCESS) { return code; } } else if (pInfo->type == TSDB_SQL_DROP_DNODE) { @@ -646,7 +650,7 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { case TSDB_SQL_USE_DB: { const char* msg = "invalid db name"; - SStrToken* pToken = taosArrayGet(pInfo->pMiscInfo->a, 0); + SStrToken* pToken = taosArrayGet(pInfo->pMiscInfo->a, 0); if (tscValidateName(pToken, false, NULL) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); @@ -673,7 +677,7 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } case TSDB_SQL_CREATE_FUNCTION: - case TSDB_SQL_DROP_FUNCTION: { + case TSDB_SQL_DROP_FUNCTION: { code = handleUserDefinedFunc(pSql, pInfo); if (code != TSDB_CODE_SUCCESS) { return code; @@ -692,7 +696,7 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } - char buf[TSDB_DB_NAME_LEN] = {0}; + char buf[TSDB_DB_NAME_LEN] = {0}; SStrToken token = taosTokenDup(&pCreateDB->dbname, buf, tListLen(buf)); if (tscValidateName(&token, false, NULL) != TSDB_CODE_SUCCESS) { @@ -764,9 +768,9 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { const char* msg1 = "invalid table name"; SStrToken* pToken = taosArrayGet(pInfo->pMiscInfo->a, 0); - bool dbIncluded = false; - char buf[TSDB_TABLE_FNAME_LEN]; - SStrToken sTblToken; + bool dbIncluded = false; + char buf[TSDB_TABLE_FNAME_LEN]; + SStrToken sTblToken; sTblToken.z = buf; if (validateTableName(pToken->z, pToken->n, &sTblToken, &dbIncluded) != TSDB_CODE_SUCCESS) { @@ -787,7 +791,7 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { SStrToken* pToken = taosArrayGet(pInfo->pMiscInfo->a, 0); - bool dbIncluded = false; + bool dbIncluded = false; char buf[TSDB_TABLE_FNAME_LEN]; SStrToken sTblToken; sTblToken.z = buf; @@ -818,7 +822,9 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { 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* 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 */ @@ -908,15 +914,15 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } case TSDB_SQL_CFG_LOCAL: { - SMiscInfo *pMiscInfo = pInfo->pMiscInfo; - const char *msg = "invalid configure options or values"; + 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 invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } - int32_t numOfToken = (int32_t) taosArrayGetSize(pMiscInfo->a); + int32_t numOfToken = (int32_t)taosArrayGetSize(pMiscInfo->a); assert(numOfToken >= 1 && numOfToken <= 2); SStrToken* t = taosArrayGet(pMiscInfo->a, 0); @@ -953,7 +959,7 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } case TSDB_SQL_SELECT: { - const char * msg1 = "no nested query supported in union clause"; + const char* msg1 = "no nested query supported in union clause"; code = loadAllTableMeta(pSql, pInfo); if (code != TSDB_CODE_SUCCESS) { return code; @@ -965,13 +971,13 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { 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); + tscTrace("0x%" PRIx64 " start to parse the %dth subclause, total:%" PRIzu, pSql->self, i, size); if (size > 1 && pSqlNode->from && pSqlNode->from->type == SQL_NODE_FROM_SUBQUERY) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } -// normalizeSqlNode(pSqlNode); // normalize the column name in each function + // normalizeSqlNode(pSqlNode); // normalize the column name in each function if ((code = validateSqlNode(pSql, pSqlNode, pQueryInfo)) != TSDB_CODE_SUCCESS) { return code; } @@ -983,7 +989,7 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { return code; } - SArray *pUdfInfo = NULL; + SArray* pUdfInfo = NULL; if (pQueryInfo->pUdfInfo) { pUdfInfo = taosArrayDup(pQueryInfo->pUdfInfo); } @@ -1000,7 +1006,7 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { // set the command/global limit parameters from the first not empty subclause to the sqlcmd object SQueryInfo* queryInfo = pCmd->pQueryInfo; - int16_t command = queryInfo->command; + int16_t command = queryInfo->command; while (command == TSDB_SQL_RETRIEVE_EMPTY_RESULT && queryInfo->sibling != NULL) { queryInfo = queryInfo->sibling; command = queryInfo->command; @@ -1036,7 +1042,7 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { case TSDB_SQL_SYNC_DB_REPLICA: { const char* msg1 = "invalid db name"; - SStrToken* pzName = taosArrayGet(pInfo->pMiscInfo->a, 0); + SStrToken* pzName = taosArrayGet(pInfo->pMiscInfo->a, 0); assert(taosArrayGetSize(pInfo->pMiscInfo->a) == 1); code = tNameSetDbName(&pTableMetaInfo->name, getAccountId(pSql), pzName); @@ -1045,7 +1051,7 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } break; } - case TSDB_SQL_COMPACT_VNODE:{ + case TSDB_SQL_COMPACT_VNODE: { const char* msg = "invalid compact"; if (setCompactVnodeInfo(pSql, pInfo) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); @@ -1069,12 +1075,12 @@ int32_t tscValidateSqlInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { */ static bool isTopBottomUniqueQuery(SQueryInfo* pQueryInfo) { size_t size = tscNumOfExprs(pQueryInfo); - + for (int32_t i = 0; i < size; ++i) { int32_t functionId = tscExprGet(pQueryInfo, i)->base.functionId; - if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_SAMPLE - || functionId == TSDB_FUNC_UNIQUE || functionId == TSDB_FUNC_TAIL) { + if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_SAMPLE || + functionId == TSDB_FUNC_UNIQUE || functionId == TSDB_FUNC_TAIL) { return true; } } @@ -1086,7 +1092,7 @@ static bool isTopBottomUniqueQuery(SQueryInfo* pQueryInfo) { static int32_t addPrimaryTsColumnForTimeWindowQuery(SQueryInfo* pQueryInfo, SSqlCmd* pCmd) { uint64_t uid = tscExprGet(pQueryInfo, 0)->base.uid; - int32_t tableIndex = COLUMN_INDEX_INITIAL_VAL; + int32_t tableIndex = COLUMN_INDEX_INITIAL_VAL; STableMetaInfo* pTableMetaInfo = NULL; for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { pTableMetaInfo = tscGetMetaInfo(pQueryInfo, i); @@ -1105,7 +1111,7 @@ static int32_t addPrimaryTsColumnForTimeWindowQuery(SQueryInfo* pQueryInfo, SSql tstrncpy(s.name, pTableMetaInfo->pTableMeta->schema[PRIMARYKEY_TIMESTAMP_COL_INDEX].name, sizeof(s.name)); } else { tstrncpy(s.name, aAggs[TSDB_FUNC_TS].name, sizeof(s.name)); - } + } SColumnIndex index = {tableIndex, PRIMARYKEY_TIMESTAMP_COL_INDEX}; tscAddFuncInSelectClause(pQueryInfo, 0, TSDB_FUNC_TS, &index, &s, TSDB_COL_NORMAL, 0); @@ -1143,7 +1149,6 @@ static int32_t checkInvalidExprForTimeWindow(SSqlCmd* pCmd, SQueryInfo* pQueryIn // for top/bottom + interval query, we do not add additional timestamp column in the front if (isTopBottomUniqueQuery(pQueryInfo)) { - // invalid sql: // top(col, k) from table_name [interval(1d)|session(ts, 1d)] order by k asc // order by normal column is not supported @@ -1168,7 +1173,7 @@ int32_t validateIntervalNode(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNode* pS SSqlCmd* pCmd = &pSql->cmd; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); if (!TPARSER_HAS_TOKEN(pSqlNode->interval.interval)) { if (TPARSER_HAS_TOKEN(pSqlNode->sliding)) { @@ -1180,7 +1185,7 @@ int32_t validateIntervalNode(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNode* pS if (interpQuery) { return addPrimaryTsColumnForTimeWindowQuery(pQueryInfo, pCmd); } - + return TSDB_CODE_SUCCESS; } @@ -1190,9 +1195,9 @@ int32_t validateIntervalNode(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNode* pS } // interval is not null - SStrToken *t = &pSqlNode->interval.interval; - if (parseNatualDuration(t->z, t->n, &pQueryInfo->interval.interval, - &pQueryInfo->interval.intervalUnit, tinfo.precision) != TSDB_CODE_SUCCESS) { + SStrToken* 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; } @@ -1202,7 +1207,8 @@ int32_t validateIntervalNode(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNode* pS if (pQueryInfo->interval.intervalUnit != 'n' && pQueryInfo->interval.intervalUnit != 'y') { // interval cannot be less than 10 milliseconds - if (convertTimePrecision(pQueryInfo->interval.interval, tinfo.precision, TSDB_TIME_PRECISION_MICRO) < tsMinIntervalTime) { + if (convertTimePrecision(pQueryInfo->interval.interval, tinfo.precision, TSDB_TIME_PRECISION_MICRO) < + tsMinIntervalTime) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } } @@ -1229,7 +1235,6 @@ int32_t validateIntervalNode(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNode* pS } static int32_t validateStateWindowNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNode, bool isStable) { - const char* msg1 = "invalid column name"; const char* msg2 = "invalid column type"; const char* msg3 = "not support state_window with group by "; @@ -1237,7 +1242,7 @@ static int32_t validateStateWindowNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SS const char* msg5 = "not support state_window on tag column"; const char* msg6 = "function not support for state_window"; - SStrToken *col = &(pSqlNode->windowstateVal.col) ; + SStrToken* col = &(pSqlNode->windowstateVal.col); if (col->z == NULL || col->n <= 0) { return TSDB_CODE_SUCCESS; } @@ -1250,19 +1255,19 @@ static int32_t validateStateWindowNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SS } pQueryInfo->groupbyExpr.numOfGroupCols = 1; - //TODO(dengyihao): check tag column + // TODO(dengyihao): check tag column if (isStable) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByName(col, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByName(col, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - int32_t numOfCols = tscGetNumOfColumns(pTableMeta); + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); + STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; + int32_t numOfCols = tscGetNumOfColumns(pTableMeta); if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } else if (index.columnIndex >= numOfCols) { @@ -1275,9 +1280,9 @@ static int32_t validateStateWindowNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SS } SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, index.columnIndex); - if (pSchema->type == TSDB_DATA_TYPE_TIMESTAMP || pSchema->type == TSDB_DATA_TYPE_FLOAT - || pSchema->type == TSDB_DATA_TYPE_DOUBLE || pSchema->type == TSDB_DATA_TYPE_NCHAR - || pSchema->type == TSDB_DATA_TYPE_BINARY) { + if (pSchema->type == TSDB_DATA_TYPE_TIMESTAMP || pSchema->type == TSDB_DATA_TYPE_FLOAT || + pSchema->type == TSDB_DATA_TYPE_DOUBLE || pSchema->type == TSDB_DATA_TYPE_NCHAR || + pSchema->type == TSDB_DATA_TYPE_BINARY) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -1296,16 +1301,16 @@ static int32_t validateStateWindowNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SS return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg6); } } - + tscColumnListInsert(pQueryInfo->colList, index.columnIndex, pTableMeta->id.uid, pSchema); - SColIndex colIndex = { .colIndex = index.columnIndex, .flag = TSDB_COL_NORMAL, .colId = pSchema->colId }; + SColIndex colIndex = {.colIndex = index.columnIndex, .flag = TSDB_COL_NORMAL, .colId = pSchema->colId}; taosArrayPush(pGroupExpr->columnInfo, &colIndex); pQueryInfo->groupbyExpr.orderType = TSDB_ORDER_ASC; pQueryInfo->stateWindow = true; return TSDB_CODE_SUCCESS; } -int32_t validateSessionNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode * pSqlNode) { +int32_t validateSessionNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNode) { const char* msg1 = "gap should be fixed time window"; const char* msg2 = "only one type time window allowed"; const char* msg3 = "invalid column name"; @@ -1313,7 +1318,7 @@ int32_t validateSessionNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode * pS const char* msg5 = "function not support for session"; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); // no session window if (!TPARSER_HAS_TOKEN(pSqlNode->sessionVal.gap)) { return TSDB_CODE_SUCCESS; @@ -1323,7 +1328,8 @@ int32_t validateSessionNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode * pS SStrToken* gap = &pSqlNode->sessionVal.gap; char timeUnit = 0; - if (parseNatualDuration(gap->z, gap->n, &pQueryInfo->sessionWindow.gap, &timeUnit, tinfo.precision) != TSDB_CODE_SUCCESS) { + if (parseNatualDuration(gap->z, gap->n, &pQueryInfo->sessionWindow.gap, &timeUnit, tinfo.precision) != + TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } @@ -1364,7 +1370,7 @@ int32_t parseIntervalOffset(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* of const char* msg4 = "wrong every format"; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); SStrToken* t = offsetToken; if (t->n == 0) { @@ -1378,8 +1384,8 @@ int32_t parseIntervalOffset(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* of return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } - if (parseNatualDuration(t->z, t->n, &pQueryInfo->interval.offset, - &pQueryInfo->interval.offsetUnit, tinfo.precision) != TSDB_CODE_SUCCESS) { + if (parseNatualDuration(t->z, t->n, &pQueryInfo->interval.offset, &pQueryInfo->interval.offsetUnit, + tinfo.precision) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; } @@ -1398,7 +1404,7 @@ int32_t parseIntervalOffset(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* of return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } } else if (pQueryInfo->interval.intervalUnit == 'n' && pQueryInfo->interval.offsetUnit == 'y') { - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } else if (pQueryInfo->interval.intervalUnit == 'y' && pQueryInfo->interval.offsetUnit == 'n') { if (pQueryInfo->interval.interval * 12 <= pQueryInfo->interval.offset) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); @@ -1421,12 +1427,12 @@ int32_t parseSlidingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* pSl const static int32_t INTERVAL_SLIDING_FACTOR = 100; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); SInterval* pInterval = &pQueryInfo->interval; if (pSliding->n == 0) { pInterval->slidingUnit = pInterval->intervalUnit; - pInterval->sliding = pInterval->interval; + pInterval->sliding = pInterval->interval; return TSDB_CODE_SUCCESS; } @@ -1449,7 +1455,7 @@ int32_t parseSlidingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* pSl return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - if ((pInterval->interval != 0) && (pInterval->interval/pInterval->sliding > INTERVAL_SLIDING_FACTOR)) { + if ((pInterval->interval != 0) && (pInterval->interval / pInterval->sliding > INTERVAL_SLIDING_FACTOR)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -1467,12 +1473,12 @@ int32_t tscSetTableFullName(SName* pName, SStrToken* pTableName, SSqlObj* pSql, SSqlCmd* pCmd = &pSql->cmd; int32_t code = TSDB_CODE_SUCCESS; int32_t idx = -1; - + if (dbIncluded) { - idx = getDelimiterIndex(pTableName); + idx = getDelimiterIndex(pTableName); } - - if (idx != -1) { // db has been specified in sql string so we ignore current db path + + if (idx != -1) { // db has been specified in sql string so we ignore current db path char* acctId = getAccountId(pSql); if (acctId == NULL || strlen(acctId) <= 0) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); @@ -1493,7 +1499,7 @@ int32_t tscSetTableFullName(SName* pName, SStrToken* pTableName, SSqlObj* pSql, char name[TSDB_TABLE_FNAME_LEN] = {0}; strncpy(name, pTableName->z, pTableName->n); - code = tNameFromString(pName, name, T_NAME_DB|T_NAME_TABLE); + code = tNameFromString(pName, name, T_NAME_DB | T_NAME_TABLE); if (code != 0) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } @@ -1514,7 +1520,7 @@ int32_t tscSetTableFullName(SName* pName, SStrToken* pTableName, SSqlObj* pSql, if (pTableName->n >= TSDB_TABLE_NAME_LEN) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); - } else if(pTableName->n == 0) { + } else if (pTableName->n == 0) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg6); } @@ -1544,7 +1550,7 @@ static bool validateTableColumnInfo(SArray* pFieldList, SSqlCmd* pCmd) { // number of fields no less than 2 size_t numOfCols = taosArrayGetSize(pFieldList); - if (numOfCols <= 1 ) { + if (numOfCols <= 1) { invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); return false; } else if (numOfCols > TSDB_MAX_COLUMNS) { @@ -1607,7 +1613,7 @@ static bool validateTagParams(SArray* pTagsList, SArray* pFieldList, SSqlCmd* pC const char* msg1 = "invalid number of tag columns"; const char* msg2 = "tag length too long"; const char* msg3 = "duplicated column names"; - //const char* msg4 = "timestamp not allowed in tags"; + // const char* msg4 = "timestamp not allowed in tags"; const char* msg5 = "invalid data type in tags"; const char* msg6 = "invalid tag name or length"; const char* msg7 = "invalid binary/nchar tag length"; @@ -1632,8 +1638,7 @@ static bool validateTagParams(SArray* pTagsList, SArray* pFieldList, SSqlCmd* pC return false; } - if ((p->type == TSDB_DATA_TYPE_BINARY && p->bytes <= 0) || - (p->type == TSDB_DATA_TYPE_NCHAR && p->bytes <= 0)) { + if ((p->type == TSDB_DATA_TYPE_BINARY && p->bytes <= 0) || (p->type == TSDB_DATA_TYPE_NCHAR && p->bytes <= 0)) { invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg7); return false; } @@ -1650,14 +1655,14 @@ static bool validateTagParams(SArray* pTagsList, SArray* pFieldList, SSqlCmd* pC } int32_t nLen = 0; - bool isJsonTag = false; + bool isJsonTag = false; for (int32_t i = 0; i < numOfTags; ++i) { TAOS_FIELD* p = taosArrayGet(pTagsList, i); if (p->bytes == 0) { invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg7); return false; } - if(p->type == TSDB_DATA_TYPE_JSON){ + if (p->type == TSDB_DATA_TYPE_JSON) { isJsonTag = true; } nLen += p->bytes; @@ -1693,7 +1698,7 @@ int32_t validateOneTag(SSqlCmd* pCmd, TAOS_FIELD* pTagField) { const char* msg7 = "too many columns"; const char* msg8 = "only support one json tag"; - STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0); + STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0); STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; int32_t numOfTags = tscGetNumOfTags(pTableMeta); @@ -1713,7 +1718,7 @@ int32_t validateOneTag(SSqlCmd* pCmd, TAOS_FIELD* pTagField) { } // no timestamp allowable - //if (pTagField->type == TSDB_DATA_TYPE_TIMESTAMP) { + // if (pTagField->type == TSDB_DATA_TYPE_TIMESTAMP) { // invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); // return false; //} @@ -1728,7 +1733,7 @@ int32_t validateOneTag(SSqlCmd* pCmd, TAOS_FIELD* pTagField) { SSchema* pTagSchema = tscGetTableTagSchema(pTableMetaInfo->pTableMeta); int32_t nLen = 0; - if (numOfTags == 1 && pTagSchema[0].type == TSDB_DATA_TYPE_JSON){ + if (numOfTags == 1 && pTagSchema[0].type == TSDB_DATA_TYPE_JSON) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg8); } @@ -1756,7 +1761,7 @@ int32_t validateOneTag(SSqlCmd* pCmd, TAOS_FIELD* pTagField) { for (int32_t i = 0; i < numOfTags + numOfCols; ++i) { if (strncasecmp(pTagField->name, pSchema[i].name, sizeof(pTagField->name) - 1) == 0) { - //return tscErrorMsgWithCode(TSDB_CODE_TSC_DUP_COL_NAMES, tscGetErrorMsgPayload(pCmd), pTagField->name, NULL); + // return tscErrorMsgWithCode(TSDB_CODE_TSC_DUP_COL_NAMES, tscGetErrorMsgPayload(pCmd), pTagField->name, NULL); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "duplicated column names"); } } @@ -1771,13 +1776,13 @@ int32_t validateOneColumn(SSqlCmd* pCmd, TAOS_FIELD* pColField) { const char* msg5 = "invalid column name or length"; const char* msg6 = "invalid column length"; -// assert(pCmd->numOfClause == 1); - STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0); + // assert(pCmd->numOfClause == 1); + STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0); STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - + int32_t numOfTags = tscGetNumOfTags(pTableMeta); int32_t numOfCols = tscGetNumOfColumns(pTableMeta); - + // no more max columns if (numOfCols >= TSDB_MAX_COLUMNS || numOfTags + numOfCols >= TSDB_MAX_COLUMNS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); @@ -1810,7 +1815,7 @@ int32_t validateOneColumn(SSqlCmd* pCmd, TAOS_FIELD* pColField) { // field name must be unique for (int32_t i = 0; i < numOfTags + numOfCols; ++i) { if (strncasecmp(pColField->name, pSchema[i].name, sizeof(pColField->name) - 1) == 0) { - //return tscErrorMsgWithCode(TSDB_CODE_TSC_DUP_COL_NAMES, tscGetErrorMsgPayload(pCmd), pColField->name, NULL); + // return tscErrorMsgWithCode(TSDB_CODE_TSC_DUP_COL_NAMES, tscGetErrorMsgPayload(pCmd), pColField->name, NULL); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "duplicated column names"); } } @@ -1847,7 +1852,7 @@ void tscInsertPrimaryTsSourceColumn(SQueryInfo* pQueryInfo, uint64_t tableUid) { } static int32_t handleScalarTypeExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t exprIndex, tSqlExprItem* pItem, - SColumnList* columnList, bool finalResult) { + SColumnList* columnList, bool finalResult) { const char* msg2 = "invalid arithmetic expression in select clause"; const char* msg3 = "tag columns can not be used in arithmetic expression"; const char* msg4 = "columns from different table mixed up in arithmetic expression"; @@ -1862,9 +1867,8 @@ static int32_t handleScalarTypeExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32 } } - tExprNode* pNode = NULL; - SArray* colList = taosArrayInit(10, sizeof(SColIndex)); + SArray* colList = taosArrayInit(10, sizeof(SColIndex)); int32_t ret = exprTreeFromSqlExpr(pCmd, &pNode, pItem->pNode, pQueryInfo, colList, NULL); if (ret != TSDB_CODE_SUCCESS) { @@ -1879,7 +1883,7 @@ static int32_t handleScalarTypeExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32 // check for if there is a tag in the scalar expression size_t numOfNode = taosArrayGetSize(colList); - for(int32_t k = 0; k < numOfNode; ++k) { + for (int32_t k = 0; k < numOfNode; ++k) { SColIndex* pIndex = taosArrayGet(colList, k); if (TSDB_COL_IS_TAG(pIndex->flag)) { tExprTreeDestroy(pNode, NULL); @@ -1901,24 +1905,24 @@ static int32_t handleScalarTypeExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32 } SColumnIndex index = {.tableIndex = tableIndex}; - SExprInfo* pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_SCALAR_EXPR, &index, pNode->resultType, pNode->resultBytes, - getNewResColId(pCmd), 0, false); + SExprInfo* pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_SCALAR_EXPR, &index, pNode->resultType, pNode->resultBytes, + getNewResColId(pCmd), 0, false); // set the colId to the result column id - pExpr->base.colInfo.colId = pExpr->base.resColId; + pExpr->base.colInfo.colId = pExpr->base.resColId; - char* name = (pItem->aliasName != NULL)? pItem->aliasName:pItem->pNode->exprToken.z; + char* name = (pItem->aliasName != NULL) ? pItem->aliasName : pItem->pNode->exprToken.z; size_t len = MIN(sizeof(pExpr->base.aliasName), pItem->pNode->exprToken.n + 1); tstrncpy(pExpr->base.aliasName, name, len); SBufferWriter bw = tbufInitWriter(NULL, false); - TRY(0) { - exprTreeToBinary(&bw, pNode); - } CATCH(code) { - tbufCloseWriter(&bw); - UNUSED(code); - // TODO: other error handling - } END_TRY + TRY(0) { exprTreeToBinary(&bw, pNode); } + CATCH(code) { + tbufCloseWriter(&bw); + UNUSED(code); + // TODO: other error handling + } + END_TRY len = tbufTell(&bw); char* c = tbufGetData(&bw, false); @@ -1926,8 +1930,8 @@ static int32_t handleScalarTypeExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32 // set the serialized binary string as the parameter of arithmetic expression tscExprAddParams(&pExpr->base, c, TSDB_DATA_TYPE_BINARY, (int32_t)len); if (finalResult) { - insertResultField(pQueryInfo, exprIndex, columnList, pExpr->base.resBytes, (int8_t)pExpr->base.resType, pExpr->base.aliasName, - pExpr); + insertResultField(pQueryInfo, exprIndex, columnList, pExpr->base.resBytes, (int8_t)pExpr->base.resType, + pExpr->base.aliasName, pExpr); } // add ts column @@ -1941,19 +1945,19 @@ static int32_t handleScalarTypeExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32 } static int32_t handleAggTypeExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t exprIndex, tSqlExprItem* pItem, - SColumnList* columnList, bool finalResult) { + SColumnList* columnList, bool finalResult) { const char* msg2 = "invalid arithmetic expression in select clause"; columnList->num = 0; - columnList->ids[0] = (SColumnIndex) {0, 0}; + columnList->ids[0] = (SColumnIndex){0, 0}; char rawName[TSDB_COL_NAME_LEN] = {0}; char aliasName[TSDB_COL_NAME_LEN] = {0}; getColumnName(pItem, aliasName, rawName, TSDB_COL_NAME_LEN); - tExprNode *pExpr = NULL; - uint64_t uid = 0; - int32_t ret = exprTreeFromSqlExpr(pCmd, &pExpr, pItem->pNode, pQueryInfo, NULL, &uid); + tExprNode* pExpr = NULL; + uint64_t uid = 0; + int32_t ret = exprTreeFromSqlExpr(pCmd, &pExpr, pItem->pNode, pQueryInfo, NULL, &uid); if (ret != TSDB_CODE_SUCCESS) { tExprTreeDestroy(pExpr, NULL); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "invalid expression in select clause"); @@ -1967,14 +1971,14 @@ static int32_t handleAggTypeExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t // the expr associated with the result field will become exprList1 in SQueryInfo, then pExpr2 in SQueryAttr insertResultField(pQueryInfo, exprIndex, columnList, pExpr->resultBytes, (int8_t)pExpr->resultType, aliasName, NULL); - int32_t slot = tscNumOfFields(pQueryInfo) - 1; + int32_t slot = tscNumOfFields(pQueryInfo) - 1; SInternalField* pInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, slot); assert(pInfo->pExpr == NULL); SExprInfo* pExprInfo = calloc(1, sizeof(SExprInfo)); // arithmetic expression always return result in the format of double float - pExprInfo->base.resBytes = pExpr->resultBytes; + pExprInfo->base.resBytes = pExpr->resultBytes; pExprInfo->base.interBytes = 0; - pExprInfo->base.resType = pExpr->resultType; + pExprInfo->base.resType = pExpr->resultType; pExprInfo->base.functionId = TSDB_FUNC_SCALAR_EXPR; pExprInfo->base.numOfParams = 1; @@ -1988,20 +1992,20 @@ static int32_t handleAggTypeExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t SBufferWriter bw = tbufInitWriter(NULL, false); - TRY(0) { - exprTreeToBinary(&bw, pInfo->pExpr->pExpr); - } CATCH(code) { - tbufCloseWriter(&bw); - UNUSED(code); - // TODO: other error handling - } END_TRY + TRY(0) { exprTreeToBinary(&bw, pInfo->pExpr->pExpr); } + CATCH(code) { + tbufCloseWriter(&bw); + UNUSED(code); + // TODO: other error handling + } + END_TRY SSqlExpr* pSqlExpr = &pInfo->pExpr->base; - pSqlExpr->param[0].nLen = (int16_t) tbufTell(&bw); - pSqlExpr->param[0].pz = tbufGetData(&bw, true); + 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); // TODO there is a memory leak + // tbufCloseWriter(&bw); // TODO there is a memory leak return TSDB_CODE_SUCCESS; } @@ -2010,15 +2014,16 @@ static int32_t handleSQLExprItem(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t int32_t sqlExprType = SQLEXPR_TYPE_UNASSIGNED; uint64_t uid; - int32_t height = 0; - int32_t code = validateSQLExprItem(pCmd, pItem->pNode, pQueryInfo, &columnList, &sqlExprType, &uid, &height); + int32_t height = 0; + int32_t code = validateSQLExprItem(pCmd, pItem->pNode, pQueryInfo, &columnList, &sqlExprType, &uid, &height); if (code != TSDB_CODE_SUCCESS) { return code; } if (height > 16) { - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "the max expression nested levels must be equal or less than 16"); + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), + "the max expression nested levels must be equal or less than 16"); } if (sqlExprType == SQLEXPR_TYPE_SCALAR) { @@ -2030,9 +2035,10 @@ static int32_t handleSQLExprItem(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t return code; } -static void addProjectQueryCol(SQueryInfo* pQueryInfo, int32_t startPos, SColumnIndex* pIndex, tSqlExprItem* pItem, int32_t colId) { +static void addProjectQueryCol(SQueryInfo* pQueryInfo, int32_t startPos, SColumnIndex* pIndex, tSqlExprItem* pItem, + int32_t colId) { SExprInfo* pExpr = doAddProjectCol(pQueryInfo, pIndex->columnIndex, pIndex->tableIndex, colId); - if( pItem->pNode->tokenId == TK_ARROW){ + if (pItem->pNode->tokenId == TK_ARROW) { tSqlExpr* right = pItem->pNode->pRight; assert(right != NULL && right->type == SQL_NODE_VALUE); tVariantAssign(&(pExpr->base.param[pExpr->base.numOfParams]), &right->value); @@ -2045,13 +2051,14 @@ static void addProjectQueryCol(SQueryInfo* pQueryInfo, int32_t startPos, SColumn SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, pIndex->columnIndex); if (pSchema->type == TSDB_DATA_TYPE_JSON && pItem->pNode->tokenId == TK_ARROW) { - if (pItem->aliasName){ + if (pItem->aliasName) { tstrncpy(pExpr->base.aliasName, pItem->aliasName, sizeof(pExpr->base.aliasName)); - }else{ + } else { tstrncpy(pExpr->base.aliasName, pItem->pNode->exprToken.z, - pItem->pNode->exprToken.n + 1 < sizeof(pExpr->base.aliasName) ? pItem->pNode->exprToken.n + 1 : sizeof(pExpr->base.aliasName)); + pItem->pNode->exprToken.n + 1 < sizeof(pExpr->base.aliasName) ? pItem->pNode->exprToken.n + 1 + : sizeof(pExpr->base.aliasName)); } - }else{ + } else { char* colName = (pItem->aliasName == NULL) ? pSchema->name : pItem->aliasName; tstrncpy(pExpr->base.aliasName, colName, sizeof(pExpr->base.aliasName)); } @@ -2064,7 +2071,8 @@ static void addProjectQueryCol(SQueryInfo* pQueryInfo, int32_t startPos, SColumn ids.num = 0; } - insertResultField(pQueryInfo, startPos, &ids, pExpr->base.resBytes, (int8_t)pExpr->base.resType, pExpr->base.aliasName, pExpr); + insertResultField(pQueryInfo, startPos, &ids, pExpr->base.resBytes, (int8_t)pExpr->base.resType, + pExpr->base.aliasName, pExpr); } static void addPrimaryTsColIntoResult(SQueryInfo* pQueryInfo, SSqlCmd* pCmd) { @@ -2077,15 +2085,15 @@ static void addPrimaryTsColIntoResult(SQueryInfo* pQueryInfo, SSqlCmd* pCmd) { } } - // set the constant column value always attached to first table. STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, PRIMARYKEY_TIMESTAMP_COL_INDEX); + SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, PRIMARYKEY_TIMESTAMP_COL_INDEX); // add the timestamp column into the output columns SColumnIndex index = {0}; // primary timestamp column info - int32_t numOfCols = (int32_t)tscNumOfFields(pQueryInfo); - tscAddFuncInSelectClause(pQueryInfo, numOfCols, TSDB_FUNC_PRJ, &index, pSchema, TSDB_COL_NORMAL, getNewResColId(pCmd)); + int32_t numOfCols = (int32_t)tscNumOfFields(pQueryInfo); + tscAddFuncInSelectClause(pQueryInfo, numOfCols, TSDB_FUNC_PRJ, &index, pSchema, TSDB_COL_NORMAL, + getNewResColId(pCmd)); SInternalField* pSupInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, numOfCols); pSupInfo->visible = false; @@ -2108,7 +2116,7 @@ static bool hasNoneUserDefineExpr(SQueryInfo* pQueryInfo) { return false; } -void genUdfList(SArray* pUdfInfo, tSqlExpr *pNode) { +void genUdfList(SArray* pUdfInfo, tSqlExpr* pNode) { if (pNode == NULL) { return; } @@ -2121,7 +2129,7 @@ void genUdfList(SArray* pUdfInfo, tSqlExpr *pNode) { if (pNode->type == SQL_NODE_SQLFUNCTION) { pNode->functionId = isValidFunction(pNode->Expr.operand.z, pNode->Expr.operand.n); - if (pNode->functionId < 0) { // extract all possible user defined function + if (pNode->functionId < 0) { // extract all possible user defined function struct SUdfInfo info = {0}; info.name = strndup(pNode->Expr.operand.z, pNode->Expr.operand.n); int32_t functionId = (int32_t)taosArrayGetSize(pUdfInfo) * (-1) - 1; @@ -2160,12 +2168,12 @@ static int32_t checkForUdf(SSqlObj* pSql, SQueryInfo* pQueryInfo, SArray* pSelec */ static SUdfInfo* isValidUdf(SArray* pUdfInfo, const char* name, int32_t len) { - if(pUdfInfo == NULL){ + if (pUdfInfo == NULL) { tscError("udfinfo is null"); return NULL; } size_t t = taosArrayGetSize(pUdfInfo); - for(int32_t i = 0; i < t; ++i) { + for (int32_t i = 0; i < t; ++i) { SUdfInfo* pUdf = taosArrayGet(pUdfInfo, i); if (strlen(pUdf->name) == len && strncasecmp(pUdf->name, name, len) == 0) { return pUdf; @@ -2194,7 +2202,7 @@ int32_t validateSelectNodeList(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pS const char* msg3 = "not support query expression"; const char* msg4 = "not support distinct mixed with proj/agg func"; const char* msg5 = "invalid function name"; - const char* msg6 = "not support distinct mixed with join"; + const char* msg6 = "not support distinct mixed with join"; const char* msg7 = "not support distinct mixed with groupby"; const char* msg8 = "not support distinct in nest query"; const char* msg9 = "_block_dist not support subquery, only support stable/table"; @@ -2210,23 +2218,22 @@ int32_t validateSelectNodeList(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pS pQueryInfo->colList = taosArrayInit(4, POINTER_BYTES); } - - bool hasDistinct = false; - bool hasAgg = false; - size_t numOfExpr = taosArrayGetSize(pSelNodeList); + bool hasDistinct = false; + bool hasAgg = false; + size_t numOfExpr = taosArrayGetSize(pSelNodeList); int32_t distIdx = -1; for (int32_t i = 0; i < numOfExpr; ++i) { - int32_t outputIndex = (int32_t)tscNumOfExprs(pQueryInfo); + int32_t outputIndex = (int32_t)tscNumOfExprs(pQueryInfo); tSqlExprItem* pItem = taosArrayGet(pSelNodeList, i); if (hasDistinct == false) { - hasDistinct = (pItem->distinct == true); - distIdx = hasDistinct ? i : -1; + hasDistinct = (pItem->distinct == true); + distIdx = hasDistinct ? i : -1; } - if(pItem->aliasName != NULL && validateColumnName(pItem->aliasName) != TSDB_CODE_SUCCESS){ + if (pItem->aliasName != NULL && validateColumnName(pItem->aliasName) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg11); } - if(pItem->aliasName != NULL && strcasecmp(pItem->aliasName, DEFAULT_PRIMARY_TIMESTAMP_COL_NAME) == 0){ + if (pItem->aliasName != NULL && strcasecmp(pItem->aliasName, DEFAULT_PRIMARY_TIMESTAMP_COL_NAME) == 0) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg11); } @@ -2238,7 +2245,7 @@ int32_t validateSelectNodeList(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pS } } else if (type == SQL_NODE_SQLFUNCTION) { hasAgg = true; - if (hasDistinct) break; + if (hasDistinct) break; pItem->pNode->functionId = isValidFunction(pItem->pNode->Expr.operand.z, pItem->pNode->Expr.operand.n); @@ -2278,7 +2285,7 @@ int32_t validateSelectNodeList(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pS if (addProjectionExprAndResultField(pCmd, pQueryInfo, pItem, outerQuery, timeWindowQuery) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; } - } else { + } else { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } @@ -2287,8 +2294,8 @@ int32_t validateSelectNodeList(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pS } } - //TODO(dengyihao), refactor as function - //handle distinct func mixed with other func + // TODO(dengyihao), refactor as function + // handle distinct func mixed with other func if (hasDistinct == true) { if (distIdx != 0 || hasAgg) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); @@ -2296,7 +2303,7 @@ int32_t validateSelectNodeList(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pS if (joinQuery) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg6); } - if (pQueryInfo->groupbyExpr.numOfGroupCols != 0) { + if (pQueryInfo->groupbyExpr.numOfGroupCols != 0) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg7); } if (pQueryInfo->pDownstream != NULL) { @@ -2306,10 +2313,10 @@ int32_t validateSelectNodeList(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pS pQueryInfo->distinct = true; } - // there is only one user-defined column in the final result field, add the timestamp column. size_t numOfSrcCols = taosArrayGetSize(pQueryInfo->colList); - if ((numOfSrcCols <= 0 || !hasNoneUserDefineExpr(pQueryInfo)) && !tscQueryTags(pQueryInfo) && !tscQueryBlockInfo(pQueryInfo)) { + if ((numOfSrcCols <= 0 || !hasNoneUserDefineExpr(pQueryInfo)) && !tscQueryTags(pQueryInfo) && + !tscQueryBlockInfo(pQueryInfo)) { addPrimaryTsColIntoResult(pQueryInfo, pCmd); } @@ -2323,7 +2330,7 @@ int32_t validateSelectNodeList(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pS int32_t insertResultField(SQueryInfo* pQueryInfo, int32_t outputIndex, SColumnList* pColList, int16_t bytes, int8_t type, char* fieldName, SExprInfo* pSqlExpr) { for (int32_t i = 0; i < pColList->num; ++i) { - int32_t tableIndex = pColList->ids[i].tableIndex; + int32_t tableIndex = pColList->ids[i].tableIndex; STableMeta* pTableMeta = pQueryInfo->pTableMetaInfo[tableIndex]->pTableMeta; int32_t numOfCols = tscGetNumOfColumns(pTableMeta); @@ -2335,24 +2342,26 @@ int32_t insertResultField(SQueryInfo* pQueryInfo, int32_t outputIndex, SColumnLi SSchema* pSchema = tscGetTableSchema(pTableMeta); tscColumnListInsert(pQueryInfo->colList, pColList->ids[i].columnIndex, uid, &pSchema[pColList->ids[i].columnIndex]); } - - TAOS_FIELD f = tscCreateField(type, fieldName, bytes); + + TAOS_FIELD f = tscCreateField(type, fieldName, bytes); SInternalField* pInfo = tscFieldInfoInsert(&pQueryInfo->fieldsInfo, outputIndex, &f); pInfo->pExpr = pSqlExpr; - + return TSDB_CODE_SUCCESS; } SExprInfo* doAddProjectCol(SQueryInfo* pQueryInfo, int32_t colIndex, int32_t tableIndex, int32_t colId) { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, tableIndex); STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - int32_t numOfCols = tscGetNumOfColumns(pTableMeta); - + int32_t numOfCols = tscGetNumOfColumns(pTableMeta); + SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, colIndex); - int16_t functionId = (int16_t)((colIndex >= numOfCols) ? TSDB_FUNC_TAGPRJ : TSDB_FUNC_PRJ); - SColumnIndex index = {.tableIndex = tableIndex,}; - + int16_t functionId = (int16_t)((colIndex >= numOfCols) ? TSDB_FUNC_TAGPRJ : TSDB_FUNC_PRJ); + SColumnIndex index = { + .tableIndex = tableIndex, + }; + if (functionId == TSDB_FUNC_TAGPRJ) { index.columnIndex = colIndex - tscGetNumOfColumns(pTableMeta); tscColumnListInsert(pTableMetaInfo->tagColList, index.columnIndex, pTableMeta->id.uid, pSchema); @@ -2361,13 +2370,13 @@ SExprInfo* doAddProjectCol(SQueryInfo* pQueryInfo, int32_t colIndex, int32_t tab } return tscExprAppend(pQueryInfo, functionId, &index, pSchema->type, pSchema->bytes, colId, 0, - (functionId == TSDB_FUNC_TAGPRJ)); + (functionId == TSDB_FUNC_TAGPRJ)); } SExprInfo* tscAddFuncInSelectClause(SQueryInfo* pQueryInfo, int32_t outputColIndex, int16_t functionId, - SColumnIndex* pIndex, SSchema* pColSchema, int16_t flag, int16_t colId) { - SExprInfo* pExpr = tscExprInsert(pQueryInfo, outputColIndex, functionId, pIndex, pColSchema->type, - pColSchema->bytes, colId, 0, TSDB_COL_IS_TAG(flag)); + SColumnIndex* pIndex, SSchema* pColSchema, int16_t flag, int16_t colId) { + SExprInfo* pExpr = tscExprInsert(pQueryInfo, outputColIndex, functionId, pIndex, pColSchema->type, pColSchema->bytes, + colId, 0, TSDB_COL_IS_TAG(flag)); tstrncpy(pExpr->base.aliasName, pColSchema->name, sizeof(pExpr->base.aliasName)); tstrncpy(pExpr->base.token, pColSchema->name, sizeof(pExpr->base.token)); @@ -2380,15 +2389,17 @@ SExprInfo* tscAddFuncInSelectClause(SQueryInfo* pQueryInfo, int32_t outputColInd pExpr->base.colInfo.flag = flag; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, pIndex->tableIndex); - + if (TSDB_COL_IS_TAG(flag)) { - tscColumnListInsert(pTableMetaInfo->tagColList, pIndex->columnIndex, pTableMetaInfo->pTableMeta->id.uid, pColSchema); + tscColumnListInsert(pTableMetaInfo->tagColList, pIndex->columnIndex, pTableMetaInfo->pTableMeta->id.uid, + pColSchema); } return pExpr; } -static int32_t doAddProjectionExprAndResultFields(SQueryInfo* pQueryInfo, SColumnIndex* pIndex, int32_t startPos, SSqlCmd* pCmd) { +static int32_t doAddProjectionExprAndResultFields(SQueryInfo* pQueryInfo, SColumnIndex* pIndex, int32_t startPos, + SSqlCmd* pCmd) { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, pIndex->tableIndex); int32_t numOfTotalColumns = 0; @@ -2396,7 +2407,7 @@ static int32_t doAddProjectionExprAndResultFields(SQueryInfo* pQueryInfo, SColum SSchema* pSchema = tscGetTableSchema(pTableMeta); STableComInfo tinfo = tscGetTableInfo(pTableMeta); - + if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { numOfTotalColumns = tinfo.numOfColumns + tinfo.numOfTags; } else { @@ -2418,10 +2429,12 @@ static int32_t doAddProjectionExprAndResultFields(SQueryInfo* pQueryInfo, SColum return numOfTotalColumns; } -int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExprItem* pItem, bool outerQuery, bool timeWindowQuery) { +int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExprItem* pItem, bool outerQuery, + bool timeWindowQuery) { const char* msg1 = "tag for normal table query is not allowed"; const char* msg2 = "invalid column name"; - const char* msg3 = "tbname/_wstart/_wstop/_wduration/_qstart/_qstop/_qduration in outer query does not match inner query result"; + const char* msg3 = + "tbname/_wstart/_wstop/_wduration/_qstart/_qstop/_qduration in outer query does not match inner query result"; const char* msg4 = "-> operate can only used in json type"; const char* msg5 = "the right value of -> operation must be string"; const char* msg6 = "select name is too long than 64, please use alias name"; @@ -2454,7 +2467,8 @@ int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, t if (pTableMeta->tableType != TSDB_TEMP_TABLE) { tscInsertPrimaryTsSourceColumn(pQueryInfo, pTableMeta->id.uid); } - } else if (tokenId == TK_STRING || tokenId == TK_INTEGER || tokenId == TK_FLOAT || tokenId == TK_BOOL) { // simple column projection query + } else if (tokenId == TK_STRING || tokenId == TK_INTEGER || tokenId == TK_FLOAT || + tokenId == TK_BOOL) { // simple column projection query SColumnIndex index = COLUMN_INDEX_INITIALIZER; // user-specified constant value as a new result column @@ -2466,24 +2480,27 @@ int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, t getNewResColId(pCmd)); tVariantAssign(&pExpr->base.param[pExpr->base.numOfParams++], &pItem->pNode->value); - }else if (tokenId == TK_ID || tokenId == TK_ARROW) { + } else if (tokenId == TK_ID || tokenId == TK_ARROW) { SColumnIndex index = COLUMN_INDEX_INITIALIZER; SStrToken* pToken = NULL; - if (tokenId == TK_ARROW){ + if (tokenId == TK_ARROW) { tSqlExpr* left = pItem->pNode->pLeft; assert(left != NULL && left->type == SQL_NODE_TABLE_COLUMN); - if (pQueryInfo->pUpstream != NULL && taosArrayGetSize(pQueryInfo->pUpstream) > 0){ // if select from subquery, pToken should be jtag->'location'. like (select jtag->'location' from (select jtag->'location' from jsons1);) + if (pQueryInfo->pUpstream != NULL && + taosArrayGetSize(pQueryInfo->pUpstream) > + 0) { // if select from subquery, pToken should be jtag->'location'. like (select jtag->'location' from + // (select jtag->'location' from jsons1);) pToken = &pItem->pNode->exprToken; - }else{ + } else { pToken = &left->columnName; } tSqlExpr* right = pItem->pNode->pRight; - if(right == NULL || right->type != SQL_NODE_VALUE || right->tokenId != TK_STRING){ + if (right == NULL || right->type != SQL_NODE_VALUE || right->tokenId != TK_STRING) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg5); } - }else { + } else { pToken = &pItem->pNode->columnName; } @@ -2491,29 +2508,29 @@ int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, t return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } - //for tbname and other pseudo columns + // for tbname and other pseudo columns if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX || TSDB_COL_IS_TSWIN_COL(index.columnIndex)) { if (outerQuery) { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); int32_t numOfCols = tscGetNumOfColumns(pTableMetaInfo->pTableMeta); - bool existed = false; + bool existed = false; SSchema* pSchema = pTableMetaInfo->pTableMeta->schema; for (int32_t i = 0; i < numOfCols; ++i) { if ((strncasecmp(pSchema[i].name, TSQL_TBNAME_L, tListLen(pSchema[i].name)) == 0 && - index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) || + index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) || (strncasecmp(pSchema[i].name, TSQL_TSWIN_START, tListLen(pSchema[i].name)) == 0 && - index.columnIndex == TSDB_TSWIN_START_COLUMN_INDEX) || + index.columnIndex == TSDB_TSWIN_START_COLUMN_INDEX) || (strncasecmp(pSchema[i].name, TSQL_TSWIN_STOP, tListLen(pSchema[i].name)) == 0 && - index.columnIndex == TSDB_TSWIN_STOP_COLUMN_INDEX) || + index.columnIndex == TSDB_TSWIN_STOP_COLUMN_INDEX) || (strncasecmp(pSchema[i].name, TSQL_TSWIN_DURATION, tListLen(pSchema[i].name)) == 0 && - index.columnIndex == TSDB_TSWIN_DURATION_COLUMN_INDEX) || + index.columnIndex == TSDB_TSWIN_DURATION_COLUMN_INDEX) || (strncasecmp(pSchema[i].name, TSQL_QUERY_START, tListLen(pSchema[i].name)) == 0 && - index.columnIndex == TSDB_QUERY_START_COLUMN_INDEX) || + index.columnIndex == TSDB_QUERY_START_COLUMN_INDEX) || (strncasecmp(pSchema[i].name, TSQL_QUERY_STOP, tListLen(pSchema[i].name)) == 0 && - index.columnIndex == TSDB_QUERY_STOP_COLUMN_INDEX) || + index.columnIndex == TSDB_QUERY_STOP_COLUMN_INDEX) || (strncasecmp(pSchema[i].name, TSQL_QUERY_DURATION, tListLen(pSchema[i].name)) == 0 && - index.columnIndex == TSDB_QUERY_DURATION_COLUMN_INDEX)) { + index.columnIndex == TSDB_QUERY_DURATION_COLUMN_INDEX)) { existed = true; index.columnIndex = i; break; @@ -2535,25 +2552,25 @@ int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, t SSchema colSchema; int16_t functionId, colType; if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { - colSchema = *tGetTbnameColumnSchema(); + colSchema = *tGetTbnameColumnSchema(); functionId = TSDB_FUNC_TAGPRJ; - colType = TSDB_COL_TAG; + colType = TSDB_COL_TAG; } else { if (!timeWindowQuery && (index.columnIndex == TSDB_TSWIN_START_COLUMN_INDEX || - index.columnIndex == TSDB_TSWIN_STOP_COLUMN_INDEX || - index.columnIndex == TSDB_TSWIN_DURATION_COLUMN_INDEX)) { + index.columnIndex == TSDB_TSWIN_STOP_COLUMN_INDEX || + index.columnIndex == TSDB_TSWIN_DURATION_COLUMN_INDEX)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg7); } - colSchema = *tGetTimeWindowColumnSchema(index.columnIndex); + colSchema = *tGetTimeWindowColumnSchema(index.columnIndex); functionId = getTimeWindowFunctionID(index.columnIndex); - colType = TSDB_COL_NORMAL; + colType = TSDB_COL_NORMAL; } char name[TSDB_COL_NAME_LEN] = {0}; getColumnName(pItem, name, colSchema.name, sizeof(colSchema.name) - 1); tstrncpy(colSchema.name, name, TSDB_COL_NAME_LEN); - /*SExprInfo* pExpr = */ tscAddFuncInSelectClause(pQueryInfo, startPos, functionId, &index, &colSchema, - colType, getNewResColId(pCmd)); + /*SExprInfo* pExpr = */ tscAddFuncInSelectClause(pQueryInfo, startPos, functionId, &index, &colSchema, colType, + getNewResColId(pCmd)); } pQueryInfo->type |= TSDB_QUERY_TYPE_PROJECTION_QUERY; } else { @@ -2568,7 +2585,8 @@ int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, t if (tokenId == TK_ARROW && pSchema->type != TSDB_DATA_TYPE_JSON) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } - if (pSchema->type == TSDB_DATA_TYPE_JSON && tokenId == TK_ARROW && pItem->pNode->exprToken.n >= TSDB_COL_NAME_LEN){ + if (pSchema->type == TSDB_DATA_TYPE_JSON && tokenId == TK_ARROW && + pItem->pNode->exprToken.n >= TSDB_COL_NAME_LEN) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg6); } @@ -2613,7 +2631,8 @@ static int32_t setExprInfoForFunctions(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SS int32_t interBufSize = 0; getResultDataInfo(pSchema->type, pSchema->bytes, f, 0, &resType, &resBytes, &interBufSize, 0, false, pUdfInfo); - SExprInfo* pExpr = tscExprAppend(pQueryInfo, f, pColIndex, resType, resBytes, getNewResColId(pCmd), interBufSize, false); + SExprInfo* pExpr = + tscExprAppend(pQueryInfo, f, pColIndex, resType, resBytes, getNewResColId(pCmd), interBufSize, false); tstrncpy(pExpr->base.aliasName, name, tListLen(pExpr->base.aliasName)); if (cvtFunc.originFuncId == TSDB_FUNC_LAST_ROW && cvtFunc.originFuncId != f) { @@ -2626,9 +2645,13 @@ static int32_t setExprInfoForFunctions(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SS pExpr->base.param[0].i64 = TSDB_ORDER_DESC; pExpr->base.param[0].nType = TSDB_DATA_TYPE_INT; } - + // for all queries, the timestamp column needs to be loaded - SSchema s = {.colId = PRIMARYKEY_TIMESTAMP_COL_INDEX, .bytes = TSDB_KEYSIZE, .type = TSDB_DATA_TYPE_TIMESTAMP,}; + SSchema s = { + .colId = PRIMARYKEY_TIMESTAMP_COL_INDEX, + .bytes = TSDB_KEYSIZE, + .type = TSDB_DATA_TYPE_TIMESTAMP, + }; tscColumnListInsert(pQueryInfo->colList, PRIMARYKEY_TIMESTAMP_COL_INDEX, pExpr->base.uid, &s); // if it is not in the final result, do not add it @@ -2646,11 +2669,11 @@ void setResultColName(char* name, tSqlExprItem* pItem, int32_t functionId, SStrT if (pItem->aliasName != NULL) { tstrncpy(name, pItem->aliasName, TSDB_COL_NAME_LEN); } else { - char uname[TSDB_COL_NAME_LEN] = {0}; + char uname[TSDB_COL_NAME_LEN] = {0}; int32_t len = MIN(pToken->n + 1, TSDB_COL_NAME_LEN); tstrncpy(uname, pToken->z, len); - if (tsKeepOriginalColumnName) { // keep the original column name + if (tsKeepOriginalColumnName) { // keep the original column name tstrncpy(name, uname, TSDB_COL_NAME_LEN); } else if (multiCols) { if (!TSDB_FUNC_IS_SCALAR(functionId)) { @@ -2667,7 +2690,7 @@ void setResultColName(char* name, tSqlExprItem* pItem, int32_t functionId, SStrT tstrncpy(name, tmp, TSDB_COL_NAME_LEN); } - } else { // use the user-input result column name + } else { // use the user-input result column name len = MIN(pItem->pNode->exprToken.n + 1, TSDB_COL_NAME_LEN); tstrncpy(name, pItem->pNode->exprToken.z, len); } @@ -2675,9 +2698,7 @@ void setResultColName(char* name, tSqlExprItem* pItem, int32_t functionId, SStrT } static void updateLastScanOrderIfNeeded(SQueryInfo* pQueryInfo) { - if (pQueryInfo->sessionWindow.gap > 0 || - pQueryInfo->stateWindow || - taosArrayGetSize(pQueryInfo->pUpstream) > 0 || + if (pQueryInfo->sessionWindow.gap > 0 || pQueryInfo->stateWindow || taosArrayGetSize(pQueryInfo->pUpstream) > 0 || tscGroupbyColumn(pQueryInfo)) { size_t numOfExpr = tscNumOfExprs(pQueryInfo); for (int32_t i = 0; i < numOfExpr; ++i) { @@ -2699,13 +2720,13 @@ static UNUSED_FUNC void updateFunctionInterBuf(SQueryInfo* pQueryInfo, bool supe SExprInfo* pExpr = tscExprGet(pQueryInfo, i); int32_t param = (int32_t)pExpr->base.param[0].i64; - getResultDataInfo(pExpr->base.colType, pExpr->base.colBytes, pExpr->base.functionId, param, &pExpr->base.resType, &pExpr->base.resBytes, - &pExpr->base.interBytes, 0, superTable, pUdfInfo); + getResultDataInfo(pExpr->base.colType, pExpr->base.colBytes, pExpr->base.functionId, param, &pExpr->base.resType, + &pExpr->base.resBytes, &pExpr->base.interBytes, 0, superTable, pUdfInfo); } } -int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t colIndex, tSqlExprItem* pItem, bool finalResult, - SUdfInfo* pUdfInfo) { +int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t colIndex, tSqlExprItem* pItem, + bool finalResult, SUdfInfo* pUdfInfo) { STableMetaInfo* pTableMetaInfo = NULL; int32_t functionId = pItem->pNode->functionId; @@ -2841,7 +2862,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col case TSDB_FUNC_MODE: case TSDB_FUNC_STATE_COUNT: case TSDB_FUNC_STATE_DURATION: - case TSDB_FUNC_HYPERLOGLOG:{ + case TSDB_FUNC_HYPERLOGLOG: { // 1. valid the number of parameters int32_t numOfParams = (pItem->pNode->Expr.paramList == NULL) ? 0 : (int32_t)taosArrayGetSize(pItem->pNode->Expr.paramList); @@ -2849,7 +2870,8 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col // no parameters or more than one parameter for function if (pItem->pNode->Expr.paramList == NULL || (functionId != TSDB_FUNC_LEASTSQR && functionId != TSDB_FUNC_DERIVATIVE && functionId != TSDB_FUNC_ELAPSED && - functionId != TSDB_FUNC_DIFF && functionId != TSDB_FUNC_STATE_COUNT && functionId != TSDB_FUNC_STATE_DURATION && numOfParams != 1) || + functionId != TSDB_FUNC_DIFF && functionId != TSDB_FUNC_STATE_COUNT && + functionId != TSDB_FUNC_STATE_DURATION && numOfParams != 1) || ((functionId == TSDB_FUNC_LEASTSQR || functionId == TSDB_FUNC_DERIVATIVE) && numOfParams != 3) || (functionId == TSDB_FUNC_ELAPSED && numOfParams != 1 && numOfParams != 2) || (functionId == TSDB_FUNC_DIFF && numOfParams != 1 && numOfParams != 2) || @@ -2910,7 +2932,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, index.columnIndex); if (functionId == TSDB_FUNC_MODE && pColumnSchema->colId == PRIMARYKEY_TIMESTAMP_COL_INDEX && - pColumnSchema->type == TSDB_DATA_TYPE_TIMESTAMP){ + pColumnSchema->type == TSDB_DATA_TYPE_TIMESTAMP) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg29); } else if (!IS_NUMERIC_TYPE(pSchema->type) && (functionId != TSDB_FUNC_ELAPSED) && (functionId != TSDB_FUNC_MODE) && (functionId != TSDB_FUNC_HYPERLOGLOG)) { @@ -2943,17 +2965,17 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col if (functionId == TSDB_FUNC_STATE_COUNT || functionId == TSDB_FUNC_STATE_DURATION) { SColumnIndex indexTS = {.tableIndex = index.tableIndex, .columnIndex = 0}; - SExprInfo* pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_PRJ, &indexTS, TSDB_DATA_TYPE_TIMESTAMP, - TSDB_KEYSIZE, 0, TSDB_KEYSIZE, false); + SExprInfo* pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_PRJ, &indexTS, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, 0, + TSDB_KEYSIZE, false); tstrncpy(pExpr->base.aliasName, aAggs[TSDB_FUNC_TS_DUMMY].name, sizeof(pExpr->base.aliasName)); SColumnList ids = createColumnList(1, 0, 0); - insertResultField(pQueryInfo, colIndex, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, - aAggs[TSDB_FUNC_TS].name, pExpr); + insertResultField(pQueryInfo, colIndex, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS].name, + pExpr); - pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_PRJ, &index, pSchema->type, - pSchema->bytes, getNewResColId(pCmd), 0, false); - tstrncpy(pExpr->base.aliasName, pParamElem->pNode->columnName.z, pParamElem->pNode->columnName.n+1); + pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_PRJ, &index, pSchema->type, pSchema->bytes, getNewResColId(pCmd), 0, + false); + tstrncpy(pExpr->base.aliasName, pParamElem->pNode->columnName.z, pParamElem->pNode->columnName.n + 1); ids = createColumnList(1, index.tableIndex, index.columnIndex); insertResultField(pQueryInfo, colIndex + 1, &ids, pExpr->base.resBytes, (int32_t)pExpr->base.resType, pExpr->base.aliasName, pExpr); @@ -3038,21 +3060,25 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col } tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t)); } else if (functionId == TSDB_FUNC_STATE_COUNT || functionId == TSDB_FUNC_STATE_DURATION) { - if (pParamElem[1].pNode->tokenId != TK_ID || !isValidStateOper(pParamElem[1].pNode->columnName.z, pParamElem[1].pNode->columnName.n)) { + if (pParamElem[1].pNode->tokenId != TK_ID || + !isValidStateOper(pParamElem[1].pNode->columnName.z, pParamElem[1].pNode->columnName.n)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } - tscExprAddParams(&pExpr->base, pParamElem[1].pNode->columnName.z, TSDB_DATA_TYPE_BINARY, pParamElem[1].pNode->columnName.n); + tscExprAddParams(&pExpr->base, pParamElem[1].pNode->columnName.z, TSDB_DATA_TYPE_BINARY, + pParamElem[1].pNode->columnName.n); if (pParamElem[2].pNode->tokenId != TK_INTEGER && pParamElem[2].pNode->tokenId != TK_FLOAT) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } tVariantAssign(&pExpr->base.param[pExpr->base.numOfParams++], &pParamElem[2].pNode->value); - if (functionId == TSDB_FUNC_STATE_DURATION){ + if (functionId == TSDB_FUNC_STATE_DURATION) { if (numOfParams == 4) { // unit must be 1s 1m 1h - if (pParamElem[3].pNode->tokenId != TK_TIMESTAMP || (pParamElem[3].pNode->value.i64 != MILLISECOND_PER_SECOND * 1000000L && - pParamElem[3].pNode->value.i64 != MILLISECOND_PER_MINUTE * 1000000L && pParamElem[3].pNode->value.i64 != MILLISECOND_PER_HOUR * 1000000L)) { + if (pParamElem[3].pNode->tokenId != TK_TIMESTAMP || + (pParamElem[3].pNode->value.i64 != MILLISECOND_PER_SECOND * 1000000L && + pParamElem[3].pNode->value.i64 != MILLISECOND_PER_MINUTE * 1000000L && + pParamElem[3].pNode->value.i64 != MILLISECOND_PER_HOUR * 1000000L)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } if (info.precision == TSDB_TIME_PRECISION_MILLI) { @@ -3062,14 +3088,14 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col } tVariantAssign(&pExpr->base.param[pExpr->base.numOfParams++], &pParamElem[3].pNode->value); - }else{ + } else { int64_t tmp = TSDB_TICK_PER_SECOND(TSDB_TIME_PRECISION_NANO); if (info.precision == TSDB_TIME_PRECISION_MILLI) { tmp /= TSDB_TICK_PER_SECOND(TSDB_TIME_PRECISION_MICRO); } else if (info.precision == TSDB_TIME_PRECISION_MICRO) { tmp /= TSDB_TICK_PER_SECOND(TSDB_TIME_PRECISION_MILLI); } - tscExprAddParams(&pExpr->base, (char *)&tmp, TSDB_DATA_TYPE_BIGINT, sizeof(tmp)); + tscExprAddParams(&pExpr->base, (char*)&tmp, TSDB_DATA_TYPE_BIGINT, sizeof(tmp)); } } } @@ -3096,10 +3122,12 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col case TSDB_FUNC_LAST: case TSDB_FUNC_SPREAD: case TSDB_FUNC_LAST_ROW: - case TSDB_FUNC_INTERP: { // link to TD-14196, if add FUNC in this place, please search TD-14196, because there are connection in exprTreeFromSqlExpr + case TSDB_FUNC_INTERP: { // link to TD-14196, if add FUNC in this place, please search TD-14196, because there are + // connection in exprTreeFromSqlExpr bool requireAllFields = (pItem->pNode->Expr.paramList == NULL); - // NOTE: has time range condition or normal column filter condition, the last_row query will be transferred to last query + // NOTE: has time range condition or normal column filter condition, the last_row query will be transferred to + // last query SConvertFunc cvtFunc = {.originFuncId = functionId, .execFuncId = functionId}; if (functionId == TSDB_FUNC_LAST_ROW && ((!TSWINDOW_IS_EQUAL(pQueryInfo->window, TSWINDOW_INITIALIZER)) || (hasNormalColumnFilter(pQueryInfo)) || @@ -3227,7 +3255,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col if (cnt != 2 && cnt != 3) valid = false; } else if (functionId == TSDB_FUNC_UNIQUE) { if (taosArrayGetSize(pItem->pNode->Expr.paramList) != 1) valid = false; - }else { + } else { if (taosArrayGetSize(pItem->pNode->Expr.paramList) != 2) valid = false; } if (!valid) { @@ -3263,7 +3291,8 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col } // 2. valid the column type - if (functionId != TSDB_FUNC_SAMPLE && functionId != TSDB_FUNC_UNIQUE && functionId != TSDB_FUNC_TAIL && !IS_NUMERIC_TYPE(pSchema->type)) { + if (functionId != TSDB_FUNC_SAMPLE && functionId != TSDB_FUNC_UNIQUE && functionId != TSDB_FUNC_TAIL && + !IS_NUMERIC_TYPE(pSchema->type)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } @@ -3300,15 +3329,15 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col false, pUdfInfo); /* - * sql function transformation - * for dp = 0, it is actually min, - * for dp = 100, it is max, + * sql function transformation + * for dp = 0, it is actually min, + * for dp = 100, it is max, */ tscInsertPrimaryTsSourceColumn(pQueryInfo, pTableMetaInfo->pTableMeta->id.uid); colIndex += 1; // the first column is ts - pExpr = tscExprAppend(pQueryInfo, functionId, &index, resultType, resultSize, getNewResColId(pCmd), - interResult, false); + pExpr = tscExprAppend(pQueryInfo, functionId, &index, resultType, resultSize, getNewResColId(pCmd), interResult, + false); tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_DOUBLE, sizeof(double)); // param2 int32 @@ -3364,15 +3393,15 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col colIndex += 1; // the first column is ts - getResultDataInfo(pSchema->type, pSchema->bytes, functionId, (int32_t)numRowsSelected, &resultType, - &resultSize, &interResult, 0, false, pUdfInfo); - pExpr = tscExprAppend(pQueryInfo, functionId, &index, resultType, resultSize, getNewResColId(pCmd), - interResult, false); + getResultDataInfo(pSchema->type, pSchema->bytes, functionId, (int32_t)numRowsSelected, &resultType, &resultSize, + &interResult, 0, false, pUdfInfo); + pExpr = tscExprAppend(pQueryInfo, functionId, &index, resultType, resultSize, getNewResColId(pCmd), interResult, + false); tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t)); } else { tVariantDump(pVariant, val, TSDB_DATA_TYPE_BIGINT, true); - if(functionId == TSDB_FUNC_UNIQUE){ // consider of memory size + if (functionId == TSDB_FUNC_UNIQUE) { // consider of memory size GET_INT64_VAL(val) = MAX_UNIQUE_RESULT_ROWS; } @@ -3388,17 +3417,17 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col const int32_t TS_COLUMN_INDEX = PRIMARYKEY_TIMESTAMP_COL_INDEX; SColumnList ids = createColumnList(1, index.tableIndex, TS_COLUMN_INDEX); - insertResultField(pQueryInfo, colIndex, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, - aAggs[TSDB_FUNC_TS].name, pExpr); + insertResultField(pQueryInfo, colIndex, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS].name, + pExpr); colIndex += 1; // the first column is ts - getResultDataInfo(pSchema->type, pSchema->bytes, functionId, (int32_t)numRowsSelected, &resultType, - &resultSize, &interResult, 0, false, pUdfInfo); - pExpr = tscExprAppend(pQueryInfo, functionId, &index, resultType, resultSize, getNewResColId(pCmd), - interResult, false); - if (functionId == TSDB_FUNC_TAIL){ + getResultDataInfo(pSchema->type, pSchema->bytes, functionId, (int32_t)numRowsSelected, &resultType, &resultSize, + &interResult, 0, false, pUdfInfo); + pExpr = tscExprAppend(pQueryInfo, functionId, &index, resultType, resultSize, getNewResColId(pCmd), interResult, + false); + if (functionId == TSDB_FUNC_TAIL) { int64_t offset = 0; - if (taosArrayGetSize(pItem->pNode->Expr.paramList) == 3){ + if (taosArrayGetSize(pItem->pNode->Expr.paramList) == 3) { tSqlExprItem* para = taosArrayGet(pItem->pNode->Expr.paramList, 2); if (para->pNode->tokenId == TK_ID || para->pNode->value.nType != TSDB_DATA_TYPE_BIGINT) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); @@ -3412,7 +3441,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t)); GET_INT64_VAL(val) = offset; tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t)); - }else{ + } else { tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t)); } } @@ -3577,9 +3606,9 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } - #define USER_INPUT_BIN 0 - #define LINEAR_BIN 1 - #define LOG_BIN 2 +#define USER_INPUT_BIN 0 +#define LINEAR_BIN 1 +#define LOG_BIN 2 int8_t binType; if (strcasecmp(pVariant->pz, "user_input") == 0) { binType = USER_INPUT_BIN; @@ -3737,8 +3766,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col } pVariant = &pParamElem[3].pNode->value; - if (pVariant == NULL || pVariant->nType != TSDB_DATA_TYPE_BIGINT || - (pVariant->i64 != 0 && pVariant->i64 != 1)) { + if (pVariant == NULL || pVariant->nType != TSDB_DATA_TYPE_BIGINT || (pVariant->i64 != 0 && pVariant->i64 != 1)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -3847,8 +3875,8 @@ void getColumnName(tSqlExprItem* pItem, char* resultFieldName, char* rawName, in strncpy(rawName, pItem->pNode->exprToken.z, len); if (pItem->aliasName != NULL) { - int32_t aliasNameLen = (int32_t) strlen(pItem->aliasName); - len = (aliasNameLen < nameLength)? aliasNameLen:nameLength; + int32_t aliasNameLen = (int32_t)strlen(pItem->aliasName); + len = (aliasNameLen < nameLength) ? aliasNameLen : nameLength; strncpy(resultFieldName, pItem->aliasName, len); } else { strncpy(resultFieldName, rawName, len); @@ -3863,7 +3891,7 @@ static bool isTablenameToken(SStrToken* token) { return (tmpToken.n == strlen(TSQL_TBNAME_L) && strncasecmp(TSQL_TBNAME_L, tmpToken.z, tmpToken.n) == 0); } -static bool isTimeWindowToken(SStrToken* token, int16_t *columnIndex) { +static bool isTimeWindowToken(SStrToken* token, int16_t* columnIndex) { SStrToken tmpToken = *token; SStrToken tableToken = {0}; @@ -3874,7 +3902,8 @@ static bool isTimeWindowToken(SStrToken* token, int16_t *columnIndex) { } else if (tmpToken.n == strlen(TSQL_TSWIN_STOP) && strncasecmp(TSQL_TSWIN_STOP, tmpToken.z, tmpToken.n) == 0) { *columnIndex = TSDB_TSWIN_STOP_COLUMN_INDEX; return true; - } else if (tmpToken.n == strlen(TSQL_TSWIN_DURATION) && strncasecmp(TSQL_TSWIN_DURATION, tmpToken.z, tmpToken.n) == 0) { + } else if (tmpToken.n == strlen(TSQL_TSWIN_DURATION) && + strncasecmp(TSQL_TSWIN_DURATION, tmpToken.z, tmpToken.n) == 0) { *columnIndex = TSDB_TSWIN_DURATION_COLUMN_INDEX; return true; } else if (tmpToken.n == strlen(TSQL_QUERY_START) && strncasecmp(TSQL_QUERY_START, tmpToken.z, tmpToken.n) == 0) { @@ -3883,7 +3912,8 @@ static bool isTimeWindowToken(SStrToken* token, int16_t *columnIndex) { } else if (tmpToken.n == strlen(TSQL_QUERY_STOP) && strncasecmp(TSQL_QUERY_STOP, tmpToken.z, tmpToken.n) == 0) { *columnIndex = TSDB_QUERY_STOP_COLUMN_INDEX; return true; - } else if (tmpToken.n == strlen(TSQL_QUERY_DURATION) && strncasecmp(TSQL_QUERY_DURATION, tmpToken.z, tmpToken.n) == 0) { + } else if (tmpToken.n == strlen(TSQL_QUERY_DURATION) && + strncasecmp(TSQL_QUERY_DURATION, tmpToken.z, tmpToken.n) == 0) { *columnIndex = TSDB_QUERY_DURATION_COLUMN_INDEX; return true; } else { @@ -3899,9 +3929,9 @@ static int16_t doGetColumnIndex(SQueryInfo* pQueryInfo, int32_t index, SStrToken int16_t columnIndex = COLUMN_INDEX_INITIAL_VAL; - char tmpTokenBuf[TSDB_MAX_BYTES_PER_ROW] = {0}; // create tmp buf to avoid alter orginal sqlstr + char tmpTokenBuf[TSDB_MAX_BYTES_PER_ROW] = {0}; // create tmp buf to avoid alter orginal sqlstr strncpy(tmpTokenBuf, pToken->z, pToken->n); - + pToken->z = tmpTokenBuf; pToken->n = stringProcess(pToken->z, pToken->n); @@ -3931,8 +3961,9 @@ int32_t doGetColumnIndexByName(SStrToken* pToken, SQueryInfo* pQueryInfo, SColum if (isTablenameToken(pToken)) { pIndex->columnIndex = TSDB_TBNAME_COLUMN_INDEX; } 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_INDEX; // just make runtime happy, need fix java test case InsertSpecialCharacterJniTest + strncasecmp(pToken->z, DEFAULT_PRIMARY_TIMESTAMP_COL_NAME, pToken->n) == 0) { + pIndex->columnIndex = PRIMARYKEY_TIMESTAMP_COL_INDEX; // just make runtime happy, need fix java test case + // InsertSpecialCharacterJniTest } else if (isTimeWindowToken(pToken, &tsWinColumnIndex)) { pIndex->columnIndex = tsWinColumnIndex; } else { @@ -3983,7 +4014,7 @@ int32_t getTableIndexImpl(SStrToken* pTableToken, SQueryInfo* pQueryInfo, SColum pIndex->tableIndex = COLUMN_INDEX_INITIAL_VAL; for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, i); - char* name = pTableMetaInfo->aliasName; + char* name = pTableMetaInfo->aliasName; if (strncasecmp(name, pTableToken->z, pTableToken->n) == 0 && strlen(name) == pTableToken->n) { pIndex->tableIndex = i; break; @@ -4027,7 +4058,7 @@ int32_t getColumnIndexByName(const SStrToken* pToken, SQueryInfo* pQueryInfo, SC int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { SSqlCmd* pCmd = &pSql->cmd; - STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0); + STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0); pCmd->command = TSDB_SQL_SHOW; @@ -4047,7 +4078,6 @@ int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { // db prefix in tagCond, show table conds in payload SStrToken* pDbPrefixToken = &pShowInfo->prefix; if (pDbPrefixToken->type != 0) { - if (pDbPrefixToken->n >= TSDB_DB_NAME_LEN) { // db name is too long return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } @@ -4074,7 +4104,7 @@ int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg6); } - if (pPattern->n > tsMaxWildCardsLen){ + if (pPattern->n > tsMaxWildCardsLen) { char tmp[64] = {0}; sprintf(tmp, msg2, tsMaxWildCardsLen); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), tmp); @@ -4092,7 +4122,7 @@ int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { if (pShowInfo->prefix.n <= 0) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg6); } - } + } return TSDB_CODE_SUCCESS; } @@ -4103,7 +4133,7 @@ int32_t setKillInfo(SSqlObj* pSql, struct SSqlInfo* pInfo, int32_t killType) { SSqlCmd* pCmd = &pSql->cmd; pCmd->command = pInfo->type; - + SStrToken* idStr = &(pInfo->pMiscInfo->id); if (idStr->n > TSDB_KILL_MSG_LEN) { return TSDB_CODE_TSC_INVALID_OPERATION; @@ -4114,8 +4144,8 @@ int32_t setKillInfo(SSqlObj* pSql, struct SSqlInfo* pInfo, int32_t killType) { strncpy(msg->queryId, idStr->z, idStr->n); const char delim = ':'; - char* connIdStr = strtok(idStr->z, &delim); - char* queryIdStr = strtok(NULL, &delim); + char* connIdStr = strtok(idStr->z, &delim); + char* queryIdStr = strtok(NULL, &delim); int32_t connId = (int32_t)strtol(connIdStr, NULL, 10); if (connId <= 0) { @@ -4136,7 +4166,7 @@ int32_t setKillInfo(SSqlObj* pSql, struct SSqlInfo* pInfo, int32_t killType) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } } - + return TSDB_CODE_SUCCESS; } static int32_t setCompactVnodeInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { @@ -4158,31 +4188,26 @@ int32_t tscTansformFuncForSTableQuery(SQueryInfo* pQueryInfo) { int32_t bytes = 0; int16_t type = 0; int32_t interBytes = 0; - + size_t size = tscNumOfExprs(pQueryInfo); for (int32_t k = 0; k < size; ++k) { - SExprInfo* pExpr = tscExprGet(pQueryInfo, k); + SExprInfo* pExpr = tscExprGet(pQueryInfo, k); int16_t functionId = pExpr->base.functionId; if (!TSDB_FUNC_IS_SCALAR(functionId)) { functionId = aAggs[pExpr->base.functionId].stableFuncId; } - int32_t colIndex = pExpr->base.colInfo.colIndex; + int32_t colIndex = pExpr->base.colInfo.colIndex; SSchema* pSrcSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, colIndex); - + if ((functionId >= TSDB_FUNC_SUM && functionId <= TSDB_FUNC_TWA) || (functionId >= TSDB_FUNC_FIRST_DST && functionId <= TSDB_FUNC_STDDEV_DST) || - (functionId >= TSDB_FUNC_RATE && functionId <= TSDB_FUNC_IRATE) || - (functionId == TSDB_FUNC_SAMPLE) || - (functionId == TSDB_FUNC_ELAPSED) || - (functionId == TSDB_FUNC_HISTOGRAM) || - (functionId == TSDB_FUNC_UNIQUE) || - (functionId == TSDB_FUNC_MODE) || - (functionId == TSDB_FUNC_TAIL) || - (functionId == TSDB_FUNC_HYPERLOGLOG)) { - if (getResultDataInfo(pSrcSchema->type, pSrcSchema->bytes, functionId, (int32_t)pExpr->base.param[0].i64, &type, &bytes, - &interBytes, 0, true, NULL) != TSDB_CODE_SUCCESS) { + (functionId >= TSDB_FUNC_RATE && functionId <= TSDB_FUNC_IRATE) || (functionId == TSDB_FUNC_SAMPLE) || + (functionId == TSDB_FUNC_ELAPSED) || (functionId == TSDB_FUNC_HISTOGRAM) || (functionId == TSDB_FUNC_UNIQUE) || + (functionId == TSDB_FUNC_MODE) || (functionId == TSDB_FUNC_TAIL) || (functionId == TSDB_FUNC_HYPERLOGLOG)) { + if (getResultDataInfo(pSrcSchema->type, pSrcSchema->bytes, functionId, (int32_t)pExpr->base.param[0].i64, &type, + &bytes, &interBytes, 0, true, NULL) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; } @@ -4202,16 +4227,16 @@ void tscRestoreFuncForSTableQuery(SQueryInfo* pQueryInfo) { if (!UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { return; } - + size_t size = tscNumOfExprs(pQueryInfo); for (int32_t i = 0; i < size; ++i) { - SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, pExpr->base.colInfo.colIndex); - + SExprInfo* pExpr = tscExprGet(pQueryInfo, i); + SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, pExpr->base.colInfo.colIndex); + // the final result size and type in the same as query on single table. // so here, set the flag to be false; int32_t inter = 0; - + int32_t functionId = pExpr->base.functionId; if (functionId < 0) { continue; @@ -4220,7 +4245,7 @@ void tscRestoreFuncForSTableQuery(SQueryInfo* pQueryInfo) { if (functionId >= TSDB_FUNC_TS && functionId <= TSDB_FUNC_DIFF) { continue; } - + if (functionId == TSDB_FUNC_FIRST_DST) { functionId = TSDB_FUNC_FIRST; } else if (functionId == TSDB_FUNC_LAST_DST) { @@ -4235,8 +4260,10 @@ void tscRestoreFuncForSTableQuery(SQueryInfo* pQueryInfo) { } bool hasUnsupportFunctionsForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { - const char* msg1 = "TWA/Diff/Derivative/Irate/CSUM/MAVG/SAMPLE/INTERP/Elapsed are not allowed to apply to super table directly"; - const char* msg2 = "TWA/Diff/Derivative/Irate/CSUM/MAVG/SAMPLE/INTERP/Elapsed only support group by tbname for super table query"; + const char* msg1 = + "TWA/Diff/Derivative/Irate/CSUM/MAVG/SAMPLE/INTERP/Elapsed are not allowed to apply to super table directly"; + const char* msg2 = + "TWA/Diff/Derivative/Irate/CSUM/MAVG/SAMPLE/INTERP/Elapsed only support group by tbname for super table query"; const char* msg3 = "functions not support for super table query"; // filter sql function not supported by metric query yet. @@ -4258,7 +4285,8 @@ bool hasUnsupportFunctionsForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) } if (tscIsTWAQuery(pQueryInfo) || tscIsDiffDerivLikeQuery(pQueryInfo) || tscIsIrateQuery(pQueryInfo) || - tscQueryContainsFunction(pQueryInfo, TSDB_FUNC_SAMPLE) || tscGetPointInterpQuery(pQueryInfo) || tscQueryContainsFunction(pQueryInfo, TSDB_FUNC_ELAPSED)) { + tscQueryContainsFunction(pQueryInfo, TSDB_FUNC_SAMPLE) || tscGetPointInterpQuery(pQueryInfo) || + tscQueryContainsFunction(pQueryInfo, TSDB_FUNC_ELAPSED)) { if (pQueryInfo->groupbyExpr.numOfGroupCols == 0) { invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); return true; @@ -4274,7 +4302,9 @@ bool hasUnsupportFunctionsForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); return true; } - } else if (tscIsSessionWindowQuery(pQueryInfo)) { + } + + if (tscIsSessionWindowQuery(pQueryInfo)) { invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); return true; } @@ -4283,8 +4313,7 @@ bool hasUnsupportFunctionsForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) } static bool groupbyTagsOrNull(SQueryInfo* pQueryInfo) { - if (pQueryInfo->groupbyExpr.columnInfo == NULL || - taosArrayGetSize(pQueryInfo->groupbyExpr.columnInfo) == 0) { + if (pQueryInfo->groupbyExpr.columnInfo == NULL || taosArrayGetSize(pQueryInfo->groupbyExpr.columnInfo) == 0) { return true; } @@ -4300,8 +4329,7 @@ static bool groupbyTagsOrNull(SQueryInfo* pQueryInfo) { } bool groupbyTbname(SQueryInfo* pQueryInfo) { - if (pQueryInfo->groupbyExpr.columnInfo == NULL || - taosArrayGetSize(pQueryInfo->groupbyExpr.columnInfo) == 0) { + if (pQueryInfo->groupbyExpr.columnInfo == NULL || taosArrayGetSize(pQueryInfo->groupbyExpr.columnInfo) == 0) { return false; } @@ -4316,10 +4344,6 @@ bool groupbyTbname(SQueryInfo* pQueryInfo) { return false; } - - - - static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool twQuery) { int32_t startIdx = 0; int32_t aggUdf = 0; @@ -4345,13 +4369,14 @@ static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool int16_t functionId = pExpr1->base.functionId; if (functionId < 0) { - SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, -1 * functionId - 1); - pUdfInfo->funcType == TSDB_UDF_TYPE_AGGREGATE ? ++aggUdf : ++scalarUdf; + SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, -1 * functionId - 1); + pUdfInfo->funcType == TSDB_UDF_TYPE_AGGREGATE ? ++aggUdf : ++scalarUdf; - continue; + continue; } - if (functionId == TSDB_FUNC_TAGPRJ || functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_TS || functionId == TSDB_FUNC_TS_DUMMY) { + if (functionId == TSDB_FUNC_TAGPRJ || functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_TS || + functionId == TSDB_FUNC_TS_DUMMY) { ++prjNum; continue; @@ -4373,7 +4398,8 @@ static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool ++queryWinNum; } - if (functionId == TSDB_FUNC_PRJ && (pExpr1->base.colInfo.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX || TSDB_COL_IS_UD_COL(pExpr1->base.colInfo.flag))) { + if (functionId == TSDB_FUNC_PRJ && (pExpr1->base.colInfo.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX || + TSDB_COL_IS_UD_COL(pExpr1->base.colInfo.flag))) { continue; } @@ -4389,7 +4415,7 @@ static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool if (funcCompatFactor != factor) { return false; } else { - if (factor == -1) { // two functions with the same -1 flag + if (factor == -1) { // two functions with the same -1 flag return false; } } @@ -4399,7 +4425,8 @@ static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool return false; } - if (functionId == TSDB_FUNC_COUNT && (pExpr1->base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX || TSDB_COL_IS_TAG(pExpr1->base.colInfo.flag))) { + if (functionId == TSDB_FUNC_COUNT && + (pExpr1->base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX || TSDB_COL_IS_TAG(pExpr1->base.colInfo.flag))) { ++countTbname; } } @@ -4434,8 +4461,8 @@ int32_t validateGroupbyNode(SQueryInfo* pQueryInfo, SArray* pList, SSqlCmd* pCmd 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 = "not support group by expression"; - //const char* msg8 = "normal column can only locate at the end of group by clause"; + // const char* msg7 = "not support group by expression"; + // const char* msg8 = "normal column can only locate at the end of group by clause"; const char* msg9 = "json tag must be use ->'key'"; const char* msg10 = "non json column can not use ->'key'"; const char* msg11 = "group by json->'key' is too long"; @@ -4468,22 +4495,22 @@ int32_t validateGroupbyNode(SQueryInfo* pQueryInfo, SArray* pList, SSqlCmd* pCmd return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - SSchema *pSchema = NULL; - int32_t tableIndex = COLUMN_INDEX_INITIAL_VAL; - int32_t numOfGroupCols = 0; + SSchema* pSchema = NULL; + int32_t tableIndex = COLUMN_INDEX_INITIAL_VAL; + int32_t numOfGroupCols = 0; size_t num = taosArrayGetSize(pList); for (int32_t i = 0; i < num; ++i) { - CommonItem * pItem = taosArrayGet(pList, i); - SStrToken token = {0}; - if(pItem->isJsonExp){ + CommonItem* pItem = taosArrayGet(pList, i); + SStrToken token = {0}; + if (pItem->isJsonExp) { assert(pItem->jsonExp->tokenId == TK_ARROW); token = pItem->jsonExp->pLeft->columnName; - }else { + } else { token.n = pItem->pVar.nLen; token.z = pItem->pVar.pz; token.type = pItem->pVar.nType; - if (pItem->pVar.nType != TSDB_DATA_TYPE_BINARY){ + if (pItem->pVar.nType != TSDB_DATA_TYPE_BINARY) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } } @@ -4508,15 +4535,15 @@ int32_t validateGroupbyNode(SQueryInfo* pQueryInfo, SArray* pList, SSqlCmd* pCmd pSchema = tscGetTableColumnSchema(pTableMeta, index.columnIndex); } - if (pSchema->type == TSDB_DATA_TYPE_JSON && !pItem->isJsonExp){ + if (pSchema->type == TSDB_DATA_TYPE_JSON && !pItem->isJsonExp) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg9); } - if (pSchema->type != TSDB_DATA_TYPE_JSON && pItem->isJsonExp){ + if (pSchema->type != TSDB_DATA_TYPE_JSON && pItem->isJsonExp) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg10); } int32_t numOfCols = tscGetNumOfColumns(pTableMeta); - bool groupTag = (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX || index.columnIndex >= numOfCols); + bool groupTag = (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX || index.columnIndex >= numOfCols); if (groupTag) { if (!UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { @@ -4528,13 +4555,17 @@ int32_t validateGroupbyNode(SQueryInfo* pQueryInfo, SArray* pList, SSqlCmd* pCmd relIndex -= numOfCols; } - SColIndex colIndex = { .colIndex = relIndex, .flag = TSDB_COL_TAG, .colId = pSchema->colId, }; - if(pItem->isJsonExp) { - if(pItem->jsonExp->exprToken.n >= tListLen(colIndex.name)){ + SColIndex colIndex = { + .colIndex = relIndex, + .flag = TSDB_COL_TAG, + .colId = pSchema->colId, + }; + if (pItem->isJsonExp) { + if (pItem->jsonExp->exprToken.n >= tListLen(colIndex.name)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg11); } tstrncpy(colIndex.name, pItem->jsonExp->exprToken.z, pItem->jsonExp->exprToken.n + 1); - }else{ + } else { tstrncpy(colIndex.name, pSchema->name, tListLen(colIndex.name)); } @@ -4550,7 +4581,7 @@ int32_t validateGroupbyNode(SQueryInfo* pQueryInfo, SArray* pList, SSqlCmd* pCmd tscColumnListInsert(pQueryInfo->colList, index.columnIndex, pTableMeta->id.uid, pSchema); - SColIndex colIndex = { .colIndex = index.columnIndex, .flag = TSDB_COL_NORMAL, .colId = pSchema->colId }; + SColIndex colIndex = {.colIndex = index.columnIndex, .flag = TSDB_COL_NORMAL, .colId = pSchema->colId}; strncpy(colIndex.name, pSchema->name, tListLen(colIndex.name)); taosArrayPush(pGroupExpr->columnInfo, &colIndex); @@ -4563,11 +4594,10 @@ int32_t validateGroupbyNode(SQueryInfo* pQueryInfo, SArray* pList, SSqlCmd* pCmd return TSDB_CODE_SUCCESS; } - static SColumnFilterInfo* addColumnFilterInfo(SColumnFilterList* filterList) { int32_t size = (filterList->numOfFilters) + 1; - char* tmp = (char*) realloc((void*)(filterList->filterInfo), sizeof(SColumnFilterInfo) * (size)); + char* tmp = (char*)realloc((void*)(filterList->filterInfo), sizeof(SColumnFilterInfo) * (size)); if (tmp != NULL) { filterList->filterInfo = (SColumnFilterInfo*)tmp; } else { @@ -4582,11 +4612,11 @@ static SColumnFilterInfo* addColumnFilterInfo(SColumnFilterList* filterList) { return pColFilterInfo; } -static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t timePrecision, SColumnFilterInfo* pColumnFilter, - int16_t colType, tSqlExpr* pExpr) { +static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t timePrecision, + SColumnFilterInfo* pColumnFilter, int16_t colType, tSqlExpr* pExpr) { const char* msg = "not supported filter condition"; - tSqlExpr *pRight = pExpr->pRight; + tSqlExpr* pRight = pExpr->pRight; if (colType >= TSDB_DATA_TYPE_TINYINT && colType <= TSDB_DATA_TYPE_BIGINT) { colType = TSDB_DATA_TYPE_BIGINT; @@ -4599,8 +4629,7 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, } } else if ((colType == TSDB_DATA_TYPE_TIMESTAMP) && (TSDB_DATA_TYPE_BIGINT == pRight->value.nType)) { if (pRight->flags & (1 << EXPR_FLAG_NS_TIMESTAMP)) { - pRight->value.i64 = - convertTimePrecision(pRight->value.i64, TSDB_TIME_PRECISION_NANO, timePrecision); + pRight->value.i64 = convertTimePrecision(pRight->value.i64, TSDB_TIME_PRECISION_NANO, timePrecision); pRight->flags &= ~(1 << EXPR_FLAG_NS_TIMESTAMP); } } @@ -4622,15 +4651,16 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, } if (pExpr->tokenId == TK_IN) { - tVariant *pVal; - if (pRight->tokenId != TK_SET || !serializeExprListToVariant(pRight->Expr.paramList, &pVal, colType, timePrecision)) { + tVariant* pVal; + if (pRight->tokenId != TK_SET || + !serializeExprListToVariant(pRight->Expr.paramList, &pVal, colType, timePrecision)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } - pColumnFilter->pz = (int64_t)calloc(1, pVal->nLen); + pColumnFilter->pz = (int64_t)calloc(1, pVal->nLen); pColumnFilter->len = pVal->nLen; pColumnFilter->filterstr = 1; - memcpy((char *)(pColumnFilter->pz), (char *)(pVal->pz), pVal->nLen); + memcpy((char*)(pColumnFilter->pz), (char*)(pVal->pz), pVal->nLen); tVariantDestroy(pVal); free(pVal); @@ -4650,14 +4680,14 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, } else if (pExpr->tokenId == TK_LE || pExpr->tokenId == TK_LT) { retVal = tVariantDump(&pRight->value, (char*)&pColumnFilter->upperBndd, colType, false); - // TK_GT,TK_GE,TK_EQ,TK_NE are based on the pColumn->lowerBndd + // TK_GT,TK_GE,TK_EQ,TK_NE are based on the pColumn->lowerBndd } else { retVal = tVariantDump(&pRight->value, (char*)&pColumnFilter->lowerBndd, colType, false); } if (retVal != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); - } + } switch (pExpr->tokenId) { case TK_LE: @@ -4722,12 +4752,15 @@ enum { TSQL_EXPR_TAG = 2, TSQL_EXPR_COLUMN = 4, TSQL_EXPR_TBNAME = 8, - TSQL_EXPR_JOIN = 16, + TSQL_EXPR_JOIN = 16, }; -#define GET_MIXED_TYPE(t) (((t) >= TSQL_EXPR_JOIN) || ((t) > TSQL_EXPR_COLUMN && (t) < TSQL_EXPR_TBNAME) || ((t) == (TSQL_EXPR_TS|TSQL_EXPR_TAG))) +#define GET_MIXED_TYPE(t) \ + (((t) >= TSQL_EXPR_JOIN) || ((t) > TSQL_EXPR_COLUMN && (t) < TSQL_EXPR_TBNAME) || \ + ((t) == (TSQL_EXPR_TS | TSQL_EXPR_TAG))) -static int32_t checkColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SColumnIndex* pIndex, tSqlExpr* pExpr, int32_t sqlOptr) { +static int32_t checkColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SColumnIndex* pIndex, tSqlExpr* pExpr, + int32_t sqlOptr) { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, pIndex->tableIndex); STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; @@ -4740,7 +4773,7 @@ static int32_t checkColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCol SColumn* pColumn = tscColumnListInsert(pQueryInfo->colList, pIndex->columnIndex, pTableMeta->id.uid, pSchema); pColumn->info.flist.numOfFilters++; - + /* * in case of TK_AND filter condition, we first find the corresponding column and build the query condition together * the already existed condition. @@ -4751,15 +4784,14 @@ static int32_t checkColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCol SColumnFilterInfo* pColFilter = calloc(1, sizeof(SColumnFilterInfo)); - pColFilter->filterstr = - ((pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) ? 1 : 0); + pColFilter->filterstr = ((pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) ? 1 : 0); if (!pColFilter->filterstr) { if (pExpr->tokenId == TK_LIKE || pExpr->tokenId == TK_MATCH || pExpr->tokenId == TK_NMATCH) { ret = invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); goto _err_ret; } - + if (pSchema->type == TSDB_DATA_TYPE_BOOL) { int32_t t = pExpr->tokenId; if (t != TK_EQ && t != TK_NE && t != TK_NOTNULL && t != TK_ISNULL && t != TK_IN) { @@ -4772,7 +4804,7 @@ static int32_t checkColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCol pColumn->columnIndex = pIndex->columnIndex; pColumn->tableUid = pTableMeta->id.uid; if (pColumn->columnIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX && pExpr->tokenId == TK_IN) { - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } STableComInfo tinfo = tscGetTableInfo(pTableMeta); @@ -4780,13 +4812,13 @@ static int32_t checkColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCol _err_ret: freeColumnFilterInfo(pColFilter, 1); - + return ret; } static int32_t getColQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr** pExpr) { int32_t ret = TSDB_CODE_SUCCESS; - + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { tSqlExpr* p1 = extractExprForSTable(pCmd, pExpr, pQueryInfo, i); if (p1 == NULL) { // no query condition on this table @@ -4795,40 +4827,35 @@ static int32_t getColQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlEx tExprNode* p = NULL; - SArray* colList = taosArrayInit(10, sizeof(SColIndex)); + SArray* colList = taosArrayInit(10, sizeof(SColIndex)); ret = exprTreeFromSqlExpr(pCmd, &p, p1, pQueryInfo, colList, NULL); taosArrayDestroy(&colList); SBufferWriter bw = tbufInitWriter(NULL, false); - TRY(0) { - exprTreeToBinary(&bw, p); - } CATCH(code) { + TRY(0) { exprTreeToBinary(&bw, p); } + CATCH(code) { tbufCloseWriter(&bw); UNUSED(code); // TODO: more error handling - } END_TRY + } + END_TRY // add to required table column list STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, i); - int64_t uid = pTableMetaInfo->pTableMeta->id.uid; + int64_t uid = pTableMetaInfo->pTableMeta->id.uid; - STblCond cond = { - .uid = uid, - .idx = i, - .len = (int32_t)(tbufTell(&bw)), - .cond = tbufGetData(&bw, true) - }; + STblCond cond = {.uid = uid, .idx = i, .len = (int32_t)(tbufTell(&bw)), .cond = tbufGetData(&bw, true)}; if (pQueryInfo->colCond == NULL) { pQueryInfo->colCond = taosArrayInit(2, sizeof(STblCond)); } - - taosArrayPush(pQueryInfo->colCond, &cond); + + taosArrayPush(pQueryInfo->colCond, &cond); tSqlExprDestroy(p1); tExprTreeDestroy(p, NULL); - + if (ret) { break; } @@ -4837,7 +4864,6 @@ static int32_t getColQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlEx return ret; } - static int32_t checkColumnQueryCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr* pExpr, int32_t relOptr) { if (pExpr == NULL) { pQueryInfo->onlyHasTagCond &= true; @@ -4854,7 +4880,8 @@ static int32_t checkColumnQueryCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, t return checkColumnQueryCondInfo(pCmd, pQueryInfo, pExpr->pRight, pExpr->tokenId); } else { // handle leaf node SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByName(&pExpr->pLeft->columnName, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByName(&pExpr->pLeft->columnName, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != + TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; } @@ -4863,7 +4890,7 @@ static int32_t checkColumnQueryCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, t } static int32_t checkAndSetJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr* pExpr) { - int32_t code = 0; + int32_t code = 0; const char* msg1 = "timestamp required for join tables"; const char* msg2 = "only support one join tag for each table"; const char* msg3 = "type of join columns must be identical"; @@ -4884,10 +4911,10 @@ static int32_t checkAndSetJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tS tSqlExpr* pLeft = pExpr->pLeft; tSqlExpr* pRight = pExpr->pRight; - if(pLeft->tokenId == TK_ARROW){ + if (pLeft->tokenId == TK_ARROW) { pLeft = pLeft->pLeft; } - if(pRight->tokenId == TK_ARROW){ + if (pRight->tokenId == TK_ARROW) { pRight = pRight->pLeft; } @@ -4897,18 +4924,18 @@ static int32_t checkAndSetJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tS } STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); - SSchema* pTagSchema1 = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, index.columnIndex); + SSchema* pTagSchema1 = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, index.columnIndex); assert(index.tableIndex >= 0 && index.tableIndex < TSDB_MAX_JOIN_TABLE_NUM); - SJoinNode **leftNode = &pQueryInfo->tagCond.joinInfo.joinTables[index.tableIndex]; + SJoinNode** leftNode = &pQueryInfo->tagCond.joinInfo.joinTables[index.tableIndex]; if (*leftNode == NULL) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } (*leftNode)->uid = pTableMetaInfo->pTableMeta->id.uid; (*leftNode)->tagColId = pTagSchema1->colId; - if(pExpr->pLeft->tokenId == TK_ARROW) { + if (pExpr->pLeft->tokenId == TK_ARROW) { tstrncpy((*leftNode)->tagJsonKeyName, pExpr->pLeft->pRight->value.pz, TSDB_MAX_JSON_KEY_LEN + 1); } @@ -4938,14 +4965,14 @@ static int32_t checkAndSetJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tS assert(index.tableIndex >= 0 && index.tableIndex < TSDB_MAX_JOIN_TABLE_NUM); - SJoinNode **rightNode = &pQueryInfo->tagCond.joinInfo.joinTables[index.tableIndex]; + SJoinNode** rightNode = &pQueryInfo->tagCond.joinInfo.joinTables[index.tableIndex]; if (*rightNode == NULL) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } (*rightNode)->uid = pTableMetaInfo->pTableMeta->id.uid; (*rightNode)->tagColId = pTagSchema2->colId; - if(pExpr->pRight->tokenId == TK_ARROW) { + if (pExpr->pRight->tokenId == TK_ARROW) { tstrncpy((*rightNode)->tagJsonKeyName, pExpr->pRight->pRight->value.pz, TSDB_MAX_JSON_KEY_LEN + 1); } @@ -4953,10 +4980,9 @@ static int32_t checkAndSetJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tS STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; index.columnIndex = index.columnIndex - tscGetNumOfColumns(pTableMeta); if (tscColumnExists(pTableMetaInfo->tagColList, pTagSchema2->colId, pTableMeta->id.uid) < 0) { - tscColumnListInsert(pTableMetaInfo->tagColList, index.columnIndex, pTableMeta->id.uid, pTagSchema2); atomic_add_fetch_32(&pTableMetaInfo->joinTagNum, 1); - + if (pTableMetaInfo->joinTagNum > 1) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -4983,7 +5009,6 @@ static int32_t checkAndSetJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tS pQueryInfo->tagCond.joinInfo.hasJoin = true; return TSDB_CODE_SUCCESS; - } static int32_t getJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr* pExpr) { @@ -4995,23 +5020,24 @@ static int32_t getJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr* return checkAndSetJoinCondInfo(pCmd, pQueryInfo, pExpr); } -static int32_t validateSQLExprItemSQLFunc(SSqlCmd* pCmd, tSqlExpr* pExpr, - SQueryInfo* pQueryInfo, SColumnList* pList, int32_t* type, uint64_t *uid, int32_t* height) { - int32_t code = TSDB_CODE_SUCCESS; +static int32_t validateSQLExprItemSQLFunc(SSqlCmd* pCmd, tSqlExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList, + int32_t* type, uint64_t* uid, int32_t* height) { + int32_t code = TSDB_CODE_SUCCESS; const char* msg1 = "invalid function parameters"; const char* msg2 = "not supported functions in arithmetic expression"; - + int32_t functionId = isValidFunction(pExpr->Expr.operand.z, pExpr->Expr.operand.n); - + pExpr->functionId = functionId; if (pExpr->Expr.paramList != NULL) { - size_t numChildren = taosArrayGetSize(pExpr->Expr.paramList); + size_t numChildren = taosArrayGetSize(pExpr->Expr.paramList); int32_t* childrenTypes = calloc(numChildren, sizeof(int32_t)); int32_t* childrenHeight = calloc(numChildren, sizeof(int32_t)); for (int32_t i = 0; i < numChildren; ++i) { tSqlExprItem* pParamElem = taosArrayGet(pExpr->Expr.paramList, i); if (TSDB_FUNC_IS_SCALAR(functionId)) { - code = validateSQLExprItem(pCmd, pParamElem->pNode, pQueryInfo, pList, childrenTypes + i, uid, childrenHeight+i); + code = + validateSQLExprItem(pCmd, pParamElem->pNode, pQueryInfo, pList, childrenTypes + i, uid, childrenHeight + i); if (code != TSDB_CODE_SUCCESS) { tfree(childrenTypes); tfree(childrenHeight); @@ -5025,15 +5051,14 @@ static int32_t validateSQLExprItemSQLFunc(SSqlCmd* pCmd, tSqlExpr* pExpr, tfree(childrenHeight); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - } { if (TSDB_FUNC_IS_SCALAR(functionId)) { - bool anyChildScalar = false; - bool anyChildAgg = false; + bool anyChildScalar = false; + bool anyChildAgg = false; int32_t maxChildrenHeight = 0; for (int i = 0; i < numChildren; ++i) { - assert (childrenTypes[i] != SQLEXPR_TYPE_UNASSIGNED); + assert(childrenTypes[i] != SQLEXPR_TYPE_UNASSIGNED); anyChildScalar = anyChildScalar || (childrenTypes[i] == SQLEXPR_TYPE_SCALAR); anyChildAgg = anyChildAgg || (childrenTypes[i] == SQLEXPR_TYPE_AGG); if (childrenHeight[i] > maxChildrenHeight) { @@ -5059,14 +5084,14 @@ static int32_t validateSQLExprItemSQLFunc(SSqlCmd* pCmd, tSqlExpr* pExpr, } tfree(childrenTypes); tfree(childrenHeight); - //end if param list is not null + // end if param list is not null } else { if (TSDB_FUNC_IS_SCALAR(functionId)) { *type = SQLEXPR_TYPE_SCALAR; } else { *type = SQLEXPR_TYPE_AGG; } - } // else param list is null + } // else param list is null if (!TSDB_FUNC_IS_SCALAR(functionId)) { int32_t outputIndex = (int32_t)tscNumOfExprs(pQueryInfo); @@ -5120,19 +5145,19 @@ static int32_t validateSQLExprItemSQLFunc(SSqlCmd* pCmd, tSqlExpr* pExpr, return TSDB_CODE_SUCCESS; } -static int32_t validateSQLExprItemOperatorExpr(SSqlCmd* pCmd, tSqlExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList, - int32_t* type, uint64_t* uid, int32_t* height) { - uint64_t uidLeft = 0; - uint64_t uidRight = 0; - int32_t leftType = SQLEXPR_TYPE_UNASSIGNED; - int32_t rightType = SQLEXPR_TYPE_UNASSIGNED; +static int32_t validateSQLExprItemOperatorExpr(SSqlCmd* pCmd, tSqlExpr* pExpr, SQueryInfo* pQueryInfo, + SColumnList* pList, int32_t* type, uint64_t* uid, int32_t* height) { + uint64_t uidLeft = 0; + uint64_t uidRight = 0; + int32_t leftType = SQLEXPR_TYPE_UNASSIGNED; + int32_t rightType = SQLEXPR_TYPE_UNASSIGNED; const char* msg1 = "arithmetic expression composed with columns from different tables"; const char* msg2 = "arithmetic expression composed with functions/columns of different types"; const char* msg3 = "comparison/logical expression involving string operands is not supported"; const char* msg4 = "comparison/logical expression involving function result is not supported"; int32_t leftHeight = 0; - int32_t ret = validateSQLExprItem(pCmd, pExpr->pLeft, pQueryInfo, pList, &leftType, &uidLeft, &leftHeight); + int32_t ret = validateSQLExprItem(pCmd, pExpr->pLeft, pQueryInfo, pList, &leftType, &uidLeft, &leftHeight); if (ret != TSDB_CODE_SUCCESS) { return ret; } @@ -5147,14 +5172,15 @@ static int32_t validateSQLExprItemOperatorExpr(SSqlCmd* pCmd, tSqlExpr* pExpr, S } *uid = uidLeft; - *height = (leftHeight > rightHeight) ? leftHeight + 1 : rightHeight+1; + *height = (leftHeight > rightHeight) ? leftHeight + 1 : rightHeight + 1; { if (leftType == SQLEXPR_TYPE_UNASSIGNED || rightType == SQLEXPR_TYPE_UNASSIGNED) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "invalid operand expression"); } // return invalid operation when one child aggregate and the other child scalar or column - if ((leftType == SQLEXPR_TYPE_AGG && rightType == SQLEXPR_TYPE_SCALAR) || (rightType == SQLEXPR_TYPE_AGG && leftType == SQLEXPR_TYPE_SCALAR)) { + if ((leftType == SQLEXPR_TYPE_AGG && rightType == SQLEXPR_TYPE_SCALAR) || + (rightType == SQLEXPR_TYPE_AGG && leftType == SQLEXPR_TYPE_SCALAR)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -5162,35 +5188,32 @@ static int32_t validateSQLExprItemOperatorExpr(SSqlCmd* pCmd, tSqlExpr* pExpr, S *type = SQLEXPR_TYPE_AGG; } else if (leftType == SQLEXPR_TYPE_VALUE && rightType == SQLEXPR_TYPE_VALUE) { *type = SQLEXPR_TYPE_VALUE; - } else if (leftType == SQLEXPR_TYPE_SCALAR || rightType == SQLEXPR_TYPE_SCALAR){ + } else if (leftType == SQLEXPR_TYPE_SCALAR || rightType == SQLEXPR_TYPE_SCALAR) { *type = SQLEXPR_TYPE_SCALAR; } // comparison/logical operations - if (pExpr->tokenId == TK_EQ || pExpr->tokenId == TK_NE || - pExpr->tokenId == TK_GT || pExpr->tokenId == TK_GE || - pExpr->tokenId == TK_LT || pExpr->tokenId == TK_LE || - pExpr->tokenId == TK_AND || pExpr->tokenId == TK_OR) { + if (pExpr->tokenId == TK_EQ || pExpr->tokenId == TK_NE || pExpr->tokenId == TK_GT || pExpr->tokenId == TK_GE || + pExpr->tokenId == TK_LT || pExpr->tokenId == TK_LE || pExpr->tokenId == TK_AND || pExpr->tokenId == TK_OR) { if ((leftType == SQLEXPR_TYPE_VALUE && pExpr->pLeft->tokenId == TK_STRING) || (rightType == SQLEXPR_TYPE_VALUE && pExpr->pRight->tokenId == TK_STRING)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } - if (leftType == SQLEXPR_TYPE_AGG || leftType == SQLEXPR_TYPE_SCALAR || - rightType == SQLEXPR_TYPE_AGG || rightType == SQLEXPR_TYPE_SCALAR) { + if (leftType == SQLEXPR_TYPE_AGG || leftType == SQLEXPR_TYPE_SCALAR || rightType == SQLEXPR_TYPE_AGG || + rightType == SQLEXPR_TYPE_SCALAR) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } - } else if (pExpr->tokenId == TK_ISNULL || pExpr->tokenId == TK_NOTNULL || - pExpr->tokenId == TK_IS || pExpr->tokenId == TK_LIKE || - pExpr->tokenId == TK_MATCH || pExpr->tokenId == TK_NMATCH || + } else if (pExpr->tokenId == TK_ISNULL || pExpr->tokenId == TK_NOTNULL || pExpr->tokenId == TK_IS || + pExpr->tokenId == TK_LIKE || pExpr->tokenId == TK_MATCH || pExpr->tokenId == TK_NMATCH || pExpr->tokenId == TK_CONTAINS || pExpr->tokenId == TK_IN) { - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "unsupported filtering operations"); + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "unsupported filtering operations"); } } return TSDB_CODE_SUCCESS; } -static int32_t validateSQLExprItem(SSqlCmd* pCmd, tSqlExpr* pExpr, - SQueryInfo* pQueryInfo, SColumnList* pList, int32_t* type, uint64_t* uid, int32_t* height) { +static int32_t validateSQLExprItem(SSqlCmd* pCmd, tSqlExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList, + int32_t* type, uint64_t* uid, int32_t* height) { const char* msg1 = "invalid column name in select clause"; const char* msg2 = "invalid data type in select clause"; const char* msg3 = "invalid select clause"; @@ -5233,7 +5256,7 @@ static int32_t validateSQLExprItem(SSqlCmd* pCmd, tSqlExpr* pExpr, return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } - //not allowing now/today keyword arithmetic operation in select expr + // not allowing now/today keyword arithmetic operation in select expr if (pExpr->exprToken.type == TK_NOW || pExpr->exprToken.type == TK_TODAY) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } @@ -5266,7 +5289,8 @@ static bool isValidExpr(tSqlExpr* pLeft, tSqlExpr* pRight, int32_t optr) { return true; } - if (pLeft->tokenId >= TK_BOOL && pLeft->tokenId <= TK_BINARY && pRight->tokenId >= TK_BOOL && pRight->tokenId <= TK_BINARY) { + if (pLeft->tokenId >= TK_BOOL && pLeft->tokenId <= TK_BINARY && pRight->tokenId >= TK_BOOL && + pRight->tokenId <= TK_BINARY) { return false; } @@ -5278,11 +5302,12 @@ static bool isValidExpr(tSqlExpr* pLeft, tSqlExpr* pRight, int32_t optr) { } static void exchangeExpr(tSqlExpr* pExpr) { - tSqlExpr* pLeft = pExpr->pLeft; + tSqlExpr* pLeft = pExpr->pLeft; tSqlExpr* pRight = pExpr->pRight; - if ((pRight->tokenId == TK_ID || pRight->tokenId == TK_ARROW) && (pLeft->tokenId == TK_INTEGER || pLeft->tokenId == TK_FLOAT || - pLeft->tokenId == TK_STRING || pLeft->tokenId == TK_BOOL)) { + if ((pRight->tokenId == TK_ID || pRight->tokenId == TK_ARROW) && + (pLeft->tokenId == TK_INTEGER || pLeft->tokenId == TK_FLOAT || pLeft->tokenId == TK_STRING || + pLeft->tokenId == TK_BOOL)) { /* * exchange value of the left handside and the value of the right-handside * to make sure that the value of filter expression always locates in @@ -5323,19 +5348,21 @@ static bool validateJoinExprNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr const char* msg8 = "tag json key is too long, no more than 256 bytes"; tSqlExpr* pRight = pExpr->pRight; - if(pRight->tokenId == TK_ARROW){ - if(!IS_VAR_DATA_TYPE(pExpr->pLeft->pRight->value.nType) || pExpr->pLeft->pRight->value.nType != pExpr->pRight->pRight->value.nType){ + if (pRight->tokenId == TK_ARROW) { + if (!IS_VAR_DATA_TYPE(pExpr->pLeft->pRight->value.nType) || + pExpr->pLeft->pRight->value.nType != pExpr->pRight->pRight->value.nType) { invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg6); return false; } - if(pExpr->pLeft->pRight->value.nLen > TSDB_MAX_JSON_KEY_LEN){ + if (pExpr->pLeft->pRight->value.nLen > TSDB_MAX_JSON_KEY_LEN) { invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg8); return false; } - if(pExpr->pLeft->pRight->value.nLen != pExpr->pRight->pRight->value.nLen - || strncmp(pExpr->pLeft->pRight->value.pz, pExpr->pRight->pRight->value.pz, pExpr->pRight->pRight->value.nLen) != 0){ - invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg7); - return false; + if (pExpr->pLeft->pRight->value.nLen != pExpr->pRight->pRight->value.nLen || + strncmp(pExpr->pLeft->pRight->value.pz, pExpr->pRight->pRight->value.pz, pExpr->pRight->pRight->value.nLen) != + 0) { + invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg7); + return false; } pRight = pExpr->pRight->pLeft; @@ -5352,7 +5379,8 @@ static bool validateJoinExprNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr SColumnIndex rightIndex = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByName(&pRight->columnName, pQueryInfo, &rightIndex, tscGetErrorMsgPayload(pCmd)) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByName(&pRight->columnName, pQueryInfo, &rightIndex, tscGetErrorMsgPayload(pCmd)) != + TSDB_CODE_SUCCESS) { invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); return false; } @@ -5362,13 +5390,15 @@ static bool validateJoinExprNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr SSchema* pLeftSchema = tscGetTableSchema(pLeftMeterMeta->pTableMeta); int16_t leftType = pLeftSchema[pLeftIndex->columnIndex].type; - tscColumnListInsert(pQueryInfo->colList, pLeftIndex->columnIndex, pLeftMeterMeta->pTableMeta->id.uid, &pLeftSchema[pLeftIndex->columnIndex]); + tscColumnListInsert(pQueryInfo->colList, pLeftIndex->columnIndex, pLeftMeterMeta->pTableMeta->id.uid, + &pLeftSchema[pLeftIndex->columnIndex]); STableMetaInfo* pRightMeterMeta = tscGetMetaInfo(pQueryInfo, rightIndex.tableIndex); SSchema* pRightSchema = tscGetTableSchema(pRightMeterMeta->pTableMeta); int16_t rightType = pRightSchema[rightIndex.columnIndex].type; - tscColumnListInsert(pQueryInfo->colList, rightIndex.columnIndex, pRightMeterMeta->pTableMeta->id.uid, &pRightSchema[rightIndex.columnIndex]); + tscColumnListInsert(pQueryInfo->colList, rightIndex.columnIndex, pRightMeterMeta->pTableMeta->id.uid, + &pRightSchema[rightIndex.columnIndex]); if (leftType != rightType) { invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); @@ -5411,13 +5441,13 @@ static int32_t setNormalExprToCond(tSqlExpr** parent, tSqlExpr* pExpr, int32_t p return TSDB_CODE_SUCCESS; } - static int32_t validateNullExpr(tSqlExpr* pExpr, STableMeta* pTableMeta, int32_t index, char* msgBuf) { const char* msg = "only support is [not] null"; tSqlExpr* pRight = pExpr->pRight; - SSchema* pSchema = tscGetTableSchema(pTableMeta); - if (pRight->tokenId == TK_NULL && pSchema[index].type != TSDB_DATA_TYPE_JSON && (!(pExpr->tokenId == TK_ISNULL || pExpr->tokenId == TK_NOTNULL))) { + SSchema* pSchema = tscGetTableSchema(pTableMeta); + if (pRight->tokenId == TK_NULL && pSchema[index].type != TSDB_DATA_TYPE_JSON && + (!(pExpr->tokenId == TK_ISNULL || pExpr->tokenId == TK_NOTNULL))) { return invalidOperationMsg(msgBuf, msg); } @@ -5426,7 +5456,7 @@ static int32_t validateNullExpr(tSqlExpr* pExpr, STableMeta* pTableMeta, int32_t return TSDB_CODE_SUCCESS; } - char *v = strndup(pRight->exprToken.z, pRight->exprToken.n); + char* v = strndup(pRight->exprToken.z, pRight->exprToken.n); int32_t len = stringProcess(v, pRight->exprToken.n); if (len > 0) { uint32_t type = 0; @@ -5449,7 +5479,7 @@ static int32_t validateLikeExpr(tSqlExpr* pExpr, STableMeta* pTableMeta, int32_t const char* msg1 = "wildcard string should be less than %d characters"; const char* msg2 = "illegal column type for like"; - tSqlExpr* pLeft = pExpr->pLeft; + tSqlExpr* pLeft = pExpr->pLeft; tSqlExpr* pRight = pExpr->pRight; if (pExpr->tokenId == TK_LIKE) { @@ -5460,7 +5490,8 @@ static int32_t validateLikeExpr(tSqlExpr* pExpr, STableMeta* pTableMeta, int32_t } SSchema* pSchema = tscGetTableSchema(pTableMeta); - if ((pLeft->tokenId != TK_ARROW) && (!isTablenameToken(&pLeft->columnName)) && !IS_VAR_DATA_TYPE(pSchema[index].type)) { + if ((pLeft->tokenId != TK_ARROW) && (!isTablenameToken(&pLeft->columnName)) && + !IS_VAR_DATA_TYPE(pSchema[index].type)) { return invalidOperationMsg(msgBuf, msg2); } } @@ -5479,12 +5510,11 @@ static int32_t validateJsonTagExpr(tSqlExpr* pExpr, char* msgBuf) { tSqlExpr* pRight = pExpr->pRight; if (pExpr->tokenId == TK_CONTAINS) { - if (pRight != NULL && !IS_VAR_DATA_TYPE(pRight->value.nType)) - return invalidOperationMsg(msgBuf, msg3); + if (pRight != NULL && !IS_VAR_DATA_TYPE(pRight->value.nType)) return invalidOperationMsg(msgBuf, msg3); if (pRight != NULL && (pRight->value.nLen > TSDB_MAX_JSON_KEY_LEN || pRight->value.nLen <= 0)) return invalidOperationMsg(msgBuf, msg2); - } else if(pExpr->tokenId == TK_IN){ + } else if (pExpr->tokenId == TK_IN) { return invalidOperationMsg(msgBuf, msg4); } else { if (pLeft != NULL && pLeft->tokenId == TK_ID && pExpr->tokenId != TK_ISNULL && pExpr->tokenId != TK_NOTNULL) { @@ -5492,8 +5522,7 @@ static int32_t validateJsonTagExpr(tSqlExpr* pExpr, char* msgBuf) { } if (pLeft != NULL && pLeft->tokenId == TK_ARROW) { - if (pLeft->pRight && !IS_VAR_DATA_TYPE(pLeft->pRight->value.nType)) - return invalidOperationMsg(msgBuf, msg3); + if (pLeft->pRight && !IS_VAR_DATA_TYPE(pLeft->pRight->value.nType)) return invalidOperationMsg(msgBuf, msg3); if (pLeft->pRight && (pLeft->pRight->value.nLen > TSDB_MAX_JSON_KEY_LEN || pLeft->pRight->value.nLen <= 0)) return invalidOperationMsg(msgBuf, msg2); if (pRight->tokenId == TK_NULL && pExpr->tokenId == TK_EQ) { @@ -5507,8 +5536,9 @@ static int32_t validateJsonTagExpr(tSqlExpr* pExpr, char* msgBuf) { } } - if (pRight->value.nType == TSDB_DATA_TYPE_BINARY){ // json value store by nchar, so need to convert from binary to nchar - if(pRight->value.nLen == 0){ + if (pRight->value.nType == + TSDB_DATA_TYPE_BINARY) { // json value store by nchar, so need to convert from binary to nchar + if (pRight->value.nLen == 0) { pRight->value.nType = TSDB_DATA_TYPE_NCHAR; return TSDB_CODE_SUCCESS; } @@ -5537,7 +5567,7 @@ static int32_t validateMatchExpr(tSqlExpr* pExpr, STableMeta* pTableMeta, int32_ return invalidOperationMsg(msgBuf, msg3); } - int errCode = 0; + int errCode = 0; regex_t regex; char regErrBuf[256] = {0}; @@ -5553,7 +5583,6 @@ static int32_t validateMatchExpr(tSqlExpr* pExpr, STableMeta* pTableMeta, int32_ return TSDB_CODE_SUCCESS; } - int32_t handleNeOptr(tSqlExpr** rexpr, tSqlExpr* expr) { tSqlExpr* left = tSqlExprClone(expr); tSqlExpr* right = expr; @@ -5566,24 +5595,24 @@ int32_t handleNeOptr(tSqlExpr** rexpr, tSqlExpr* expr) { return TSDB_CODE_SUCCESS; } -void convertWhereStringCharset(tSqlExpr* pRight){ - if(pRight->value.nType != TSDB_DATA_TYPE_BINARY || pRight->value.nLen == 0){ +void convertWhereStringCharset(tSqlExpr* pRight) { + if (pRight->value.nType != TSDB_DATA_TYPE_BINARY || pRight->value.nLen == 0) { return; } - char *newData = calloc(pRight->value.nLen * TSDB_NCHAR_SIZE, 1); - if(!newData){ + char* newData = calloc(pRight->value.nLen * TSDB_NCHAR_SIZE, 1); + if (!newData) { tscError("convertWhereStringCharset calloc memory error"); return; } int len = 0; - if(!taosMbsToUcs4(pRight->value.pz, pRight->value.nLen, newData, pRight->value.nLen * TSDB_NCHAR_SIZE, &len)){ + if (!taosMbsToUcs4(pRight->value.pz, pRight->value.nLen, newData, pRight->value.nLen * TSDB_NCHAR_SIZE, &len)) { tscError("nchar in where condition mbsToUcs4 error"); free(newData); return; } char* tmp = realloc(pRight->value.pz, len); - if (!tmp){ + if (!tmp) { tscError("convertWhereStringCharset realloc memory error"); free(newData); return; @@ -5604,13 +5633,13 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSql const char* msg5 = "not support ordinary column join"; const char* msg6 = "illegal condition expression"; - tSqlExpr* pLeft = (*pExpr)->pLeft; + tSqlExpr* pLeft = (*pExpr)->pLeft; tSqlExpr* pRight = (*pExpr)->pRight; SStrToken* colName = NULL; - if(pLeft->tokenId == TK_ARROW){ + if (pLeft->tokenId == TK_ARROW) { colName = &(pLeft->pLeft->columnName); - }else{ + } else { colName = &(pLeft->columnName); } @@ -5648,11 +5677,12 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSql SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, index.columnIndex); - if (pSchema->type == TSDB_DATA_TYPE_NCHAR){ + if (pSchema->type == TSDB_DATA_TYPE_NCHAR) { convertWhereStringCharset(pRight); } - if (pSchema->type == TSDB_DATA_TYPE_TIMESTAMP && index.columnIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX) { // query on time range + if (pSchema->type == TSDB_DATA_TYPE_TIMESTAMP && + index.columnIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX) { // query on time range if (!validateJoinExprNode(pCmd, pQueryInfo, *pExpr, &index)) { return TSDB_CODE_TSC_INVALID_OPERATION; } @@ -5663,7 +5693,7 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSql pCondExpr->tsJoin = true; assert(index.tableIndex >= 0 && index.tableIndex < TSDB_MAX_JOIN_TABLE_NUM); - SJoinNode **leftNode = &pQueryInfo->tagCond.joinInfo.joinTables[index.tableIndex]; + SJoinNode** leftNode = &pQueryInfo->tagCond.joinInfo.joinTables[index.tableIndex]; if (*leftNode == NULL) { *leftNode = calloc(1, sizeof(SJoinNode)); if (*leftNode == NULL) { @@ -5673,7 +5703,8 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSql int16_t leftIdx = index.tableIndex; - if (getColumnIndexByName(&pRight->columnName, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByName(&pRight->columnName, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != + TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -5681,7 +5712,7 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSql return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } - SJoinNode **rightNode = &pQueryInfo->tagCond.joinInfo.joinTables[index.tableIndex]; + SJoinNode** rightNode = &pQueryInfo->tagCond.joinInfo.joinTables[index.tableIndex]; if (*rightNode == NULL) { *rightNode = calloc(1, sizeof(SJoinNode)); if (*rightNode == NULL) { @@ -5711,7 +5742,7 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSql *type |= TSQL_EXPR_JOIN; } } else { - tSqlExpr *rexpr = NULL; + tSqlExpr* rexpr = NULL; if ((*pExpr)->tokenId == TK_NE) { handleNeOptr(&rexpr, *pExpr); } else { @@ -5732,14 +5763,15 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSql } // check for json tag operation -> and ? - if (pSchema->type == TSDB_DATA_TYPE_JSON){ + if (pSchema->type == TSDB_DATA_TYPE_JSON) { code = validateJsonTagExpr(*pExpr, tscGetErrorMsgPayload(pCmd)); if (code != TSDB_CODE_SUCCESS) { return code; } } - if (joinQuery && pRight != NULL && (pRight->tokenId == TK_ID || pRight->tokenId == TK_ARROW)) { // join on tag columns for stable query + if (joinQuery && pRight != NULL && + (pRight->tokenId == TK_ID || pRight->tokenId == TK_ARROW)) { // join on tag columns for stable query if (!validateJoinExprNode(pCmd, pQueryInfo, *pExpr, &index)) { return TSDB_CODE_TSC_INVALID_OPERATION; } @@ -5754,11 +5786,10 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSql // do nothing // ret = setExprToCond(pCmd, &pCondExpr->pTagCond, // *pExpr, NULL, parentOptr); - tSqlExpr *rexpr = NULL; - if ((*pExpr)->tokenId == TK_NE && (pSchema->type != TSDB_DATA_TYPE_BINARY - && pSchema->type != TSDB_DATA_TYPE_NCHAR - && pSchema->type != TSDB_DATA_TYPE_BOOL - && pSchema->type != TSDB_DATA_TYPE_JSON)) { + tSqlExpr* rexpr = NULL; + if ((*pExpr)->tokenId == TK_NE && + (pSchema->type != TSDB_DATA_TYPE_BINARY && pSchema->type != TSDB_DATA_TYPE_NCHAR && + pSchema->type != TSDB_DATA_TYPE_BOOL && pSchema->type != TSDB_DATA_TYPE_JSON)) { handleNeOptr(&rexpr, *pExpr); *pExpr = rexpr; } @@ -5774,14 +5805,16 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSql if (pRight->tokenId == TK_ID) { if (joinQuery) { - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg5); // other column cannot be served as the join column + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), + msg5); // other column cannot be served as the join column } else { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg6); } } - tSqlExpr *rexpr = NULL; - if ((*pExpr)->tokenId == TK_NE && (pSchema->type != TSDB_DATA_TYPE_BINARY && pSchema->type != TSDB_DATA_TYPE_NCHAR && pSchema->type != TSDB_DATA_TYPE_BOOL)) { + tSqlExpr* rexpr = NULL; + if ((*pExpr)->tokenId == TK_NE && (pSchema->type != TSDB_DATA_TYPE_BINARY && + pSchema->type != TSDB_DATA_TYPE_NCHAR && pSchema->type != TSDB_DATA_TYPE_BOOL)) { handleNeOptr(&rexpr, *pExpr); } else { rexpr = *pExpr; @@ -5794,17 +5827,16 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSql return ret; } -int32_t getQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SCondExpr* pCondExpr, - int32_t* type, int32_t* tbIdx, int32_t parentOptr, tSqlExpr** columnExpr, - tSqlExpr** tsExpr, bool joinQuery) { +int32_t getQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SCondExpr* pCondExpr, int32_t* type, + int32_t* tbIdx, int32_t parentOptr, tSqlExpr** columnExpr, tSqlExpr** tsExpr, bool joinQuery) { if (pExpr == NULL) { return TSDB_CODE_SUCCESS; } - tSqlExpr *columnLeft = NULL; - tSqlExpr *columnRight = NULL; - tSqlExpr *tsLeft = NULL; - tSqlExpr *tsRight = NULL; + tSqlExpr* columnLeft = NULL; + tSqlExpr* columnRight = NULL; + tSqlExpr* tsLeft = NULL; + tSqlExpr* tsRight = NULL; int32_t ret = 0; @@ -5828,12 +5860,14 @@ int32_t getQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr** pExpr int32_t rightTbIdx = 0; if (!tSqlExprIsParentOfLeaf(*pExpr)) { - ret = getQueryCondExpr(pCmd, pQueryInfo, &(*pExpr)->pLeft, pCondExpr, type ? &leftType : NULL, &leftTbIdx, (*pExpr)->tokenId, &columnLeft, &tsLeft, joinQuery); + ret = getQueryCondExpr(pCmd, pQueryInfo, &(*pExpr)->pLeft, pCondExpr, type ? &leftType : NULL, &leftTbIdx, + (*pExpr)->tokenId, &columnLeft, &tsLeft, joinQuery); if (ret != TSDB_CODE_SUCCESS) { goto err_ret; } - ret = getQueryCondExpr(pCmd, pQueryInfo, &(*pExpr)->pRight, pCondExpr, type ? &rightType : NULL, &rightTbIdx, (*pExpr)->tokenId, &columnRight, &tsRight, joinQuery); + ret = getQueryCondExpr(pCmd, pQueryInfo, &(*pExpr)->pRight, pCondExpr, type ? &rightType : NULL, &rightTbIdx, + (*pExpr)->tokenId, &columnRight, &tsRight, joinQuery); if (ret != TSDB_CODE_SUCCESS) { goto err_ret; } @@ -5869,7 +5903,7 @@ int32_t getQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr** pExpr } if (type) { - *type = leftType|rightType; + *type = leftType | rightType; } *tbIdx = (leftTbIdx == rightTbIdx) ? leftTbIdx : -1; @@ -5888,7 +5922,8 @@ int32_t getQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr** pExpr goto err_ret; } - ret = handleExprInQueryCond(pCmd, pQueryInfo, pExpr, pCondExpr, type, tbIdx, parentOptr, columnExpr, tsExpr, joinQuery); + ret = + handleExprInQueryCond(pCmd, pQueryInfo, pExpr, pCondExpr, type, tbIdx, parentOptr, columnExpr, tsExpr, joinQuery); if (ret) { goto err_ret; } @@ -5904,7 +5939,8 @@ err_ret: return ret; } -static void doExtractExprForSTable(SSqlCmd* pCmd, tSqlExpr** pExpr, SQueryInfo* pQueryInfo, tSqlExpr** pOut, int32_t tableIndex) { +static void doExtractExprForSTable(SSqlCmd* pCmd, tSqlExpr** pExpr, SQueryInfo* pQueryInfo, tSqlExpr** pOut, + int32_t tableIndex) { if (*pExpr == NULL) { *pOut = NULL; return; @@ -5914,10 +5950,11 @@ static void doExtractExprForSTable(SSqlCmd* pCmd, tSqlExpr** pExpr, SQueryInfo* tSqlExpr* pLeft = (*pExpr)->pLeft; SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if(pLeft->tokenId == TK_ARROW) { + if (pLeft->tokenId == TK_ARROW) { pLeft = pLeft->pLeft; } - if (getColumnIndexByName(&pLeft->columnName, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByName(&pLeft->columnName, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != + TSDB_CODE_SUCCESS) { return; } @@ -5963,19 +6000,23 @@ int tableNameCompar(const void* lhs, const void* rhs) { int32_t mergeTimeRange(SSqlCmd* pCmd, STimeWindow* res, STimeWindow* win, int32_t optr) { const char* msg0 = "only one time stamp window allowed"; -#define SET_EMPTY_RANGE(w) do { (w)->skey = INT64_MAX; (w)->ekey = INT64_MIN; } while (0) +#define SET_EMPTY_RANGE(w) \ + do { \ + (w)->skey = INT64_MAX; \ + (w)->ekey = INT64_MIN; \ + } while (0) #define IS_EMPTY_RANGE(w) ((w)->skey == INT64_MAX && (w)->ekey == INT64_MIN) - + if (optr == TSDB_RELATION_AND) { if (res->skey > win->ekey || win->skey > res->ekey) { SET_EMPTY_RANGE(res); return TSDB_CODE_SUCCESS; } - + if (res->skey < win->skey) { res->skey = win->skey; } - + if (res->ekey > win->ekey) { res->ekey = win->ekey; } @@ -6000,7 +6041,7 @@ int32_t mergeTimeRange(SSqlCmd* pCmd, STimeWindow* res, STimeWindow* win, int32_ if (res->skey > win->skey) { res->skey = win->skey; } - + if (res->ekey < win->ekey) { res->ekey = win->ekey; } @@ -6010,7 +6051,7 @@ int32_t mergeTimeRange(SSqlCmd* pCmd, STimeWindow* res, STimeWindow* win, int32_ static int32_t createTimeRangeExpr(tSqlExpr** pExpr, STimeWindow* win, uint32_t tokenId) { *pExpr = calloc(1, sizeof(tSqlExpr)); - + (*pExpr)->type = SQL_NODE_VALUE; (*pExpr)->tokenId = tokenId; (*pExpr)->value.nType = TSDB_DATA_TYPE_VALUE_ARRAY; @@ -6025,7 +6066,7 @@ static int32_t createTimeRangeExpr(tSqlExpr** pExpr, STimeWindow* win, uint32_t static int32_t convertTimeRangeFromExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr* pExpr) { const char* msg0 = "invalid timestamp or operator for timestamp"; - int32_t code = 0; + int32_t code = 0; STimeWindow win = {.skey = INT64_MIN, .ekey = INT64_MAX}; if (pExpr == NULL) { @@ -6033,27 +6074,27 @@ static int32_t convertTimeRangeFromExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, t return TSDB_CODE_SUCCESS; } pQueryInfo->onlyHasTagCond &= false; - if (!tSqlExprIsParentOfLeaf(pExpr)) { - code = convertTimeRangeFromExpr(pCmd, pQueryInfo, pExpr->pLeft); - if (code) { - return code; - } + code = convertTimeRangeFromExpr(pCmd, pQueryInfo, pExpr->pLeft); + if (code) { + return code; + } - code = convertTimeRangeFromExpr(pCmd, pQueryInfo, pExpr->pRight); - if (code) { - return code; - } + code = convertTimeRangeFromExpr(pCmd, pQueryInfo, pExpr->pRight); + if (code) { + return code; + } } else { SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByName(&pExpr->pLeft->columnName, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByName(&pExpr->pLeft->columnName, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != + TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; } STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); - + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + tSqlExpr* pRight = pExpr->pRight; if (getTimeRange(&win, pRight, pExpr->tokenId, tinfo.precision) != TSDB_CODE_SUCCESS) { @@ -6086,7 +6127,7 @@ static int32_t validateJoinExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCondExpr STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { // for stable join, tag columns - // must be present for join + // must be present for join if (pCondExpr->pJoinExpr == NULL) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } @@ -6130,7 +6171,7 @@ static void doAddJoinTagsColumnsIntoTagList(SSqlCmd* pCmd, SQueryInfo* pQueryInf SSchema* pSchema = tscGetTableTagSchema(pTableMetaInfo->pTableMeta); tscColumnListInsert(pTableMetaInfo->tagColList, &index, &pSchema[index.columnIndex]); - + if (getColumnIndexByName(pCmd, &pCondExpr->pJoinExpr->pRight->ColName, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { tscError("%p: invalid column name (right)", pQueryInfo); } @@ -6144,15 +6185,15 @@ static void doAddJoinTagsColumnsIntoTagList(SSqlCmd* pCmd, SQueryInfo* pQueryInf } */ -static int32_t validateTagCondExpr(SSqlCmd* pCmd, tExprNode *p) { - const char *msg1 = "invalid tag operator"; +static int32_t validateTagCondExpr(SSqlCmd* pCmd, tExprNode* p) { + const char* msg1 = "invalid tag operator"; const char* msg2 = "not supported filter condition"; - + do { if (p->nodeType != TSQL_NODE_EXPR) { break; } - + if (!p->_node.pLeft || !p->_node.pRight) { break; } @@ -6165,23 +6206,23 @@ static int32_t validateTagCondExpr(SSqlCmd* pCmd, tExprNode *p) { if (p->_node.pRight && (retVal = validateTagCondExpr(pCmd, p->_node.pRight)) != TSDB_CODE_SUCCESS) { return retVal; } - + if (IS_ARITHMETIC_OPTR(p->_node.optr)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - + if (!IS_RELATION_OPTR(p->_node.optr)) { break; } - - tVariant * vVariant = NULL; - int32_t schemaType = -1; - + + tVariant* vVariant = NULL; + int32_t schemaType = -1; + if (p->_node.pLeft->nodeType == TSQL_NODE_VALUE && p->_node.pRight->nodeType == TSQL_NODE_COL) { if (!p->_node.pRight->pSchema) { break; } - + vVariant = p->_node.pLeft->pVal; schemaType = p->_node.pRight->pSchema->type; } else if (p->_node.pLeft->nodeType == TSQL_NODE_COL && p->_node.pRight->nodeType == TSQL_NODE_VALUE) { @@ -6209,19 +6250,19 @@ static int32_t validateTagCondExpr(SSqlCmd* pCmd, tExprNode *p) { } if (schemaType == TSDB_DATA_TYPE_BINARY) { - char *tmp = calloc(1, bufLen * TSDB_NCHAR_SIZE); + char* tmp = calloc(1, bufLen * TSDB_NCHAR_SIZE); retVal = tVariantDump(vVariant, tmp, schemaType, false); free(tmp); } else if (schemaType == TSDB_DATA_TYPE_NCHAR) { // pRight->value.nLen + 1 is larger than the actual nchar string length - char *tmp = calloc(1, bufLen * TSDB_NCHAR_SIZE); + char* tmp = calloc(1, bufLen * TSDB_NCHAR_SIZE); retVal = tVariantDump(vVariant, tmp, schemaType, false); free(tmp); } else { double tmp; retVal = tVariantDump(vVariant, (char*)&tmp, schemaType, false); } - + if (retVal != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -6236,7 +6277,7 @@ static int32_t getTagQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCondE if (pCondExpr->pTagCond == NULL) { return ret; } - + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { tSqlExpr* p1 = extractExprForSTable(pCmd, &pCondExpr->pTagCond, pQueryInfo, i); if (p1 == NULL) { // no query condition on this table @@ -6244,38 +6285,38 @@ static int32_t getTagQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCondE } tExprNode* p = NULL; - + SArray* colList = taosArrayInit(10, sizeof(SColIndex)); ret = exprTreeFromSqlExpr(pCmd, &p, p1, pQueryInfo, colList, NULL); - //if (ret == TSDB_CODE_SUCCESS) { - // ret = filterInitFromTree(p, &pQueryInfo->tagFilter, (int32_t)taosArrayGetSize(colList), NULL); - //} - + // if (ret == TSDB_CODE_SUCCESS) { + // ret = filterInitFromTree(p, &pQueryInfo->tagFilter, (int32_t)taosArrayGetSize(colList), NULL); + // } + SBufferWriter bw = tbufInitWriter(NULL, false); - TRY(0) { - exprTreeToBinary(&bw, p); - } CATCH(code) { + TRY(0) { exprTreeToBinary(&bw, p); } + CATCH(code) { tbufCloseWriter(&bw); UNUSED(code); // TODO: more error handling - } END_TRY + } + END_TRY // add to required table column list STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, i); - int64_t uid = pTableMetaInfo->pTableMeta->id.uid; - int32_t numOfCols = tscGetNumOfColumns(pTableMetaInfo->pTableMeta); - + int64_t uid = pTableMetaInfo->pTableMeta->id.uid; + int32_t numOfCols = tscGetNumOfColumns(pTableMetaInfo->pTableMeta); + size_t num = taosArrayGetSize(colList); - for(int32_t j = 0; j < num; ++j) { - SColIndex* pIndex = taosArrayGet(colList, j); + for (int32_t j = 0; j < num; ++j) { + SColIndex* pIndex = taosArrayGet(colList, j); SColumnIndex index = {.tableIndex = i, .columnIndex = pIndex->colIndex - numOfCols}; SSchema* s = tscGetTableSchema(pTableMetaInfo->pTableMeta); tscColumnListInsert(pTableMetaInfo->tagColList, index.columnIndex, pTableMetaInfo->pTableMeta->id.uid, &s[pIndex->colIndex]); } - + tsSetSTableQueryCond(&pQueryInfo->tagCond, uid, &bw); tSqlExprCompact(&pCondExpr->pTagCond); @@ -6284,10 +6325,11 @@ static int32_t getTagQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCondE } tSqlExprDestroy(p1); - tExprTreeDestroy(p, NULL); //TODO - + tExprTreeDestroy(p, NULL); // TODO + taosArrayDestroy(&colList); - if (pQueryInfo->tagCond.pCond != NULL && taosArrayGetSize(pQueryInfo->tagCond.pCond) > 0 && !UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { + if (pQueryInfo->tagCond.pCond != NULL && taosArrayGetSize(pQueryInfo->tagCond.pCond) > 0 && + !UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "filter on tag not supported for normal table"); } @@ -6304,7 +6346,7 @@ int32_t validateJoinNodes(SQueryInfo* pQueryInfo, SSqlObj* pSql) { const char* msg2 = "tag required for join stables"; for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { - SJoinNode *node = pQueryInfo->tagCond.joinInfo.joinTables[i]; + SJoinNode* node = pQueryInfo->tagCond.joinInfo.joinTables[i]; if (node == NULL || node->tsJoin == NULL || taosArrayGetSize(node->tsJoin) <= 0) { return invalidOperationMsg(tscGetErrorMsgPayload(&pSql->cmd), msg1); @@ -6314,7 +6356,7 @@ int32_t validateJoinNodes(SQueryInfo* pQueryInfo, SSqlObj* pSql) { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { - SJoinNode *node = pQueryInfo->tagCond.joinInfo.joinTables[i]; + SJoinNode* node = pQueryInfo->tagCond.joinInfo.joinTables[i]; if (node == NULL || node->tagJoin == NULL || taosArrayGetSize(node->tagJoin) <= 0) { return invalidOperationMsg(tscGetErrorMsgPayload(&pSql->cmd), msg2); @@ -6325,11 +6367,10 @@ int32_t validateJoinNodes(SQueryInfo* pQueryInfo, SSqlObj* pSql) { return TSDB_CODE_SUCCESS; } - void mergeJoinNodesImpl(int8_t* r, int8_t* p, int16_t* tidx, SJoinNode** nodes, int32_t type) { - SJoinNode *node = nodes[*tidx]; - SArray* arr = (type == 0) ? node->tsJoin : node->tagJoin; - size_t size = taosArrayGetSize(arr); + SJoinNode* node = nodes[*tidx]; + SArray* arr = (type == 0) ? node->tsJoin : node->tagJoin; + size_t size = taosArrayGetSize(arr); p[*tidx] = 1; @@ -6388,7 +6429,6 @@ int32_t mergeJoinNodes(SQueryInfo* pQueryInfo, SSqlObj* pSql) { if (taosArrayGetSize(pQueryInfo->tagCond.joinInfo.joinTables[0]->tagJoin) != pQueryInfo->numOfTables) { return invalidOperationMsg(tscGetErrorMsgPayload(&pSql->cmd), msg2); } - } return TSDB_CODE_SUCCESS; @@ -6400,13 +6440,13 @@ static int32_t getQueryTimeRange(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr if (*pExpr == NULL) { return ret; } - - //multiple tables's query time range mixed together - + + // multiple tables's query time range mixed together + tExprNode* p = NULL; - void *filter = NULL; + void* filter = NULL; - SArray* colList = taosArrayInit(10, sizeof(SColIndex)); + SArray* colList = taosArrayInit(10, sizeof(SColIndex)); ret = exprTreeFromSqlExpr(pCmd, &p, *pExpr, pQueryInfo, colList, NULL); taosArrayDestroy(&colList); @@ -6414,7 +6454,7 @@ static int32_t getQueryTimeRange(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr goto _ret; } - ret = filterInitFromTree(p, &filter, FI_OPTION_NO_REWRITE|FI_OPTION_TIMESTAMP); + ret = filterInitFromTree(p, &filter, FI_OPTION_NO_REWRITE | FI_OPTION_TIMESTAMP); if (ret != TSDB_CODE_SUCCESS) { goto _ret; } @@ -6433,28 +6473,26 @@ _ret: return ret; } - - int32_t validateWhereNode(SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SSqlObj* pSql, bool joinQuery) { if (pExpr == NULL) { return TSDB_CODE_SUCCESS; } const char* msg1 = "invalid expression"; -// const char* msg2 = "invalid filter expression"; + // const char* msg2 = "invalid filter expression"; int32_t ret = TSDB_CODE_SUCCESS; // tags query condition may be larger than 512bytes, therefore, we need to prepare enough large space - SCondExpr condExpr = {0}; + SCondExpr condExpr = {0}; if ((*pExpr)->pLeft == NULL || (*pExpr)->pRight == NULL) { return invalidOperationMsg(tscGetErrorMsgPayload(&pSql->cmd), msg1); } - int32_t type = 0; - int32_t tbIdx = 0; - int32_t *etype = &type; + int32_t type = 0; + int32_t tbIdx = 0; + int32_t* etype = &type; #if 0 //DISABLE PARENT CONDITION GROUP TYPE CHECK @@ -6463,7 +6501,8 @@ int32_t validateWhereNode(SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SSqlObj* pSq } #endif - if ((ret = getQueryCondExpr(&pSql->cmd, pQueryInfo, pExpr, &condExpr, etype, &tbIdx, (*pExpr)->tokenId, &condExpr.pColumnCond, &condExpr.pTimewindow, joinQuery)) != TSDB_CODE_SUCCESS) { + if ((ret = getQueryCondExpr(&pSql->cmd, pQueryInfo, pExpr, &condExpr, etype, &tbIdx, (*pExpr)->tokenId, + &condExpr.pColumnCond, &condExpr.pTimewindow, joinQuery)) != TSDB_CODE_SUCCESS) { goto PARSE_WHERE_EXIT; } @@ -6511,12 +6550,12 @@ int32_t validateWhereNode(SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SSqlObj* pSq goto PARSE_WHERE_EXIT; } - //if (!validateFilterExpr(pQueryInfo)) { - // ret = invalidOperationMsg(tscGetErrorMsgPayload(&pSql->cmd), msg2); - // goto PARSE_WHERE_EXIT; - //} + // if (!validateFilterExpr(pQueryInfo)) { + // ret = invalidOperationMsg(tscGetErrorMsgPayload(&pSql->cmd), msg2); + // goto PARSE_WHERE_EXIT; + // } - //doAddJoinTagsColumnsIntoTagList(&pSql->cmd, pQueryInfo, &condExpr); + // doAddJoinTagsColumnsIntoTagList(&pSql->cmd, pQueryInfo, &condExpr); if (condExpr.tsJoin) { ret = validateJoinNodes(pQueryInfo, pSql); if (ret) { @@ -6627,7 +6666,8 @@ int32_t tsRewriteFieldNameIfNecessary(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) { char* fieldName = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, i)->name; for (int32_t j = i + 1; j < pQueryInfo->fieldsInfo.numOfOutput; ++j) { - if (strncasecmp(fieldName, tscFieldInfoGetField(&pQueryInfo->fieldsInfo, j)->name, (TSDB_COL_NAME_LEN - 1)) == 0) { + if (strncasecmp(fieldName, tscFieldInfoGetField(&pQueryInfo->fieldsInfo, j)->name, (TSDB_COL_NAME_LEN - 1)) == + 0) { const char* msg = "duplicated column name in new table"; return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } @@ -6658,7 +6698,7 @@ int32_t validateFillNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNo if ((!isTimeWindowQuery(pQueryInfo)) && (!pointInterp)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg5); } - + if (QUERY_IS_JOIN_QUERY(pQueryInfo->type) && (!pointInterp)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg7); } @@ -6674,11 +6714,11 @@ int32_t validateFillNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNo if (pItem->pVar.nType != TSDB_DATA_TYPE_BINARY) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } - + size_t numOfFields = tscNumOfFields(pQueryInfo); - + if (pQueryInfo->fillVal == NULL) { - pQueryInfo->fillVal = calloc(numOfFields, sizeof(int64_t)); + pQueryInfo->fillVal = calloc(numOfFields, sizeof(int64_t)); pQueryInfo->numOfFillVal = (int32_t)numOfFields; if (pQueryInfo->fillVal == NULL) { return TSDB_CODE_TSC_OUT_OF_MEMORY; @@ -6718,7 +6758,7 @@ int32_t validateFillNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNo numOfFillVal = (int32_t)numOfFields; } } else { - numOfFillVal = (int16_t)((num > (int32_t)numOfFields) ? (int32_t)numOfFields : num); + numOfFillVal = (int16_t)((num > (int32_t)numOfFields) ? (int32_t)numOfFields : num); } int32_t j = 1; @@ -6727,17 +6767,17 @@ int32_t validateFillNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNo TAOS_FIELD* pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, i); if (pField->type == TSDB_DATA_TYPE_BINARY || pField->type == TSDB_DATA_TYPE_NCHAR) { - setVardataNull((char*) &pQueryInfo->fillVal[i], pField->type); + setVardataNull((char*)&pQueryInfo->fillVal[i], pField->type); continue; } tVariant* p = taosArrayGet(pFillToken, j); - int32_t ret = tVariantDump(p, (char*)&pQueryInfo->fillVal[i], pField->type, true); + int32_t ret = tVariantDump(p, (char*)&pQueryInfo->fillVal[i], pField->type, true); if (ret != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } } - + if ((num < numOfFields) || ((num - 1 < numOfFields) && pointInterp)) { tVariantListItem* lastItem = taosArrayGetLast(pFillToken); @@ -6745,7 +6785,7 @@ int32_t validateFillNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNo TAOS_FIELD* pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, i); if (pField->type == TSDB_DATA_TYPE_BINARY || pField->type == TSDB_DATA_TYPE_NCHAR) { - setVardataNull((char*) &pQueryInfo->fillVal[i], pField->type); + setVardataNull((char*)&pQueryInfo->fillVal[i], pField->type); } else { tVariantDump(&lastItem->pVar, (char*)&pQueryInfo->fillVal[i], pField->type, true); } @@ -6756,10 +6796,10 @@ int32_t validateFillNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNo } size_t numOfExprs = tscNumOfExprs(pQueryInfo); - for(int32_t i = 0; i < numOfExprs; ++i) { + for (int32_t i = 0; i < numOfExprs; ++i) { SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - if (pExpr->base.functionId == TSDB_FUNC_TOP || pExpr->base.functionId == TSDB_FUNC_BOTTOM - || pExpr->base.functionId == TSDB_FUNC_SAMPLE || pExpr->base.functionId == TSDB_FUNC_HISTOGRAM) { + if (pExpr->base.functionId == TSDB_FUNC_TOP || pExpr->base.functionId == TSDB_FUNC_BOTTOM || + pExpr->base.functionId == TSDB_FUNC_SAMPLE || pExpr->base.functionId == TSDB_FUNC_HISTOGRAM) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } } @@ -6767,29 +6807,28 @@ int32_t validateFillNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNo return TSDB_CODE_SUCCESS; } - - int32_t validateRangeNode(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNode* pSqlNode) { - const char *msg0 = "invalid usage of range clause"; + const char* msg0 = "invalid usage of range clause"; const char* msg1 = "invalid timestamp in range"; - SSqlCmd* pCmd = &pSql->cmd; - + SSqlCmd* pCmd = &pSql->cmd; + bool interpQuery = tscIsPointInterpQuery(pQueryInfo); if ((!interpQuery) && (pSqlNode->pRange.start || pSqlNode->pRange.end)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg0); } - + if (pSqlNode->pRange.start == NULL || pSqlNode->pRange.end == NULL) { pQueryInfo->range.skey = INT64_MIN; pQueryInfo->range.ekey = INT64_MIN; - tscDebug("0x%"PRIx64" range [%"PRId64",%"PRId64"], ts [%"PRId64",%"PRId64"]", pSql->self, pQueryInfo->range.skey, pQueryInfo->range.ekey, pQueryInfo->window.skey, pQueryInfo->window.ekey); + tscDebug("0x%" PRIx64 " range [%" PRId64 ",%" PRId64 "], ts [%" PRId64 ",%" PRId64 "]", pSql->self, + pQueryInfo->range.skey, pQueryInfo->range.ekey, pQueryInfo->window.skey, pQueryInfo->window.ekey); return TSDB_CODE_SUCCESS; } STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); if (getTimeRange(&pQueryInfo->range, pSqlNode->pRange.start, TK_GE, tinfo.precision) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); @@ -6803,19 +6842,25 @@ int32_t validateRangeNode(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNode* pSqlN return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - if ((pQueryInfo->range.skey > pQueryInfo->window.ekey && (pQueryInfo->fillType == TSDB_FILL_NONE || pQueryInfo->fillType == TSDB_FILL_LINEAR || pQueryInfo->fillType == TSDB_FILL_NEXT)) - || (pQueryInfo->range.ekey < pQueryInfo->window.skey && (pQueryInfo->fillType == TSDB_FILL_NONE || pQueryInfo->fillType == TSDB_FILL_LINEAR || pQueryInfo->fillType == TSDB_FILL_PREV))) { - tscDebug("0x%"PRIx64" range [%"PRId64",%"PRId64"], ts [%"PRId64",%"PRId64"], no output result", pSql->self, pQueryInfo->range.skey, pQueryInfo->range.ekey, pQueryInfo->window.skey, pQueryInfo->window.ekey); + if ((pQueryInfo->range.skey > pQueryInfo->window.ekey && + (pQueryInfo->fillType == TSDB_FILL_NONE || pQueryInfo->fillType == TSDB_FILL_LINEAR || + pQueryInfo->fillType == TSDB_FILL_NEXT)) || + (pQueryInfo->range.ekey < pQueryInfo->window.skey && + (pQueryInfo->fillType == TSDB_FILL_NONE || pQueryInfo->fillType == TSDB_FILL_LINEAR || + pQueryInfo->fillType == TSDB_FILL_PREV))) { + tscDebug("0x%" PRIx64 " range [%" PRId64 ",%" PRId64 "], ts [%" PRId64 ",%" PRId64 "], no output result", + pSql->self, pQueryInfo->range.skey, pQueryInfo->range.ekey, pQueryInfo->window.skey, + pQueryInfo->window.ekey); pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; return TSDB_CODE_SUCCESS; } - tscDebug("0x%"PRIx64" range [%"PRId64",%"PRId64"], ts [%"PRId64",%"PRId64"]", pSql->self, pQueryInfo->range.skey, pQueryInfo->range.ekey, pQueryInfo->window.skey, pQueryInfo->window.ekey); + tscDebug("0x%" PRIx64 " range [%" PRId64 ",%" PRId64 "], ts [%" PRId64 ",%" PRId64 "]", pSql->self, + pQueryInfo->range.skey, pQueryInfo->range.ekey, pQueryInfo->window.skey, pQueryInfo->window.ekey); return TSDB_CODE_SUCCESS; } - static void setDefaultOrderInfo(SQueryInfo* pQueryInfo) { /* set default timestamp order information for all queries */ STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); @@ -6823,7 +6868,7 @@ static void setDefaultOrderInfo(SQueryInfo* pQueryInfo) { pQueryInfo->order.order = TSDB_ORDER_ASC; if (isTopBottomUniqueQuery(pQueryInfo)) { pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; - } else { // in case of select tbname from super_table, the default order column can not be the primary ts column + } else { // in case of select tbname from super_table, the default order column can not be the primary ts column pQueryInfo->order.orderColId = INT32_MIN; // todo define a macro } @@ -6857,9 +6902,9 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq setDefaultOrderInfo(pQueryInfo); STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); if (pSqlNode->pSortOrder == NULL) { - return TSDB_CODE_SUCCESS; - } - char* pMsgBuf = tscGetErrorMsgPayload(pCmd); + return TSDB_CODE_SUCCESS; + } + char* pMsgBuf = tscGetErrorMsgPayload(pCmd); SArray* pSortOrder = pSqlNode->pSortOrder; /* @@ -6888,16 +6933,16 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq return invalidOperationMsg(pMsgBuf, msg10); } - SStrToken columnName = {0}; + SStrToken columnName = {0}; CommonItem* pItem = taosArrayGet(pSortOrder, 0); - if (pItem->isJsonExp){ + if (pItem->isJsonExp) { assert(pItem->jsonExp->tokenId == TK_ARROW); columnName = pItem->jsonExp->pLeft->columnName; - }else{ + } else { // handle the first part of order by tVariant* pVar = &pItem->pVar; - if (pVar->nType != TSDB_DATA_TYPE_BINARY){ + if (pVar->nType != TSDB_DATA_TYPE_BINARY) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } @@ -6907,7 +6952,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq } SColumnIndex index = COLUMN_INDEX_INITIALIZER; - bool udf = false; + bool udf = false; if (pQueryInfo->pUdfInfo && taosArrayGetSize(pQueryInfo->pUdfInfo) > 0) { int32_t usize = (int32_t)taosArrayGetSize(pQueryInfo->pUdfInfo); @@ -6930,7 +6975,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq bool orderByTS = false; bool orderByGroupbyCol = false; - if (index.columnIndex >= tscGetNumOfColumns(pTableMetaInfo->pTableMeta)) { // order by tag1 + if (index.columnIndex >= tscGetNumOfColumns(pTableMetaInfo->pTableMeta)) { // order by tag1 int32_t relTagIndex = index.columnIndex - tscGetNumOfColumns(pTableMetaInfo->pTableMeta); // it is a tag column @@ -6939,23 +6984,23 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq } SColIndex* pColIndex = taosArrayGet(pQueryInfo->groupbyExpr.columnInfo, 0); if (relTagIndex == pColIndex->colIndex && pColIndex->flag == TSDB_COL_TAG) { - if (tscGetColumnSchemaById(pTableMetaInfo->pTableMeta, pColIndex->colId)->type == TSDB_DATA_TYPE_JSON){ - if(!pItem->isJsonExp){ + if (tscGetColumnSchemaById(pTableMetaInfo->pTableMeta, pColIndex->colId)->type == TSDB_DATA_TYPE_JSON) { + if (!pItem->isJsonExp) { return invalidOperationMsg(pMsgBuf, msg14); } - if(pItem->jsonExp->exprToken.n >= sizeof(pColIndex->name)){ + if (pItem->jsonExp->exprToken.n >= sizeof(pColIndex->name)) { return invalidOperationMsg(pMsgBuf, msg13); } - if(strncmp(pColIndex->name, pItem->jsonExp->exprToken.z, pItem->jsonExp->exprToken.n) == 0){ + if (strncmp(pColIndex->name, pItem->jsonExp->exprToken.z, pItem->jsonExp->exprToken.n) == 0) { orderByTags = true; - }else{ + } else { orderByTags = false; } - }else{ + } else { orderByTags = true; } } - } else if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { // order by tbname + } else if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { // order by tbname // it is a tag column if (pQueryInfo->groupbyExpr.columnInfo == NULL) { return invalidOperationMsg(pMsgBuf, msg4); @@ -6964,10 +7009,10 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq if (TSDB_TBNAME_COLUMN_INDEX == pColIndex->colIndex) { orderByTags = true; } - }else if (index.columnIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX) { // order by ts + } else if (index.columnIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX) { // order by ts orderByTS = true; - }else{ // order by normal column - SArray *columnInfo = pQueryInfo->groupbyExpr.columnInfo; + } else { // order by normal column + SArray* columnInfo = pQueryInfo->groupbyExpr.columnInfo; if (columnInfo != NULL && taosArrayGetSize(columnInfo) > 0) { SColIndex* pColIndex = taosArrayGet(columnInfo, 0); if (pColIndex->colIndex == index.columnIndex) { @@ -6984,11 +7029,10 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq if (tscIsDiffDerivLikeQuery(pQueryInfo)) { return invalidOperationMsg(pMsgBuf, msg12); } - //pQueryInfo->groupbyExpr.orderIndex = index.columnIndex - tscGetNumOfColumns(pTableMetaInfo->pTableMeta); + // pQueryInfo->groupbyExpr.orderIndex = index.columnIndex - tscGetNumOfColumns(pTableMetaInfo->pTableMeta); pQueryInfo->groupbyExpr.orderType = pItem->sortOrder; } else if (orderByGroupbyCol) { - pQueryInfo->groupbyExpr.orderType = pItem->sortOrder; if (udf) { return invalidOperationMsg(pMsgBuf, msg11); @@ -7018,16 +7062,16 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq // orderby ts query on super table if (tscOrderedProjectionQueryOnSTable(pQueryInfo, 0)) { - addPrimaryTsColIntoResult(pQueryInfo, pCmd); + addPrimaryTsColIntoResult(pQueryInfo, pCmd); } } - if(taosArrayGetSize(pSortOrder) == 2){ + if (taosArrayGetSize(pSortOrder) == 2) { SStrToken cname = {0}; pItem = taosArrayGet(pSqlNode->pSortOrder, 1); - if (pItem->isJsonExp){ + if (pItem->isJsonExp) { assert(pItem->jsonExp->tokenId == TK_ARROW); cname = pItem->jsonExp->pLeft->columnName; - }else{ + } else { tVariant* pVar = &pItem->pVar; cname.n = pVar->nLen; @@ -7044,14 +7088,15 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq pQueryInfo->order.order = pItem->sortOrder; pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; } - } else if (UTIL_TABLE_IS_NORMAL_TABLE(pTableMetaInfo) || UTIL_TABLE_IS_CHILD_TABLE(pTableMetaInfo)) { // check order by clause for normal table & temp table + } 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 invalidOperationMsg(pMsgBuf, msg1); } - if (index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX && !isTopBottomUniqueQuery(pQueryInfo)){ - bool validOrder = false; - SArray *columnInfo = pQueryInfo->groupbyExpr.columnInfo; + if (index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX && !isTopBottomUniqueQuery(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); @@ -7061,8 +7106,8 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq return invalidOperationMsg(pMsgBuf, msg7); } pQueryInfo->groupbyExpr.orderType = pItem->sortOrder; - }else if (isTopBottomUniqueQuery(pQueryInfo)) { - SArray *columnInfo = pQueryInfo->groupbyExpr.columnInfo; + } else if (isTopBottomUniqueQuery(pQueryInfo)) { + SArray* columnInfo = pQueryInfo->groupbyExpr.columnInfo; if (columnInfo != NULL && taosArrayGetSize(columnInfo) > 0) { SColIndex* pColIndex = taosArrayGet(columnInfo, 0); @@ -7083,7 +7128,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq } pQueryInfo->order.order = pItem->sortOrder; pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; - }else{ + } else { pQueryInfo->order.order = pItem->sortOrder; pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; } @@ -7111,16 +7156,17 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq if (tscIsProjectionQuery(pQueryInfo)) { bool found = false; for (int32_t i = 0; i < tscNumOfExprs(pQueryInfo); ++i) { - SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - if (pExpr->base.functionId == TSDB_FUNC_PRJ && pExpr->base.colInfo.colId == pSchema[index.columnIndex].colId) { - found = true; - break; - } + SExprInfo* pExpr = tscExprGet(pQueryInfo, i); + if (pExpr->base.functionId == TSDB_FUNC_PRJ && pExpr->base.colInfo.colId == pSchema[index.columnIndex].colId) { + found = true; + break; + } } if (!found) { int32_t numOfCols = (int32_t)tscNumOfFields(pQueryInfo); - tscAddFuncInSelectClause(pQueryInfo, numOfCols, TSDB_FUNC_PRJ, &index, pSchema, TSDB_COL_NORMAL, getNewResColId(pCmd)); + tscAddFuncInSelectClause(pQueryInfo, numOfCols, TSDB_FUNC_PRJ, &index, pSchema, TSDB_COL_NORMAL, + getNewResColId(pCmd)); SInternalField* pSupInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, numOfCols); pSupInfo->visible = false; @@ -7170,15 +7216,15 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { int32_t code = TSDB_CODE_SUCCESS; - SSqlCmd* pCmd = &pSql->cmd; + SSqlCmd* pCmd = &pSql->cmd; SAlterTableInfo* pAlterSQL = pInfo->pAlterInfo; - SQueryInfo* pQueryInfo = tscGetQueryInfo(pCmd); + SQueryInfo* pQueryInfo = tscGetQueryInfo(pCmd); STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, DEFAULT_TABLE_INDEX); - bool dbIncluded = false; + bool dbIncluded = false; SStrToken tmpToken = pAlterSQL->name; - tmpToken.z= strndup(pAlterSQL->name.z, pAlterSQL->name.n); + tmpToken.z = strndup(pAlterSQL->name.z, pAlterSQL->name.n); if (tscValidateName(&tmpToken, true, &dbIncluded) != TSDB_CODE_SUCCESS) { free(tmpToken.z); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); @@ -7196,7 +7242,7 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { return code; } - char* pMsg = tscGetErrorMsgPayload(pCmd); + char* pMsg = tscGetErrorMsgPayload(pCmd); STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; if (pAlterSQL->tableType == TSDB_SUPER_TABLE && !(UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo))) { @@ -7210,7 +7256,8 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } } else if ((pAlterSQL->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) && (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo))) { return invalidOperationMsg(pMsg, msg4); - } else if ((pAlterSQL->type == TSDB_ALTER_TABLE_ADD_COLUMN || pAlterSQL->type == TSDB_ALTER_TABLE_DROP_COLUMN || pAlterSQL->type == TSDB_ALTER_TABLE_CHANGE_COLUMN) && + } else if ((pAlterSQL->type == TSDB_ALTER_TABLE_ADD_COLUMN || pAlterSQL->type == TSDB_ALTER_TABLE_DROP_COLUMN || + pAlterSQL->type == TSDB_ALTER_TABLE_CHANGE_COLUMN) && UTIL_TABLE_IS_CHILD_TABLE(pTableMetaInfo)) { return invalidOperationMsg(pMsg, msg6); } @@ -7222,7 +7269,7 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } TAOS_FIELD* p = taosArrayGet(pFieldList, 0); - int32_t ret = validateOneTag(pCmd, p); + int32_t ret = validateOneTag(pCmd, p); if (ret != TSDB_CODE_SUCCESS) { return ret; } @@ -7312,9 +7359,9 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { // the following is used to handle tags value for table created according to super table pCmd->command = TSDB_SQL_UPDATE_TAGS_VAL; - SArray* pVarList = pAlterSQL->varList; + SArray* pVarList = pAlterSQL->varList; tVariantListItem* item = taosArrayGet(pVarList, 0); - int16_t numOfTags = tscGetNumOfTags(pTableMeta); + int16_t numOfTags = tscGetNumOfTags(pTableMeta); SColumnIndex columnIndex = COLUMN_INDEX_INITIALIZER; SStrToken name = {.z = item->pVar.pz, .n = item->pVar.nLen}; @@ -7327,7 +7374,7 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } tVariantListItem* pItem = taosArrayGet(pVarList, 1); - SSchema* pTagsSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, columnIndex.columnIndex); + SSchema* pTagsSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, columnIndex.columnIndex); if (IS_VAR_DATA_TYPE(pTagsSchema->type) && (pItem->pVar.nLen > pTagsSchema->bytes)) { return invalidOperationMsg(pMsg, msg14); @@ -7335,12 +7382,12 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { STableComInfo tinfo = tscGetTableInfo(pTableMeta); if (pItem->pVar.nType == TSDB_DATA_TYPE_BINARY) { - code = convertTimestampStrToInt64(&(pItem->pVar), tinfo.precision); - if (code != TSDB_CODE_SUCCESS) { - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg26); - } + code = convertTimestampStrToInt64(&(pItem->pVar), tinfo.precision); + if (code != TSDB_CODE_SUCCESS) { + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg26); + } } else if (pItem->pVar.nType == TSDB_DATA_TYPE_TIMESTAMP) { - pItem->pVar.i64 = convertTimePrecision(pItem->pVar.i64, TSDB_TIME_PRECISION_NANO, tinfo.precision); + pItem->pVar.i64 = convertTimePrecision(pItem->pVar.i64, TSDB_TIME_PRECISION_NANO, tinfo.precision); } } @@ -7373,36 +7420,36 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { int32_t size = sizeof(SUpdateTableTagValMsg) + pTagsSchema->bytes + schemaLen + TSDB_EXTRA_PAYLOAD_SIZE; if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, size)) { - tscError("0x%"PRIx64" failed to malloc for alter table pMsg", pSql->self); + tscError("0x%" PRIx64 " failed to malloc for alter table pMsg", pSql->self); return TSDB_CODE_TSC_OUT_OF_MEMORY; } - SUpdateTableTagValMsg* pUpdateMsg = (SUpdateTableTagValMsg*) pCmd->payload; + SUpdateTableTagValMsg* pUpdateMsg = (SUpdateTableTagValMsg*)pCmd->payload; pUpdateMsg->head.vgId = htonl(pTableMeta->vgId); - pUpdateMsg->tid = htonl(pTableMeta->id.tid); - pUpdateMsg->uid = htobe64(pTableMeta->id.uid); - pUpdateMsg->colId = htons(pTagsSchema->colId); - pUpdateMsg->type = pTagsSchema->type; - pUpdateMsg->bytes = htons(pTagsSchema->bytes); - pUpdateMsg->tversion = htons(pTableMeta->tversion); + pUpdateMsg->tid = htonl(pTableMeta->id.tid); + pUpdateMsg->uid = htobe64(pTableMeta->id.uid); + pUpdateMsg->colId = htons(pTagsSchema->colId); + pUpdateMsg->type = pTagsSchema->type; + pUpdateMsg->bytes = htons(pTagsSchema->bytes); + pUpdateMsg->tversion = htons(pTableMeta->tversion); pUpdateMsg->numOfTags = htons(numOfTags); pUpdateMsg->schemaLen = htonl(schemaLen); // the schema is located after the pMsg body, then followed by true tag value - char* d = pUpdateMsg->data; + char* d = pUpdateMsg->data; SSchema* pTagCols = tscGetTableTagSchema(pTableMeta); for (int i = 0; i < numOfTags; ++i) { - STColumn* pCol = (STColumn*) d; + STColumn* pCol = (STColumn*)d; pCol->colId = htons(pTagCols[i].colId); pCol->bytes = htons(pTagCols[i].bytes); - pCol->type = pTagCols[i].type; + pCol->type = pTagCols[i].type; pCol->offset = 0; d += sizeof(STColumn); } - if (pTagsSchema->type == TSDB_DATA_TYPE_JSON){ + if (pTagsSchema->type == TSDB_DATA_TYPE_JSON) { SKVRow row = tdGetKVRowFromBuilder(&kvRowBuilder); tdDestroyKVRowBuilder(&kvRowBuilder); if (row == NULL) { @@ -7412,29 +7459,28 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { kvRowCpy(pUpdateMsg->data + schemaLen, row); free(row); - }else{ + } else { // copy the tag value to pMsg body - if (tVariantDump(&pItem->pVar, pUpdateMsg->data + schemaLen, pTagsSchema->type, true) - != TSDB_CODE_SUCCESS){ + if (tVariantDump(&pItem->pVar, pUpdateMsg->data + schemaLen, pTagsSchema->type, true) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(pMsg, msg13); } } int32_t len = 0; - if(pTagsSchema->type == TSDB_DATA_TYPE_JSON){ + if (pTagsSchema->type == TSDB_DATA_TYPE_JSON) { len = kvRowLen(pUpdateMsg->data + schemaLen); - }else if (!IS_VAR_DATA_TYPE(pTagsSchema->type)) { + } else if (!IS_VAR_DATA_TYPE(pTagsSchema->type)) { len = tDataTypes[pTagsSchema->type].bytes; } else { len = varDataTLen(pUpdateMsg->data + schemaLen); - if(len > pTagsSchema->bytes) return invalidOperationMsg(pMsg, msg14); + if (len > pTagsSchema->bytes) return invalidOperationMsg(pMsg, msg14); } - + pUpdateMsg->tagValLen = htonl(len); // length may be changed after dump data - + int32_t total = sizeof(SUpdateTableTagValMsg) + len + schemaLen; pUpdateMsg->head.contLen = htonl(total); - + } else if (pAlterSQL->type == TSDB_ALTER_TABLE_ADD_COLUMN) { SArray* pFieldList = pAlterSQL->pAddColumns; if (taosArrayGetSize(pFieldList) > 1) { @@ -7443,11 +7489,11 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } TAOS_FIELD* p = taosArrayGet(pFieldList, 0); - int32_t ret = validateOneColumn(pCmd, p); + int32_t ret = validateOneColumn(pCmd, p); if (ret != TSDB_CODE_SUCCESS) { return ret; } - + tscFieldInfoAppend(&pQueryInfo->fieldsInfo, p); } else if (pAlterSQL->type == TSDB_ALTER_TABLE_DROP_COLUMN) { if (tscGetNumOfColumns(pTableMeta) == TSDB_MIN_COLUMNS) { // @@ -7514,9 +7560,9 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { return tscErrorMsgWithCode(TSDB_CODE_TSC_INVALID_COLUMN_LENGTH, pMsg, pItem->name, NULL); } - SSchema* pSchema = (SSchema*) pTableMetaInfo->pTableMeta->schema; - int16_t numOfColumns = pTableMetaInfo->pTableMeta->tableInfo.numOfColumns; - int16_t i; + SSchema* pSchema = (SSchema*)pTableMetaInfo->pTableMeta->schema; + int16_t numOfColumns = pTableMetaInfo->pTableMeta->tableInfo.numOfColumns; + int16_t i; uint32_t nLen = 0; for (i = 0; i < numOfColumns; ++i) { nLen += (i != columnIndex.columnIndex) ? pSchema[i].bytes : pItem->bytes; @@ -7528,7 +7574,7 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { stringProcess(name.z, name.n); TAOS_FIELD f = tscCreateField(pColSchema->type, name.z, pItem->bytes); tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); - }else if (pAlterSQL->type == TSDB_ALTER_TABLE_MODIFY_TAG_COLUMN) { + } else if (pAlterSQL->type == TSDB_ALTER_TABLE_MODIFY_TAG_COLUMN) { if (taosArrayGetSize(pAlterSQL->pAddColumns) >= 2) { return invalidOperationMsg(pMsg, msg16); } @@ -7568,10 +7614,10 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } SSchema* pSchema = tscGetTableTagSchema(pTableMetaInfo->pTableMeta); - int16_t numOfTags = tscGetNumOfTags(pTableMetaInfo->pTableMeta); - int32_t numOfCols = tscGetNumOfColumns(pTableMetaInfo->pTableMeta); - int32_t tagIndex = columnIndex.columnIndex - numOfCols; - assert(tagIndex>=0); + int16_t numOfTags = tscGetNumOfTags(pTableMetaInfo->pTableMeta); + int32_t numOfCols = tscGetNumOfColumns(pTableMetaInfo->pTableMeta); + int32_t tagIndex = columnIndex.columnIndex - numOfCols; + assert(tagIndex >= 0); uint32_t nLen = 0; for (int i = 0; i < numOfTags; ++i) { nLen += (i != tagIndex) ? pSchema[i].bytes : pItem->bytes; @@ -7590,17 +7636,16 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } int32_t validateSqlFunctionInStreamSql(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { - const char* msg0 = "sample interval can not be less than 10ms."; - const char* msg1 = "functions not allowed in select clause"; + const char* msg0 = "sample interval can not be less than 10ms."; + const char* msg1 = "functions not allowed in select clause"; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); if (pQueryInfo->interval.interval != 0 && - convertTimePrecision(pQueryInfo->interval.interval, tinfo.precision, TSDB_TIME_PRECISION_MILLI)< 10 && - pQueryInfo->interval.intervalUnit != 'n' && - pQueryInfo->interval.intervalUnit != 'y') { + convertTimePrecision(pQueryInfo->interval.interval, tinfo.precision, TSDB_TIME_PRECISION_MILLI) < 10 && + pQueryInfo->interval.intervalUnit != 'n' && pQueryInfo->interval.intervalUnit != 'y') { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg0); } - + size_t size = taosArrayGetSize(pQueryInfo->exprList); for (int32_t i = 0; i < size; ++i) { int32_t functId = tscExprGet(pQueryInfo, i)->base.functionId; @@ -7621,7 +7666,7 @@ int32_t validateFunctionsInIntervalOrGroupbyQuery(SSqlCmd* pCmd, SQueryInfo* pQu // multi-output set/ todo refactor size_t size = taosArrayGetSize(pQueryInfo->exprList); - + for (int32_t k = 0; k < size; ++k) { SExprInfo* pExpr = tscExprGet(pQueryInfo, k); @@ -7657,11 +7702,9 @@ int32_t validateFunctionsInIntervalOrGroupbyQuery(SSqlCmd* pCmd, SQueryInfo* pQu } int32_t f = pExpr->base.functionId; - if ((f == TSDB_FUNC_PRJ && pExpr->base.numOfParams == 0) || - f == TSDB_FUNC_DIFF || f == TSDB_FUNC_SCALAR_EXPR || f == TSDB_FUNC_DERIVATIVE || - f == TSDB_FUNC_CSUM || f == TSDB_FUNC_MAVG || - f == TSDB_FUNC_STATE_COUNT || f == TSDB_FUNC_STATE_DURATION) - { + if ((f == TSDB_FUNC_PRJ && pExpr->base.numOfParams == 0) || f == TSDB_FUNC_DIFF || f == TSDB_FUNC_SCALAR_EXPR || + f == TSDB_FUNC_DERIVATIVE || f == TSDB_FUNC_CSUM || f == TSDB_FUNC_MAVG || f == TSDB_FUNC_STATE_COUNT || + f == TSDB_FUNC_STATE_DURATION) { isProjectionFunction = true; break; } @@ -7679,8 +7722,7 @@ typedef struct SDNodeDynConfOption { int32_t len; // name string length } SDNodeDynConfOption; - -int32_t validateEp(char* ep) { +int32_t validateEp(char* ep) { char buf[TSDB_EP_LEN + 1] = {0}; tstrncpy(buf, ep, TSDB_EP_LEN); @@ -7701,28 +7743,26 @@ int32_t validateEp(char* ep) { } int32_t validateDNodeConfig(SMiscInfo* pOptions) { - int32_t numOfToken = (int32_t) taosArrayGetSize(pOptions->a); + int32_t numOfToken = (int32_t)taosArrayGetSize(pOptions->a); if (numOfToken < 2 || numOfToken > 3) { return TSDB_CODE_TSC_INVALID_OPERATION; } - const int tokenLogEnd = 2; - const int tokenBalance = 2; - const int tokenMonitor = 3; - const int tokenDebugFlag = 4; - const int tokenDebugFlagEnd = 19; - const int tokenOfflineInterval = 20; - const int tokenKeepTimeOffset = 21; + const int tokenLogEnd = 2; + const int tokenBalance = 2; + const int tokenMonitor = 3; + const int tokenDebugFlag = 4; + const int tokenDebugFlagEnd = 19; + const int tokenOfflineInterval = 20; + const int tokenKeepTimeOffset = 21; const SDNodeDynConfOption cfgOptions[] = { - {"resetLog", 8}, {"resetQueryCache", 15}, {"balance", 7}, {"monitor", 7}, - {"debugFlag", 9}, {"monDebugFlag", 12}, {"vDebugFlag", 10}, {"mDebugFlag", 10}, - {"cDebugFlag", 10}, {"httpDebugFlag", 13}, {"qDebugflag", 10}, {"sdbDebugFlag", 12}, - {"uDebugFlag", 10}, {"tsdbDebugFlag", 13}, {"sDebugflag", 10}, {"rpcDebugFlag", 12}, - {"dDebugFlag", 10}, {"wDebugFlag", 10}, {"tmrDebugFlag", 12}, - {"cqDebugFlag", 11}, - {"offlineInterval", 15}, - {"keepTimeOffset", 14}, + {"resetLog", 8}, {"resetQueryCache", 15}, {"balance", 7}, {"monitor", 7}, + {"debugFlag", 9}, {"monDebugFlag", 12}, {"vDebugFlag", 10}, {"mDebugFlag", 10}, + {"cDebugFlag", 10}, {"httpDebugFlag", 13}, {"qDebugflag", 10}, {"sdbDebugFlag", 12}, + {"uDebugFlag", 10}, {"tsdbDebugFlag", 13}, {"sDebugflag", 10}, {"rpcDebugFlag", 12}, + {"dDebugFlag", 10}, {"wDebugFlag", 10}, {"tmrDebugFlag", 12}, {"cqDebugFlag", 11}, + {"offlineInterval", 15}, {"keepTimeOffset", 14}, }; SStrToken* pOptionToken = taosArrayGet(pOptions->a, 1); @@ -7738,8 +7778,8 @@ int32_t validateDNodeConfig(SMiscInfo* pOptions) { } else if ((strncasecmp(cfgOptions[tokenBalance].name, pOptionToken->z, pOptionToken->n) == 0) && (cfgOptions[tokenBalance].len == pOptionToken->n)) { SStrToken* pValToken = taosArrayGet(pOptions->a, 2); - int32_t vnodeId = 0; - int32_t dnodeId = 0; + int32_t vnodeId = 0; + int32_t dnodeId = 0; stringProcess(pValToken->z, pValToken->n); bool parseOk = taosCheckBalanceCfgOptions(pValToken->z, &vnodeId, &dnodeId); if (!parseOk) { @@ -7793,7 +7833,7 @@ int32_t validateDNodeConfig(SMiscInfo* pOptions) { } int32_t validateLocalConfig(SMiscInfo* pOptions) { - int32_t numOfToken = (int32_t) taosArrayGetSize(pOptions->a); + int32_t numOfToken = (int32_t)taosArrayGetSize(pOptions->a); if (numOfToken < 1 || numOfToken > 2) { return TSDB_CODE_TSC_INVALID_OPERATION; } @@ -7801,15 +7841,13 @@ int32_t validateLocalConfig(SMiscInfo* pOptions) { SDNodeDynConfOption LOCAL_DYNAMIC_CFG_OPTIONS[6] = {{"resetLog", 8}, {"rpcDebugFlag", 12}, {"tmrDebugFlag", 12}, {"cDebugFlag", 10}, {"uDebugFlag", 10}, {"debugFlag", 9}}; - SStrToken* pOptionToken = taosArrayGet(pOptions->a, 0); if (numOfToken == 1) { // reset log does not need value for (int32_t i = 0; i < 1; ++i) { SDNodeDynConfOption* pOption = &LOCAL_DYNAMIC_CFG_OPTIONS[i]; - if ((pOption->len == pOptionToken->n) && - (strncasecmp(pOption->name, pOptionToken->z, pOptionToken->n) == 0)) { + if ((pOption->len == pOptionToken->n) && (strncasecmp(pOption->name, pOptionToken->z, pOptionToken->n) == 0)) { return TSDB_CODE_SUCCESS; } } @@ -7823,8 +7861,7 @@ int32_t validateLocalConfig(SMiscInfo* pOptions) { for (int32_t i = 1; i < tListLen(LOCAL_DYNAMIC_CFG_OPTIONS); ++i) { SDNodeDynConfOption* pOption = &LOCAL_DYNAMIC_CFG_OPTIONS[i]; - if ((pOption->len == pOptionToken->n) - && (strncasecmp(pOption->name, pOptionToken->z, pOptionToken->n) == 0)) { + if ((pOption->len == pOptionToken->n) && (strncasecmp(pOption->name, pOptionToken->z, pOptionToken->n) == 0)) { return TSDB_CODE_SUCCESS; } } @@ -7883,16 +7920,16 @@ int32_t validateLimitNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlN pQueryInfo->limit = pSqlNode->limit; pQueryInfo->clauseLimit = pQueryInfo->limit.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); - + + 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 invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg0); } if (pQueryInfo->limit.limit == 0) { - tscDebug("0x%"PRIx64" limit 0, no output result", pSql->self); + tscDebug("0x%" PRIx64 " limit 0, no output result", pSql->self); pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; return TSDB_CODE_SUCCESS; } @@ -7912,7 +7949,7 @@ int32_t validateLimitNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlN } if (pQueryInfo->slimit.limit == 0) { - tscDebug("0x%"PRIx64" slimit 0, no output result", pSql->self); + tscDebug("0x%" PRIx64 " slimit 0, no output result", pSql->self); pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; return TSDB_CODE_SUCCESS; } @@ -7923,18 +7960,18 @@ int32_t validateLimitNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlN * And then launching multiple async-queries against all qualified virtual nodes, during the first-stage * query operation. */ -// assert(allVgroupInfoRetrieved(pQueryInfo)); + // assert(allVgroupInfoRetrieved(pQueryInfo)); // 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); + tscDebug("0x%" PRIx64 " no table in super table, no output result", pSql->self); pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; return TSDB_CODE_SUCCESS; } // keep original limitation value in globalLimit pQueryInfo->clauseLimit = pQueryInfo->limit.limit; - pQueryInfo->prjOffset = pQueryInfo->limit.offset; + pQueryInfo->prjOffset = pQueryInfo->limit.offset; pQueryInfo->vgroupLimit = -1; if (tscOrderedProjectionQueryOnSTable(pQueryInfo, 0)) { @@ -7972,7 +8009,7 @@ static int32_t setKeepOption(SSqlCmd* pCmd, SCreateDbMsg* pMsg, SCreateDbInfo* p if (pKeep != NULL) { size_t s = taosArrayGetSize(pKeep); #ifdef _STORAGE - if (s >= 4 ||s <= 0) { + if (s >= 4 || s <= 0) { #else if (s != 1) { #endif @@ -7986,8 +8023,8 @@ static int32_t setKeepOption(SSqlCmd* pCmd, SCreateDbMsg* pMsg, SCreateDbInfo* p int32_t daysToKeep0 = (int32_t)p0->pVar.i64; int32_t daysToKeep1 = (int32_t)(int32_t)p1->pVar.i64; int32_t daysToKeep2 = (int32_t)p2->pVar.i64; - if (daysToKeep0 <= 0 || daysToKeep1 <= 0 || daysToKeep2 <= 0 || - daysToKeep0 > TSDB_MAX_KEEP || daysToKeep1 > TSDB_MAX_KEEP || daysToKeep2 > TSDB_MAX_KEEP) { + if (daysToKeep0 <= 0 || daysToKeep1 <= 0 || daysToKeep2 <= 0 || daysToKeep0 > TSDB_MAX_KEEP || + daysToKeep1 > TSDB_MAX_KEEP || daysToKeep2 > TSDB_MAX_KEEP) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -7998,7 +8035,6 @@ static int32_t setKeepOption(SSqlCmd* pCmd, SCreateDbMsg* pMsg, SCreateDbInfo* p pMsg->daysToKeep0 = htonl(daysToKeep0); pMsg->daysToKeep1 = htonl(daysToKeep1); pMsg->daysToKeep2 = htonl(daysToKeep2); - } return TSDB_CODE_SUCCESS; @@ -8028,7 +8064,6 @@ static int32_t setTimePrecision(SSqlCmd* pCmd, SCreateDbMsg* pMsg, SCreateDbInfo } } - return TSDB_CODE_SUCCESS; } @@ -8053,7 +8088,7 @@ static void setCreateDBOption(SCreateDbMsg* pMsg, SCreateDbInfo* pCreateDb) { } int32_t parseCreateDBOptions(SSqlCmd* pCmd, SCreateDbInfo* pCreateDbSql) { - SCreateDbMsg* pMsg = (SCreateDbMsg *)(pCmd->payload); + SCreateDbMsg* pMsg = (SCreateDbMsg*)(pCmd->payload); setCreateDBOption(pMsg, pCreateDbSql); if (setKeepOption(pCmd, pMsg, pCreateDbSql) != TSDB_CODE_SUCCESS) { @@ -8076,7 +8111,7 @@ void addGroupInfoForSubquery(SSqlObj* pParentObj, SSqlObj* pSql, int32_t subClau if (pParentQueryInfo->groupbyExpr.numOfGroupCols > 0) { SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd); - SExprInfo* pExpr = NULL; + SExprInfo* pExpr = NULL; size_t size = taosArrayGetSize(pQueryInfo->exprList); if (size > 0) { @@ -8087,10 +8122,10 @@ void addGroupInfoForSubquery(SSqlObj* pParentObj, SSqlObj* pSql, int32_t subClau STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pParentQueryInfo, tableIndex); uint64_t uid = pTableMetaInfo->pTableMeta->id.uid; - int16_t colId = tscGetJoinTagColIdByUid(&pQueryInfo->tagCond, uid); + int16_t colId = tscGetJoinTagColIdByUid(&pQueryInfo->tagCond, uid); - SSchema* pTagSchema = tscGetColumnSchemaById(pTableMetaInfo->pTableMeta, colId); - int16_t colIndex = tscGetTagColIndexById(pTableMetaInfo->pTableMeta, colId); + SSchema* pTagSchema = tscGetColumnSchemaById(pTableMetaInfo->pTableMeta, colId); + int16_t colIndex = tscGetTagColIndexById(pTableMetaInfo->pTableMeta, colId); SColumnIndex index = {.tableIndex = 0, .columnIndex = colIndex}; char* name = pTagSchema->name; @@ -8124,7 +8159,7 @@ static void doLimitOutputNormalColOfGroupby(SExprInfo* pExpr) { void doAddGroupColumnForSubquery(SQueryInfo* pQueryInfo, int32_t tagIndex, SSqlCmd* pCmd) { SColIndex* pColIndex = taosArrayGet(pQueryInfo->groupbyExpr.columnInfo, tagIndex); - size_t size = tscNumOfExprs(pQueryInfo); + size_t size = tscNumOfExprs(pQueryInfo); STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); @@ -8132,11 +8167,11 @@ void doAddGroupColumnForSubquery(SQueryInfo* pQueryInfo, int32_t tagIndex, SSqlC SColumnIndex colIndex = {.tableIndex = 0, .columnIndex = pColIndex->colIndex}; SExprInfo* pExprInfo = tscAddFuncInSelectClause(pQueryInfo, (int32_t)size, TSDB_FUNC_PRJ, &colIndex, pSchema, - TSDB_COL_NORMAL, getNewResColId(pCmd)); + TSDB_COL_NORMAL, getNewResColId(pCmd)); strncpy(pExprInfo->base.token, pExprInfo->base.colInfo.name, tListLen(pExprInfo->base.token)); - int32_t numOfFields = tscNumOfFields(pQueryInfo); + int32_t numOfFields = tscNumOfFields(pQueryInfo); SInternalField* pInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, numOfFields - 1); doLimitOutputNormalColOfGroupby(pInfo->pExpr); @@ -8145,11 +8180,11 @@ void doAddGroupColumnForSubquery(SQueryInfo* pQueryInfo, int32_t tagIndex, SSqlC static void doUpdateSqlFunctionForTagPrj(SQueryInfo* pQueryInfo) { int32_t tagLength = 0; - size_t size = taosArrayGetSize(pQueryInfo->exprList); + size_t size = taosArrayGetSize(pQueryInfo->exprList); -//todo is 0?? + // todo is 0?? STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - bool isSTable = UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo); + bool isSTable = UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo); for (int32_t i = 0; i < size; ++i) { SExprInfo* pExpr = tscExprGet(pQueryInfo, i); @@ -8171,25 +8206,27 @@ static void doUpdateSqlFunctionForTagPrj(SQueryInfo* pQueryInfo) { } if ((pExpr->base.functionId != TSDB_FUNC_TAG_DUMMY && pExpr->base.functionId != TSDB_FUNC_TS_DUMMY) && - !(pExpr->base.functionId == TSDB_FUNC_PRJ && TSDB_COL_IS_UD_COL(pExpr->base.colInfo.flag))) { + !(pExpr->base.functionId == TSDB_FUNC_PRJ && TSDB_COL_IS_UD_COL(pExpr->base.colInfo.flag))) { SSchema* pColSchema = &pSchema[pExpr->base.colInfo.colIndex]; - getResultDataInfo(pColSchema->type, pColSchema->bytes, pExpr->base.functionId, (int32_t)pExpr->base.param[0].i64, &pExpr->base.resType, - &pExpr->base.resBytes, &pExpr->base.interBytes, tagLength, isSTable, NULL); + getResultDataInfo(pColSchema->type, pColSchema->bytes, pExpr->base.functionId, (int32_t)pExpr->base.param[0].i64, + &pExpr->base.resType, &pExpr->base.resBytes, &pExpr->base.interBytes, tagLength, isSTable, + NULL); } } } static int32_t doUpdateSqlFunctionForColPrj(SQueryInfo* pQueryInfo) { size_t size = taosArrayGetSize(pQueryInfo->exprList); - + for (int32_t i = 0; i < size; ++i) { SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - if (pExpr->base.functionId == TSDB_FUNC_PRJ && (!TSDB_COL_IS_UD_COL(pExpr->base.colInfo.flag) && (pExpr->base.colInfo.colId != PRIMARYKEY_TIMESTAMP_COL_INDEX))) { + if (pExpr->base.functionId == TSDB_FUNC_PRJ && (!TSDB_COL_IS_UD_COL(pExpr->base.colInfo.flag) && + (pExpr->base.colInfo.colId != PRIMARYKEY_TIMESTAMP_COL_INDEX))) { bool qualifiedCol = false; for (int32_t j = 0; j < pQueryInfo->groupbyExpr.numOfGroupCols; ++j) { SColIndex* pColIndex = taosArrayGet(pQueryInfo->groupbyExpr.columnInfo, j); - + if (pExpr->base.colInfo.colId == pColIndex->colId) { qualifiedCol = true; doLimitOutputNormalColOfGroupby(pExpr); @@ -8212,12 +8249,13 @@ static bool tagColumnInGroupby(SGroupbyExpr* pGroupbyExpr, int16_t columnId, int for (int32_t j = 0; j < pGroupbyExpr->numOfGroupCols; ++j) { SColIndex* pColIndex = taosArrayGet(pGroupbyExpr->columnInfo, j); - if (type == TSDB_DATA_TYPE_JSON && name != NULL){ - if (columnId == pColIndex->colId && strncmp(pColIndex->name, name, tListLen(pColIndex->name)) == 0 && TSDB_COL_IS_TAG(pColIndex->flag )) { + if (type == TSDB_DATA_TYPE_JSON && name != NULL) { + if (columnId == pColIndex->colId && strncmp(pColIndex->name, name, tListLen(pColIndex->name)) == 0 && + TSDB_COL_IS_TAG(pColIndex->flag)) { return true; } - }else{ - if (columnId == pColIndex->colId && TSDB_COL_IS_TAG(pColIndex->flag )) { + } else { + if (columnId == pColIndex->colId && TSDB_COL_IS_TAG(pColIndex->flag)) { return true; } } @@ -8229,12 +8267,12 @@ static bool tagColumnInGroupby(SGroupbyExpr* pGroupbyExpr, int16_t columnId, int static bool onlyTagPrjFunction(SQueryInfo* pQueryInfo) { bool hasTagPrj = false; bool hasColumnPrj = false; - + size_t size = taosArrayGetSize(pQueryInfo->exprList); for (int32_t i = 0; i < size; ++i) { SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - if (pExpr->base.functionId == TSDB_FUNC_PRJ || - (pExpr->base.functionId == TSDB_FUNC_SCALAR_EXPR && ((pQueryInfo->type & TSDB_QUERY_TYPE_PROJECTION_QUERY) != 0))) { + if (pExpr->base.functionId == TSDB_FUNC_PRJ || (pExpr->base.functionId == TSDB_FUNC_SCALAR_EXPR && + ((pQueryInfo->type & TSDB_QUERY_TYPE_PROJECTION_QUERY) != 0))) { hasColumnPrj = true; } else if (pExpr->base.functionId == TSDB_FUNC_TAGPRJ) { hasTagPrj = true; @@ -8255,7 +8293,8 @@ static bool allTagPrjInGroupby(SQueryInfo* pQueryInfo) { continue; } - if (!tagColumnInGroupby(&pQueryInfo->groupbyExpr, pExpr->base.colInfo.colId, pExpr->base.resType, pExpr->base.param[0].pz)) { + if (!tagColumnInGroupby(&pQueryInfo->groupbyExpr, pExpr->base.colInfo.colId, pExpr->base.resType, + pExpr->base.param[0].pz)) { allInGroupby = false; break; } @@ -8267,7 +8306,7 @@ static bool allTagPrjInGroupby(SQueryInfo* pQueryInfo) { static void updateTagPrjFunction(SQueryInfo* pQueryInfo) { size_t size = taosArrayGetSize(pQueryInfo->exprList); - + for (int32_t i = 0; i < size; ++i) { SExprInfo* pExpr = tscExprGet(pQueryInfo, i); if (pExpr->base.functionId == TSDB_FUNC_TAGPRJ) { @@ -8280,14 +8319,13 @@ static void updateTagPrjFunction(SQueryInfo* pQueryInfo) { retrun false : expr is not in groupbu column. return true : expr is in groupby column. */ -static bool check_expr_in_groupby_colum(SGroupbyExpr* pGroupbyExpr, SExprInfo* pExpr){ +static bool check_expr_in_groupby_colum(SGroupbyExpr* pGroupbyExpr, SExprInfo* pExpr) { SColIndex* pIndex = NULL; - assert( pExpr); - if (NULL == pGroupbyExpr) - return false; - for (int32_t k = 0; k < pGroupbyExpr->numOfGroupCols ; ++k) { + assert(pExpr); + if (NULL == pGroupbyExpr) return false; + for (int32_t k = 0; k < pGroupbyExpr->numOfGroupCols; ++k) { pIndex = taosArrayGet(pGroupbyExpr->columnInfo, k); - if (!strcmp(pIndex->name,&pExpr->base.colInfo.name[1])){ // notes:first char is dot, skip one char. + if (!strcmp(pIndex->name, &pExpr->base.colInfo.name[1])) { // notes:first char is dot, skip one char. return true; } } @@ -8308,15 +8346,15 @@ static int32_t checkUpdateTagPrjFunctions(SQueryInfo* pQueryInfo, char* msg) { int16_t numOfScalar = 0; int16_t numOfSelectivity = 0; int16_t numOfAggregation = 0; - int16_t numOfTimeWindow = 0; + int16_t numOfTimeWindow = 0; - size_t numOfExprs = taosArrayGetSize(pQueryInfo->exprList); + size_t numOfExprs = taosArrayGetSize(pQueryInfo->exprList); SGroupbyExpr* pGroupbyExpr = &pQueryInfo->groupbyExpr; for (int32_t i = 0; i < numOfExprs; ++i) { SExprInfo* pExpr = taosArrayGetP(pQueryInfo->exprList, i); if (pExpr->base.functionId == TSDB_FUNC_TAGPRJ || (pExpr->base.functionId == TSDB_FUNC_PRJ && pExpr->base.colInfo.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX)) { - if (false == check_expr_in_groupby_colum(pGroupbyExpr,pExpr)) { + if (false == check_expr_in_groupby_colum(pGroupbyExpr, pExpr)) { tagTsColExists = true; // selectivity + ts/tag column break; } @@ -8328,8 +8366,8 @@ static int32_t checkUpdateTagPrjFunctions(SQueryInfo* pQueryInfo, char* msg) { int16_t functionId = pExpr->base.functionId; if (functionId == TSDB_FUNC_TAGPRJ || functionId == TSDB_FUNC_PRJ || functionId == TSDB_FUNC_TS || - functionId == TSDB_FUNC_SCALAR_EXPR || functionId == TSDB_FUNC_TS_DUMMY || functionId == TSDB_FUNC_STATE_COUNT || - functionId == TSDB_FUNC_STATE_DURATION) { + functionId == TSDB_FUNC_SCALAR_EXPR || functionId == TSDB_FUNC_TS_DUMMY || + functionId == TSDB_FUNC_STATE_COUNT || functionId == TSDB_FUNC_STATE_DURATION) { continue; } @@ -8385,7 +8423,7 @@ static int32_t checkUpdateTagPrjFunctions(SQueryInfo* pQueryInfo, char* msg) { */ for (int32_t i = 0; i < numOfExprs; ++i) { SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - int16_t functionId = pExpr->base.functionId; + int16_t functionId = pExpr->base.functionId; if (TSDB_FUNC_IS_SCALAR(functionId)) { continue; } @@ -8395,7 +8433,7 @@ static int32_t checkUpdateTagPrjFunctions(SQueryInfo* pQueryInfo, char* msg) { } if ((functionId == TSDB_FUNC_LAST_ROW) || (functionId == TSDB_FUNC_INTERP) || - (functionId == TSDB_FUNC_LAST_DST && (pExpr->base.colInfo.flag & TSDB_COL_NULL) != 0)) { + (functionId == TSDB_FUNC_LAST_DST && (pExpr->base.colInfo.flag & TSDB_COL_NULL) != 0)) { // do nothing } else { return invalidOperationMsg(msg, msg1); @@ -8432,43 +8470,43 @@ static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd, SQueryInfo* pQueryInfo const char* msg1 = "interval not allowed in group by normal column"; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - SSchema* pSchema = tscGetTableSchema(pTableMetaInfo->pTableMeta); + SSchema* pSchema = tscGetTableSchema(pTableMetaInfo->pTableMeta); SSchema* tagSchema = NULL; if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo) || UTIL_TABLE_IS_CHILD_TABLE(pTableMetaInfo)) { tagSchema = tscGetTableTagSchema(pTableMetaInfo->pTableMeta); } - SSchema tmp = {.type = 0, .name = "", .colId = 0, .bytes = 0}; + SSchema tmp = {.type = 0, .name = "", .colId = 0, .bytes = 0}; SSchema* s = &tmp; for (int32_t i = 0; i < pQueryInfo->groupbyExpr.numOfGroupCols; ++i) { SColIndex* pColIndex = taosArrayGet(pQueryInfo->groupbyExpr.columnInfo, i); - int16_t colIndex = pColIndex->colIndex; + int16_t colIndex = pColIndex->colIndex; if (colIndex == TSDB_TBNAME_COLUMN_INDEX) { s = tGetTbnameColumnSchema(); } else { if (TSDB_COL_IS_TAG(pColIndex->flag)) { - if(tagSchema){ + if (tagSchema) { s = &tagSchema[colIndex]; } } else { s = &pSchema[colIndex]; } } - - if (TSDB_COL_IS_TAG(pColIndex->flag)) { + if (TSDB_COL_IS_TAG(pColIndex->flag)) { int32_t f = TSDB_FUNC_TAG; if (tscIsDiffDerivLikeQuery(pQueryInfo)) { f = TSDB_FUNC_TAGPRJ; } - int32_t pos = tscGetFirstInvisibleFieldPos(pQueryInfo); + int32_t pos = tscGetFirstInvisibleFieldPos(pQueryInfo); SColumnIndex index = {.tableIndex = pQueryInfo->groupbyExpr.tableIndex, .columnIndex = colIndex}; - SExprInfo* pExpr = tscExprInsert(pQueryInfo, pos, f, &index, s->type, s->bytes, getNewResColId(pCmd), s->bytes, true); + SExprInfo* pExpr = + tscExprInsert(pQueryInfo, pos, f, &index, s->type, s->bytes, getNewResColId(pCmd), s->bytes, true); // NOTE: tag column does not add to source column list SColumnList ids = createColumnList(1, 0, pColIndex->colIndex); insertResultField(pQueryInfo, pos, &ids, s->bytes, (int8_t)s->type, pColIndex->name, pExpr); @@ -8476,11 +8514,10 @@ static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd, SQueryInfo* pQueryInfo memset(pExpr->base.aliasName, 0, sizeof(pExpr->base.aliasName)); tstrncpy(pExpr->base.aliasName, pColIndex->name, sizeof(pExpr->base.aliasName)); tstrncpy(pExpr->base.token, pColIndex->name, sizeof(pExpr->base.token)); - if(s->type == TSDB_DATA_TYPE_JSON){ + if (s->type == TSDB_DATA_TYPE_JSON) { SStrToken t0 = {.z = pColIndex->name}; getJsonKey(&t0); - tVariantCreateFromBinary(&(pExpr->base.param[pExpr->base.numOfParams]), t0.z, - t0.n, TSDB_DATA_TYPE_BINARY); + tVariantCreateFromBinary(&(pExpr->base.param[pExpr->base.numOfParams]), t0.z, t0.n, TSDB_DATA_TYPE_BINARY); pExpr->base.numOfParams++; assert(t0.n < strlen(pColIndex->name)); memmove(pColIndex->name, t0.z, t0.n); @@ -8503,7 +8540,7 @@ static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd, SQueryInfo* pQueryInfo } } - //if the group by column does not required by user, add an invisible column into the final result set. + // if the group by column does not required by user, add an invisible column into the final result set. if (!hasGroupColumn) { doAddGroupColumnForSubquery(pQueryInfo, i, pCmd); } @@ -8517,11 +8554,11 @@ static int32_t doTagFunctionCheck(SQueryInfo* pQueryInfo) { bool tagProjection = false; bool tableCounting = false; - int32_t numOfCols = (int32_t) tscNumOfExprs(pQueryInfo); + int32_t numOfCols = (int32_t)tscNumOfExprs(pQueryInfo); for (int32_t i = 0; i < numOfCols; ++i) { SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - int32_t functionId = pExpr->base.functionId; + int32_t functionId = pExpr->base.functionId; if (functionId == TSDB_FUNC_TAGPRJ) { tagProjection = true; @@ -8534,7 +8571,7 @@ static int32_t doTagFunctionCheck(SQueryInfo* pQueryInfo) { } } - return (tableCounting && tagProjection)? -1:0; + return (tableCounting && tagProjection) ? -1 : 0; } int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, char* msg) { @@ -8542,7 +8579,8 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, char* const char* msg3 = "group by/session/state_window not allowed on projection query"; const char* msg4 = "retrieve tags not compatible with group by or interval query"; const char* msg5 = "functions can not be mixed up"; - const char* msg6 = "TWA/Diff/Derivative/Irate/CSum/MAvg/Elapsed/stateCount/stateDuration only support group by tbname"; + const char* msg6 = + "TWA/Diff/Derivative/Irate/CSum/MAvg/Elapsed/stateCount/stateDuration only support group by tbname"; const char* msg7 = "unique/state function not supported in state window query"; const char* msg8 = "histogram function not supported in time window query"; @@ -8584,7 +8622,7 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, char* size_t size = tscNumOfExprs(pQueryInfo); for (int32_t i = 0; i < size; ++i) { SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - int32_t f = pExpr->base.functionId; + int32_t f = pExpr->base.functionId; if (f < 0) { SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, -1 * f - 1); @@ -8594,14 +8632,14 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, char* continue; } - if (f == TSDB_FUNC_SCALAR_EXPR && - (pQueryInfo->type & TSDB_QUERY_TYPE_PROJECTION_QUERY) != 0) { + if (f == TSDB_FUNC_SCALAR_EXPR && (pQueryInfo->type & TSDB_QUERY_TYPE_PROJECTION_QUERY) != 0) { return invalidOperationMsg(msg, msg1); } - if ((!pQueryInfo->stateWindow) && (f == TSDB_FUNC_DIFF || f == TSDB_FUNC_DERIVATIVE || f == TSDB_FUNC_TWA || - f == TSDB_FUNC_IRATE || f == TSDB_FUNC_CSUM || f == TSDB_FUNC_MAVG || f == TSDB_FUNC_ELAPSED || - f == TSDB_FUNC_STATE_COUNT || f == TSDB_FUNC_STATE_DURATION)) { + if ((!pQueryInfo->stateWindow) && + (f == TSDB_FUNC_DIFF || f == TSDB_FUNC_DERIVATIVE || f == TSDB_FUNC_TWA || f == TSDB_FUNC_IRATE || + f == TSDB_FUNC_CSUM || f == TSDB_FUNC_MAVG || f == TSDB_FUNC_ELAPSED || f == TSDB_FUNC_STATE_COUNT || + f == TSDB_FUNC_STATE_DURATION)) { for (int32_t j = 0; j < pQueryInfo->groupbyExpr.numOfGroupCols; ++j) { SColIndex* pColIndex = taosArrayGet(pQueryInfo->groupbyExpr.columnInfo, j); if (j == 0) { @@ -8614,14 +8652,14 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, char* } } - if (pQueryInfo->stateWindow && (f == TSDB_FUNC_UNIQUE || f == TSDB_FUNC_STATE_COUNT || f == TSDB_FUNC_STATE_DURATION)) { + if (pQueryInfo->stateWindow && + (f == TSDB_FUNC_UNIQUE || f == TSDB_FUNC_STATE_COUNT || f == TSDB_FUNC_STATE_DURATION)) { return invalidOperationMsg(msg, msg7); } if (IS_MULTIOUTPUT(aAggs[f].status) && f != TSDB_FUNC_TOP && f != TSDB_FUNC_BOTTOM && f != TSDB_FUNC_DIFF && - f != TSDB_FUNC_MAVG && f != TSDB_FUNC_CSUM && f != TSDB_FUNC_SAMPLE && - f != TSDB_FUNC_DERIVATIVE && f != TSDB_FUNC_TAGPRJ && f != TSDB_FUNC_PRJ && - f != TSDB_FUNC_UNIQUE && f != TSDB_FUNC_TAIL) { + f != TSDB_FUNC_MAVG && f != TSDB_FUNC_CSUM && f != TSDB_FUNC_SAMPLE && f != TSDB_FUNC_DERIVATIVE && + f != TSDB_FUNC_TAGPRJ && f != TSDB_FUNC_PRJ && f != TSDB_FUNC_UNIQUE && f != TSDB_FUNC_TAIL) { return invalidOperationMsg(msg, msg1); } } @@ -8645,44 +8683,41 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, char* } } - int32_t validateFunctionFromUpstream(SQueryInfo* pQueryInfo, char* msg) { - const char* msg1 = "TWA/Diff/Derivative/Irate/elapsed are not allowed to apply to super table without group by tbname"; + const char* msg1 = + "TWA/Diff/Derivative/Irate/elapsed are not allowed to apply to super table without group by tbname"; const char* msg2 = "group by not supported in nested interp query"; const char* msg3 = "order by not supported in nested interp query"; const char* msg4 = "first column should be timestamp for interp query"; const char* msg5 = "interp input may be invalid"; int32_t numOfExprs = (int32_t)tscNumOfExprs(pQueryInfo); - size_t upNum = taosArrayGetSize(pQueryInfo->pUpstream); - + size_t upNum = taosArrayGetSize(pQueryInfo->pUpstream); + for (int32_t i = 0; i < numOfExprs; ++i) { SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - + int32_t f = pExpr->base.functionId; - if (f == TSDB_FUNC_DERIVATIVE || - f == TSDB_FUNC_TWA || - f == TSDB_FUNC_IRATE || - f == TSDB_FUNC_DIFF || - f == TSDB_FUNC_ELAPSED || - f == TSDB_FUNC_STATE_COUNT || - f == TSDB_FUNC_STATE_DURATION) { + if (f == TSDB_FUNC_DERIVATIVE || f == TSDB_FUNC_TWA || f == TSDB_FUNC_IRATE || f == TSDB_FUNC_DIFF || + f == TSDB_FUNC_ELAPSED || f == TSDB_FUNC_STATE_COUNT || f == TSDB_FUNC_STATE_DURATION) { for (int32_t j = 0; j < upNum; ++j) { - SQueryInfo* pUp = taosArrayGetP(pQueryInfo->pUpstream, j); - STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pUp, 0); - bool isSTable = UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo); - if ((!isSTable) || groupbyTbname(pUp)||pUp->interval.interval > 0) { + SQueryInfo* pUp = taosArrayGetP(pQueryInfo->pUpstream, j); + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pUp, 0); + bool isSTable = UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo); + if ((!isSTable) || groupbyTbname(pUp) || pUp->interval.interval > 0) { return TSDB_CODE_SUCCESS; } } - + return invalidOperationMsg(msg, msg1); } else if (f == TSDB_FUNC_INTERP) { if (pQueryInfo->groupbyExpr.columnInfo) { return invalidOperationMsg(msg, msg2); } - if (pQueryInfo->order.order == TSDB_ORDER_DESC || (pQueryInfo->order.orderColId != INT32_MIN && pQueryInfo->order.orderColId != PRIMARYKEY_TIMESTAMP_COL_INDEX)) { + if (pQueryInfo->order.order == TSDB_ORDER_DESC || + (pQueryInfo->order.orderColId != INT32_MIN && + pQueryInfo->order.orderColId != PRIMARYKEY_TIMESTAMP_COL_INDEX)) { return invalidOperationMsg(msg, msg3); } @@ -8691,8 +8726,9 @@ int32_t validateFunctionFromUpstream(SQueryInfo* pQueryInfo, char* msg) { if (pUp->groupbyExpr.columnInfo) { return invalidOperationMsg(msg, msg2); } - - if (pUp->order.order == TSDB_ORDER_DESC || (pUp->order.orderColId != INT32_MIN && pUp->order.orderColId != PRIMARYKEY_TIMESTAMP_COL_INDEX)) { + + if (pUp->order.order == TSDB_ORDER_DESC || + (pUp->order.orderColId != INT32_MIN && pUp->order.orderColId != PRIMARYKEY_TIMESTAMP_COL_INDEX)) { return invalidOperationMsg(msg, msg3); } @@ -8703,8 +8739,8 @@ int32_t validateFunctionFromUpstream(SQueryInfo* pQueryInfo, char* msg) { return invalidOperationMsg(msg, msg4); } - STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pUp, 0); - bool isSTable = UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo); + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pUp, 0); + bool isSTable = UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo); if (!isSTable) { continue; } @@ -8712,11 +8748,10 @@ int32_t validateFunctionFromUpstream(SQueryInfo* pQueryInfo, char* msg) { if (TSDB_QUERY_HAS_TYPE(pUp->type, TSDB_QUERY_TYPE_PROJECTION_QUERY)) { return invalidOperationMsg(msg, msg5); } - + for (int32_t n = 0; n < exprNum; ++n) { expr = taosArrayGetP(pUp->exprList, n); - if (expr->functionId == TSDB_FUNC_TOP || - expr->functionId == TSDB_FUNC_BOTTOM || + if (expr->functionId == TSDB_FUNC_TOP || expr->functionId == TSDB_FUNC_BOTTOM || expr->functionId == TSDB_FUNC_SAMPLE) { if (expr->param[0].i64 > 1) { return invalidOperationMsg(msg, msg5); @@ -8731,35 +8766,34 @@ int32_t validateFunctionFromUpstream(SQueryInfo* pQueryInfo, char* msg) { return TSDB_CODE_SUCCESS; } - int32_t doLocalQueryProcess(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSqlNode) { const char* msg1 = "only one expression allowed"; const char* msg2 = "invalid expression in select clause"; const char* msg3 = "invalid function"; SArray* pExprList = pSqlNode->pSelNodeList; - size_t size = taosArrayGetSize(pExprList); + size_t size = taosArrayGetSize(pExprList); if (size != 1) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - bool server_status = false; + bool server_status = false; tSqlExprItem* pExprItem = taosArrayGet(pExprList, 0); - tSqlExpr* pExpr = pExprItem->pNode; + tSqlExpr* pExpr = pExprItem->pNode; if (pExpr->Expr.operand.z == NULL) { - //handle 'select 1' + // handle 'select 1' if (pExpr->exprToken.n == 1 && 0 == strncasecmp(pExpr->exprToken.z, "1", 1)) { - server_status = true; + server_status = true; } else { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); - } - } + } + } // TODO redefine the function - SDNodeDynConfOption functionsInfo[5] = {{"database()", 10}, - {"server_version()", 16}, - {"server_status()", 15}, - {"client_version()", 16}, - {"current_user()", 14}}; + SDNodeDynConfOption functionsInfo[5] = {{"database()", 10}, + {"server_version()", 16}, + {"server_status()", 15}, + {"client_version()", 16}, + {"current_user()", 14}}; int32_t index = -1; if (server_status == true) { @@ -8776,26 +8810,34 @@ int32_t doLocalQueryProcess(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq switch (index) { case 0: - pQueryInfo->command = TSDB_SQL_CURRENT_DB;break; + pQueryInfo->command = TSDB_SQL_CURRENT_DB; + break; case 1: - pQueryInfo->command = TSDB_SQL_SERV_VERSION;break; + pQueryInfo->command = TSDB_SQL_SERV_VERSION; + break; case 2: - pQueryInfo->command = TSDB_SQL_SERV_STATUS;break; + pQueryInfo->command = TSDB_SQL_SERV_STATUS; + break; case 3: - pQueryInfo->command = TSDB_SQL_CLI_VERSION;break; + pQueryInfo->command = TSDB_SQL_CLI_VERSION; + break; case 4: - pQueryInfo->command = TSDB_SQL_CURRENT_USER;break; - default: { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } + pQueryInfo->command = TSDB_SQL_CURRENT_USER; + break; + default: { + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); + } } - + SColumnIndex ind = {0}; - SExprInfo* pExpr1 = tscExprAppend(pQueryInfo, TSDB_FUNC_TAG_DUMMY, &ind, TSDB_DATA_TYPE_INT, - tDataTypes[TSDB_DATA_TYPE_INT].bytes, getNewResColId(pCmd), tDataTypes[TSDB_DATA_TYPE_INT].bytes, false); + SExprInfo* pExpr1 = + tscExprAppend(pQueryInfo, TSDB_FUNC_TAG_DUMMY, &ind, TSDB_DATA_TYPE_INT, tDataTypes[TSDB_DATA_TYPE_INT].bytes, + getNewResColId(pCmd), tDataTypes[TSDB_DATA_TYPE_INT].bytes, false); tSqlExprItem* item = taosArrayGet(pExprList, 0); - const char* name = (item->aliasName != NULL)? item->aliasName:functionsInfo[index].name; + const char* name = (item->aliasName != NULL) ? item->aliasName : functionsInfo[index].name; tstrncpy(pExpr1->base.aliasName, name, tListLen(pExpr1->base.aliasName)); - + return TSDB_CODE_SUCCESS; } @@ -8831,8 +8873,8 @@ int32_t tscCheckCreateDbParams(SSqlCmd* pCmd, SCreateDbMsg* pCreate) { int32_t val = htonl(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); + 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 invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } @@ -8845,8 +8887,8 @@ int32_t tscCheckCreateDbParams(SSqlCmd* pCmd, SCreateDbMsg* pCreate) { val = htonl(pCreate->maxTables); if (val != -1 && (val < TSDB_MIN_TABLES || val > TSDB_MAX_TABLES)) { - snprintf(msg, tListLen(msg), "invalid db option maxSessions: %d valid range: [%d, %d]", val, - TSDB_MIN_TABLES, TSDB_MAX_TABLES); + snprintf(msg, tListLen(msg), "invalid db option maxSessions: %d valid range: [%d, %d]", val, TSDB_MIN_TABLES, + TSDB_MAX_TABLES); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } @@ -8859,15 +8901,15 @@ int32_t tscCheckCreateDbParams(SSqlCmd* pCmd, SCreateDbMsg* pCreate) { val = htonl(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); + snprintf(msg, tListLen(msg), "invalid db option commitTime: %d valid range: [%d, %d]", val, TSDB_MIN_COMMIT_TIME, + TSDB_MAX_COMMIT_TIME); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } val = htonl(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); + snprintf(msg, tListLen(msg), "invalid db option fsyncPeriod: %d valid range: [%d, %d]", val, TSDB_MIN_FSYNC_PERIOD, + TSDB_MAX_FSYNC_PERIOD); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } @@ -8879,14 +8921,12 @@ int32_t tscCheckCreateDbParams(SSqlCmd* pCmd, SCreateDbMsg* pCreate) { } val = (int16_t)htons(pCreate->partitions); - if (val != -1 && - (val < TSDB_MIN_DB_PARTITON_OPTION || val > TSDB_MAX_DB_PARTITON_OPTION)) { + if (val != -1 && (val < TSDB_MIN_DB_PARTITON_OPTION || val > TSDB_MAX_DB_PARTITON_OPTION)) { snprintf(msg, tListLen(msg), "invalid topic option partition: %d valid range: [%d, %d]", val, TSDB_MIN_DB_PARTITON_OPTION, TSDB_MAX_DB_PARTITON_OPTION); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } - return TSDB_CODE_SUCCESS; } @@ -8901,7 +8941,7 @@ void tscPrintSelNodeList(SSqlObj* pSql, int32_t subClauseIndex) { int32_t totalBufSize = 1024; - char str[1024+1] = {0}; + char str[1024 + 1] = {0}; int32_t offset = 0; offset += sprintf(str, "num:%d [", size); @@ -8910,7 +8950,7 @@ void tscPrintSelNodeList(SSqlObj* pSql, int32_t subClauseIndex) { char tmpBuf[1024] = {0}; int32_t tmpLen = 0; - char *name = NULL; + char* name = NULL; if (pExpr->base.functionId < 0) { SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, -1 * pExpr->base.functionId - 1); @@ -8921,12 +8961,10 @@ void tscPrintSelNodeList(SSqlObj* pSql, int32_t subClauseIndex) { name = aScalarFunctions[TSDB_FUNC_SCALAR_INDEX(pExpr->base.functionId)].name; } - tmpLen = - sprintf(tmpBuf, "%s(uid:%" PRIu64 ", %d)", name, pExpr->base.uid, pExpr->base.colInfo.colId); + tmpLen = sprintf(tmpBuf, "%s(uid:%" PRIu64 ", %d)", name, pExpr->base.uid, pExpr->base.colInfo.colId); if (tmpLen + offset >= totalBufSize - 1) break; - offset += sprintf(str + offset, "%s", tmpBuf); if (i < size - 1) { @@ -8937,7 +8975,7 @@ void tscPrintSelNodeList(SSqlObj* pSql, int32_t subClauseIndex) { assert(offset < totalBufSize); str[offset] = ']'; assert(offset < totalBufSize); - tscDebug("0x%"PRIx64" select clause:%s", pSql->self, str); + tscDebug("0x%" PRIx64 " select clause:%s", pSql->self, str); } int32_t doCheckForCreateTable(SSqlObj* pSql, int32_t subClauseIndex, SSqlInfo* pInfo) { @@ -8956,14 +8994,14 @@ int32_t doCheckForCreateTable(SSqlObj* pSql, int32_t subClauseIndex, SSqlInfo* p // if sql specifies db, use it, otherwise use default db SStrToken* pzTableName = &(pCreateTable->name); - + bool dbIncluded = false; if (tscValidateName(pzTableName, true, &dbIncluded) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } int32_t code = tscSetTableFullName(&pTableMetaInfo->name, pzTableName, pSql, dbIncluded); - if(code != TSDB_CODE_SUCCESS) { + if (code != TSDB_CODE_SUCCESS) { return code; } @@ -8973,7 +9011,7 @@ int32_t doCheckForCreateTable(SSqlObj* pSql, int32_t subClauseIndex, SSqlInfo* p } int32_t col = 0; - size_t numOfFields = taosArrayGetSize(pFieldList); + size_t numOfFields = taosArrayGetSize(pFieldList); for (; col < numOfFields; ++col) { TAOS_FIELD* p = taosArrayGet(pFieldList, col); @@ -8989,7 +9027,7 @@ int32_t doCheckForCreateTable(SSqlObj* pSql, int32_t subClauseIndex, SSqlInfo* p tscFieldInfoAppend(&pQueryInfo->fieldsInfo, p); } - pCmd->count =(int32_t) numOfTags; + pCmd->count = (int32_t)numOfTags; } return TSDB_CODE_SUCCESS; @@ -9019,13 +9057,13 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { STableMetaInfo* pStableMetaInfo = tscGetMetaInfo(pQueryInfo, STABLE_INDEX); // super table name, create table by using dst - int32_t numOfTables = (int32_t) taosArrayGetSize(pCreateTable->childTableInfo); - for(int32_t j = 0; j < numOfTables; ++j) { + int32_t numOfTables = (int32_t)taosArrayGetSize(pCreateTable->childTableInfo); + for (int32_t j = 0; j < numOfTables; ++j) { SCreatedTableInfo* pCreateTableInfo = taosArrayGet(pCreateTable->childTableInfo, j); SStrToken* pToken = &pCreateTableInfo->stableName; - bool dbIncluded = false; + bool dbIncluded = false; char buf[TSDB_TABLE_FNAME_LEN]; SStrToken sTblToken; sTblToken.z = buf; @@ -9056,9 +9094,9 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { size_t valSize = taosArrayGetSize(pValList); // too long tag values will return invalid sql, not be truncated automatically - SSchema *pTagSchema = tscGetTableTagSchema(pStableMetaInfo->pTableMeta); + SSchema* pTagSchema = tscGetTableTagSchema(pStableMetaInfo->pTableMeta); STableComInfo tinfo = tscGetTableInfo(pStableMetaInfo->pTableMeta); - STagData *pTag = &pCreateTableInfo->tagdata; + STagData* pTag = &pCreateTableInfo->tagdata; SKVRowBuilder kvRowBuilder = {0}; if (tdInitKVRowBuilder(&kvRowBuilder) < 0) { @@ -9066,7 +9104,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { } SArray* pNameList = NULL; - size_t nameSize = 0; + size_t nameSize = 0; int32_t schemaSize = tscGetNumOfTags(pStableMetaInfo->pTableMeta); int32_t ret = TSDB_CODE_SUCCESS; @@ -9089,7 +9127,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { for (int32_t i = 0; i < nameSize; ++i) { SStrToken* sToken = taosArrayGet(pNameList, i); - char tmpTokenBuf[TSDB_MAX_BYTES_PER_ROW] = {0}; // create tmp buf to avoid alter orginal sqlstr + 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; @@ -9104,7 +9142,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { // 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]; + SSchema* pSchema = &pTagSchema[t]; char tagVal[TSDB_MAX_TAGS_LEN] = {0}; if (pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) { @@ -9197,24 +9235,25 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { } // encode json tag string - if(schemaSize == 1 && pTagSchema[0].type == TSDB_DATA_TYPE_JSON){ + if (schemaSize == 1 && pTagSchema[0].type == TSDB_DATA_TYPE_JSON) { if (valSize != schemaSize) { tdDestroyKVRowBuilder(&kvRowBuilder); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg5); } tVariantListItem* pItem = taosArrayGet(pValList, 0); - if(pItem->pVar.nType != TSDB_DATA_TYPE_BINARY && pItem->pVar.nType != TSDB_DATA_TYPE_NULL){ + if (pItem->pVar.nType != TSDB_DATA_TYPE_BINARY && pItem->pVar.nType != TSDB_DATA_TYPE_NULL) { tscError("json type error, should be string"); tdDestroyKVRowBuilder(&kvRowBuilder); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg7); } - if(pItem->pVar.nLen > TSDB_MAX_JSON_TAGS_LEN/TSDB_NCHAR_SIZE){ + if (pItem->pVar.nLen > TSDB_MAX_JSON_TAGS_LEN / TSDB_NCHAR_SIZE) { tscError("json tag too long"); tdDestroyKVRowBuilder(&kvRowBuilder); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } - ret = parseJsontoTagData(pItem->pVar.pz, pItem->pVar.nLen, &kvRowBuilder, tscGetErrorMsgPayload(pCmd), pTagSchema[0].colId); + ret = parseJsontoTagData(pItem->pVar.pz, pItem->pVar.nLen, &kvRowBuilder, tscGetErrorMsgPayload(pCmd), + pTagSchema[0].colId); if (ret != TSDB_CODE_SUCCESS) { tdDestroyKVRowBuilder(&kvRowBuilder); return ret; @@ -9235,7 +9274,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { kvRowCpy(pTag->data, row); free(row); - + bool dbIncluded2 = false; // table name if (tscValidateName(&(pCreateTableInfo->name), true, &dbIncluded2) != TSDB_CODE_SUCCESS) { @@ -9279,21 +9318,22 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { // if sql specifies db, use it, otherwise use default db SStrToken* pName = &(pCreateTable->name); - SSqlNode* pSqlNode = pCreateTable->pSelect; - bool dbIncluded1 = false; + SSqlNode* pSqlNode = pCreateTable->pSelect; + bool dbIncluded1 = false; if (tscValidateName(pName, true, &dbIncluded1) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - + // check to valid and create to name - if(pInfo->pCreateTableInfo->to.n > 0) { + if (pInfo->pCreateTableInfo->to.n > 0) { bool dbInclude = false; if (tscValidateName(&pInfo->pCreateTableInfo->to, false, &dbInclude) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - int32_t code = tscSetTableFullName(&pInfo->pCreateTableInfo->toSName, &pInfo->pCreateTableInfo->to, pSql, dbInclude); - if(code != TSDB_CODE_SUCCESS) { + int32_t code = + tscSetTableFullName(&pInfo->pCreateTableInfo->toSName, &pInfo->pCreateTableInfo->to, pSql, dbInclude); + if (code != TSDB_CODE_SUCCESS) { return code; } } @@ -9303,14 +9343,14 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg6); } - if (pFromInfo->type == SQL_NODE_FROM_SUBQUERY){ - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg9); + if (pFromInfo->type == SQL_NODE_FROM_SUBQUERY) { + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg9); } SRelElementPair* p1 = taosArrayGet(pFromInfo->list, 0); - SStrToken srcToken = {.z = p1->tableName.z, .n = p1->tableName.n, .type = p1->tableName.type}; + SStrToken srcToken = {.z = p1->tableName.z, .n = p1->tableName.n, .type = p1->tableName.type}; - bool dbIncluded2 = false; + bool dbIncluded2 = false; char buf[TSDB_TABLE_FNAME_LEN]; SStrToken sTblToken; sTblToken.z = buf; @@ -9330,7 +9370,8 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { return code; } - if (validateSelectNodeList(&pSql->cmd, pQueryInfo, pSqlNode->pSelNodeList, false, false, false) != TSDB_CODE_SUCCESS) { + if (validateSelectNodeList(&pSql->cmd, pQueryInfo, pSqlNode->pSelNodeList, false, false, false) != + TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; } @@ -9347,7 +9388,8 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { return TSDB_CODE_TSC_INVALID_OPERATION; } - if (isTimeWindowQuery(pQueryInfo) && (validateFunctionsInIntervalOrGroupbyQuery(pCmd, pQueryInfo) != TSDB_CODE_SUCCESS)) { + if (isTimeWindowQuery(pQueryInfo) && + (validateFunctionsInIntervalOrGroupbyQuery(pCmd, pQueryInfo) != TSDB_CODE_SUCCESS)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -9415,36 +9457,37 @@ int32_t checkQueryRangeForFill(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { return TSDB_CODE_SUCCESS; } - bool initialWindows = TSWINDOW_IS_EQUAL(pQueryInfo->window, TSWINDOW_INITIALIZER); - if (initialWindows) { - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); - } - - int64_t timeRange = ABS(pQueryInfo->window.skey - pQueryInfo->window.ekey); + bool initialWindows = TSWINDOW_IS_EQUAL(pQueryInfo->window, TSWINDOW_INITIALIZER); + if (initialWindows) { + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); + } - int64_t intervalRange = 0; - if (pQueryInfo->interval.intervalUnit == 'n' || pQueryInfo->interval.intervalUnit == 'y') { - int64_t f = 1; - if (pQueryInfo->interval.intervalUnit == 'n') { - f = 30L * MILLISECOND_PER_DAY; - } else if (pQueryInfo->interval.intervalUnit == 'y') { - f = 365L * MILLISECOND_PER_DAY; - } + int64_t timeRange = ABS(pQueryInfo->window.skey - pQueryInfo->window.ekey); - intervalRange = pQueryInfo->interval.interval * f; - } else { - intervalRange = pQueryInfo->interval.interval; - } - // number of result is not greater than 10,000,000 - if ((timeRange == 0) || (timeRange / intervalRange) >= MAX_INTERVAL_TIME_WINDOW) { - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); + int64_t intervalRange = 0; + if (pQueryInfo->interval.intervalUnit == 'n' || pQueryInfo->interval.intervalUnit == 'y') { + int64_t f = 1; + if (pQueryInfo->interval.intervalUnit == 'n') { + f = 30L * MILLISECOND_PER_DAY; + } else if (pQueryInfo->interval.intervalUnit == 'y') { + f = 365L * MILLISECOND_PER_DAY; } - return TSDB_CODE_SUCCESS; + intervalRange = pQueryInfo->interval.interval * f; + } else { + intervalRange = pQueryInfo->interval.interval; + } + // number of result is not greater than 10,000,000 + if ((timeRange == 0) || (timeRange / intervalRange) >= MAX_INTERVAL_TIME_WINDOW) { + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); + } + + return TSDB_CODE_SUCCESS; } // TODO normalize the function expression and compare it -int32_t tscGetExprFilters(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelectNodeList, tSqlExpr* pSqlExpr, SExprInfo** pExpr) { +int32_t tscGetExprFilters(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelectNodeList, tSqlExpr* pSqlExpr, + SExprInfo** pExpr) { const char* msg1 = "invalid sql expression in having"; *pExpr = NULL; @@ -9455,8 +9498,8 @@ int32_t tscGetExprFilters(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelect return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - tSqlExprItem *pParam = NULL; - SSchema schema = {0}; + tSqlExprItem* pParam = NULL; + SSchema schema = {0}; if (pSqlExpr->Expr.paramList != NULL) { pParam = taosArrayGet(pSqlExpr->Expr.paramList, 0); @@ -9467,10 +9510,11 @@ int32_t tscGetExprFilters(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelect STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); schema = *tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, index.columnIndex); } else { - schema = (SSchema) {.colId = PRIMARYKEY_TIMESTAMP_COL_INDEX, .type = TSDB_DATA_TYPE_TIMESTAMP, .bytes = TSDB_KEYSIZE}; + schema = + (SSchema){.colId = PRIMARYKEY_TIMESTAMP_COL_INDEX, .type = TSDB_DATA_TYPE_TIMESTAMP, .bytes = TSDB_KEYSIZE}; } - for(int32_t i = 0; i < nx; ++i) { + for (int32_t i = 0; i < nx; ++i) { SExprInfo* pExprInfo = tscExprGet(pQueryInfo, i); if (pExprInfo->base.functionId == pSqlExpr->functionId && pExprInfo->base.colInfo.colId == schema.colId) { ++pQueryInfo->havingFieldNum; @@ -9479,44 +9523,44 @@ int32_t tscGetExprFilters(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelect } } -// size_t num = taosArrayGetSize(pSelectNodeList); -// for(int32_t i = 0; i < num; ++i) { -// tSqlExprItem* pItem = taosArrayGet(pSelectNodeList, i); -// -// if (tSqlExprCompare(pItem->pNode, pSqlExpr) == 0) { // exists, not added it, -// -// SColumnIndex index = COLUMN_INDEX_INITIALIZER; -// int32_t functionId = pSqlExpr->functionId; -// if (pSqlExpr->Expr.paramList == NULL) { -// index.columnIndex = 0; -// index.tableIndex = 0; -// } else { -// tSqlExprItem* pParamElem = taosArrayGet(pSqlExpr->Expr.paramList, 0); -// SStrToken* pToken = &pParamElem->pNode->columnName; -// getColumnIndexByName(pToken, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)); -// } -// -// size_t numOfNodeInSel = tscNumOfExprs(pQueryInfo); -// for(int32_t k = 0; k < numOfNodeInSel; ++k) { -// SExprInfo* pExpr1 = tscExprGet(pQueryInfo, k); -// -// if (pExpr1->base.functionId != functionId) { -// continue; -// } -// -// if (pExpr1->base.colInfo.colIndex != index.columnIndex) { -// continue; -// } -// -// ++pQueryInfo->havingFieldNum; -// *pExpr = pExpr1; -// break; -// } -// -// assert(*pExpr != NULL); -// return TSDB_CODE_SUCCESS; -// } -// } + // size_t num = taosArrayGetSize(pSelectNodeList); + // for(int32_t i = 0; i < num; ++i) { + // tSqlExprItem* pItem = taosArrayGet(pSelectNodeList, i); + // + // if (tSqlExprCompare(pItem->pNode, pSqlExpr) == 0) { // exists, not added it, + // + // SColumnIndex index = COLUMN_INDEX_INITIALIZER; + // int32_t functionId = pSqlExpr->functionId; + // if (pSqlExpr->Expr.paramList == NULL) { + // index.columnIndex = 0; + // index.tableIndex = 0; + // } else { + // tSqlExprItem* pParamElem = taosArrayGet(pSqlExpr->Expr.paramList, 0); + // SStrToken* pToken = &pParamElem->pNode->columnName; + // getColumnIndexByName(pToken, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)); + // } + // + // size_t numOfNodeInSel = tscNumOfExprs(pQueryInfo); + // for(int32_t k = 0; k < numOfNodeInSel; ++k) { + // SExprInfo* pExpr1 = tscExprGet(pQueryInfo, k); + // + // if (pExpr1->base.functionId != functionId) { + // continue; + // } + // + // if (pExpr1->base.colInfo.colIndex != index.columnIndex) { + // continue; + // } + // + // ++pQueryInfo->havingFieldNum; + // *pExpr = pExpr1; + // break; + // } + // + // assert(*pExpr != NULL); + // return TSDB_CODE_SUCCESS; + // } + // } tSqlExprItem item = {.pNode = pSqlExpr, .aliasName = NULL, .distinct = false}; @@ -9538,7 +9582,8 @@ int32_t tscGetExprFilters(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelect return TSDB_CODE_SUCCESS; } -static int32_t handleExprInHavingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelectNodeList, tSqlExpr* pExpr, int32_t sqlOptr) { +static int32_t handleExprInHavingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelectNodeList, tSqlExpr* pExpr, + int32_t sqlOptr) { const char* msg1 = "non binary column not support like/match operator"; const char* msg2 = "invalid operator for binary column in having clause"; const char* msg3 = "invalid operator for bool column in having clause"; @@ -9549,7 +9594,7 @@ static int32_t handleExprInHavingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, S * in case of TK_AND filter condition, we first find the corresponding column and build the query condition together * the already existed condition. */ - SExprInfo *expr = NULL; + SExprInfo* expr = NULL; if (sqlOptr == TK_AND) { int32_t ret = tscGetExprFilters(pCmd, pQueryInfo, pSelectNodeList, pExpr->pLeft, &expr); if (ret) { @@ -9586,14 +9631,9 @@ static int32_t handleExprInHavingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, S ((expr->base.resType == TSDB_DATA_TYPE_BINARY || expr->base.resType == TSDB_DATA_TYPE_NCHAR) ? 1 : 0); if (pColFilter->filterstr) { - if (pExpr->tokenId != TK_EQ - && pExpr->tokenId != TK_NE - && pExpr->tokenId != TK_ISNULL - && pExpr->tokenId != TK_NOTNULL - && pExpr->tokenId != TK_LIKE - && pExpr->tokenId != TK_MATCH - && pExpr->tokenId != TK_NMATCH - ) { + if (pExpr->tokenId != TK_EQ && pExpr->tokenId != TK_NE && pExpr->tokenId != TK_ISNULL && + pExpr->tokenId != TK_NOTNULL && pExpr->tokenId != TK_LIKE && pExpr->tokenId != TK_MATCH && + pExpr->tokenId != TK_NMATCH) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } } else { @@ -9609,7 +9649,7 @@ static int32_t handleExprInHavingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, S } STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; + STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; int32_t ret = doExtractColumnFilterInfo(pCmd, pQueryInfo, pTableMeta->tableInfo.precision, pColFilter, expr->base.resType, pExpr); @@ -9620,7 +9660,8 @@ static int32_t handleExprInHavingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, S return TSDB_CODE_SUCCESS; } -int32_t getHavingExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelectNodeList, tSqlExpr* pExpr, int32_t parentOptr) { +int32_t getHavingExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelectNodeList, tSqlExpr* pExpr, + int32_t parentOptr) { if (pExpr == NULL) { return TSDB_CODE_SUCCESS; } @@ -9649,7 +9690,7 @@ int32_t getHavingExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelectNode exchangeExpr(pExpr); - pLeft = pExpr->pLeft; + pLeft = pExpr->pLeft; pRight = pExpr->pRight; if (pLeft->type != SQL_NODE_SQLFUNCTION) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); @@ -9672,19 +9713,15 @@ int32_t getHavingExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelectNode size_t size = taosArrayGetSize(pLeft->Expr.paramList); for (int32_t i = 0; i < size; i++) { tSqlExprItem* pParamItem = taosArrayGet(pLeft->Expr.paramList, i); - tSqlExpr* pExpr1 = pParamItem->pNode; + tSqlExpr* pExpr1 = pParamItem->pNode; - if (pLeft->functionId == TSDB_FUNC_STATE_COUNT || pLeft->functionId == TSDB_FUNC_STATE_DURATION){ + if (pLeft->functionId == TSDB_FUNC_STATE_COUNT || pLeft->functionId == TSDB_FUNC_STATE_DURATION) { if (i == 1 && pExpr1->tokenId == TK_ID) continue; - if (pLeft->functionId == TSDB_FUNC_STATE_DURATION && i == 3 && pExpr1->tokenId == TK_TIMESTAMP) - continue; + if (pLeft->functionId == TSDB_FUNC_STATE_DURATION && i == 3 && pExpr1->tokenId == TK_TIMESTAMP) continue; } - if (pExpr1->tokenId != TK_ALL && - pExpr1->tokenId != TK_ID && - pExpr1->tokenId != TK_STRING && - pExpr1->tokenId != TK_INTEGER && - pExpr1->tokenId != TK_FLOAT) { + if (pExpr1->tokenId != TK_ALL && pExpr1->tokenId != TK_ID && pExpr1->tokenId != TK_STRING && + pExpr1->tokenId != TK_INTEGER && pExpr1->tokenId != TK_FLOAT) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } @@ -9694,15 +9731,15 @@ int32_t getHavingExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelectNode if (pExpr1->tokenId == TK_ID) { SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if ((getColumnIndexByName(&pExpr1->columnName, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != TSDB_CODE_SUCCESS)) { + if ((getColumnIndexByName(&pExpr1->columnName, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != + TSDB_CODE_SUCCESS)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); - STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; + STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; - if (index.columnIndex <= 0 || - index.columnIndex >= tscGetNumOfColumns(pTableMeta)) { + if (index.columnIndex <= 0 || index.columnIndex >= tscGetNumOfColumns(pTableMeta)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } } @@ -9740,7 +9777,7 @@ int32_t validateHavingClause(SQueryInfo* pQueryInfo, tSqlExpr* pExpr, SSqlCmd* p return ret; } - //REDO function check + // REDO function check if (!functionCompatibleCheck(pQueryInfo, joinQuery, timeWindowQuery)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -9751,27 +9788,27 @@ int32_t validateHavingClause(SQueryInfo* pQueryInfo, tSqlExpr* pExpr, SSqlCmd* p static int32_t getTableNameFromSqlNode(SSqlNode* pSqlNode, SArray* tableNameList, char* msgBuf, SSqlObj* pSql) { const char* msg1 = "invalid table name"; - int32_t numOfTables = (int32_t) taosArrayGetSize(pSqlNode->from->list); + int32_t numOfTables = (int32_t)taosArrayGetSize(pSqlNode->from->list); assert(pSqlNode->from->type == SQL_NODE_FROM_TABLELIST); - for(int32_t j = 0; j < numOfTables; ++j) { + for (int32_t j = 0; j < numOfTables; ++j) { SRelElementPair* item = taosArrayGet(pSqlNode->from->list, j); SStrToken* t = &item->tableName; if (t->type == TK_INTEGER || t->type == TK_FLOAT) { return invalidOperationMsg(msgBuf, msg1); } - - bool dbIncluded = false; + + bool dbIncluded = false; char buf[TSDB_TABLE_FNAME_LEN]; SStrToken sTblToken; sTblToken.z = buf; - + if (validateTableName(t->z, t->n, &sTblToken, &dbIncluded) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(msgBuf, msg1); } - SName name = {0}; + SName name = {0}; int32_t code = tscSetTableFullName(&name, &sTblToken, pSql, dbIncluded); if (code != TSDB_CODE_SUCCESS) { return code; @@ -9784,9 +9821,9 @@ static int32_t getTableNameFromSqlNode(SSqlNode* pSqlNode, SArray* tableNameList } static int32_t getTableNameFromSubquery(SSqlNode* pSqlNode, SArray* tableNameList, char* msgBuf, SSqlObj* pSql) { - int32_t numOfSub = (int32_t) taosArrayGetSize(pSqlNode->from->list); + int32_t numOfSub = (int32_t)taosArrayGetSize(pSqlNode->from->list); - for(int32_t j = 0; j < numOfSub; ++j) { + for (int32_t j = 0; j < numOfSub; ++j) { SRelElementPair* sub = taosArrayGet(pSqlNode->from->list, j); int32_t num = (int32_t)taosArrayGetSize(sub->pSubquery); @@ -9795,7 +9832,7 @@ static int32_t getTableNameFromSubquery(SSqlNode* pSqlNode, SArray* tableNameLis if (p->from == NULL) { return TSDB_CODE_TSC_INVALID_OPERATION; } - + if (p->from->type == SQL_NODE_FROM_TABLELIST) { int32_t code = getTableNameFromSqlNode(p, tableNameList, msgBuf, pSql); if (code != TSDB_CODE_SUCCESS) { @@ -9810,10 +9847,8 @@ static int32_t getTableNameFromSubquery(SSqlNode* pSqlNode, SArray* tableNameLis return TSDB_CODE_SUCCESS; } -void tscTableMetaCallBack(void *param, TAOS_RES *res, int code); -static void freeElem(void* p) { - tfree(*(char**)p); -} +void tscTableMetaCallBack(void* param, TAOS_RES* res, int code); +static void freeElem(void* p) { tfree(*(char**)p); } int32_t tnameComparFn(const void* p1, const void* p2) { SName* pn1 = (SName*)p1; @@ -9821,15 +9856,15 @@ int32_t tnameComparFn(const void* p1, const void* p2) { int32_t ret = strncmp(pn1->acctId, pn2->acctId, tListLen(pn1->acctId)); if (ret != 0) { - return ret > 0? 1:-1; + return ret > 0 ? 1 : -1; } else { ret = strncmp(pn1->dbname, pn2->dbname, tListLen(pn1->dbname)); if (ret != 0) { - return ret > 0? 1:-1; + return ret > 0 ? 1 : -1; } else { ret = strncmp(pn1->tname, pn2->tname, tListLen(pn1->tname)); if (ret != 0) { - return ret > 0? 1:-1; + return ret > 0 ? 1 : -1; } else { return 0; } @@ -9847,11 +9882,11 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) { int32_t code = TSDB_CODE_SUCCESS; - SArray* tableNameList = NULL; - SArray* pVgroupList = NULL; - SArray* plist = NULL; + SArray* tableNameList = NULL; + SArray* pVgroupList = NULL; + SArray* plist = NULL; STableMeta* pTableMeta = NULL; - size_t tableMetaCapacity = 0; + size_t tableMetaCapacity = 0; SQueryInfo* pQueryInfo = tscGetQueryInfo(pCmd); pCmd->pTableMetaMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK); @@ -9878,7 +9913,7 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) { } } - char name[TSDB_TABLE_FNAME_LEN] = {0}; + char name[TSDB_TABLE_FNAME_LEN] = {0}; plist = taosArrayInit(4, POINTER_BYTES); pVgroupList = taosArrayInit(4, POINTER_BYTES); @@ -9886,27 +9921,29 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) { taosArraySort(tableNameList, tnameComparFn); taosArrayRemoveDuplicate(tableNameList, tnameComparFn, NULL); - STableMeta* pSTMeta = (STableMeta *)(pSql->pBuf); - size_t numOfTables = taosArrayGetSize(tableNameList); + STableMeta* pSTMeta = (STableMeta*)(pSql->pBuf); + size_t numOfTables = taosArrayGetSize(tableNameList); for (int32_t i = 0; i < numOfTables; ++i) { SName* pname = taosArrayGet(tableNameList, i); tNameExtractFullName(pname, name); size_t len = strlen(name); - - if (NULL == taosHashGetCloneExt(UTIL_GET_TABLEMETA(pSql), name, len, NULL, (void **)&pTableMeta, &tableMetaCapacity)) { + + if (NULL == + taosHashGetCloneExt(UTIL_GET_TABLEMETA(pSql), name, len, NULL, (void**)&pTableMeta, &tableMetaCapacity)) { // not found tfree(pTableMeta); } if (pTableMeta && pTableMeta->id.uid > 0) { - tscDebug("0x%"PRIx64" retrieve table meta %s from local buf", pSql->self, name); + tscDebug("0x%" PRIx64 " retrieve table meta %s from local buf", pSql->self, name); // avoid mem leak, may should update pTableMeta void* pVgroupIdList = NULL; if (pTableMeta->tableType == TSDB_CHILD_TABLE) { - code = tscCreateTableMetaFromSTableMeta(pSql, (STableMeta **)(&pTableMeta), name, &tableMetaCapacity, (STableMeta **)(&pSTMeta)); - pSql->pBuf = (void *)pSTMeta; + code = tscCreateTableMetaFromSTableMeta(pSql, (STableMeta**)(&pTableMeta), name, &tableMetaCapacity, + (STableMeta**)(&pSTMeta)); + pSql->pBuf = (void*)pSTMeta; // create the child table meta from super table failed, try load it from mnode if (code != TSDB_CODE_SUCCESS) { @@ -9916,33 +9953,34 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) { } } else if (pTableMeta->tableType == TSDB_SUPER_TABLE) { // the vgroup list of super table is not kept in local buffer, so here need retrieve it from the mnode each time - tscDebug("0x%"PRIx64" try to acquire cached super table %s vgroup id list", pSql->self, name); + tscDebug("0x%" PRIx64 " try to acquire cached super table %s vgroup id list", pSql->self, name); void* pv = taosCacheAcquireByKey(UTIL_GET_VGROUPLIST(pSql), name, len); if (pv == NULL) { char* t = strdup(name); taosArrayPush(pVgroupList, &t); - tscDebug("0x%"PRIx64" failed to retrieve stable %s vgroup id list in cache, try fetch from mnode", pSql->self, name); + tscDebug("0x%" PRIx64 " failed to retrieve stable %s vgroup id list in cache, try fetch from mnode", + pSql->self, name); } else { - tFilePage* pdata = (tFilePage*) pv; - pVgroupIdList = taosArrayInit((size_t) pdata->num, sizeof(int32_t)); + tFilePage* pdata = (tFilePage*)pv; + pVgroupIdList = taosArrayInit((size_t)pdata->num, sizeof(int32_t)); if (pVgroupIdList == NULL) { return TSDB_CODE_TSC_OUT_OF_MEMORY; } - taosArrayAddBatch(pVgroupIdList, pdata->data, (int32_t) pdata->num); + taosArrayAddBatch(pVgroupIdList, pdata->data, (int32_t)pdata->num); taosCacheRelease(UTIL_GET_VGROUPLIST(pSql), &pv, false); } } if (taosHashGet(pCmd->pTableMetaMap, name, len) == NULL) { - STableMeta* pMeta = tscTableMetaDup(pTableMeta); - STableMetaVgroupInfo tvi = { .pTableMeta = pMeta, .vgroupIdList = pVgroupIdList}; + STableMeta* pMeta = tscTableMetaDup(pTableMeta); + STableMetaVgroupInfo tvi = {.pTableMeta = pMeta, .vgroupIdList = pVgroupIdList}; taosHashPut(pCmd->pTableMetaMap, name, len, &tvi, sizeof(STableMetaVgroupInfo)); } } else { // Add to the retrieve table meta array list. // If the tableMeta is missing, the cached vgroup list for the corresponding super table will be ignored. - tscDebug("0x%"PRIx64" failed to retrieve table meta %s from local buf", pSql->self, name); + tscDebug("0x%" PRIx64 " failed to retrieve table meta %s from local buf", pSql->self, name); char* t = strdup(name); taosArrayPush(plist, &t); @@ -9978,10 +10016,10 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) { } else if (taosArrayGetSize(pQueryInfo->pUdfInfo) > 0) { int32_t usize = (int32_t)taosArrayGetSize(pQueryInfo->pUdfInfo); int32_t exist = 0; - + for (int32_t j = 0; j < usize; ++j) { SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, j); - int32_t len = (int32_t)strlen(pUdfInfo->name); + int32_t len = (int32_t)strlen(pUdfInfo->name); if (len == t->n && strncasecmp(info.name, pUdfInfo->name, t->n) == 0) { exist = 1; break; @@ -9993,18 +10031,20 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) { } } - info.functionId = (int32_t)taosArrayGetSize(pQueryInfo->pUdfInfo) * (-1) - 1;; + info.functionId = (int32_t)taosArrayGetSize(pQueryInfo->pUdfInfo) * (-1) - 1; + ; taosArrayPush(pQueryInfo->pUdfInfo, &info); if (taosArrayGetSize(pQueryInfo->pUdfInfo) > 1) { code = tscInvalidOperationMsg(pCmd->payload, "only one udf allowed", NULL); goto _end; - } + } } } } // load the table meta for a given table name list - if (taosArrayGetSize(plist) > 0 || taosArrayGetSize(pVgroupList) > 0 || (pQueryInfo->pUdfInfo && taosArrayGetSize(pQueryInfo->pUdfInfo) > 0)) { + if (taosArrayGetSize(plist) > 0 || taosArrayGetSize(pVgroupList) > 0 || + (pQueryInfo->pUdfInfo && taosArrayGetSize(pQueryInfo->pUdfInfo) > 0)) { code = getMultiTableMetaFromMnode(pSql, plist, pVgroupList, pQueryInfo->pUdfInfo, tscTableMetaCallBack, true); } @@ -10032,7 +10072,7 @@ static int32_t doLoadAllTableMeta(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNod const char* msg3 = "alias name too long"; const char* msg4 = "self join not allowed"; - int32_t code = TSDB_CODE_SUCCESS; + int32_t code = TSDB_CODE_SUCCESS; SSqlCmd* pCmd = &pSql->cmd; if (numOfTables > taosHashGetSize(pCmd->pTableMetaMap)) { @@ -10044,14 +10084,14 @@ static int32_t doLoadAllTableMeta(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNod tscAddEmptyMetaInfo(pQueryInfo); } - SRelElementPair *item = taosArrayGet(pSqlNode->from->list, i); - SStrToken *oriName = &item->tableName; + SRelElementPair* item = taosArrayGet(pSqlNode->from->list, i); + SStrToken* oriName = &item->tableName; if (oriName->type == TK_INTEGER || oriName->type == TK_FLOAT) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - bool dbIncluded = false; + bool dbIncluded = false; char buf[TSDB_TABLE_FNAME_LEN]; SStrToken sTblToken; sTblToken.z = buf; @@ -10085,7 +10125,7 @@ static int32_t doLoadAllTableMeta(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNod tNameExtractFullName(&pTableMetaInfo->name, fname); STableMetaVgroupInfo* p = taosHashGet(pCmd->pTableMetaMap, fname, strnlen(fname, TSDB_TABLE_FNAME_LEN)); - pTableMetaInfo->pTableMeta = tscTableMetaDup(p->pTableMeta); + pTableMetaInfo->pTableMeta = tscTableMetaDup(p->pTableMeta); pTableMetaInfo->tableMetaCapacity = tscGetTableMetaSize(pTableMetaInfo->pTableMeta); assert(pTableMetaInfo->pTableMeta != NULL); @@ -10098,16 +10138,18 @@ static int32_t doLoadAllTableMeta(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNod return TSDB_CODE_TSC_OUT_OF_MEMORY; } - pTableMetaInfo->vgroupList->numOfVgroups = (int32_t) s; - for(int32_t j = 0; j < s; ++j) { + pTableMetaInfo->vgroupList->numOfVgroups = (int32_t)s; + for (int32_t j = 0; j < s; ++j) { int32_t* id = taosArrayGet(p->vgroupIdList, j); // check if current buffer contains the vgroup info. If not, add it - SNewVgroupInfo existVgroupInfo = {.inUse = -1,}; + SNewVgroupInfo existVgroupInfo = { + .inUse = -1, + }; taosHashGetClone(UTIL_GET_VGROUPMAP(pSql), id, sizeof(*id), NULL, &existVgroupInfo); assert(existVgroupInfo.inUse >= 0); - SVgroupMsg *pVgroup = &pTableMetaInfo->vgroupList->vgroups[j]; + SVgroupMsg* pVgroup = &pTableMetaInfo->vgroupList->vgroups[j]; pVgroup->numOfEps = existVgroupInfo.numOfEps; pVgroup->vgId = existVgroupInfo.vgId; @@ -10123,23 +10165,23 @@ static STableMeta* extractTempTableMetaFromSubquery(SQueryInfo* pUpstream) { STableMetaInfo* pUpstreamTableMetaInfo = tscGetMetaInfo(pUpstream, 0); int32_t numOfColumns = pUpstream->fieldsInfo.numOfOutput; - STableMeta *meta = calloc(1, sizeof(STableMeta) + sizeof(SSchema) * numOfColumns); + STableMeta* meta = calloc(1, sizeof(STableMeta) + sizeof(SSchema) * numOfColumns); meta->tableType = TSDB_TEMP_TABLE; - STableComInfo *info = &meta->tableInfo; + STableComInfo* info = &meta->tableInfo; info->numOfColumns = numOfColumns; - info->precision = pUpstreamTableMetaInfo->pTableMeta->tableInfo.precision; - info->numOfTags = 0; + info->precision = pUpstreamTableMetaInfo->pTableMeta->tableInfo.precision; + info->numOfTags = 0; int32_t n = 0; - for(int32_t i = 0; i < numOfColumns; ++i) { + for (int32_t i = 0; i < numOfColumns; ++i) { SInternalField* pField = tscFieldInfoGetInternalField(&pUpstream->fieldsInfo, i); if (!pField->visible) { continue; } meta->schema[n].bytes = pField->field.bytes; - meta->schema[n].type = pField->field.type; + meta->schema[n].type = pField->field.type; SExprInfo* pExpr = pField->pExpr; meta->schema[n].colId = pExpr->base.resColId; @@ -10154,7 +10196,8 @@ static STableMeta* extractTempTableMetaFromSubquery(SQueryInfo* pUpstream) { return meta; } -static int32_t doValidateSubquery(SSqlNode* pSqlNode, int32_t index, SSqlObj* pSql, SQueryInfo* pQueryInfo, char* msgBuf) { +static int32_t doValidateSubquery(SSqlNode* pSqlNode, int32_t index, SSqlObj* pSql, SQueryInfo* pQueryInfo, + char* msgBuf) { SRelElementPair* subInfo = taosArrayGet(pSqlNode->from->list, index); // union all is not supported currently @@ -10166,7 +10209,7 @@ static int32_t doValidateSubquery(SSqlNode* pSqlNode, int32_t index, SSqlObj* pS SQueryInfo* pSub = calloc(1, sizeof(SQueryInfo)); tscInitQueryInfo(pSub); - SArray *pUdfInfo = NULL; + SArray* pUdfInfo = NULL; if (pQueryInfo->pUdfInfo) { pUdfInfo = taosArrayDup(pQueryInfo->pUdfInfo); } @@ -10187,7 +10230,7 @@ static int32_t doValidateSubquery(SSqlNode* pSqlNode, int32_t index, SSqlObj* pS return TSDB_CODE_TSC_OUT_OF_MEMORY; } - pTableMetaInfo1->pTableMeta = extractTempTableMetaFromSubquery(pSub); + pTableMetaInfo1->pTableMeta = extractTempTableMetaFromSubquery(pSub); pTableMetaInfo1->tableMetaCapacity = tscGetTableMetaSize(pTableMetaInfo1->pTableMeta); if (subInfo->aliasName.n > 0) { @@ -10215,9 +10258,9 @@ static int32_t doValidateSubquery(SSqlNode* pSqlNode, int32_t index, SSqlObj* pS // all columns are added into the table column list STableMeta* pMeta = pTableMetaInfo1->pTableMeta; - int32_t startOffset = (int32_t) taosArrayGetSize(pQueryInfo->colList); + int32_t startOffset = (int32_t)taosArrayGetSize(pQueryInfo->colList); - for(int32_t i = 0; i < pMeta->tableInfo.numOfColumns; ++i) { + for (int32_t i = 0; i < pMeta->tableInfo.numOfColumns; ++i) { tscColumnListInsert(pQueryInfo->colList, i + startOffset, pMeta->id.uid, &pMeta->schema[i]); } @@ -10232,14 +10275,15 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf 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* msg6 = "not support stddev/percentile in the outer query yet"; - const char* msg7 = "derivative/twa/rate/irate/diff/tail/stateCount/stateDuration requires timestamp column exists in subquery"; + const char* msg7 = + "derivative/twa/rate/irate/diff/tail/stateCount/stateDuration requires timestamp column exists in subquery"; const char* msg8 = "condition missing for join query"; const char* msg9 = "not support 3 level select"; int32_t code = TSDB_CODE_SUCCESS; SSqlCmd* pCmd = &pSql->cmd; - STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); if (pTableMetaInfo == NULL) { pTableMetaInfo = tscAddEmptyMetaInfo(pQueryInfo); } @@ -10266,7 +10310,7 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf for (int32_t i = 0; i < numOfSub; ++i) { // check if there is 3 level select SRelElementPair* subInfo = taosArrayGet(pSqlNode->from->list, i); - SSqlNode* p = taosArrayGetP(subInfo->pSubquery, 0); + SSqlNode* p = taosArrayGetP(subInfo->pSubquery, 0); if (p->from->type == SQL_NODE_FROM_SUBQUERY) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg9); } @@ -10278,8 +10322,7 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf } int32_t timeWindowQuery = - (TPARSER_HAS_TOKEN(pSqlNode->interval.interval) || - TPARSER_HAS_TOKEN(pSqlNode->sessionVal.gap) || + (TPARSER_HAS_TOKEN(pSqlNode->interval.interval) || TPARSER_HAS_TOKEN(pSqlNode->sessionVal.gap) || TPARSER_HAS_TOKEN(pSqlNode->windowstateVal.col)); int32_t joinQuery = (pSqlNode->from != NULL && taosArrayGetSize(pSqlNode->from->list) > 1); @@ -10291,7 +10334,6 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf return TSDB_CODE_TSC_INVALID_OPERATION; } - if (validateSelectNodeList(pCmd, pQueryInfo, pSqlNode->pSelNodeList, false, timeWindowQuery, true) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; @@ -10316,9 +10358,8 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf SExprInfo* pExpr = tscExprGet(pQueryInfo, i); int32_t f = pExpr->base.functionId; - if (f == TSDB_FUNC_DERIVATIVE || f == TSDB_FUNC_TWA || f == TSDB_FUNC_IRATE || - f == TSDB_FUNC_RATE || f == TSDB_FUNC_DIFF || f == TSDB_FUNC_TAIL || - f == TSDB_FUNC_STATE_COUNT || f == TSDB_FUNC_STATE_DURATION) { + if (f == TSDB_FUNC_DERIVATIVE || f == TSDB_FUNC_TWA || f == TSDB_FUNC_IRATE || f == TSDB_FUNC_RATE || + f == TSDB_FUNC_DIFF || f == TSDB_FUNC_TAIL || f == TSDB_FUNC_STATE_COUNT || f == TSDB_FUNC_STATE_DURATION) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg7); } } @@ -10364,7 +10405,7 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf // disable group result mixed up if interval/session window query exists. if (isTimeWindowQuery(pQueryInfo)) { size_t num = taosArrayGetSize(pQueryInfo->pUpstream); - for(int32_t i = 0; i < num; ++i) { + for (int32_t i = 0; i < num; ++i) { SQueryInfo* pUp = taosArrayGetP(pQueryInfo->pUpstream, i); pUp->multigroupResult = false; } @@ -10393,7 +10434,7 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf return code; } -// updateFunctionInterBuf(pQueryInfo, false); + // updateFunctionInterBuf(pQueryInfo, false); updateLastScanOrderIfNeeded(pQueryInfo); if ((code = validateFillNode(pCmd, pQueryInfo, pSqlNode)) != TSDB_CODE_SUCCESS) { @@ -10412,14 +10453,14 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf } // set all query tables, which are maybe more than one. - code = doLoadAllTableMeta(pSql, pQueryInfo, pSqlNode, (int32_t) numOfTables); + code = doLoadAllTableMeta(pSql, pQueryInfo, pSqlNode, (int32_t)numOfTables); if (code != TSDB_CODE_SUCCESS) { return code; } bool isSTable = UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo); - int32_t type = isSTable? TSDB_QUERY_TYPE_STABLE_QUERY:TSDB_QUERY_TYPE_TABLE_QUERY; + int32_t type = isSTable ? TSDB_QUERY_TYPE_STABLE_QUERY : TSDB_QUERY_TYPE_TABLE_QUERY; TSDB_QUERY_SET_TYPE(pQueryInfo->type, type); int32_t joinQuery = (pSqlNode->from != NULL && taosArrayGetSize(pSqlNode->from->list) > 1); @@ -10437,14 +10478,13 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf pSqlNode->pWhere = NULL; } else { - if (taosArrayGetSize(pSqlNode->from->list) > 1) { // Cross join not allowed yet + if (taosArrayGetSize(pSqlNode->from->list) > 1) { // Cross join not allowed yet return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "cross join not supported yet"); } } int32_t timeWindowQuery = - (TPARSER_HAS_TOKEN(pSqlNode->interval.interval) || - TPARSER_HAS_TOKEN(pSqlNode->sessionVal.gap) || + (TPARSER_HAS_TOKEN(pSqlNode->interval.interval) || TPARSER_HAS_TOKEN(pSqlNode->sessionVal.gap) || TPARSER_HAS_TOKEN(pSqlNode->windowstateVal.col)); if (validateSelectNodeList(pCmd, pQueryInfo, pSqlNode->pSelNodeList, joinQuery, timeWindowQuery, false) != @@ -10476,7 +10516,8 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf SExprInfo* pExpr1 = tscExprGet(pQueryInfo, 0); if (pExpr1->base.functionId != TSDB_FUNC_TID_TAG) { - if ((pQueryInfo->colCond && taosArrayGetSize(pQueryInfo->colCond) > 0) || IS_TSWINDOW_SPECIFIED(pQueryInfo->window)) { + if ((pQueryInfo->colCond && taosArrayGetSize(pQueryInfo->colCond) > 0) || + IS_TSWINDOW_SPECIFIED(pQueryInfo->window)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg5); } } @@ -10496,7 +10537,8 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf return TSDB_CODE_TSC_INVALID_OPERATION; } - if (isTimeWindowQuery(pQueryInfo) && (validateFunctionsInIntervalOrGroupbyQuery(pCmd, pQueryInfo) != TSDB_CODE_SUCCESS)) { + if (isTimeWindowQuery(pQueryInfo) && + (validateFunctionsInIntervalOrGroupbyQuery(pCmd, pQueryInfo) != TSDB_CODE_SUCCESS)) { return TSDB_CODE_TSC_INVALID_OPERATION; } @@ -10525,13 +10567,13 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf return code; } - if ((code = doFunctionsCompatibleCheck(pCmd, pQueryInfo,tscGetErrorMsgPayload(pCmd))) != TSDB_CODE_SUCCESS) { + if ((code = doFunctionsCompatibleCheck(pCmd, pQueryInfo, tscGetErrorMsgPayload(pCmd))) != TSDB_CODE_SUCCESS) { return code; } updateLastScanOrderIfNeeded(pQueryInfo); tscFieldInfoUpdateOffset(pQueryInfo); -// updateFunctionInterBuf(pQueryInfo, isSTable); + // updateFunctionInterBuf(pQueryInfo, isSTable); if ((code = validateFillNode(pCmd, pQueryInfo, pSqlNode)) != TSDB_CODE_SUCCESS) { return code; @@ -10540,28 +10582,27 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf if ((code = validateRangeNode(pSql, pQueryInfo, pSqlNode)) != TSDB_CODE_SUCCESS) { return code; } - } - { // set the query info + { // set the query info pQueryInfo->projectionQuery = tscIsProjectionQuery(pQueryInfo); pQueryInfo->hasFilter = tscHasColumnFilter(pQueryInfo); pQueryInfo->simpleAgg = isSimpleAggregateRv(pQueryInfo); pQueryInfo->onlyTagQuery = onlyTagPrjFunction(pQueryInfo); pQueryInfo->groupbyColumn = tscGroupbyColumn(pQueryInfo); pQueryInfo->groupbyTag = tscGroupbyTag(pQueryInfo); - pQueryInfo->arithmeticOnAgg = tsIsArithmeticQueryOnAggResult(pQueryInfo); + pQueryInfo->arithmeticOnAgg = tsIsArithmeticQueryOnAggResult(pQueryInfo); pQueryInfo->orderProjectQuery = tscOrderedProjectionQueryOnSTable(pQueryInfo, 0); SExprInfo** p = NULL; - int32_t numOfExpr = 0; + int32_t numOfExpr = 0; pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); code = createProjectionExpr(pQueryInfo, pTableMetaInfo, &p, &numOfExpr); if (pQueryInfo->exprList1 == NULL) { pQueryInfo->exprList1 = taosArrayInit(4, POINTER_BYTES); } - taosArrayAddBatch(pQueryInfo->exprList1, (void*) p, numOfExpr); + taosArrayAddBatch(pQueryInfo->exprList1, (void*)p, numOfExpr); tfree(p); } @@ -10576,11 +10617,12 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf return TSDB_CODE_SUCCESS; // Does not build query message here } -int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pSqlExpr, SQueryInfo* pQueryInfo, SArray* pCols, uint64_t *uid) { +int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode** pExpr, const tSqlExpr* pSqlExpr, SQueryInfo* pQueryInfo, + SArray* pCols, uint64_t* uid) { if (pSqlExpr->type != SQL_NODE_EXPR && pSqlExpr->type != SQL_NODE_SQLFUNCTION) { assert(pSqlExpr->pLeft == NULL && pSqlExpr->pRight == NULL); if (pSqlExpr->type == SQL_NODE_VALUE) { - if(pSqlExpr->value.nType == -1){ + if (pSqlExpr->value.nType == -1) { return TSDB_CODE_TSC_INVALID_VALUE; } int32_t ret = TSDB_CODE_SUCCESS; @@ -10590,7 +10632,7 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS tVariantAssign((*pExpr)->pVal, &pSqlExpr->value); STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, pQueryInfo->curTableIdx); - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); (*pExpr)->precision = tinfo.precision; STableMeta* pTableMeta = tscGetMetaInfo(pQueryInfo, pQueryInfo->curTableIdx)->pTableMeta; @@ -10602,7 +10644,8 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, idx->colIndex); // convert time by precision - if (pSchema != NULL && TSDB_DATA_TYPE_TIMESTAMP == pSchema->type && TSDB_DATA_TYPE_BINARY == (*pExpr)->pVal->nType) { + if (pSchema != NULL && TSDB_DATA_TYPE_TIMESTAMP == pSchema->type && + TSDB_DATA_TYPE_BINARY == (*pExpr)->pVal->nType) { ret = setColumnFilterInfoForTimestamp(pCmd, pQueryInfo, (*pExpr)->pVal); } } @@ -10651,12 +10694,12 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS *pExpr = calloc(1, sizeof(tExprNode)); (*pExpr)->nodeType = TSQL_NODE_TYPE; (*pExpr)->pType = calloc(1, sizeof(TAOS_FIELD)); - - *(*pExpr)->pType = pSqlExpr->dataType; - + + *(*pExpr)->pType = pSqlExpr->dataType; + return TSDB_CODE_SUCCESS; } else if (pSqlExpr->tokenId == TK_SET) { - int32_t colType = -1; + int32_t colType = -1; STableMeta* pTableMeta = tscGetMetaInfo(pQueryInfo, pQueryInfo->curTableIdx)->pTableMeta; if (pCols != NULL) { size_t colSize = taosArrayGetSize(pCols); @@ -10673,14 +10716,14 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS } } } - tVariant *pVal; + tVariant* 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 = tscGetMetaInfo(pQueryInfo, pQueryInfo->curTableIdx); - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); if (serializeExprListToVariant(pSqlExpr->Expr.paramList, &pVal, colType, tinfo.precision) == false) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "not support filter expression"); } @@ -10698,7 +10741,7 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS if (pSqlExpr->type == SQL_NODE_EXPR) { tExprNode* pLeft = NULL; - tExprNode* pRight= NULL; + tExprNode* pRight = NULL; if (pSqlExpr->pLeft != NULL) { int32_t ret = exprTreeFromSqlExpr(pCmd, &pLeft, pSqlExpr->pLeft, pQueryInfo, pCols, uid); @@ -10709,15 +10752,17 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS if (pSqlExpr->tokenId == TK_BITAND && pSqlExpr->pLeft != NULL && pSqlExpr->pRight != NULL) { // for example: col type is "bool" but expr "col & 1" received - uint8_t colType = pLeft->pSchema->type; - SStrToken *exprToken = &pSqlExpr->pRight->exprToken; + uint8_t colType = pLeft->pSchema->type; + SStrToken* exprToken = &pSqlExpr->pRight->exprToken; if (pSqlExpr->pLeft->type == SQL_NODE_TABLE_COLUMN && pSqlExpr->pRight->type == SQL_NODE_VALUE) { if (colType == TSDB_DATA_TYPE_BOOL) { - if ((exprToken->n != 4 || strncasecmp(exprToken->z, "true", 4)) && (exprToken->n != 5 || strncasecmp(exprToken->z, "false", 5))) { + if ((exprToken->n != 4 || strncasecmp(exprToken->z, "true", 4)) && + (exprToken->n != 5 || strncasecmp(exprToken->z, "false", 5))) { return TSDB_CODE_TSC_INVALID_OPERATION; } } else if (IS_SIGNED_NUMERIC_TYPE(colType) || IS_UNSIGNED_NUMERIC_TYPE(colType)) { - if ((exprToken->n == 4 && strncasecmp(exprToken->z, "true", 4) == 0) || (exprToken->n == 5 || strncasecmp(exprToken->z, "false", 5) == 0)) { + if ((exprToken->n == 4 && strncasecmp(exprToken->z, "true", 4) == 0) || + (exprToken->n == 5 || strncasecmp(exprToken->z, "false", 5) == 0)) { return TSDB_CODE_TSC_INVALID_OPERATION; } } @@ -10732,7 +10777,7 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS } } - *pExpr = (tExprNode *)calloc(1, sizeof(tExprNode)); + *pExpr = (tExprNode*)calloc(1, sizeof(tExprNode)); (*pExpr)->nodeType = TSQL_NODE_EXPR; (*pExpr)->_node.hasPK = false; @@ -10758,7 +10803,7 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS // NOTE: binary|nchar data allows the >|< type filter if ((*pExpr)->_node.optr != TSDB_RELATION_EQUAL && (*pExpr)->_node.optr != TSDB_RELATION_NOT_EQUAL) { if (pRight != NULL && pRight->nodeType == TSQL_NODE_VALUE) { - if (pLeft->_node.optr == TSDB_RELATION_ARROW){ + if (pLeft->_node.optr == TSDB_RELATION_ARROW) { pLeft = pLeft->_node.pLeft; } if (pRight->pVal->nType == TSDB_DATA_TYPE_BOOL && pLeft->nodeType == TSQL_NODE_COL) { @@ -10777,19 +10822,19 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS (*pExpr)->_func.functionId = pSqlExpr->functionId; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, pQueryInfo->curTableIdx); - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); (*pExpr)->precision = tinfo.precision; SArray* paramList = pSqlExpr->Expr.paramList; - size_t paramSize = paramList ? taosArrayGetSize(paramList) : 0; + size_t paramSize = paramList ? taosArrayGetSize(paramList) : 0; if (paramSize > 0) { (*pExpr)->_func.numChildren = (int32_t)paramSize; (*pExpr)->_func.pChildren = (tExprNode**)calloc(paramSize, sizeof(tExprNode*)); } for (int32_t i = 0; i < paramSize; ++i) { tSqlExprItem* param = taosArrayGet(paramList, i); - tSqlExpr* paramNode = param->pNode; - int32_t ret = exprTreeFromSqlExpr(pCmd, (*pExpr)->_func.pChildren+i, paramNode, pQueryInfo, pCols, uid); + tSqlExpr* paramNode = param->pNode; + int32_t ret = exprTreeFromSqlExpr(pCmd, (*pExpr)->_func.pChildren + i, paramNode, pQueryInfo, pCols, uid); if (ret != TSDB_CODE_SUCCESS) { return ret; } @@ -10799,15 +10844,13 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS *pExpr = calloc(1, sizeof(tExprNode)); (*pExpr)->nodeType = TSQL_NODE_COL; (*pExpr)->pSchema = calloc(1, sizeof(SSchema)); - if (tsKeepOriginalColumnName && // TD-14196, tsKeepOriginalColumnName params makes logic special - (pSqlExpr->functionId == TSDB_FUNC_FIRST || - pSqlExpr->functionId == TSDB_FUNC_LAST || - pSqlExpr->functionId == TSDB_FUNC_SPREAD || - pSqlExpr->functionId == TSDB_FUNC_LAST_ROW || + if (tsKeepOriginalColumnName && // TD-14196, tsKeepOriginalColumnName params makes logic special + (pSqlExpr->functionId == TSDB_FUNC_FIRST || pSqlExpr->functionId == TSDB_FUNC_LAST || + pSqlExpr->functionId == TSDB_FUNC_SPREAD || pSqlExpr->functionId == TSDB_FUNC_LAST_ROW || pSqlExpr->functionId == TSDB_FUNC_INTERP)) { tSqlExprItem* pParamElem = taosArrayGet(pSqlExpr->Expr.paramList, 0); strncpy((*pExpr)->pSchema->name, pParamElem->pNode->columnName.z, pParamElem->pNode->columnName.n); - }else{ + } else { strncpy((*pExpr)->pSchema->name, pSqlExpr->exprToken.z, pSqlExpr->exprToken.n); } @@ -10826,8 +10869,8 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS } break; } - } // endfor each expr - } // end not scalar function + } // endfor each expr + } // end not scalar function } return TSDB_CODE_SUCCESS; } diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index b9834b45b0d89c66e886365bd525001b995ca7a6..8673a1844c4e42439e492fb22c0e1c7ac83d3f05 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -16,30 +16,30 @@ #include "os.h" #include "taosdef.h" #include "taosmsg.h" -#include "texpr.h" #include "tdigest.h" -#include "ttype.h" +#include "texpr.h" #include "tsdb.h" +#include "ttype.h" +#include "hashfunc.h" #include "qAggMain.h" #include "qFill.h" #include "qHistogram.h" #include "qPercentile.h" #include "qTsbuf.h" -#include "queryLog.h" #include "qUdf.h" +#include "queryLog.h" #include "tcompare.h" -#include "hashfunc.h" #define GET_INPUT_DATA_LIST(x) ((char *)((x)->pInput)) #define GET_INPUT_DATA(x, y) (GET_INPUT_DATA_LIST(x) + (y) * (x)->inputBytes) -#define GET_TS_LIST(x) ((TSKEY*)((x)->ptsList)) +#define GET_TS_LIST(x) ((TSKEY *)((x)->ptsList)) #define GET_TS_DATA(x, y) (GET_TS_LIST(x)[(y)]) #define GET_TRUE_DATA_TYPE() \ int32_t type = 0; \ - if (pCtx->currentStage == MERGE_STAGE) { \ + if (pCtx->currentStage == MERGE_STAGE) { \ type = pCtx->outputType; \ assert(pCtx->inputType == TSDB_DATA_TYPE_BINARY); \ } else { \ @@ -68,12 +68,12 @@ } \ } while (0) -#define DO_UPDATE_TAG_COLUMNS_WITHOUT_TS(ctx) \ - do { \ +#define DO_UPDATE_TAG_COLUMNS_WITHOUT_TS(ctx) \ + do { \ for (int32_t _i = 0; _i < (ctx)->tagInfo.numOfTagCols; ++_i) { \ - SQLFunctionCtx *__ctx = (ctx)->tagInfo.pTagCtxList[_i]; \ - aAggs[TSDB_FUNC_TAG].xFunction(__ctx); \ - } \ + SQLFunctionCtx *__ctx = (ctx)->tagInfo.pTagCtxList[_i]; \ + aAggs[TSDB_FUNC_TAG].xFunction(__ctx); \ + } \ } while (0); void noop1(SQLFunctionCtx *UNUSED_PARAM(pCtx)) {} @@ -83,7 +83,7 @@ void doFinalizer(SQLFunctionCtx *pCtx) { RESET_RESULT_INFO(GET_RES_INFO(pCtx)); typedef struct tValuePair { tVariant v; int64_t timestamp; - char * pTags; // the corresponding tags of each record in the final result + char *pTags; // the corresponding tags of each record in the final result } tValuePair; typedef struct SSpreadInfo { @@ -147,7 +147,7 @@ typedef struct SLeastsquaresInfo { typedef struct SAPercentileInfo { SHistogramInfo *pHisto; - TDigest* pTDigest; + TDigest *pTDigest; } SAPercentileInfo; typedef struct STSCompInfo { @@ -155,52 +155,52 @@ typedef struct STSCompInfo { } STSCompInfo; typedef struct SRateInfo { - double correctionValue; - double firstValue; - TSKEY firstKey; - double lastValue; - TSKEY lastKey; - int8_t hasResult; // flag to denote has value - bool isIRate; // true for IRate functions, false for Rate functions + double correctionValue; + double firstValue; + TSKEY firstKey; + double lastValue; + TSKEY lastKey; + int8_t hasResult; // flag to denote has value + bool isIRate; // true for IRate functions, false for Rate functions } SRateInfo; typedef struct SDerivInfo { - double prevValue; // previous value - TSKEY prevTs; // previous timestamp - bool ignoreNegative;// ignore the negative value - int64_t tsWindow; // time window for derivative - bool valueSet; // the value has been set already + double prevValue; // previous value + TSKEY prevTs; // previous timestamp + bool ignoreNegative; // ignore the negative value + int64_t tsWindow; // time window for derivative + bool valueSet; // the value has been set already } SDerivInfo; typedef struct { union { - double d64CumSum; - int64_t i64CumSum; + double d64CumSum; + int64_t i64CumSum; uint64_t u64CumSum; }; } SCumSumInfo; typedef struct { int32_t pos; - double sum; + double sum; int32_t numPointsK; - double* points; + double *points; bool kPointsMeet; } SMovingAvgInfo; -typedef struct { - int32_t totalPoints; - int32_t numSampled; - int16_t colBytes; - char *values; +typedef struct { + int32_t totalPoints; + int32_t numSampled; + int16_t colBytes; + char *values; int64_t *timeStamps; - char *taglists; + char *taglists; } SSampleFuncInfo; typedef struct SElapsedInfo { int8_t hasResult; - TSKEY min; - TSKEY max; + TSKEY min; + TSKEY max; } SElapsedInfo; typedef struct { @@ -208,11 +208,10 @@ typedef struct { bool ignoreNegative; union { int64_t i64Prev; - double d64Prev; + double d64Prev; }; } SDiffFuncInfo; - typedef struct { union { int64_t countPrev; @@ -221,45 +220,45 @@ typedef struct { } SStateInfo; typedef struct { - double lower; // >lower - double upper; // <=upper + double lower; // >lower + double upper; // <=upper double count; } SHistogramFuncBin; -typedef struct{ - int32_t numOfBins; - int32_t normalized; - SHistogramFuncBin* orderedBins; +typedef struct { + int32_t numOfBins; + int32_t normalized; + SHistogramFuncBin *orderedBins; } SHistogramFuncInfo; typedef struct { - int64_t timestamp; - char data[]; + int64_t timestamp; + char data[]; } UniqueUnit; typedef struct { - int32_t num; - char res[]; + int32_t num; + char res[]; } SUniqueFuncInfo; typedef struct { - int64_t count; - char data[]; + int64_t count; + char data[]; } ModeUnit; typedef struct { - int32_t num; - char res[]; + int32_t num; + char res[]; } SModeFuncInfo; typedef struct { - int64_t timestamp; - char data[]; + int64_t timestamp; + char data[]; } TailUnit; typedef struct { - int32_t num; - TailUnit **res; + int32_t num; + TailUnit **res; } STailInfo; static void *getOutputInfo(SQLFunctionCtx *pCtx) { @@ -268,31 +267,32 @@ static void *getOutputInfo(SQLFunctionCtx *pCtx) { // only the first_stage_merge is directly written data into final output buffer if (pCtx->stableQuery && pCtx->currentStage != MERGE_STAGE) { return pCtx->pOutput; - } else { // during normal table query and super table at the secondary_stage, result is written to intermediate buffer + } else { // during normal table query and super table at the secondary_stage, result is written to intermediate + // buffer return GET_ROWCELL_INTERBUF(pResInfo); } } /* hyperloglog start */ -#define HLL_BUCKET_BITS 14 // The bits of the bucket -#define HLL_DATA_BITS (64-HLL_BUCKET_BITS) -#define HLL_BUCKETS (1<>3; j++) { + for (int32_t j = 0; j < HLL_BUCKETS >> 3; j++) { if (*word == 0) { bucketHisto[0] += 8; } else { - bytes = (uint8_t*) word; + bytes = (uint8_t *)word; bucketHisto[bytes[0]]++; bucketHisto[bytes[1]]++; bucketHisto[bytes[2]]++; @@ -314,8 +314,8 @@ static double hllTau(double x) { x = sqrt(x); zPrime = z; y *= 0.5; - z -= pow(1 - x, 2)*y; - } while(zPrime != z); + z -= pow(1 - x, 2) * y; + } while (zPrime != z); return z / 3; } @@ -329,35 +329,36 @@ static double hllSigma(double x) { zPrime = z; z += x * y; y += y; - } while(zPrime != z); + } while (zPrime != z); return z; } -// estimate the cardinality, the algorithm refer this paper: "New cardinality estimation algorithms for HyperLogLog sketches" +// estimate the cardinality, the algorithm refer this paper: "New cardinality estimation algorithms for HyperLogLog +// sketches" static uint64_t hllCountCnt(uint8_t *buckets) { - double m = HLL_BUCKETS; + double m = HLL_BUCKETS; int32_t buckethisto[64] = {0}; - hllBucketHisto(buckets,buckethisto); + hllBucketHisto(buckets, buckethisto); - double z = m * hllTau((m-buckethisto[HLL_DATA_BITS+1])/(double)m); + double z = m * hllTau((m - buckethisto[HLL_DATA_BITS + 1]) / (double)m); for (int j = HLL_DATA_BITS; j >= 1; --j) { z += buckethisto[j]; z *= 0.5; } - z += m * hllSigma(buckethisto[0]/(double)m); - double E = (double)llroundl(HLL_ALPHA_INF*m*m/z); + z += m * hllSigma(buckethisto[0] / (double)m); + double E = (double)llroundl(HLL_ALPHA_INF * m * m / z); - return (uint64_t) E; + return (uint64_t)E; } static uint8_t hllCountNum(void *ele, int32_t elesize, int32_t *buk) { - uint64_t hash = MurmurHash3_64(ele,elesize); - int32_t index = hash & HLL_BUCKET_MASK; + uint64_t hash = MurmurHash3_64(ele, elesize); + int32_t index = hash & HLL_BUCKET_MASK; hash >>= HLL_BUCKET_BITS; - hash |= ((uint64_t)1<inputBytes; - if(IS_VAR_DATA_TYPE(pCtx->inputType)) { + if (IS_VAR_DATA_TYPE(pCtx->inputType)) { elesize = varDataLen(val); val = varDataVal(val); } int32_t index = 0; - uint8_t count = hllCountNum(val,elesize,&index); + uint8_t count = hllCountNum(val, elesize, &index); uint8_t oldcount = pHLLInfo->buckets[index]; if (count > oldcount) { pHLLInfo->buckets[index] = count; @@ -389,7 +390,7 @@ static void hll_function(SQLFunctionCtx *pCtx) { static void hll_func_merge(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SHLLInfo *pHLLInfo = (SHLLInfo *)GET_ROWCELL_INTERBUF(pResInfo); + SHLLInfo *pHLLInfo = (SHLLInfo *)GET_ROWCELL_INTERBUF(pResInfo); SHLLInfo *pData = (SHLLInfo *)GET_INPUT_DATA_LIST(pCtx); for (int i = 0; i < HLL_BUCKETS; i++) { @@ -409,7 +410,8 @@ static void hll_func_finalizer(SQLFunctionCtx *pCtx) { /* hyperloglog end */ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionId, int32_t param, int16_t *type, - int32_t *bytes, int32_t *interBytes, int16_t extLength, bool isSuperTable, SUdfInfo* pUdfInfo) { + int32_t *bytes, int32_t *interBytes, int16_t extLength, bool isSuperTable, + SUdfInfo *pUdfInfo) { if (!isValidDataType(dataType)) { qError("Illegal data type %d or data type length %d", dataType, dataBytes); return TSDB_CODE_TSC_INVALID_OPERATION; @@ -420,8 +422,7 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI if (functionId == TSDB_FUNC_TS || functionId == TSDB_FUNC_TS_DUMMY || functionId == TSDB_FUNC_TAG_DUMMY || functionId == TSDB_FUNC_DIFF || functionId == TSDB_FUNC_PRJ || functionId == TSDB_FUNC_TAGPRJ || - functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_INTERP) - { + functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_INTERP) { *type = (int16_t)dataType; *bytes = dataBytes; @@ -433,12 +434,11 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI *interBytes = 0; } - return TSDB_CODE_SUCCESS; } // (uid, tid) + VGID + TAGSIZE + VARSTR_HEADER_SIZE - if (functionId == TSDB_FUNC_TID_TAG) { // todo use struct + if (functionId == TSDB_FUNC_TID_TAG) { // todo use struct *type = TSDB_DATA_TYPE_BINARY; *bytes = (dataBytes + sizeof(int16_t) + sizeof(int64_t) + sizeof(int32_t) + sizeof(int32_t) + VARSTR_HEADER_SIZE); *interBytes = 0; @@ -459,7 +459,6 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI return TSDB_CODE_SUCCESS; } - if (functionId == TSDB_FUNC_TS_COMP) { *type = TSDB_DATA_TYPE_BINARY; *bytes = 1; // this results is compressed ts data, only one byte @@ -563,7 +562,7 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI int64_t size = sizeof(ModeUnit) + dataBytes; size *= MAX_MODE_INNER_RESULT_ROWS; size += sizeof(SModeFuncInfo); - if (size > MAX_MODE_INNER_RESULT_SIZE){ + if (size > MAX_MODE_INNER_RESULT_SIZE) { size = MAX_MODE_INNER_RESULT_SIZE; } *bytes = (int32_t)size; @@ -583,7 +582,7 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI return TSDB_CODE_SUCCESS; } else if (functionId == TSDB_FUNC_SAMPLE) { *type = TSDB_DATA_TYPE_BINARY; - *bytes = (sizeof(SSampleFuncInfo) + dataBytes*param + sizeof(int64_t)*param + extLength*param); + *bytes = (sizeof(SSampleFuncInfo) + dataBytes * param + sizeof(int64_t) * param + extLength * param); *interBytes = *bytes; return TSDB_CODE_SUCCESS; @@ -595,8 +594,9 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI return TSDB_CODE_SUCCESS; } else if (functionId == TSDB_FUNC_APERCT) { *type = TSDB_DATA_TYPE_BINARY; - int16_t bytesHist = sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1) + sizeof(SHistogramInfo) + sizeof(SAPercentileInfo); - int32_t bytesDigest = (int32_t) (sizeof(SAPercentileInfo) + TDIGEST_SIZE(COMPRESSION)); + int16_t bytesHist = + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1) + sizeof(SHistogramInfo) + sizeof(SAPercentileInfo); + int32_t bytesDigest = (int32_t)(sizeof(SAPercentileInfo) + TDIGEST_SIZE(COMPRESSION)); *bytes = MAX(bytesHist, bytesDigest); *interBytes = *bytes; @@ -641,7 +641,7 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI *type = TSDB_DATA_TYPE_DOUBLE; *bytes = sizeof(double); int16_t bytesHist = sizeof(SAPercentileInfo) + sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1); - int32_t bytesDigest = (int32_t) (sizeof(SAPercentileInfo) + TDIGEST_SIZE(COMPRESSION)); + int32_t bytesDigest = (int32_t)(sizeof(SAPercentileInfo) + TDIGEST_SIZE(COMPRESSION)); *interBytes = MAX(bytesHist, bytesDigest); return TSDB_CODE_SUCCESS; } else if (functionId == TSDB_FUNC_TWA) { @@ -714,17 +714,17 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI int64_t size = sizeof(UniqueUnit) + dataBytes + extLength; size *= param; size += sizeof(SUniqueFuncInfo); - if (size > MAX_UNIQUE_RESULT_SIZE){ + if (size > MAX_UNIQUE_RESULT_SIZE) { size = MAX_UNIQUE_RESULT_SIZE; } *interBytes = (int32_t)size; - } else if(functionId == TSDB_FUNC_MODE) { + } else if (functionId == TSDB_FUNC_MODE) { *type = (int16_t)dataType; *bytes = dataBytes; int64_t size = sizeof(ModeUnit) + dataBytes; size *= MAX_MODE_INNER_RESULT_ROWS; size += sizeof(SModeFuncInfo); - if (size > MAX_MODE_INNER_RESULT_SIZE){ + if (size > MAX_MODE_INNER_RESULT_SIZE) { size = MAX_MODE_INNER_RESULT_SIZE; } *interBytes = (int32_t)size; @@ -743,7 +743,7 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI } else if (functionId == TSDB_FUNC_SAMPLE) { *type = (int16_t)dataType; *bytes = dataBytes; - size_t size = sizeof(SSampleFuncInfo) + dataBytes*param + sizeof(int64_t)*param + extLength*param; + size_t size = sizeof(SSampleFuncInfo) + dataBytes * param + sizeof(int64_t) * param + extLength * param; *interBytes = (int32_t)size; } else if (functionId == TSDB_FUNC_LAST_ROW) { *type = (int16_t)dataType; @@ -774,10 +774,9 @@ bool isTimeWindowFunction(int32_t functionId) { } // TODO use hash table -int32_t isValidFunction(const char* name, int32_t len) { - +int32_t isValidFunction(const char *name, int32_t len) { for (int32_t i = 0; i < TSDB_FUNC_SCALAR_NUM_FUNCTIONS; ++i) { - int32_t nameLen = (int32_t) strlen(aScalarFunctions[i].name); + int32_t nameLen = (int32_t)strlen(aScalarFunctions[i].name); if (len != nameLen) { continue; } @@ -787,8 +786,8 @@ int32_t isValidFunction(const char* name, int32_t len) { } } - for(int32_t i = 0; i < TSDB_FUNC_MAX_NUM; ++i) { - int32_t nameLen = (int32_t) strlen(aAggs[i].name); + for (int32_t i = 0; i < TSDB_FUNC_MAX_NUM; ++i) { + int32_t nameLen = (int32_t)strlen(aAggs[i].name); if (len != nameLen) { continue; } @@ -800,24 +799,30 @@ int32_t isValidFunction(const char* name, int32_t len) { return -1; } -bool isValidStateOper(char *oper, int32_t len){ +bool isValidStateOper(char *oper, int32_t len) { return strncmp(oper, "lt", len) == 0 || strncmp(oper, "gt", len) == 0 || strncmp(oper, "le", len) == 0 || strncmp(oper, "ge", len) == 0 || strncmp(oper, "ne", len) == 0 || strncmp(oper, "eq", len) == 0; } -#define STATEOPER(OPER, COMP, TYPE) if (strncmp(oper->pz, OPER, oper->nLen) == 0) {\ -if (pVar->nType == TSDB_DATA_TYPE_BIGINT && *(TYPE)data COMP pVar->i64) return true;\ -else if(pVar->nType == TSDB_DATA_TYPE_DOUBLE && *(TYPE)data COMP pVar->dKey) return true;\ -else return false;} +#define STATEOPER(OPER, COMP, TYPE) \ + if (strncmp(oper->pz, OPER, oper->nLen) == 0) { \ + if (pVar->nType == TSDB_DATA_TYPE_BIGINT && *(TYPE)data COMP pVar->i64) \ + return true; \ + else if (pVar->nType == TSDB_DATA_TYPE_DOUBLE && *(TYPE)data COMP pVar->dKey) \ + return true; \ + else \ + return false; \ + } -#define STATEJUDGE(TYPE) STATEOPER("lt", <, TYPE)\ -STATEOPER("gt", >, TYPE)\ -STATEOPER("le", <=, TYPE)\ -STATEOPER("ge", >=, TYPE)\ -STATEOPER("ne", !=, TYPE)\ -STATEOPER("eq", ==, TYPE) +#define STATEJUDGE(TYPE) \ + STATEOPER("lt", <, TYPE) \ + STATEOPER("gt", >, TYPE) \ + STATEOPER("le", <=, TYPE) \ + STATEOPER("ge", >=, TYPE) \ + STATEOPER("ne", !=, TYPE) \ + STATEOPER("eq", ==, TYPE) -static bool isStateOperTrue(void *data, int16_t type, tVariant *oper, tVariant *pVar){ +static bool isStateOperTrue(void *data, int16_t type, tVariant *oper, tVariant *pVar) { switch (type) { case TSDB_DATA_TYPE_INT: { STATEJUDGE(int32_t *) @@ -831,7 +836,8 @@ static bool isStateOperTrue(void *data, int16_t type, tVariant *oper, tVariant * case TSDB_DATA_TYPE_BIGINT: { STATEJUDGE(int64_t *) break; - }case TSDB_DATA_TYPE_UBIGINT: { + } + case TSDB_DATA_TYPE_UBIGINT: { STATEJUDGE(uint64_t *) break; } @@ -870,7 +876,7 @@ static bool isStateOperTrue(void *data, int16_t type, tVariant *oper, tVariant * return false; } -static bool function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResultInfo) { +static bool function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResultInfo) { if (pResultInfo->initialized) { return false; } @@ -921,7 +927,8 @@ static void count_function(SQLFunctionCtx *pCtx) { numOfElem += 1; } } else { - //when counting on the primary time stamp column and no statistics data is presented, use the size value directly. + // when counting on the primary time stamp column and no statistics data is presented, use the size value + // directly. numOfElem = pCtx->size; } } @@ -952,7 +959,7 @@ static void count_func_merge(SQLFunctionCtx *pCtx) { * @param filterCols * @return */ -int32_t countRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { +int32_t countRequired(SQLFunctionCtx *pCtx, STimeWindow *w, int32_t colId) { if (colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) { return BLK_DATA_NO_NEEDED; } else { @@ -960,34 +967,32 @@ int32_t countRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { } } -int32_t noDataRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { - return BLK_DATA_NO_NEEDED; -} -#define LIST_ADD_N_DOUBLE_FLOAT(x, ctx, p, t, numOfElem, tsdbType) \ - do { \ - t *d = (t *)(p); \ - for (int32_t i = 0; i < (ctx)->size; ++i) { \ - if (((ctx)->hasNull) && isNull((char *)&(d)[i], tsdbType)) { \ - continue; \ - }; \ - SET_DOUBLE_VAL(&(x) , GET_DOUBLE_VAL(&(x)) + GET_FLOAT_VAL(&(d)[i])); \ - (numOfElem)++; \ - } \ - } while(0) -#define LIST_ADD_N_DOUBLE(x, ctx, p, t, numOfElem, tsdbType) \ - do { \ +int32_t noDataRequired(SQLFunctionCtx *pCtx, STimeWindow *w, int32_t colId) { return BLK_DATA_NO_NEEDED; } +#define LIST_ADD_N_DOUBLE_FLOAT(x, ctx, p, t, numOfElem, tsdbType) \ + do { \ + t *d = (t *)(p); \ + for (int32_t i = 0; i < (ctx)->size; ++i) { \ + if (((ctx)->hasNull) && isNull((char *)&(d)[i], tsdbType)) { \ + continue; \ + }; \ + SET_DOUBLE_VAL(&(x), GET_DOUBLE_VAL(&(x)) + GET_FLOAT_VAL(&(d)[i])); \ + (numOfElem)++; \ + } \ + } while (0) +#define LIST_ADD_N_DOUBLE(x, ctx, p, t, numOfElem, tsdbType) \ + do { \ t *d = (t *)(p); \ for (int32_t i = 0; i < (ctx)->size; ++i) { \ if (((ctx)->hasNull) && isNull((char *)&(d)[i], tsdbType)) { \ continue; \ }; \ - SET_DOUBLE_VAL(&(x) , (x) + (d)[i]); \ + SET_DOUBLE_VAL(&(x), (x) + (d)[i]); \ (numOfElem)++; \ } \ - } while(0) + } while (0) #define LIST_ADD_N(x, ctx, p, t, numOfElem, tsdbType) \ - do { \ + do { \ t *d = (t *)(p); \ for (int32_t i = 0; i < (ctx)->size; ++i) { \ if (((ctx)->hasNull) && isNull((char *)&(d)[i], tsdbType)) { \ @@ -996,7 +1001,7 @@ int32_t noDataRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { (x) += (d)[i]; \ (numOfElem)++; \ } \ - } while(0) + } while (0) #define UPDATE_DATA(ctx, left, right, num, sign, k) \ do { \ @@ -1021,7 +1026,7 @@ int32_t noDataRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { if ((ctx)->hasNull && isNull((char *)&(list)[i], tsdbType)) { \ continue; \ } \ - TSKEY key = (ctx)->ptsList != NULL? GET_TS_DATA(ctx, i):0; \ + TSKEY key = (ctx)->ptsList != NULL ? GET_TS_DATA(ctx, i) : 0; \ UPDATE_DATA(ctx, val, (list)[i], num, sign, key); \ } @@ -1047,8 +1052,8 @@ static void do_sum(SQLFunctionCtx *pCtx) { uint64_t *retVal = (uint64_t *)pCtx->pOutput; *retVal += (uint64_t)pCtx->preAggVals.statis.sum; } else if (IS_FLOAT_TYPE(pCtx->inputType)) { - double *retVal = (double*) pCtx->pOutput; - SET_DOUBLE_VAL(retVal, *retVal + GET_DOUBLE_VAL((const char*)&(pCtx->preAggVals.statis.sum))); + double *retVal = (double *)pCtx->pOutput; + SET_DOUBLE_VAL(retVal, *retVal + GET_DOUBLE_VAL((const char *)&(pCtx->preAggVals.statis.sum))); } } else { // computing based on the true data block void *pData = GET_INPUT_DATA_LIST(pCtx); @@ -1114,7 +1119,7 @@ static void sum_func_merge(SQLFunctionCtx *pCtx) { assert(pCtx->stableQuery); for (int32_t i = 0; i < pCtx->size; ++i) { - char * input = GET_INPUT_DATA(pCtx, i); + char *input = GET_INPUT_DATA(pCtx, i); SSumInfo *pInput = (SSumInfo *)input; if (pInput->hasResult != DATA_SET_FLAG) { continue; @@ -1125,7 +1130,7 @@ static void sum_func_merge(SQLFunctionCtx *pCtx) { if (IS_SIGNED_NUMERIC_TYPE(type)) { *(int64_t *)pCtx->pOutput += pInput->isum; } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) { - *(uint64_t *) pCtx->pOutput += pInput->usum; + *(uint64_t *)pCtx->pOutput += pInput->usum; } else { SET_DOUBLE_VAL((double *)pCtx->pOutput, *(double *)pCtx->pOutput + pInput->dsum); } @@ -1139,16 +1144,12 @@ static void sum_func_merge(SQLFunctionCtx *pCtx) { } } -static int32_t statisRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { - return BLK_DATA_STATIS_NEEDED; -} +static int32_t statisRequired(SQLFunctionCtx *pCtx, STimeWindow *w, int32_t colId) { return BLK_DATA_STATIS_NEEDED; } -static int32_t dataBlockRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { - return BLK_DATA_ALL_NEEDED; -} +static int32_t dataBlockRequired(SQLFunctionCtx *pCtx, STimeWindow *w, int32_t colId) { return BLK_DATA_ALL_NEEDED; } // todo: if column in current data block are null, opt for this case -static int32_t firstFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { +static int32_t firstFuncRequired(SQLFunctionCtx *pCtx, STimeWindow *w, int32_t colId) { if (pCtx->order == TSDB_ORDER_DESC) { return BLK_DATA_NO_NEEDED; } @@ -1161,7 +1162,7 @@ static int32_t firstFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t c } } -static int32_t lastFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { +static int32_t lastFuncRequired(SQLFunctionCtx *pCtx, STimeWindow *w, int32_t colId) { if (pCtx->order != pCtx->param[0].i64) { return BLK_DATA_NO_NEEDED; } @@ -1173,7 +1174,7 @@ static int32_t lastFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t co } } -static int32_t firstDistFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { +static int32_t firstDistFuncRequired(SQLFunctionCtx *pCtx, STimeWindow *w, int32_t colId) { if (pCtx->order == TSDB_ORDER_DESC) { return BLK_DATA_NO_NEEDED; } @@ -1185,7 +1186,7 @@ static int32_t firstDistFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32 // the pCtx should be set to current Ctx and output buffer before call this function. Otherwise, pCtx->pOutput is // the previous windowRes output buffer, not current unloaded block. In this case, the following filter is invalid - SFirstLastInfo *pInfo = (SFirstLastInfo*) (pCtx->pOutput + pCtx->inputBytes); + SFirstLastInfo *pInfo = (SFirstLastInfo *)(pCtx->pOutput + pCtx->inputBytes); if (pInfo->hasResult != DATA_SET_FLAG) { return BLK_DATA_ALL_NEEDED; } else { // data in current block is not earlier than current result @@ -1193,7 +1194,7 @@ static int32_t firstDistFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32 } } -static int32_t lastDistFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { +static int32_t lastDistFuncRequired(SQLFunctionCtx *pCtx, STimeWindow *w, int32_t colId) { if (pCtx->order != pCtx->param[0].i64) { return BLK_DATA_NO_NEEDED; } @@ -1205,7 +1206,7 @@ static int32_t lastDistFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_ // the pCtx should be set to current Ctx and output buffer before call this function. Otherwise, pCtx->pOutput is // the previous windowRes output buffer, not current unloaded block. In this case, the following filter is invalid - SFirstLastInfo *pInfo = (SFirstLastInfo*) (pCtx->pOutput + pCtx->inputBytes); + SFirstLastInfo *pInfo = (SFirstLastInfo *)(pCtx->pOutput + pCtx->inputBytes); if (pInfo->hasResult != DATA_SET_FLAG) { return BLK_DATA_ALL_NEEDED; } else { @@ -1213,7 +1214,7 @@ static int32_t lastDistFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_ } } -static int32_t tailFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { +static int32_t tailFuncRequired(SQLFunctionCtx *pCtx, STimeWindow *w, int32_t colId) { // not initialized yet, it is the first block, load it. if (pCtx->pOutput == NULL) { return BLK_DATA_ALL_NEEDED; @@ -1221,9 +1222,9 @@ static int32_t tailFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t co // the pCtx should be set to current Ctx and output buffer before call this function. Otherwise, pCtx->pOutput is // the previous windowRes output buffer, not current unloaded block. In this case, the following filter is invalid - STailInfo *pInfo = (STailInfo*) (pCtx->pOutput); + STailInfo *pInfo = (STailInfo *)(pCtx->pOutput); TailUnit **pList = pInfo->res; - if (pInfo->num >= pCtx->param[0].i64 && pList[0]->timestamp > w->ekey){ + if (pInfo->num >= pCtx->param[0].i64 && pList[0]->timestamp > w->ekey) { return BLK_DATA_NO_NEEDED; } else { return BLK_DATA_ALL_NEEDED; @@ -1245,14 +1246,14 @@ static void avg_function(SQLFunctionCtx *pCtx) { SAvgInfo *pAvgInfo = (SAvgInfo *)GET_ROWCELL_INTERBUF(pResInfo); double *pVal = &pAvgInfo->sum; - if (pCtx->preAggVals.isSet) { // Pre-aggregation + if (pCtx->preAggVals.isSet) { // Pre-aggregation notNullElems = pCtx->size - pCtx->preAggVals.statis.numOfNull; assert(notNullElems >= 0); if (IS_SIGNED_NUMERIC_TYPE(pCtx->inputType)) { *pVal += pCtx->preAggVals.statis.sum; - } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->inputType)) { - *pVal += (uint64_t) pCtx->preAggVals.statis.sum; + } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->inputType)) { + *pVal += (uint64_t)pCtx->preAggVals.statis.sum; } else if (pCtx->inputType == TSDB_DATA_TYPE_DOUBLE || pCtx->inputType == TSDB_DATA_TYPE_FLOAT) { *pVal += GET_DOUBLE_VAL((const char *)&(pCtx->preAggVals.statis.sum)); } @@ -1302,7 +1303,7 @@ static void avg_function(SQLFunctionCtx *pCtx) { static void avg_func_merge(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - double *sum = (double*) pCtx->pOutput; + double *sum = (double *)pCtx->pOutput; char *input = GET_INPUT_DATA_LIST(pCtx); for (int32_t i = 0; i < pCtx->size; ++i, input += pCtx->inputBytes) { @@ -1332,7 +1333,7 @@ static void avg_finalizer(SQLFunctionCtx *pCtx) { return; } - SET_DOUBLE_VAL((double *)pCtx->pOutput,(*(double *)pCtx->pOutput) / *(int64_t *)GET_ROWCELL_INTERBUF(pResInfo)); + SET_DOUBLE_VAL((double *)pCtx->pOutput, (*(double *)pCtx->pOutput) / *(int64_t *)GET_ROWCELL_INTERBUF(pResInfo)); } else { // this is the secondary merge, only in the secondary merge, the input type is TSDB_DATA_TYPE_BINARY assert(IS_NUMERIC_TYPE(pCtx->inputType)); SAvgInfo *pAvgInfo = (SAvgInfo *)GET_ROWCELL_INTERBUF(pResInfo); @@ -1362,7 +1363,7 @@ static void minMax_function(SQLFunctionCtx *pCtx, char *pOutput, int32_t isMin, return; } - void* tval = NULL; + void *tval = NULL; int16_t index = 0; if (isMin) { @@ -1381,9 +1382,9 @@ static void minMax_function(SQLFunctionCtx *pCtx, char *pOutput, int32_t isMin, * * The following codes of 3 lines will be removed later. */ -// if (index < 0 || index >= pCtx->size + pCtx->startOffset) { -// index = 0; -// } + // if (index < 0 || index >= pCtx->size + pCtx->startOffset) { + // index = 0; + // } // the index is the original position, not the relative position key = pCtx->ptsList[index]; @@ -1438,10 +1439,10 @@ static void minMax_function(SQLFunctionCtx *pCtx, char *pOutput, int32_t isMin, UPDATE_DATA(pCtx, *data, val, notNullElems, isMin, key); } } else if (pCtx->inputType == TSDB_DATA_TYPE_DOUBLE) { - double *data = (double *)pOutput; - double val = GET_DOUBLE_VAL(tval); + double *data = (double *)pOutput; + double val = GET_DOUBLE_VAL(tval); - UPDATE_DATA(pCtx, *data, val, notNullElems, isMin, key); + UPDATE_DATA(pCtx, *data, val, notNullElems, isMin, key); } else if (pCtx->inputType == TSDB_DATA_TYPE_FLOAT) { float *data = (float *)pOutput; double val = GET_DOUBLE_VAL(tval); @@ -1464,16 +1465,16 @@ static void minMax_function(SQLFunctionCtx *pCtx, char *pOutput, int32_t isMin, TYPED_LOOPCHECK_N(int16_t, pOutput, p, pCtx, pCtx->inputType, isMin, *notNullElems); } else if (pCtx->inputType == TSDB_DATA_TYPE_INT) { int32_t *pData = p; - int32_t *retVal = (int32_t*) pOutput; + int32_t *retVal = (int32_t *)pOutput; for (int32_t i = 0; i < pCtx->size; ++i) { - if (pCtx->hasNull && isNull((const char*)&pData[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&pData[i], pCtx->inputType)) { continue; } if ((*retVal < pData[i]) ^ isMin) { *retVal = pData[i]; - if(tsList) { + if (tsList) { TSKEY k = tsList[i]; DO_UPDATE_TAG_COLUMNS(pCtx, k); } @@ -1503,7 +1504,7 @@ static void minMax_function(SQLFunctionCtx *pCtx, char *pOutput, int32_t isMin, } } -static bool min_func_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResultInfo) { +static bool min_func_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResultInfo) { if (!function_setup(pCtx, pResultInfo)) { return false; // not initialized since it has been initialized } @@ -1515,7 +1516,7 @@ static bool min_func_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResultInfo *((int8_t *)pCtx->pOutput) = INT8_MAX; break; case TSDB_DATA_TYPE_UTINYINT: - *(uint8_t *) pCtx->pOutput = UINT8_MAX; + *(uint8_t *)pCtx->pOutput = UINT8_MAX; break; case TSDB_DATA_TYPE_SMALLINT: *((int16_t *)pCtx->pOutput) = INT16_MAX; @@ -1548,7 +1549,7 @@ static bool min_func_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResultInfo return true; } -static bool max_func_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResultInfo) { +static bool max_func_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResultInfo) { if (!function_setup(pCtx, pResultInfo)) { return false; // not initialized since it has been initialized } @@ -1748,13 +1749,13 @@ static void max_func_merge(SQLFunctionCtx *pCtx) { static void stddev_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SStddevInfo *pStd = GET_ROWCELL_INTERBUF(pResInfo); + SStddevInfo *pStd = GET_ROWCELL_INTERBUF(pResInfo); if (pCtx->currentStage == REPEAT_SCAN && pStd->stage == 0) { pStd->stage++; avg_finalizer(pCtx); - pResInfo->initialized = true; // set it initialized to avoid re-initialization + pResInfo->initialized = true; // set it initialized to avoid re-initialization // save average value into tmpBuf, for second stage scan SAvgInfo *pAvg = GET_ROWCELL_INTERBUF(pResInfo); @@ -1762,7 +1763,7 @@ static void stddev_function(SQLFunctionCtx *pCtx) { pStd->avg = GET_DOUBLE_VAL(pCtx->pOutput); assert((isnan(pAvg->sum) && pAvg->num == 0) || (pStd->num == pAvg->num && pStd->avg == pAvg->sum)); } - + if (pStd->stage == 0) { // the first stage is to calculate average value avg_function(pCtx); @@ -1771,14 +1772,14 @@ static void stddev_function(SQLFunctionCtx *pCtx) { // if pStd->num == 0, there are no numbers in the first round check. No need to do the second round double *retVal = &pStd->res; double avg = pStd->avg; - - void *pData = GET_INPUT_DATA_LIST(pCtx); + + void *pData = GET_INPUT_DATA_LIST(pCtx); int32_t num = 0; switch (pCtx->inputType) { case TSDB_DATA_TYPE_INT: { for (int32_t i = 0; i < pCtx->size; ++i) { - if (pCtx->hasNull && isNull((const char*) (&((int32_t *)pData)[i]), pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)(&((int32_t *)pData)[i]), pCtx->inputType)) { continue; } num += 1; @@ -1825,14 +1826,14 @@ static void stddev_function(SQLFunctionCtx *pCtx) { default: qError("stddev function not support data type:%d", pCtx->inputType); } - + SET_VAL(pCtx, 1, 1); } } static void stddev_finalizer(SQLFunctionCtx *pCtx) { SStddevInfo *pStd = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); - + if (pStd->num <= 0) { setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); } else { @@ -1840,19 +1841,19 @@ static void stddev_finalizer(SQLFunctionCtx *pCtx) { SET_DOUBLE_VAL(retValue, sqrt(pStd->res / pStd->num)); SET_VAL(pCtx, 1, 1); } - + doFinalizer(pCtx); } ////////////////////////////////////////////////////////////////////////////////////// -int32_t tsCompare(const void* p1, const void* p2) { - TSKEY k = *(TSKEY*)p1; - SResPair* pair = (SResPair*)p2; +int32_t tsCompare(const void *p1, const void *p2) { + TSKEY k = *(TSKEY *)p1; + SResPair *pair = (SResPair *)p2; if (k == pair->key) { return 0; } else { - return k < pair->key? -1:1; + return k < pair->key ? -1 : 1; } } @@ -1863,21 +1864,21 @@ static void stddev_dst_function(SQLFunctionCtx *pCtx) { double *retVal = &pStd->res; // all data are null, no need to proceed - SArray* resList = (SArray*) pCtx->param[0].pz; + SArray *resList = (SArray *)pCtx->param[0].pz; if (resList == NULL) { return; } // find the correct group average results according to the tag value - int32_t len = (int32_t) taosArrayGetSize(resList); + int32_t len = (int32_t)taosArrayGetSize(resList); assert(len > 0); double avg = 0; if (len == 1) { - SResPair* p = taosArrayGet(resList, 0); + SResPair *p = taosArrayGet(resList, 0); avg = p->avg; } else { // todo opt performance by using iterator since the timestamp lsit is matched with the output result - SResPair* p = bsearch(&pCtx->startTs, resList->pData, len, sizeof(SResPair), tsCompare); + SResPair *p = bsearch(&pCtx->startTs, resList->pData, len, sizeof(SResPair), tsCompare); if (p == NULL) { return; } @@ -1885,13 +1886,13 @@ static void stddev_dst_function(SQLFunctionCtx *pCtx) { avg = p->avg; } - void *pData = GET_INPUT_DATA_LIST(pCtx); + void *pData = GET_INPUT_DATA_LIST(pCtx); int32_t num = 0; switch (pCtx->inputType) { case TSDB_DATA_TYPE_INT: { for (int32_t i = 0; i < pCtx->size; ++i) { - if (pCtx->hasNull && isNull((const char*) (&((int32_t *)pData)[i]), pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)(&((int32_t *)pData)[i]), pCtx->inputType)) { continue; } num += 1; @@ -1948,9 +1949,9 @@ static void stddev_dst_function(SQLFunctionCtx *pCtx) { static void stddev_dst_merge(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SStddevdstInfo* pRes = GET_ROWCELL_INTERBUF(pResInfo); + SStddevdstInfo *pRes = GET_ROWCELL_INTERBUF(pResInfo); - char *input = GET_INPUT_DATA_LIST(pCtx); + char *input = GET_INPUT_DATA_LIST(pCtx); for (int32_t i = 0; i < pCtx->size; ++i, input += pCtx->inputBytes) { SStddevdstInfo *pInput = (SStddevdstInfo *)input; @@ -1978,44 +1979,44 @@ static void stddev_dst_finalizer(SQLFunctionCtx *pCtx) { } ////////////////////////////////////////////////////////////////////////////////////// -static bool first_last_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool first_last_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } - + // used to keep the timestamp for comparison pCtx->param[1].nType = 0; pCtx->param[1].i64 = 0; - + return true; } // todo opt for null block static void first_function(SQLFunctionCtx *pCtx) { - SResultRowCellInfo* pResInfo = GET_RES_INFO(pCtx); - int32_t notNullElems = 0; - int32_t step = 1; - int32_t i = 0; - bool inputAsc = true; + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + int32_t notNullElems = 0; + int32_t step = 1; + int32_t i = 0; + bool inputAsc = true; // input data come from sub query, input data order equal to sub query order - if(pCtx->numOfParams == 3) { - if(pCtx->param[2].nType == TSDB_DATA_TYPE_INT && pCtx->param[2].i64 == TSDB_ORDER_DESC) { + if (pCtx->numOfParams == 3) { + if (pCtx->param[2].nType == TSDB_DATA_TYPE_INT && pCtx->param[2].i64 == TSDB_ORDER_DESC) { step = -1; i = pCtx->size - 1; inputAsc = false; } } else if (pCtx->order == TSDB_ORDER_DESC) { - return ; + return; } - if(pCtx->order == TSDB_ORDER_ASC && inputAsc) { - for (int32_t m = 0; m < pCtx->size; ++m, i+=step) { + if (pCtx->order == TSDB_ORDER_ASC && inputAsc) { + for (int32_t m = 0; m < pCtx->size; ++m, i += step) { char *data = GET_INPUT_DATA(pCtx, i); if (pCtx->hasNull && isNull(data, pCtx->inputType)) { continue; } - + memcpy(pCtx->pOutput, data, pCtx->inputBytes); if (pCtx->ptsList != NULL) { TSKEY k = GET_TS_DATA(pCtx, i); @@ -2025,12 +2026,12 @@ static void first_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pInfo = GET_RES_INFO(pCtx); pInfo->hasResult = DATA_SET_FLAG; pInfo->complete = true; - + notNullElems++; break; } } else { // desc order - for (int32_t m = 0; m < pCtx->size; ++m, i+=step) { + for (int32_t m = 0; m < pCtx->size; ++m, i += step) { char *data = GET_INPUT_DATA(pCtx, i); if (pCtx->hasNull && isNull(data, pCtx->inputType) && (!pCtx->requireNull)) { continue; @@ -2038,12 +2039,12 @@ static void first_function(SQLFunctionCtx *pCtx) { TSKEY ts = pCtx->ptsList ? GET_TS_DATA(pCtx, i) : 0; - char* buf = GET_ROWCELL_INTERBUF(pResInfo); - if (pResInfo->hasResult != DATA_SET_FLAG || (*(TSKEY*)buf) > ts) { + char *buf = GET_ROWCELL_INTERBUF(pResInfo); + if (pResInfo->hasResult != DATA_SET_FLAG || (*(TSKEY *)buf) > ts) { pResInfo->hasResult = DATA_SET_FLAG; memcpy(pCtx->pOutput, data, pCtx->inputBytes); - *(TSKEY*)buf = ts; + *(TSKEY *)buf = ts; DO_UPDATE_TAG_COLUMNS(pCtx, ts); } @@ -2056,14 +2057,14 @@ static void first_function(SQLFunctionCtx *pCtx) { static void first_data_assign_impl(SQLFunctionCtx *pCtx, char *pData, int32_t index) { int64_t *timestamp = GET_TS_LIST(pCtx); - + SFirstLastInfo *pInfo = (SFirstLastInfo *)(pCtx->pOutput + pCtx->inputBytes); - + if (pInfo->hasResult != DATA_SET_FLAG || timestamp[index] < pInfo->ts) { memcpy(pCtx->pOutput, pData, pCtx->inputBytes); pInfo->hasResult = DATA_SET_FLAG; pInfo->ts = timestamp[index]; - + DO_UPDATE_TAG_COLUMNS(pCtx, pInfo->ts); } } @@ -2078,10 +2079,10 @@ static void first_dist_function(SQLFunctionCtx *pCtx) { * 1. data block that are not loaded * 2. scan data files in desc order */ - if (pCtx->order == TSDB_ORDER_DESC/* || pCtx->preAggVals.dataBlockLoaded == false*/) { + if (pCtx->order == TSDB_ORDER_DESC /* || pCtx->preAggVals.dataBlockLoaded == false*/) { return; } - + int32_t notNullElems = 0; // find the first not null value @@ -2090,37 +2091,37 @@ static void first_dist_function(SQLFunctionCtx *pCtx) { if (pCtx->hasNull && isNull(data, pCtx->inputType)) { continue; } - + first_data_assign_impl(pCtx, data, i); - + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); pResInfo->hasResult = DATA_SET_FLAG; - + notNullElems++; break; } - + SET_VAL(pCtx, notNullElems, 1); } static void first_dist_func_merge(SQLFunctionCtx *pCtx) { assert(pCtx->stableQuery); - char * pData = GET_INPUT_DATA_LIST(pCtx); - SFirstLastInfo *pInput = (SFirstLastInfo*) (pData + pCtx->outputBytes); + char *pData = GET_INPUT_DATA_LIST(pCtx); + SFirstLastInfo *pInput = (SFirstLastInfo *)(pData + pCtx->outputBytes); if (pInput->hasResult != DATA_SET_FLAG) { return; } - + // The param[1] is used to keep the initial value of max ts value if (pCtx->param[1].nType != pCtx->outputType || pCtx->param[1].i64 > pInput->ts) { memcpy(pCtx->pOutput, pData, pCtx->outputBytes); pCtx->param[1].i64 = pInput->ts; pCtx->param[1].nType = pCtx->outputType; - + DO_UPDATE_TAG_COLUMNS(pCtx, pInput->ts); } - + SET_VAL(pCtx, 1, 1); GET_RES_INFO(pCtx)->hasResult = DATA_SET_FLAG; } @@ -2134,14 +2135,14 @@ static void first_dist_func_merge(SQLFunctionCtx *pCtx) { * least one data in this block that is not null.(TODO opt for this case) */ static void last_function(SQLFunctionCtx *pCtx) { - SResultRowCellInfo* pResInfo = GET_RES_INFO(pCtx); - int32_t notNullElems = 0; - int32_t step = -1; - int32_t i = pCtx->size - 1; + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + int32_t notNullElems = 0; + int32_t step = -1; + int32_t i = pCtx->size - 1; // input data come from sub query, input data order equal to sub query order - if(pCtx->numOfParams == 3) { - if(pCtx->param[2].nType == TSDB_DATA_TYPE_INT && pCtx->param[2].i64 == TSDB_ORDER_DESC) { + if (pCtx->numOfParams == 3) { + if (pCtx->param[2].nType == TSDB_DATA_TYPE_INT && pCtx->param[2].i64 == TSDB_ORDER_DESC) { step = 1; i = 0; } @@ -2175,12 +2176,12 @@ static void last_function(SQLFunctionCtx *pCtx) { TSKEY ts = pCtx->ptsList ? GET_TS_DATA(pCtx, i) : 0; - char* buf = GET_ROWCELL_INTERBUF(pResInfo); - if (pResInfo->hasResult != DATA_SET_FLAG || (*(TSKEY*)buf) < ts) { + char *buf = GET_ROWCELL_INTERBUF(pResInfo); + if (pResInfo->hasResult != DATA_SET_FLAG || (*(TSKEY *)buf) < ts) { pResInfo->hasResult = DATA_SET_FLAG; memcpy(pCtx->pOutput, data, pCtx->inputBytes); - *(TSKEY*)buf = ts; + *(TSKEY *)buf = ts; DO_UPDATE_TAG_COLUMNS(pCtx, ts); } @@ -2194,18 +2195,18 @@ static void last_function(SQLFunctionCtx *pCtx) { static void last_data_assign_impl(SQLFunctionCtx *pCtx, char *pData, int32_t index) { int64_t *timestamp = GET_TS_LIST(pCtx); - + SFirstLastInfo *pInfo = (SFirstLastInfo *)(pCtx->pOutput + pCtx->inputBytes); - + if (pInfo->hasResult != DATA_SET_FLAG || pInfo->ts < timestamp[index]) { #if defined(_DEBUG_VIEW) qDebug("assign index:%d, ts:%" PRId64 ", val:%d, ", index, timestamp[index], *(int32_t *)pData); #endif - + memcpy(pCtx->pOutput, pData, pCtx->inputBytes); pInfo->hasResult = DATA_SET_FLAG; pInfo->ts = timestamp[index]; - + DO_UPDATE_TAG_COLUMNS(pCtx, pInfo->ts); } } @@ -2224,19 +2225,19 @@ static void last_dist_function(SQLFunctionCtx *pCtx) { char *data = GET_INPUT_DATA(pCtx, i); if (pCtx->hasNull && isNull(data, pCtx->inputType)) { if (!pCtx->requireNull) { - continue; + continue; } } - + last_data_assign_impl(pCtx, data, i); - + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); pResInfo->hasResult = DATA_SET_FLAG; - + notNullElems++; break; } - + SET_VAL(pCtx, notNullElems, 1); } @@ -2247,12 +2248,12 @@ static void last_dist_function(SQLFunctionCtx *pCtx) { */ static void last_dist_func_merge(SQLFunctionCtx *pCtx) { char *pData = GET_INPUT_DATA_LIST(pCtx); - - SFirstLastInfo *pInput = (SFirstLastInfo*) (pData + pCtx->outputBytes); + + SFirstLastInfo *pInput = (SFirstLastInfo *)(pData + pCtx->outputBytes); if (pInput->hasResult != DATA_SET_FLAG) { return; } - + /* * param[1] used to keep the corresponding timestamp to decide if current result is * the true last result @@ -2261,10 +2262,10 @@ static void last_dist_func_merge(SQLFunctionCtx *pCtx) { memcpy(pCtx->pOutput, pData, pCtx->outputBytes); pCtx->param[1].i64 = pInput->ts; pCtx->param[1].nType = pCtx->outputType; - + DO_UPDATE_TAG_COLUMNS(pCtx, pInput->ts); } - + SET_VAL(pCtx, 1, 1); GET_RES_INFO(pCtx)->hasResult = DATA_SET_FLAG; } @@ -2279,16 +2280,16 @@ static void last_row_function(SQLFunctionCtx *pCtx) { // assign the last element in current data block assignVal(pCtx->pOutput, pData + (pCtx->size - 1) * pCtx->inputBytes, pCtx->inputBytes, pCtx->inputType); - + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); pResInfo->hasResult = DATA_SET_FLAG; - + // set the result to final result buffer in case of super table query if (pCtx->stableQuery) { SLastrowInfo *pInfo1 = (SLastrowInfo *)(pCtx->pOutput + pCtx->inputBytes); pInfo1->ts = GET_TS_DATA(pCtx, pCtx->size - 1); pInfo1->hasResult = DATA_SET_FLAG; - + DO_UPDATE_TAG_COLUMNS(pCtx, pInfo1->ts); } else { TSKEY ts = GET_TS_DATA(pCtx, pCtx->size - 1); @@ -2305,7 +2306,7 @@ static void last_row_finalizer(SQLFunctionCtx *pCtx) { setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); return; } - + GET_RES_INFO(pCtx)->numOfRes = 1; doFinalizer(pCtx); } @@ -2316,18 +2317,18 @@ static void valuePairAssign(tValuePair *dst, int16_t type, const char *val, int6 dst->v.nType = type; dst->v.i64 = *(int64_t *)val; dst->timestamp = tsKey; - + int32_t size = 0; if (stage == MERGE_STAGE) { memcpy(dst->pTags, pTags, (size_t)pTagInfo->tagsLen); } else { // the tags are dumped from the ctx tag fields for (int32_t i = 0; i < pTagInfo->numOfTagCols; ++i) { - SQLFunctionCtx* ctx = pTagInfo->pTagCtxList[i]; + SQLFunctionCtx *ctx = pTagInfo->pTagCtxList[i]; if (ctx->functionId == TSDB_FUNC_TS_DUMMY) { ctx->tag.nType = TSDB_DATA_TYPE_BIGINT; ctx->tag.i64 = tsKey; } - + tVariantDump(&ctx->tag, dst->pTags + size, ctx->tag.nType, true); size += pTagInfo->pTagCtxList[i]->outputBytes; } @@ -2341,11 +2342,10 @@ static void valuePairAssign(tValuePair *dst, int16_t type, const char *val, int6 memcpy((dst)->pTags, (src)->pTags, (size_t)(__l)); \ } while (0) -static int32_t topBotComparFn(const void *p1, const void *p2, const void *param) -{ - uint16_t type = *(uint16_t *) param; - tValuePair *val1 = *(tValuePair **) p1; - tValuePair *val2 = *(tValuePair **) p2; +static int32_t topBotComparFn(const void *p1, const void *p2, const void *param) { + uint16_t type = *(uint16_t *)param; + tValuePair *val1 = *(tValuePair **)p1; + tValuePair *val2 = *(tValuePair **)p2; if (IS_SIGNED_NUMERIC_TYPE(type)) { if (val1->v.i64 == val2->v.i64) { @@ -2353,12 +2353,12 @@ static int32_t topBotComparFn(const void *p1, const void *p2, const void *param) } return (val1->v.i64 > val2->v.i64) ? 1 : -1; - } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) { - if (val1->v.u64 == val2->v.u64) { - return 0; - } + } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) { + if (val1->v.u64 == val2->v.u64) { + return 0; + } - return (val1->v.u64 > val2->v.u64) ? 1 : -1; + return (val1->v.u64 > val2->v.u64) ? 1 : -1; } if (val1->v.dKey == val2->v.dKey) { @@ -2368,13 +2368,12 @@ static int32_t topBotComparFn(const void *p1, const void *p2, const void *param) return (val1->v.dKey > val2->v.dKey) ? 1 : -1; } -static void topBotSwapFn(void *dst, void *src, const void *param) -{ - char tag[32768]; - tValuePair temp; - uint16_t tagLen = *(uint16_t *) param; - tValuePair *vdst = *(tValuePair **) dst; - tValuePair *vsrc = *(tValuePair **) src; +static void topBotSwapFn(void *dst, void *src, const void *param) { + char tag[32768]; + tValuePair temp; + uint16_t tagLen = *(uint16_t *)param; + tValuePair *vdst = *(tValuePair **)dst; + tValuePair *vsrc = *(tValuePair **)src; memset(tag, 0, sizeof(tag)); temp.pTags = tag; @@ -2388,22 +2387,24 @@ static void do_top_function_add(STopBotInfo *pInfo, int32_t maxLen, void *pData, SExtTagsInfo *pTagInfo, char *pTags, int16_t stage) { tVariant val = {0}; tVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type); - + tValuePair **pList = pInfo->res; assert(pList != NULL); if (pInfo->num < maxLen) { valuePairAssign(pList[pInfo->num], type, (const char *)&val.i64, ts, pTags, pTagInfo, stage); - taosheapsort((void *) pList, sizeof(tValuePair **), pInfo->num + 1, (const void *) &type, topBotComparFn, (const void *) &pTagInfo->tagsLen, topBotSwapFn, 0); - + taosheapsort((void *)pList, sizeof(tValuePair **), pInfo->num + 1, (const void *)&type, topBotComparFn, + (const void *)&pTagInfo->tagsLen, topBotSwapFn, 0); + pInfo->num++; } else { if ((IS_SIGNED_NUMERIC_TYPE(type) && val.i64 > pList[0]->v.i64) || (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u64 > pList[0]->v.u64) || (IS_FLOAT_TYPE(type) && val.dKey > pList[0]->v.dKey)) { valuePairAssign(pList[0], type, (const char *)&val.i64, ts, pTags, pTagInfo, stage); - taosheapadjust((void *) pList, sizeof(tValuePair **), 0, maxLen - 1, (const void *) &type, topBotComparFn, (const void *) &pTagInfo->tagsLen, topBotSwapFn, 0); + taosheapadjust((void *)pList, sizeof(tValuePair **), 0, maxLen - 1, (const void *)&type, topBotComparFn, + (const void *)&pTagInfo->tagsLen, topBotSwapFn, 0); } } } @@ -2419,7 +2420,8 @@ static void do_bottom_function_add(STopBotInfo *pInfo, int32_t maxLen, void *pDa if (pInfo->num < maxLen) { valuePairAssign(pList[pInfo->num], type, (const char *)&val.i64, ts, pTags, pTagInfo, stage); - taosheapsort((void *) pList, sizeof(tValuePair **), pInfo->num + 1, (const void *) &type, topBotComparFn, (const void *) &pTagInfo->tagsLen, topBotSwapFn, 1); + taosheapsort((void *)pList, sizeof(tValuePair **), pInfo->num + 1, (const void *)&type, topBotComparFn, + (const void *)&pTagInfo->tagsLen, topBotSwapFn, 1); pInfo->num++; } else { @@ -2427,7 +2429,8 @@ static void do_bottom_function_add(STopBotInfo *pInfo, int32_t maxLen, void *pDa (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u64 < pList[0]->v.u64) || (IS_FLOAT_TYPE(type) && val.dKey < pList[0]->v.dKey)) { valuePairAssign(pList[0], type, (const char *)&val.i64, ts, pTags, pTagInfo, stage); - taosheapadjust((void *) pList, sizeof(tValuePair **), 0, maxLen - 1, (const void *) &type, topBotComparFn, (const void *) &pTagInfo->tagsLen, topBotSwapFn, 1); + taosheapadjust((void *)pList, sizeof(tValuePair **), 0, maxLen - 1, (const void *)&type, topBotComparFn, + (const void *)&pTagInfo->tagsLen, topBotSwapFn, 1); } } } @@ -2435,7 +2438,7 @@ static void do_bottom_function_add(STopBotInfo *pInfo, int32_t maxLen, void *pDa static int32_t resAscComparFn(const void *pLeft, const void *pRight) { tValuePair *pLeftElem = *(tValuePair **)pLeft; tValuePair *pRightElem = *(tValuePair **)pRight; - + if (pLeftElem->timestamp == pRightElem->timestamp) { return 0; } else { @@ -2448,14 +2451,14 @@ static int32_t resDescComparFn(const void *pLeft, const void *pRight) { return - static int32_t resDataAscComparFn(const void *pLeft, const void *pRight) { tValuePair *pLeftElem = *(tValuePair **)pLeft; tValuePair *pRightElem = *(tValuePair **)pRight; - + if (IS_FLOAT_TYPE(pLeftElem->v.nType)) { if (pLeftElem->v.dKey == pRightElem->v.dKey) { return 0; } else { return pLeftElem->v.dKey > pRightElem->v.dKey ? 1 : -1; } - } else if (IS_SIGNED_NUMERIC_TYPE(pLeftElem->v.nType)){ + } else if (IS_SIGNED_NUMERIC_TYPE(pLeftElem->v.nType)) { if (pLeftElem->v.i64 == pRightElem->v.i64) { return 0; } else { @@ -2474,10 +2477,10 @@ static int32_t resDataDescComparFn(const void *pLeft, const void *pRight) { retu static void copyTopBotRes(SQLFunctionCtx *pCtx, int32_t type) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - STopBotInfo *pRes = GET_ROWCELL_INTERBUF(pResInfo); - + STopBotInfo *pRes = GET_ROWCELL_INTERBUF(pResInfo); + tValuePair **tvp = pRes->res; - + int32_t step = QUERY_ASC_FORWARD_STEP; int32_t len = (int32_t)(GET_RES_INFO(pCtx)->numOfRes); @@ -2533,24 +2536,24 @@ static void copyTopBotRes(SQLFunctionCtx *pCtx, int32_t type) { return; } } - + // set the output timestamp of each record. TSKEY *output = pCtx->ptsOutputBuf; for (int32_t i = 0; i < len; ++i, output += step) { *output = tvp[i]->timestamp; } - + // set the corresponding tag data for each record // todo check malloc failure if (pCtx->tagInfo.numOfTagCols == 0) { - return ; + return; } char **pData = calloc(pCtx->tagInfo.numOfTagCols, POINTER_BYTES); for (int32_t i = 0; i < pCtx->tagInfo.numOfTagCols; ++i) { pData[i] = pCtx->tagInfo.pTagCtxList[i]->pOutput; } - + for (int32_t i = 0; i < len; ++i, output += step) { int16_t offset = 0; for (int32_t j = 0; j < pCtx->tagInfo.numOfTagCols; ++j) { @@ -2559,7 +2562,7 @@ static void copyTopBotRes(SQLFunctionCtx *pCtx, int32_t type) { pData[j] += pCtx->tagInfo.pTagCtxList[j]->outputBytes; } } - + tfree(pData); } @@ -2571,13 +2574,13 @@ static void copyTopBotRes(SQLFunctionCtx *pCtx, int32_t type) { */ static void buildTopBotStruct(STopBotInfo *pTopBotInfo, SQLFunctionCtx *pCtx) { char *tmp = (char *)pTopBotInfo + sizeof(STopBotInfo); - pTopBotInfo->res = (tValuePair**) tmp; + pTopBotInfo->res = (tValuePair **)tmp; tmp += POINTER_BYTES * pCtx->param[0].i64; size_t size = sizeof(tValuePair) + pCtx->tagInfo.tagsLen; for (int32_t i = 0; i < pCtx->param[0].i64; ++i) { - pTopBotInfo->res[i] = (tValuePair*) tmp; + pTopBotInfo->res[i] = (tValuePair *)tmp; pTopBotInfo->res[i]->pTags = tmp + sizeof(tValuePair); tmp += size; } @@ -2590,18 +2593,19 @@ bool topbot_datablock_filter(SQLFunctionCtx *pCtx, const char *minval, const cha } STopBotInfo *pTopBotInfo = getOutputInfo(pCtx); - + // required number of results are not reached, continue load data block if (pTopBotInfo->num < pCtx->param[0].i64) { return true; } - - if ((void *)pTopBotInfo->res[0] != (void *)((char *)pTopBotInfo + sizeof(STopBotInfo) + POINTER_BYTES * pCtx->param[0].i64)) { + + if ((void *)pTopBotInfo->res[0] != + (void *)((char *)pTopBotInfo + sizeof(STopBotInfo) + POINTER_BYTES * pCtx->param[0].i64)) { buildTopBotStruct(pTopBotInfo, pCtx); } - tValuePair **pRes = (tValuePair**) pTopBotInfo->res; - + tValuePair **pRes = (tValuePair **)pTopBotInfo->res; + if (pCtx->functionId == TSDB_FUNC_TOP) { switch (pCtx->inputType) { case TSDB_DATA_TYPE_TINYINT: @@ -2639,11 +2643,11 @@ bool topbot_datablock_filter(SQLFunctionCtx *pCtx, const char *minval, const cha } } -static bool top_bottom_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool top_bottom_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } - + STopBotInfo *pInfo = getOutputInfo(pCtx); buildTopBotStruct(pInfo, pCtx); return true; @@ -2658,27 +2662,27 @@ static void top_function(SQLFunctionCtx *pCtx) { if ((void *)pRes->res[0] != (void *)((char *)pRes + sizeof(STopBotInfo) + POINTER_BYTES * pCtx->param[0].i64)) { buildTopBotStruct(pRes, pCtx); } - + for (int32_t i = 0; i < pCtx->size; ++i) { char *data = GET_INPUT_DATA(pCtx, i); if (pCtx->hasNull && isNull(data, pCtx->inputType)) { continue; } - + notNullElems++; // NOTE: Set the default timestamp if it is missing [todo refactor] - TSKEY ts = (pCtx->ptsList != NULL)? GET_TS_DATA(pCtx, i):0; + TSKEY ts = (pCtx->ptsList != NULL) ? GET_TS_DATA(pCtx, i) : 0; do_top_function_add(pRes, (int32_t)pCtx->param[0].i64, data, ts, pCtx->inputType, &pCtx->tagInfo, NULL, 0); } - + if (!pCtx->hasNull) { assert(pCtx->size == notNullElems); } - + // treat the result as only one result SET_VAL(pCtx, notNullElems, 1); - + if (notNullElems > 0) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); pResInfo->hasResult = DATA_SET_FLAG; @@ -2687,21 +2691,21 @@ static void top_function(SQLFunctionCtx *pCtx) { static void top_func_merge(SQLFunctionCtx *pCtx) { STopBotInfo *pInput = (STopBotInfo *)GET_INPUT_DATA_LIST(pCtx); - + // construct the input data struct from binary data buildTopBotStruct(pInput, pCtx); - + STopBotInfo *pOutput = getOutputInfo(pCtx); - + // the intermediate result is binary, we only use the output data type for (int32_t i = 0; i < pInput->num; ++i) { - int16_t type = (pCtx->outputType == TSDB_DATA_TYPE_FLOAT)? TSDB_DATA_TYPE_DOUBLE:pCtx->outputType; - do_top_function_add(pOutput, (int32_t)pCtx->param[0].i64, &pInput->res[i]->v.i64, pInput->res[i]->timestamp, - type, &pCtx->tagInfo, pInput->res[i]->pTags, pCtx->currentStage); + int16_t type = (pCtx->outputType == TSDB_DATA_TYPE_FLOAT) ? TSDB_DATA_TYPE_DOUBLE : pCtx->outputType; + do_top_function_add(pOutput, (int32_t)pCtx->param[0].i64, &pInput->res[i]->v.i64, pInput->res[i]->timestamp, type, + &pCtx->tagInfo, pInput->res[i]->pTags, pCtx->currentStage); } - + SET_VAL(pCtx, pInput->num, pOutput->num); - + if (pOutput->num > 0) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); pResInfo->hasResult = DATA_SET_FLAG; @@ -2710,9 +2714,9 @@ static void top_func_merge(SQLFunctionCtx *pCtx) { static void bottom_function(SQLFunctionCtx *pCtx) { int32_t notNullElems = 0; - + STopBotInfo *pRes = getOutputInfo(pCtx); - + if ((void *)pRes->res[0] != (void *)((char *)pRes + sizeof(STopBotInfo) + POINTER_BYTES * pCtx->param[0].i64)) { buildTopBotStruct(pRes, pCtx); } @@ -2725,17 +2729,17 @@ static void bottom_function(SQLFunctionCtx *pCtx) { notNullElems++; // NOTE: Set the default timestamp if it is missing [todo refactor] - TSKEY ts = (pCtx->ptsList != NULL)? GET_TS_DATA(pCtx, i):0; + TSKEY ts = (pCtx->ptsList != NULL) ? GET_TS_DATA(pCtx, i) : 0; do_bottom_function_add(pRes, (int32_t)pCtx->param[0].i64, data, ts, pCtx->inputType, &pCtx->tagInfo, NULL, 0); } - + if (!pCtx->hasNull) { assert(pCtx->size == notNullElems); } - + // treat the result as only one result SET_VAL(pCtx, notNullElems, 1); - + if (notNullElems > 0) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); pResInfo->hasResult = DATA_SET_FLAG; @@ -2744,21 +2748,21 @@ static void bottom_function(SQLFunctionCtx *pCtx) { static void bottom_func_merge(SQLFunctionCtx *pCtx) { STopBotInfo *pInput = (STopBotInfo *)GET_INPUT_DATA_LIST(pCtx); - + // construct the input data struct from binary data buildTopBotStruct(pInput, pCtx); - + STopBotInfo *pOutput = getOutputInfo(pCtx); - + // the intermediate result is binary, we only use the output data type for (int32_t i = 0; i < pInput->num; ++i) { int16_t type = (pCtx->outputType == TSDB_DATA_TYPE_FLOAT) ? TSDB_DATA_TYPE_DOUBLE : pCtx->outputType; - do_bottom_function_add(pOutput, (int32_t)pCtx->param[0].i64, &pInput->res[i]->v.i64, pInput->res[i]->timestamp, type, - &pCtx->tagInfo, pInput->res[i]->pTags, pCtx->currentStage); + do_bottom_function_add(pOutput, (int32_t)pCtx->param[0].i64, &pInput->res[i]->v.i64, pInput->res[i]->timestamp, + type, &pCtx->tagInfo, pInput->res[i]->pTags, pCtx->currentStage); } SET_VAL(pCtx, pInput->num, pOutput->num); - + if (pOutput->num > 0) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); pResInfo->hasResult = DATA_SET_FLAG; @@ -2767,17 +2771,17 @@ static void bottom_func_merge(SQLFunctionCtx *pCtx) { static void top_bottom_func_finalizer(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - + // data in temporary list is less than the required number of results, not enough qualified number of results STopBotInfo *pRes = GET_ROWCELL_INTERBUF(pResInfo); if (pRes->num == 0) { // no result assert(pResInfo->hasResult != DATA_SET_FLAG); // TODO: } - + GET_RES_INFO(pCtx)->numOfRes = pRes->num; tValuePair **tvp = pRes->res; - + // user specify the order of output by sort the result according to timestamp if (pCtx->param[2].i64 == PRIMARYKEY_TIMESTAMP_COL_INDEX) { __compar_fn_t comparator = (pCtx->param[3].i64 == TSDB_ORDER_ASC) ? resAscComparFn : resDescComparFn; @@ -2786,15 +2790,15 @@ static void top_bottom_func_finalizer(SQLFunctionCtx *pCtx) { __compar_fn_t comparator = (pCtx->param[3].i64 == TSDB_ORDER_ASC) ? resDataAscComparFn : resDataDescComparFn; qsort(tvp, (size_t)pResInfo->numOfRes, POINTER_BYTES, comparator); } - + GET_TRUE_DATA_TYPE(); copyTopBotRes(pCtx, type); - + doFinalizer(pCtx); } /////////////////////////////////////////////////////////////////////////////////////////////// -static bool percentile_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResultInfo) { +static bool percentile_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResultInfo) { if (!function_setup(pCtx, pResultInfo)) { return false; } @@ -2810,9 +2814,9 @@ static bool percentile_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* static void percentile_function(SQLFunctionCtx *pCtx) { int32_t notNullElems = 0; - + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SPercentileInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); + SPercentileInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); if (pCtx->currentStage == REPEAT_SCAN && pInfo->stage == 0) { pInfo->stage += 1; @@ -2820,7 +2824,7 @@ static void percentile_function(SQLFunctionCtx *pCtx) { // all data are null, set it completed if (pInfo->numOfElems == 0) { pResInfo->complete = true; - + return; } else { pInfo->pMemBucket = tMemBucketCreate(pCtx->inputBytes, pCtx->inputType, pInfo->minval, pInfo->maxval); @@ -2884,37 +2888,37 @@ static void percentile_function(SQLFunctionCtx *pCtx) { if (pCtx->hasNull && isNull(data, pCtx->inputType)) { continue; } - + notNullElems += 1; tMemBucketPut(pInfo->pMemBucket, data, 1); } - + SET_VAL(pCtx, notNullElems, 1); pResInfo->hasResult = DATA_SET_FLAG; } static void percentile_finalizer(SQLFunctionCtx *pCtx) { double v = pCtx->param[0].nType == TSDB_DATA_TYPE_INT ? pCtx->param[0].i64 : pCtx->param[0].dKey; - + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SPercentileInfo* ppInfo = (SPercentileInfo *) GET_ROWCELL_INTERBUF(pResInfo); + SPercentileInfo *ppInfo = (SPercentileInfo *)GET_ROWCELL_INTERBUF(pResInfo); - tMemBucket * pMemBucket = ppInfo->pMemBucket; + tMemBucket *pMemBucket = ppInfo->pMemBucket; if (pMemBucket == NULL || pMemBucket->total == 0) { // check for null assert(ppInfo->numOfElems == 0); setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); } else { SET_DOUBLE_VAL((double *)pCtx->pOutput, getPercentile(pMemBucket, v)); } - + tMemBucketDestroy(pMemBucket); doFinalizer(pCtx); } ////////////////////////////////////////////////////////////////////////////////// -static void buildHistogramInfo(SAPercentileInfo* pInfo) { - pInfo->pHisto = (SHistogramInfo*) ((char*) pInfo + sizeof(SAPercentileInfo)); - pInfo->pHisto->elems = (SHistBin*) ((char*)pInfo->pHisto + sizeof(SHistogramInfo)); +static void buildHistogramInfo(SAPercentileInfo *pInfo) { + pInfo->pHisto = (SHistogramInfo *)((char *)pInfo + sizeof(SAPercentileInfo)); + pInfo->pHisto->elems = (SHistBin *)((char *)pInfo->pHisto + sizeof(SHistogramInfo)); } // @@ -2926,10 +2930,10 @@ static bool tdigest_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResultInfo) if (!function_setup(pCtx, pResultInfo)) { return false; } - + // new TDigest SAPercentileInfo *pInfo = getOutputInfo(pCtx); - char *tmp = (char *)pInfo + sizeof(SAPercentileInfo); + char *tmp = (char *)pInfo + sizeof(SAPercentileInfo); pInfo->pTDigest = tdigestNewFrom(tmp, COMPRESSION); return true; } @@ -2938,12 +2942,12 @@ static void tdigest_do(SQLFunctionCtx *pCtx) { int32_t notNullElems = 0; SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SAPercentileInfo * pAPerc = getOutputInfo(pCtx); + SAPercentileInfo *pAPerc = getOutputInfo(pCtx); assert(pAPerc->pTDigest != NULL); - if(pAPerc->pTDigest == NULL) { + if (pAPerc->pTDigest == NULL) { qError("tdigest_do tdigest is null."); - return ; + return; } for (int32_t i = 0; i < pCtx->size; ++i) { @@ -2953,8 +2957,8 @@ static void tdigest_do(SQLFunctionCtx *pCtx) { } notNullElems += 1; - double v = 0; // value - long long w = 1; // weigth + double v = 0; // value + long long w = 1; // weigth GET_TYPED_DATA(v, double, pCtx->inputType, data); tdigestAdd(pAPerc->pTDigest, v, w); } @@ -2972,22 +2976,22 @@ static void tdigest_do(SQLFunctionCtx *pCtx) { static void tdigest_merge(SQLFunctionCtx *pCtx) { SAPercentileInfo *pInput = (SAPercentileInfo *)GET_INPUT_DATA_LIST(pCtx); assert(pInput->pTDigest); - pInput->pTDigest = (TDigest*)((char*)pInput + sizeof(SAPercentileInfo)); + pInput->pTDigest = (TDigest *)((char *)pInput + sizeof(SAPercentileInfo)); tdigestAutoFill(pInput->pTDigest, COMPRESSION); // input merge no elements , no need merge - if(pInput->pTDigest->num_centroids == 0 && pInput->pTDigest->num_buffered_pts == 0) { - return ; + if (pInput->pTDigest->num_centroids == 0 && pInput->pTDigest->num_buffered_pts == 0) { + return; } SAPercentileInfo *pOutput = getOutputInfo(pCtx); - if(pOutput->pTDigest->num_centroids == 0) { + if (pOutput->pTDigest->num_centroids == 0) { memcpy(pOutput->pTDigest, pInput->pTDigest, (size_t)TDIGEST_SIZE(COMPRESSION)); tdigestAutoFill(pOutput->pTDigest, COMPRESSION); } else { tdigestMerge(pOutput->pTDigest, pInput->pTDigest); } - + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); pResInfo->hasResult = DATA_SET_FLAG; SET_VAL(pCtx, 1, 1); @@ -2997,11 +3001,11 @@ static void tdigest_finalizer(SQLFunctionCtx *pCtx) { double q = (pCtx->param[0].nType == TSDB_DATA_TYPE_INT) ? pCtx->param[0].i64 : pCtx->param[0].dKey; SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SAPercentileInfo * pAPerc = getOutputInfo(pCtx); + SAPercentileInfo *pAPerc = getOutputInfo(pCtx); if (pCtx->currentStage == MERGE_STAGE) { if (pResInfo->hasResult == DATA_SET_FLAG) { // check for null - double res = tdigestQuantile(pAPerc->pTDigest, q/100); + double res = tdigestQuantile(pAPerc->pTDigest, q / 100); memcpy(pCtx->pOutput, &res, sizeof(double)); } else { setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); @@ -3009,7 +3013,7 @@ static void tdigest_finalizer(SQLFunctionCtx *pCtx) { } } else { if (pAPerc->pTDigest->size > 0) { - double res = tdigestQuantile(pAPerc->pTDigest, q/100); + double res = tdigestQuantile(pAPerc->pTDigest, q / 100); memcpy(pCtx->pOutput, &res, sizeof(double)); } else { // no need to free setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); @@ -3022,17 +3026,17 @@ static void tdigest_finalizer(SQLFunctionCtx *pCtx) { } ////////////////////////////////////////////////////////////////////////////////// -int32_t getAlgo(SQLFunctionCtx * pCtx) { - if(pCtx->numOfParams != 2){ +int32_t getAlgo(SQLFunctionCtx *pCtx) { + if (pCtx->numOfParams != 2) { return ALGO_DEFAULT; } - if(pCtx->param[1].nType != TSDB_DATA_TYPE_INT) { + if (pCtx->param[1].nType != TSDB_DATA_TYPE_INT) { return ALGO_DEFAULT; } return (int32_t)pCtx->param[1].i64; } -static bool apercentile_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResultInfo) { +static bool apercentile_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResultInfo) { if (getAlgo(pCtx) == ALGO_TDIGEST) { return tdigest_setup(pCtx, pResultInfo); } @@ -3040,10 +3044,10 @@ static bool apercentile_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* if (!function_setup(pCtx, pResultInfo)) { return false; } - + SAPercentileInfo *pInfo = getOutputInfo(pCtx); buildHistogramInfo(pInfo); - + char *tmp = (char *)pInfo + sizeof(SAPercentileInfo); pInfo->pHisto = tHistogramCreateFrom(tmp, MAX_HISTOGRAM_BIN); return true; @@ -3056,32 +3060,32 @@ static void apercentile_function(SQLFunctionCtx *pCtx) { } int32_t notNullElems = 0; - - SResultRowCellInfo * pResInfo = GET_RES_INFO(pCtx); - SAPercentileInfo *pInfo = getOutputInfo(pCtx); + + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + SAPercentileInfo *pInfo = getOutputInfo(pCtx); buildHistogramInfo(pInfo); assert(pInfo->pHisto->elems != NULL); - + for (int32_t i = 0; i < pCtx->size; ++i) { char *data = GET_INPUT_DATA(pCtx, i); if (pCtx->hasNull && isNull(data, pCtx->inputType)) { continue; } - + notNullElems += 1; double v = 0; GET_TYPED_DATA(v, double, pCtx->inputType, data); tHistogramAdd(&pInfo->pHisto, v); } - + if (!pCtx->hasNull) { assert(pCtx->size == notNullElems); } - + SET_VAL(pCtx, notNullElems, 1); - + if (notNullElems > 0) { pResInfo->hasResult = DATA_SET_FLAG; } @@ -3094,27 +3098,27 @@ static void apercentile_func_merge(SQLFunctionCtx *pCtx) { } SAPercentileInfo *pInput = (SAPercentileInfo *)GET_INPUT_DATA_LIST(pCtx); - - pInput->pHisto = (SHistogramInfo*) ((char *)pInput + sizeof(SAPercentileInfo)); - pInput->pHisto->elems = (SHistBin*) ((char *)pInput->pHisto + sizeof(SHistogramInfo)); + + pInput->pHisto = (SHistogramInfo *)((char *)pInput + sizeof(SAPercentileInfo)); + pInput->pHisto->elems = (SHistBin *)((char *)pInput->pHisto + sizeof(SHistogramInfo)); if (pInput->pHisto->numOfElems <= 0) { return; } - + SAPercentileInfo *pOutput = getOutputInfo(pCtx); buildHistogramInfo(pOutput); - SHistogramInfo *pHisto = pOutput->pHisto; - + SHistogramInfo *pHisto = pOutput->pHisto; + if (pHisto->numOfElems <= 0) { memcpy(pHisto, pInput->pHisto, sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1)); - pHisto->elems = (SHistBin*) ((char *)pHisto + sizeof(SHistogramInfo)); + pHisto->elems = (SHistBin *)((char *)pHisto + sizeof(SHistogramInfo)); } else { - //TODO(dengyihao): avoid memcpy - pHisto->elems = (SHistBin*) ((char *)pHisto + sizeof(SHistogramInfo)); + // TODO(dengyihao): avoid memcpy + pHisto->elems = (SHistBin *)((char *)pHisto + sizeof(SHistogramInfo)); SHistogramInfo *pRes = tHistogramMerge(pHisto, pInput->pHisto, MAX_HISTOGRAM_BIN); memcpy(pHisto, pRes, sizeof(SHistogramInfo) + sizeof(SHistBin) * MAX_HISTOGRAM_BIN); - pHisto->elems = (SHistBin*) ((char *)pHisto + sizeof(SHistogramInfo)); + pHisto->elems = (SHistBin *)((char *)pHisto + sizeof(SHistogramInfo)); tHistogramDestroy(&pRes); } @@ -3130,17 +3134,17 @@ static void apercentile_finalizer(SQLFunctionCtx *pCtx) { } double v = (pCtx->param[0].nType == TSDB_DATA_TYPE_INT) ? pCtx->param[0].i64 : pCtx->param[0].dKey; - - SResultRowCellInfo * pResInfo = GET_RES_INFO(pCtx); - SAPercentileInfo *pOutput = GET_ROWCELL_INTERBUF(pResInfo); + + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + SAPercentileInfo *pOutput = GET_ROWCELL_INTERBUF(pResInfo); if (pCtx->currentStage == MERGE_STAGE) { if (pResInfo->hasResult == DATA_SET_FLAG) { // check for null assert(pOutput->pHisto->numOfElems > 0); - + double ratio[] = {v}; double *res = tHistogramUniform(pOutput->pHisto, ratio, 1); - + memcpy(pCtx->pOutput, res, sizeof(double)); free(res); } else { @@ -3150,7 +3154,7 @@ static void apercentile_finalizer(SQLFunctionCtx *pCtx) { } else { if (pOutput->pHisto->numOfElems > 0) { double ratio[] = {v}; - + double *res = tHistogramUniform(pOutput->pHisto, ratio, 1); memcpy(pCtx->pOutput, res, sizeof(double)); free(res); @@ -3159,18 +3163,18 @@ static void apercentile_finalizer(SQLFunctionCtx *pCtx) { return; } } - + doFinalizer(pCtx); } ///////////////////////////////////////////////////////////////////////////////// -static bool leastsquares_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool leastsquares_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } SLeastsquaresInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); - + // 2*3 matrix pInfo->startVal = pCtx->param[0].dKey; return true; @@ -3195,29 +3199,29 @@ static bool leastsquares_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo } static void leastsquares_function(SQLFunctionCtx *pCtx) { - SResultRowCellInfo * pResInfo = GET_RES_INFO(pCtx); - SLeastsquaresInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); - + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + SLeastsquaresInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); + double(*param)[3] = pInfo->mat; double x = pInfo->startVal; - + void *pData = GET_INPUT_DATA_LIST(pCtx); - + int32_t numOfElem = 0; switch (pCtx->inputType) { case TSDB_DATA_TYPE_INT: { int32_t *p = pData; // LEASTSQR_CAL_LOOP(pCtx, param, pParamData, p); for (int32_t i = 0; i < pCtx->size; ++i) { - if (pCtx->hasNull && isNull((const char*) p, pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)p, pCtx->inputType)) { continue; } - + param[0][0] += x * x; param[0][1] += x; param[0][2] += x * p[i]; param[1][2] += p[i]; - + x += pCtx->param[1].dKey; numOfElem++; } @@ -3269,45 +3273,45 @@ static void leastsquares_function(SQLFunctionCtx *pCtx) { break; } } - + pInfo->startVal = x; pInfo->num += numOfElem; - + if (pInfo->num > 0) { pResInfo->hasResult = DATA_SET_FLAG; } - + SET_VAL(pCtx, numOfElem, 1); } static void leastsquares_finalizer(SQLFunctionCtx *pCtx) { // no data in query - SResultRowCellInfo * pResInfo = GET_RES_INFO(pCtx); - SLeastsquaresInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); - + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + SLeastsquaresInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); + if (pInfo->num == 0) { setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); return; } - + double(*param)[3] = pInfo->mat; - + param[1][1] = (double)pInfo->num; param[1][0] = param[0][1]; - + param[0][0] -= param[1][0] * (param[0][1] / param[1][1]); param[0][2] -= param[1][2] * (param[0][1] / param[1][1]); param[0][1] = 0; param[1][2] -= param[0][2] * (param[1][0] / param[0][0]); param[1][0] = 0; param[0][2] /= param[0][0]; - + param[1][2] /= param[1][1]; - + int32_t maxOutputSize = TSDB_AVG_FUNCTION_INTER_BUFFER_SIZE - VARSTR_HEADER_SIZE; - size_t n = snprintf(varDataVal(pCtx->pOutput), maxOutputSize, "{slop:%.6lf, intercept:%.6lf}", - param[0][2], param[1][2]); - + size_t n = + snprintf(varDataVal(pCtx->pOutput), maxOutputSize, "{slop:%.6lf, intercept:%.6lf}", param[0][2], param[1][2]); + varDataSetLen(pCtx->pOutput, n); doFinalizer(pCtx); } @@ -3318,7 +3322,7 @@ static void date_col_output_function(SQLFunctionCtx *pCtx) { } static void col_project_function(SQLFunctionCtx *pCtx) { - if (pCtx->colId <= TSDB_UD_COLUMN_INDEX && pCtx->colId > TSDB_RES_COL_ID) { // user-specified constant value + if (pCtx->colId <= TSDB_UD_COLUMN_INDEX && pCtx->colId > TSDB_RES_COL_ID) { // user-specified constant value return; } @@ -3332,13 +3336,13 @@ static void col_project_function(SQLFunctionCtx *pCtx) { char *pData = GET_INPUT_DATA_LIST(pCtx); if (pCtx->order == TSDB_ORDER_ASC) { // ASC - int32_t numOfRows = (pCtx->param[0].i64 == 1)? 1:pCtx->size; - memcpy(pCtx->pOutput, pData, (size_t) numOfRows * pCtx->inputBytes); + int32_t numOfRows = (pCtx->param[0].i64 == 1) ? 1 : pCtx->size; + memcpy(pCtx->pOutput, pData, (size_t)numOfRows * pCtx->inputBytes); } else { // DESC - for(int32_t i = 0; i < pCtx->size; ++i) { - char* dst = pCtx->pOutput + (pCtx->size - 1 - i) * pCtx->inputBytes; - char* src = pData + i * pCtx->inputBytes; + for (int32_t i = 0; i < pCtx->size; ++i) { + char *dst = pCtx->pOutput + (pCtx->size - 1 - i) * pCtx->inputBytes; + char *src = pData + i * pCtx->inputBytes; if (IS_VAR_DATA_TYPE(pCtx->inputType)) varDataCopy(dst, src); else @@ -3354,12 +3358,12 @@ static void col_project_function(SQLFunctionCtx *pCtx) { */ static void tag_project_function(SQLFunctionCtx *pCtx) { INC_INIT_VAL(pCtx, pCtx->size); - + assert(pCtx->inputBytes == pCtx->outputBytes); tVariantDump(&pCtx->tag, pCtx->pOutput, pCtx->outputType, true); - char* data = pCtx->pOutput; + char *data = pCtx->pOutput; pCtx->pOutput += pCtx->outputBytes; // directly copy from the first one @@ -3389,45 +3393,44 @@ static void tag_function(SQLFunctionCtx *pCtx) { static void copy_function(SQLFunctionCtx *pCtx) { SET_VAL(pCtx, pCtx->size, 1); - + char *pData = GET_INPUT_DATA_LIST(pCtx); assignVal(pCtx->pOutput, pData, pCtx->inputBytes, pCtx->inputType); } static void full_copy_function(SQLFunctionCtx *pCtx) { copy_function(pCtx); - + for (int t = 0; t < pCtx->tagInfo.numOfTagCols; ++t) { - SQLFunctionCtx* tagCtx = pCtx->tagInfo.pTagCtxList[t]; + SQLFunctionCtx *tagCtx = pCtx->tagInfo.pTagCtxList[t]; if (tagCtx->functionId == TSDB_FUNC_TAG_DUMMY) { aAggs[TSDB_FUNC_TAG].xFunction(tagCtx); } } } - -static bool diff_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool diff_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } - - SDiffFuncInfo* pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo); + + SDiffFuncInfo *pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo); pDiffInfo->valueAssigned = false; pDiffInfo->i64Prev = 0; pDiffInfo->ignoreNegative = (pCtx->param[0].i64 == 1) ? true : false; return true; } -static bool deriv_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResultInfo) { +static bool deriv_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResultInfo) { if (!function_setup(pCtx, pResultInfo)) { return false; } // diff function require the value is set to -1 - SDerivInfo* pDerivInfo = GET_ROWCELL_INTERBUF(pResultInfo); + SDerivInfo *pDerivInfo = GET_ROWCELL_INTERBUF(pResultInfo); pDerivInfo->ignoreNegative = pCtx->param[1].i64; - pDerivInfo->prevTs = -1; + pDerivInfo->prevTs = -1; pDerivInfo->tsWindow = pCtx->param[0].i64; pDerivInfo->valueSet = false; return false; @@ -3435,7 +3438,7 @@ static bool deriv_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResu static void deriv_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SDerivInfo* pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo); + SDerivInfo *pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo); void *data = GET_INPUT_DATA_LIST(pCtx); @@ -3457,20 +3460,21 @@ static void deriv_function(SQLFunctionCtx *pCtx) { } if (!pDerivInfo->valueSet) { // initial value is not set yet - pDerivInfo->valueSet = true; + pDerivInfo->valueSet = true; } else { - SET_DOUBLE_VAL(pOutput, ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs)); + SET_DOUBLE_VAL( + pOutput, ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs)); if (pDerivInfo->ignoreNegative && *pOutput < 0) { } else { *pTimestamp = tsList[i]; - pOutput += 1; + pOutput += 1; pTimestamp += 1; notNullElems++; } } pDerivInfo->prevValue = pData[i]; - pDerivInfo->prevTs = tsList[i]; + pDerivInfo->prevTs = tsList[i]; } break; @@ -3484,20 +3488,20 @@ static void deriv_function(SQLFunctionCtx *pCtx) { } if (!pDerivInfo->valueSet) { // initial value is not set yet - pDerivInfo->valueSet = true; + pDerivInfo->valueSet = true; } else { *pOutput = ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs); if (pDerivInfo->ignoreNegative && *pOutput < 0) { } else { *pTimestamp = tsList[i]; - pOutput += 1; + pOutput += 1; pTimestamp += 1; notNullElems++; } } - pDerivInfo->prevValue = (double) pData[i]; - pDerivInfo->prevTs = tsList[i]; + pDerivInfo->prevValue = (double)pData[i]; + pDerivInfo->prevTs = tsList[i]; } break; } @@ -3510,20 +3514,20 @@ static void deriv_function(SQLFunctionCtx *pCtx) { } if (!pDerivInfo->valueSet) { // initial value is not set yet - pDerivInfo->valueSet = true; + pDerivInfo->valueSet = true; } else { *pOutput = ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs); if (pDerivInfo->ignoreNegative && *pOutput < 0) { } else { *pTimestamp = tsList[i]; - pOutput += 1; + pOutput += 1; pTimestamp += 1; notNullElems++; } } pDerivInfo->prevValue = pData[i]; - pDerivInfo->prevTs = tsList[i]; + pDerivInfo->prevTs = tsList[i]; } break; } @@ -3537,20 +3541,20 @@ static void deriv_function(SQLFunctionCtx *pCtx) { } if (!pDerivInfo->valueSet) { // initial value is not set yet - pDerivInfo->valueSet = true; + pDerivInfo->valueSet = true; } else { *pOutput = ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs); if (pDerivInfo->ignoreNegative && *pOutput < 0) { } else { *pTimestamp = tsList[i]; - pOutput += 1; + pOutput += 1; pTimestamp += 1; notNullElems++; } } pDerivInfo->prevValue = pData[i]; - pDerivInfo->prevTs = tsList[i]; + pDerivInfo->prevTs = tsList[i]; } break; } @@ -3563,20 +3567,20 @@ static void deriv_function(SQLFunctionCtx *pCtx) { } if (!pDerivInfo->valueSet) { // initial value is not set yet - pDerivInfo->valueSet = true; + pDerivInfo->valueSet = true; } else { *pOutput = ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs); if (pDerivInfo->ignoreNegative && *pOutput < 0) { } else { *pTimestamp = tsList[i]; - pOutput += 1; + pOutput += 1; pTimestamp += 1; notNullElems++; } } pDerivInfo->prevValue = pData[i]; - pDerivInfo->prevTs = tsList[i]; + pDerivInfo->prevTs = tsList[i]; } break; } @@ -3589,21 +3593,21 @@ static void deriv_function(SQLFunctionCtx *pCtx) { } if (!pDerivInfo->valueSet) { // initial value is not set yet - pDerivInfo->valueSet = true; + pDerivInfo->valueSet = true; } else { *pOutput = ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs); if (pDerivInfo->ignoreNegative && *pOutput < 0) { } else { *pTimestamp = tsList[i]; - pOutput += 1; + pOutput += 1; pTimestamp += 1; notNullElems++; } } pDerivInfo->prevValue = pData[i]; - pDerivInfo->prevTs = tsList[i]; + pDerivInfo->prevTs = tsList[i]; } break; } @@ -3612,7 +3616,7 @@ static void deriv_function(SQLFunctionCtx *pCtx) { } if (notNullElems > 0) { for (int t = 0; t < pCtx->tagInfo.numOfTagCols; ++t) { - SQLFunctionCtx* tagCtx = pCtx->tagInfo.pTagCtxList[t]; + SQLFunctionCtx *tagCtx = pCtx->tagInfo.pTagCtxList[t]; if (tagCtx->functionId == TSDB_FUNC_TAG_DUMMY) { aAggs[TSDB_FUNC_TAGPRJ].xFunction(tagCtx); } @@ -3621,11 +3625,10 @@ static void deriv_function(SQLFunctionCtx *pCtx) { GET_RES_INFO(pCtx)->numOfRes += notNullElems; } - // TODO difference in date column static void diff_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SDiffFuncInfo *pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo); + SDiffFuncInfo *pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo); void *data = GET_INPUT_DATA_LIST(pCtx); bool isFirstBlock = (pDiffInfo->valueAssigned == false); @@ -3635,8 +3638,8 @@ static void diff_function(SQLFunctionCtx *pCtx) { int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->order); int32_t i = (pCtx->order == TSDB_ORDER_ASC) ? 0 : pCtx->size - 1; - TSKEY* pTimestamp = pCtx->ptsOutputBuf; - TSKEY* tsList = GET_TS_LIST(pCtx); + TSKEY *pTimestamp = pCtx->ptsOutputBuf; + TSKEY *tsList = GET_TS_LIST(pCtx); switch (pCtx->inputType) { case TSDB_DATA_TYPE_INT: { @@ -3644,7 +3647,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { int32_t *pOutput = (int32_t *)pCtx->pOutput; for (; i < pCtx->size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&pData[i], pCtx->inputType)) { continue; } @@ -3652,8 +3655,8 @@ static void diff_function(SQLFunctionCtx *pCtx) { int32_t diff = (int32_t)(pData[i] - pDiffInfo->i64Prev); if (diff >= 0 || !pDiffInfo->ignoreNegative) { *pOutput = (int32_t)(pData[i] - pDiffInfo->i64Prev); // direct previous may be null - *pTimestamp = (tsList != NULL)? tsList[i]:0; - pOutput += 1; + *pTimestamp = (tsList != NULL) ? tsList[i] : 0; + pOutput += 1; pTimestamp += 1; notNullElems++; } @@ -3670,7 +3673,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { int64_t *pOutput = (int64_t *)pCtx->pOutput; for (; i < pCtx->size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&pData[i], pCtx->inputType)) { continue; } @@ -3678,8 +3681,8 @@ static void diff_function(SQLFunctionCtx *pCtx) { int64_t diff = pData[i] - pDiffInfo->i64Prev; if (diff >= 0 || !pDiffInfo->ignoreNegative) { *pOutput = pData[i] - pDiffInfo->i64Prev; // direct previous may be null - *pTimestamp = (tsList != NULL)? tsList[i]:0; - pOutput += 1; + *pTimestamp = (tsList != NULL) ? tsList[i] : 0; + pOutput += 1; pTimestamp += 1; notNullElems++; } @@ -3696,7 +3699,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { double *pOutput = (double *)pCtx->pOutput; for (; i < pCtx->size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&pData[i], pCtx->inputType)) { continue; } @@ -3704,8 +3707,8 @@ static void diff_function(SQLFunctionCtx *pCtx) { double diff = pData[i] - pDiffInfo->d64Prev; if (diff >= 0 || !pDiffInfo->ignoreNegative) { SET_DOUBLE_VAL(pOutput, pData[i] - pDiffInfo->d64Prev); // direct previous may be null - *pTimestamp = (tsList != NULL)? tsList[i]:0; - pOutput += 1; + *pTimestamp = (tsList != NULL) ? tsList[i] : 0; + pOutput += 1; pTimestamp += 1; notNullElems++; } @@ -3722,16 +3725,16 @@ static void diff_function(SQLFunctionCtx *pCtx) { float *pOutput = (float *)pCtx->pOutput; for (; i < pCtx->size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&pData[i], pCtx->inputType)) { continue; } - if (pDiffInfo->valueAssigned) { + if (pDiffInfo->valueAssigned) { float diff = (float)(pData[i] - pDiffInfo->d64Prev); if (diff >= 0 || !pDiffInfo->ignoreNegative) { - *pOutput = (float)(pData[i] - pDiffInfo->d64Prev); - *pTimestamp = (tsList != NULL)? tsList[i]:0; - pOutput += 1; + *pOutput = (float)(pData[i] - pDiffInfo->d64Prev); + *pTimestamp = (tsList != NULL) ? tsList[i] : 0; + pOutput += 1; pTimestamp += 1; notNullElems++; } @@ -3748,7 +3751,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { int16_t *pOutput = (int16_t *)pCtx->pOutput; for (; i < pCtx->size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&pData[i], pCtx->inputType)) { continue; } @@ -3756,8 +3759,8 @@ static void diff_function(SQLFunctionCtx *pCtx) { int16_t diff = (int16_t)(pData[i] - pDiffInfo->i64Prev); if (diff >= 0 || !pDiffInfo->ignoreNegative) { *pOutput = (int16_t)(pData[i] - pDiffInfo->i64Prev); - *pTimestamp = (tsList != NULL)? tsList[i]:0; - pOutput += 1; + *pTimestamp = (tsList != NULL) ? tsList[i] : 0; + pOutput += 1; pTimestamp += 1; notNullElems++; } @@ -3782,8 +3785,8 @@ static void diff_function(SQLFunctionCtx *pCtx) { int8_t diff = (int8_t)(pData[i] - pDiffInfo->i64Prev); if (diff >= 0 || !pDiffInfo->ignoreNegative) { *pOutput = (int8_t)(pData[i] - pDiffInfo->i64Prev); - *pTimestamp = (tsList != NULL)? tsList[i]:0; - pOutput += 1; + *pTimestamp = (tsList != NULL) ? tsList[i] : 0; + pOutput += 1; pTimestamp += 1; notNullElems++; } @@ -3808,7 +3811,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { assert(pCtx->hasNull); } else { for (int t = 0; t < pCtx->tagInfo.numOfTagCols; ++t) { - SQLFunctionCtx* tagCtx = pCtx->tagInfo.pTagCtxList[t]; + SQLFunctionCtx *tagCtx = pCtx->tagInfo.pTagCtxList[t]; if (tagCtx->functionId == TSDB_FUNC_TAG_DUMMY) { aAggs[TSDB_FUNC_TAGPRJ].xFunction(tagCtx); } @@ -3819,9 +3822,9 @@ static void diff_function(SQLFunctionCtx *pCtx) { } } -char *getScalarExprColumnData(void *param, const char* name, int32_t colId) { +char *getScalarExprColumnData(void *param, const char *name, int32_t colId) { SScalarExprSupport *pSupport = (SScalarExprSupport *)param; - + int32_t index = -1; for (int32_t i = 0; i < pSupport->numOfCols; ++i) { if (colId == pSupport->colList[i].colId) { @@ -3829,7 +3832,7 @@ char *getScalarExprColumnData(void *param, const char* name, int32_t colId) { break; } } - + assert(index >= 0); return pSupport->data[index] + pSupport->offset * pSupport->colList[index].bytes; } @@ -3837,7 +3840,7 @@ char *getScalarExprColumnData(void *param, const char* name, int32_t colId) { static void scalar_expr_function(SQLFunctionCtx *pCtx) { GET_RES_INFO(pCtx)->numOfRes += pCtx->size; SScalarExprSupport *sas = (SScalarExprSupport *)pCtx->param[1].pz; - tExprOperandInfo output; + tExprOperandInfo output; output.data = pCtx->pOutput; exprTreeNodeTraverse(sas->pExprInfo->pExpr, pCtx->size, &output, sas, pCtx->order, getScalarExprColumnData); } @@ -3850,23 +3853,23 @@ static void scalar_expr_function(SQLFunctionCtx *pCtx) { continue; \ } \ if (inputData[i] < minOutput) { \ - minOutput = (double)inputData[i]; \ + minOutput = (double)inputData[i]; \ } \ if (inputData[i] > maxOutput) { \ - maxOutput = (double)inputData[i]; \ + maxOutput = (double)inputData[i]; \ } \ numOfNotNullElem++; \ } \ } ///////////////////////////////////////////////////////////////////////////////// -static bool spread_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool spread_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } - + SSpreadInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); - + // this is the server-side setup function in client-side, the secondary merge do not need this procedure if (pCtx->currentStage == MERGE_STAGE) { pCtx->param[0].dKey = DBL_MAX; @@ -3875,21 +3878,21 @@ static bool spread_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pRes pInfo->min = DBL_MAX; pInfo->max = -DBL_MAX; } - + return true; } static void spread_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SSpreadInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); - + SSpreadInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); + int32_t numOfElems = 0; // todo : opt with pre-calculated result // column missing cause the hasNull to be true if (pCtx->preAggVals.isSet) { numOfElems = pCtx->size - pCtx->preAggVals.statis.numOfNull; - + // all data are null in current data block, ignore current data block if (numOfElems == 0) { goto _spread_over; @@ -3908,18 +3911,18 @@ static void spread_function(SQLFunctionCtx *pCtx) { if (pInfo->min > GET_DOUBLE_VAL((const char *)&(pCtx->preAggVals.statis.min))) { pInfo->min = GET_DOUBLE_VAL((const char *)&(pCtx->preAggVals.statis.min)); } - + if (pInfo->max < GET_DOUBLE_VAL((const char *)&(pCtx->preAggVals.statis.max))) { pInfo->max = GET_DOUBLE_VAL((const char *)&(pCtx->preAggVals.statis.max)); } } - + goto _spread_over; } - + void *pData = GET_INPUT_DATA_LIST(pCtx); numOfElems = 0; - + if (pCtx->inputType == TSDB_DATA_TYPE_TINYINT) { LIST_MINMAX_N(pCtx, pInfo->min, pInfo->max, pCtx->size, pData, int8_t, pCtx->inputType, numOfElems); } else if (pCtx->inputType == TSDB_DATA_TYPE_SMALLINT) { @@ -3941,19 +3944,19 @@ static void spread_function(SQLFunctionCtx *pCtx) { } else if (pCtx->inputType == TSDB_DATA_TYPE_UBIGINT) { LIST_MINMAX_N(pCtx, pInfo->min, pInfo->max, pCtx->size, pData, uint64_t, pCtx->inputType, numOfElems); } - + if (!pCtx->hasNull) { assert(pCtx->size == numOfElems); } - - _spread_over: + +_spread_over: SET_VAL(pCtx, numOfElems, 1); - + if (numOfElems > 0) { pResInfo->hasResult = DATA_SET_FLAG; pInfo->hasResult = DATA_SET_FLAG; } - + // keep the data into the final output buffer for super table query since this execution may be the last one if (pCtx->stableQuery) { memcpy(pCtx->pOutput, GET_ROWCELL_INTERBUF(pResInfo), sizeof(SSpreadInfo)); @@ -3969,15 +3972,15 @@ void spread_func_merge(SQLFunctionCtx *pCtx) { if (pData->hasResult != DATA_SET_FLAG) { return; } - + if (pCtx->param[0].dKey > pData->min) { pCtx->param[0].dKey = pData->min; } - + if (pCtx->param[3].dKey < pData->max) { pCtx->param[3].dKey = pData->max; } - + GET_RES_INFO(pCtx)->hasResult = DATA_SET_FLAG; } @@ -3990,70 +3993,69 @@ void spread_function_finalizer(SQLFunctionCtx *pCtx) { if (pCtx->currentStage == MERGE_STAGE) { assert(pCtx->inputType == TSDB_DATA_TYPE_BINARY); - + if (pResInfo->hasResult != DATA_SET_FLAG) { setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); return; } - + SET_DOUBLE_VAL((double *)pCtx->pOutput, pCtx->param[3].dKey - pCtx->param[0].dKey); } else { assert(IS_NUMERIC_TYPE(pCtx->inputType) || (pCtx->inputType == TSDB_DATA_TYPE_TIMESTAMP)); - + SSpreadInfo *pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); if (pInfo->hasResult != DATA_SET_FLAG) { setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); return; } - + SET_DOUBLE_VAL((double *)pCtx->pOutput, pInfo->max - pInfo->min); } - + GET_RES_INFO(pCtx)->numOfRes = 1; // todo add test case doFinalizer(pCtx); } - /** * param[1]: start time * param[2]: end time * @param pCtx */ -static bool twa_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool twa_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } STwaInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); - pInfo->p.key = INT64_MIN; - pInfo->win = TSWINDOW_INITIALIZER; + pInfo->p.key = INT64_MIN; + pInfo->win = TSWINDOW_INITIALIZER; return true; } static double twa_get_area(SPoint1 s, SPoint1 e) { - if ((s.val >= 0 && e.val >= 0)|| (s.val <=0 && e.val <= 0)) { + if ((s.val >= 0 && e.val >= 0) || (s.val <= 0 && e.val <= 0)) { return (s.val + e.val) * (e.key - s.key) / 2; } - double x = (s.key * e.val - e.key * s.val)/(e.val - s.val); + double x = (s.key * e.val - e.key * s.val) / (e.val - s.val); double val = (s.val * (x - s.key) + e.val * (e.key - x)) / 2; return val; } -static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t index, int32_t size) { - int32_t notNullElems = 0; +static int32_t twa_function_impl(SQLFunctionCtx *pCtx, int32_t index, int32_t size) { + int32_t notNullElems = 0; SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); STwaInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); TSKEY *tsList = GET_TS_LIST(pCtx); - int32_t i = index; - int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->order); - SPoint1* last = &pInfo->p; + int32_t i = index; + int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->order); + SPoint1 *last = &pInfo->p; if (pCtx->start.key != INT64_MIN) { assert((pCtx->start.key < tsList[i] && pCtx->order == TSDB_ORDER_ASC) || - (pCtx->start.key > tsList[i] && pCtx->order == TSDB_ORDER_DESC)); + (pCtx->start.key > tsList[i] && pCtx->order == TSDB_ORDER_DESC)); assert(last->key == INT64_MIN); @@ -4077,11 +4079,11 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t index, int32_t si } // calculate the value of - switch(pCtx->inputType) { + switch (pCtx->inputType) { case TSDB_DATA_TYPE_TINYINT: { - int8_t *val = (int8_t*) GET_INPUT_DATA(pCtx, 0); + int8_t *val = (int8_t *)GET_INPUT_DATA(pCtx, 0); for (; i < size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &val[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&val[i], pCtx->inputType)) { continue; } @@ -4091,111 +4093,111 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t index, int32_t si SPoint1 st; st.key = tsList[i]; st.val = val[i]; -#endif +#endif pInfo->dOutput += twa_get_area(pInfo->p, st); pInfo->p = st; } break; } case TSDB_DATA_TYPE_SMALLINT: { - int16_t *val = (int16_t*) GET_INPUT_DATA(pCtx, 0); + int16_t *val = (int16_t *)GET_INPUT_DATA(pCtx, 0); for (; i < size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &val[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&val[i], pCtx->inputType)) { continue; } - + #ifndef _TD_NINGSI_60 SPoint1 st = {.key = tsList[i], .val = val[i]}; #else SPoint1 st; st.key = tsList[i]; st.val = val[i]; -#endif +#endif pInfo->dOutput += twa_get_area(pInfo->p, st); pInfo->p = st; } break; } case TSDB_DATA_TYPE_INT: { - int32_t *val = (int32_t*) GET_INPUT_DATA(pCtx, 0); + int32_t *val = (int32_t *)GET_INPUT_DATA(pCtx, 0); for (; i < size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &val[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&val[i], pCtx->inputType)) { continue; } - + #ifndef _TD_NINGSI_60 SPoint1 st = {.key = tsList[i], .val = val[i]}; #else SPoint1 st; st.key = tsList[i]; st.val = val[i]; -#endif +#endif pInfo->dOutput += twa_get_area(pInfo->p, st); pInfo->p = st; } break; } case TSDB_DATA_TYPE_BIGINT: { - int64_t *val = (int64_t*) GET_INPUT_DATA(pCtx, 0); + int64_t *val = (int64_t *)GET_INPUT_DATA(pCtx, 0); for (; i < size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &val[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&val[i], pCtx->inputType)) { continue; } - + #ifndef _TD_NINGSI_60 - SPoint1 st = {.key = tsList[i], .val = (double) val[i]}; + SPoint1 st = {.key = tsList[i], .val = (double)val[i]}; #else SPoint1 st; st.key = tsList[i]; st.val = (double)val[i]; -#endif +#endif pInfo->dOutput += twa_get_area(pInfo->p, st); pInfo->p = st; } break; } case TSDB_DATA_TYPE_FLOAT: { - float *val = (float*) GET_INPUT_DATA(pCtx, 0); + float *val = (float *)GET_INPUT_DATA(pCtx, 0); for (; i < size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &val[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&val[i], pCtx->inputType)) { continue; } - + #ifndef _TD_NINGSI_60 SPoint1 st = {.key = tsList[i], .val = val[i]}; #else SPoint1 st; st.key = tsList[i]; st.val = (double)val[i]; -#endif +#endif pInfo->dOutput += twa_get_area(pInfo->p, st); pInfo->p = st; } break; } case TSDB_DATA_TYPE_DOUBLE: { - double *val = (double*) GET_INPUT_DATA(pCtx, 0); + double *val = (double *)GET_INPUT_DATA(pCtx, 0); for (; i < size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &val[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&val[i], pCtx->inputType)) { continue; } - + #ifndef _TD_NINGSI_60 SPoint1 st = {.key = tsList[i], .val = val[i]}; #else SPoint1 st; st.key = tsList[i]; st.val = val[i]; -#endif +#endif pInfo->dOutput += twa_get_area(pInfo->p, st); pInfo->p = st; } break; } case TSDB_DATA_TYPE_UTINYINT: { - uint8_t *val = (uint8_t*) GET_INPUT_DATA(pCtx, 0); + uint8_t *val = (uint8_t *)GET_INPUT_DATA(pCtx, 0); for (; i < size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &val[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&val[i], pCtx->inputType)) { continue; } @@ -4205,16 +4207,16 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t index, int32_t si SPoint1 st; st.key = tsList[i]; st.val = val[i]; -#endif +#endif pInfo->dOutput += twa_get_area(pInfo->p, st); pInfo->p = st; } break; } case TSDB_DATA_TYPE_USMALLINT: { - uint16_t *val = (uint16_t*) GET_INPUT_DATA(pCtx, 0); + uint16_t *val = (uint16_t *)GET_INPUT_DATA(pCtx, 0); for (; i < size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &val[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&val[i], pCtx->inputType)) { continue; } @@ -4224,16 +4226,16 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t index, int32_t si SPoint1 st; st.key = tsList[i]; st.val = val[i]; -#endif +#endif pInfo->dOutput += twa_get_area(pInfo->p, st); pInfo->p = st; } break; } case TSDB_DATA_TYPE_UINT: { - uint32_t *val = (uint32_t*) GET_INPUT_DATA(pCtx, 0); + uint32_t *val = (uint32_t *)GET_INPUT_DATA(pCtx, 0); for (; i < size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &val[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&val[i], pCtx->inputType)) { continue; } @@ -4243,41 +4245,42 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t index, int32_t si SPoint1 st; st.key = tsList[i]; st.val = val[i]; -#endif +#endif pInfo->dOutput += twa_get_area(pInfo->p, st); pInfo->p = st; } break; } case TSDB_DATA_TYPE_UBIGINT: { - uint64_t *val = (uint64_t*) GET_INPUT_DATA(pCtx, 0); + uint64_t *val = (uint64_t *)GET_INPUT_DATA(pCtx, 0); for (; i < size && i >= 0; i += step) { - if (pCtx->hasNull && isNull((const char*) &val[i], pCtx->inputType)) { + if (pCtx->hasNull && isNull((const char *)&val[i], pCtx->inputType)) { continue; } - + #ifndef _TD_NINGSI_60 - SPoint1 st = {.key = tsList[i], .val = (double) val[i]}; + SPoint1 st = {.key = tsList[i], .val = (double)val[i]}; #else SPoint1 st; st.key = tsList[i]; - st.val = (double) val[i]; -#endif + st.val = (double)val[i]; +#endif pInfo->dOutput += twa_get_area(pInfo->p, st); pInfo->p = st; } break; } - default: assert(0); + default: + assert(0); } // the last interpolated time window value if (pCtx->end.key != INT64_MIN) { - pInfo->dOutput += twa_get_area(pInfo->p, pCtx->end); + pInfo->dOutput += twa_get_area(pInfo->p, pCtx->end); pInfo->p = pCtx->end; } - pInfo->win.ekey = pInfo->p.key; + pInfo->win.ekey = pInfo->p.key; return notNullElems; } @@ -4285,11 +4288,11 @@ static void twa_function(SQLFunctionCtx *pCtx) { void *data = GET_INPUT_DATA_LIST(pCtx); SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - STwaInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); - + STwaInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); + // skip null value int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->order); - int32_t i = (pCtx->order == TSDB_ORDER_ASC)? 0:(pCtx->size - 1); + int32_t i = (pCtx->order == TSDB_ORDER_ASC) ? 0 : (pCtx->size - 1); while (pCtx->hasNull && i < pCtx->size && i >= 0 && isNull((char *)data + pCtx->inputBytes * i, pCtx->inputType)) { i += step; } @@ -4300,11 +4303,11 @@ static void twa_function(SQLFunctionCtx *pCtx) { } SET_VAL(pCtx, notNullElems, 1); - + if (notNullElems > 0) { pResInfo->hasResult = DATA_SET_FLAG; } - + if (pCtx->stableQuery) { memcpy(pCtx->pOutput, pInfo, sizeof(STwaInfo)); } @@ -4318,14 +4321,14 @@ static void twa_function(SQLFunctionCtx *pCtx) { void twa_function_copy(SQLFunctionCtx *pCtx) { assert(pCtx->inputType == TSDB_DATA_TYPE_BINARY); SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - + memcpy(GET_ROWCELL_INTERBUF(pResInfo), pCtx->pInput, (size_t)pCtx->inputBytes); pResInfo->hasResult = ((STwaInfo *)pCtx->pInput)->hasResult; } void twa_function_finalizer(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - + STwaInfo *pInfo = (STwaInfo *)GET_ROWCELL_INTERBUF(pResInfo); if (pInfo->hasResult != DATA_SET_FLAG) { setNull(pCtx->pOutput, TSDB_DATA_TYPE_DOUBLE, sizeof(double)); @@ -4336,61 +4339,62 @@ void twa_function_finalizer(SQLFunctionCtx *pCtx) { if (pInfo->win.ekey == pInfo->win.skey) { SET_DOUBLE_VAL((double *)pCtx->pOutput, pInfo->p.val); } else { - SET_DOUBLE_VAL((double *)pCtx->pOutput , pInfo->dOutput / (pInfo->win.ekey - pInfo->win.skey)); + SET_DOUBLE_VAL((double *)pCtx->pOutput, pInfo->dOutput / (pInfo->win.ekey - pInfo->win.skey)); } - + GET_RES_INFO(pCtx)->numOfRes = 1; doFinalizer(pCtx); } static void interp_function(SQLFunctionCtx *pCtx) { - int32_t fillType = (int32_t) pCtx->param[2].i64; - //bool ascQuery = (pCtx->order == TSDB_ORDER_ASC); + int32_t fillType = (int32_t)pCtx->param[2].i64; + // bool ascQuery = (pCtx->order == TSDB_ORDER_ASC); if (pCtx->start.key == pCtx->startTs) { assert(pCtx->start.key != INT64_MIN); - + COPY_TYPED_DATA(pCtx->pOutput, pCtx->inputType, &pCtx->start.val); - - goto interp_success_exit; + + goto interp_success_exit; } else if (pCtx->end.key == pCtx->startTs && pCtx->end.key != INT64_MIN && fillType == TSDB_FILL_NEXT) { COPY_TYPED_DATA(pCtx->pOutput, pCtx->inputType, &pCtx->end.val); - - goto interp_success_exit; + + goto interp_success_exit; } switch (fillType) { case TSDB_FILL_NULL: setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); break; - + case TSDB_FILL_SET_VALUE: tVariantDump(&pCtx->param[1], pCtx->pOutput, pCtx->inputType, true); break; - + case TSDB_FILL_LINEAR: - if (pCtx->start.key == INT64_MIN || pCtx->start.key > pCtx->startTs - || pCtx->end.key == INT64_MIN || pCtx->end.key < pCtx->startTs) { + if (pCtx->start.key == INT64_MIN || pCtx->start.key > pCtx->startTs || pCtx->end.key == INT64_MIN || + pCtx->end.key < pCtx->startTs) { goto interp_exit; } double v1 = -1, v2 = -1; GET_TYPED_DATA(v1, double, pCtx->inputType, &pCtx->start.val); GET_TYPED_DATA(v2, double, pCtx->inputType, &pCtx->end.val); - + SPoint point1 = {.key = pCtx->start.key, .val = &v1}; SPoint point2 = {.key = pCtx->end.key, .val = &v2}; - SPoint point = {.key = pCtx->startTs, .val = pCtx->pOutput}; + SPoint point = {.key = pCtx->startTs, .val = pCtx->pOutput}; int32_t srcType = pCtx->inputType; if (isNull((char *)&pCtx->start.val, srcType) || isNull((char *)&pCtx->end.val, srcType)) { setNull(pCtx->pOutput, srcType, pCtx->inputBytes); } else { bool exceedMax = false, exceedMin = false; - taosGetLinearInterpolationVal(&point, pCtx->outputType, &point1, &point2, TSDB_DATA_TYPE_DOUBLE, &exceedMax, &exceedMin); + taosGetLinearInterpolationVal(&point, pCtx->outputType, &point1, &point2, TSDB_DATA_TYPE_DOUBLE, &exceedMax, + &exceedMin); if (exceedMax || exceedMin) { __compar_fn_t func = getComparFunc((int32_t)pCtx->inputType, 0); - if (func(&pCtx->start.val, &pCtx->end.val) <= 0) { + if (func(&pCtx->start.val, &pCtx->end.val) <= 0) { COPY_TYPED_DATA(pCtx->pOutput, pCtx->inputType, exceedMax ? &pCtx->start.val : &pCtx->end.val); } else { COPY_TYPED_DATA(pCtx->pOutput, pCtx->inputType, exceedMax ? &pCtx->end.val : &pCtx->start.val); @@ -4411,7 +4415,7 @@ static void interp_function(SQLFunctionCtx *pCtx) { if (pCtx->end.key == INT64_MIN || pCtx->end.key < pCtx->startTs) { goto interp_exit; } - + COPY_TYPED_DATA(pCtx->pOutput, pCtx->inputType, &pCtx->end.val); break; @@ -4420,10 +4424,9 @@ static void interp_function(SQLFunctionCtx *pCtx) { goto interp_exit; } +interp_success_exit: -interp_success_exit: - - *(TSKEY*)pCtx->ptsOutputBuf = pCtx->startTs; + *(TSKEY *)pCtx->ptsOutputBuf = pCtx->startTs; INC_INIT_VAL(pCtx, 1); @@ -4436,7 +4439,7 @@ interp_exit: return; } -static bool ts_comp_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool ts_comp_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; // not initialized since it has been initialized } @@ -4449,10 +4452,10 @@ static bool ts_comp_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pRe static void ts_comp_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - STSBuf * pTSbuf = ((STSCompInfo *)(GET_ROWCELL_INTERBUF(pResInfo)))->pTSBuf; - + STSBuf *pTSbuf = ((STSCompInfo *)(GET_ROWCELL_INTERBUF(pResInfo)))->pTSBuf; + const char *input = GET_INPUT_DATA_LIST(pCtx); - + // primary ts must be existed, so no need to check its existance if (pCtx->order == TSDB_ORDER_ASC) { tsBufAppend(pTSbuf, (int32_t)pCtx->param[0].i64, &pCtx->tag, input, pCtx->size * TSDB_KEYSIZE); @@ -4462,19 +4465,19 @@ static void ts_comp_function(SQLFunctionCtx *pCtx) { tsBufAppend(pTSbuf, (int32_t)pCtx->param[0].i64, &pCtx->tag, d, (int32_t)TSDB_KEYSIZE); } } - + SET_VAL(pCtx, pCtx->size, 1); pResInfo->hasResult = DATA_SET_FLAG; } static void ts_comp_finalize(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - + STSCompInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); - STSBuf * pTSbuf = pInfo->pTSBuf; - + STSBuf *pTSbuf = pInfo->pTSBuf; + tsBufFlush(pTSbuf); - qDebug("total timestamp :%"PRId64, pTSbuf->numOfTotal); + qDebug("total timestamp :%" PRId64, pTSbuf->numOfTotal); // TODO refactor transfer ownership of current file *(FILE **)pCtx->pOutput = pTSbuf->f; @@ -4495,7 +4498,7 @@ static void ts_comp_finalize(SQLFunctionCtx *pCtx) { ////////////////////////////////////////////////////////////////////////////////////////////// // rate functions -static double do_calc_rate(const SRateInfo* pRateInfo, double tickPerSec) { +static double do_calc_rate(const SRateInfo *pRateInfo, double tickPerSec) { if ((INT64_MIN == pRateInfo->lastKey) || (INT64_MIN == pRateInfo->firstKey) || (pRateInfo->firstKey >= pRateInfo->lastKey)) { return 0.0; @@ -4510,31 +4513,31 @@ static double do_calc_rate(const SRateInfo* pRateInfo, double tickPerSec) { diff -= pRateInfo->firstValue; } } else { - diff = pRateInfo->correctionValue + pRateInfo->lastValue - pRateInfo->firstValue; + diff = pRateInfo->correctionValue + pRateInfo->lastValue - pRateInfo->firstValue; if (diff <= 0) { return 0; } } - + int64_t duration = pRateInfo->lastKey - pRateInfo->firstKey; if (duration == 0) { return 0; } - return (duration > 0)? ((double)diff) / (duration/tickPerSec):0.0; + return (duration > 0) ? ((double)diff) / (duration / tickPerSec) : 0.0; } -static bool rate_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool rate_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } - + SRateInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); pInfo->correctionValue = 0; - pInfo->firstKey = INT64_MIN; - pInfo->lastKey = INT64_MIN; - pInfo->firstValue = (double) INT64_MIN; - pInfo->lastValue = (double) INT64_MIN; + pInfo->firstKey = INT64_MIN; + pInfo->lastKey = INT64_MIN; + pInfo->firstValue = (double)INT64_MIN; + pInfo->lastValue = (double)INT64_MIN; pInfo->hasResult = 0; pInfo->isIRate = (pCtx->functionId == TSDB_FUNC_IRATE); @@ -4543,51 +4546,51 @@ static bool rate_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResIn static void rate_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - + int32_t notNullElems = 0; SRateInfo *pRateInfo = (SRateInfo *)GET_ROWCELL_INTERBUF(pResInfo); TSKEY *primaryKey = GET_TS_LIST(pCtx); - + qDebug("%p rate_function() size:%d, hasNull:%d", pCtx, pCtx->size, pCtx->hasNull); - + for (int32_t i = 0; i < pCtx->size; ++i) { char *pData = GET_INPUT_DATA(pCtx, i); if (pCtx->hasNull && isNull(pData, pCtx->inputType)) { qDebug("%p rate_function() index of null data:%d", pCtx, i); continue; } - + notNullElems++; - + double v = 0; GET_TYPED_DATA(v, double, pCtx->inputType, pData); - + if ((INT64_MIN == pRateInfo->firstValue) || (INT64_MIN == pRateInfo->firstKey)) { pRateInfo->firstValue = v; pRateInfo->firstKey = primaryKey[i]; } - + if (INT64_MIN == pRateInfo->lastValue) { pRateInfo->lastValue = v; } else if (v < pRateInfo->lastValue) { pRateInfo->correctionValue += pRateInfo->lastValue; } - + pRateInfo->lastValue = v; - pRateInfo->lastKey = primaryKey[i]; + pRateInfo->lastKey = primaryKey[i]; } - + if (!pCtx->hasNull) { assert(pCtx->size == notNullElems); } - + SET_VAL(pCtx, notNullElems, 1); - + if (notNullElems > 0) { pRateInfo->hasResult = DATA_SET_FLAG; - pResInfo->hasResult = DATA_SET_FLAG; + pResInfo->hasResult = DATA_SET_FLAG; } - + // keep the data into the final output buffer for super table query since this execution may be the last one if (pCtx->stableQuery) { memcpy(pCtx->pOutput, GET_ROWCELL_INTERBUF(pResInfo), sizeof(SRateInfo)); @@ -4596,51 +4599,51 @@ static void rate_function(SQLFunctionCtx *pCtx) { static void rate_func_copy(SQLFunctionCtx *pCtx) { assert(pCtx->inputType == TSDB_DATA_TYPE_BINARY); - + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); memcpy(GET_ROWCELL_INTERBUF(pResInfo), pCtx->pInput, (size_t)pCtx->inputBytes); - pResInfo->hasResult = ((SRateInfo*)pCtx->pInput)->hasResult; + pResInfo->hasResult = ((SRateInfo *)pCtx->pInput)->hasResult; } static void rate_finalizer(SQLFunctionCtx *pCtx) { - SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SRateInfo *pRateInfo = (SRateInfo *)GET_ROWCELL_INTERBUF(pResInfo); + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + SRateInfo *pRateInfo = (SRateInfo *)GET_ROWCELL_INTERBUF(pResInfo); if (pRateInfo->hasResult != DATA_SET_FLAG) { setNull(pCtx->pOutput, TSDB_DATA_TYPE_DOUBLE, sizeof(double)); return; } - - SET_DOUBLE_VAL((double*) pCtx->pOutput, do_calc_rate(pRateInfo, (double) TSDB_TICK_PER_SECOND(pCtx->param[0].i64))); + + SET_DOUBLE_VAL((double *)pCtx->pOutput, do_calc_rate(pRateInfo, (double)TSDB_TICK_PER_SECOND(pCtx->param[0].i64))); // cannot set the numOfIteratedElems again since it is set during previous iteration - pResInfo->numOfRes = 1; + pResInfo->numOfRes = 1; pResInfo->hasResult = DATA_SET_FLAG; - + doFinalizer(pCtx); } static void irate_function(SQLFunctionCtx *pCtx) { - SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); int32_t notNullElems = 0; - SRateInfo *pRateInfo = (SRateInfo *)GET_ROWCELL_INTERBUF(pResInfo); - TSKEY *primaryKey = GET_TS_LIST(pCtx); + SRateInfo *pRateInfo = (SRateInfo *)GET_ROWCELL_INTERBUF(pResInfo); + TSKEY *primaryKey = GET_TS_LIST(pCtx); for (int32_t i = pCtx->size - 1; i >= 0; --i) { char *pData = GET_INPUT_DATA(pCtx, i); if (pCtx->hasNull && isNull(pData, pCtx->inputType)) { continue; } - + notNullElems++; - + double v = 0; GET_TYPED_DATA(v, double, pCtx->inputType, pData); if (INT64_MIN == pRateInfo->lastKey) { pRateInfo->lastValue = v; - pRateInfo->lastKey = primaryKey[i]; + pRateInfo->lastKey = primaryKey[i]; continue; } @@ -4651,25 +4654,25 @@ static void irate_function(SQLFunctionCtx *pCtx) { } pRateInfo->lastValue = v; - pRateInfo->lastKey = primaryKey[i]; - + pRateInfo->lastKey = primaryKey[i]; + continue; } - + if ((INT64_MIN == pRateInfo->firstKey) || primaryKey[i] > pRateInfo->firstKey) { pRateInfo->firstValue = v; pRateInfo->firstKey = primaryKey[i]; break; } } - + SET_VAL(pCtx, notNullElems, 1); - + if (notNullElems > 0) { pRateInfo->hasResult = DATA_SET_FLAG; - pResInfo->hasResult = DATA_SET_FLAG; + pResInfo->hasResult = DATA_SET_FLAG; } - + // keep the data into the final output buffer for super table query since this execution may be the last one if (pCtx->stableQuery) { memcpy(pCtx->pOutput, GET_ROWCELL_INTERBUF(pResInfo), sizeof(SRateInfo)); @@ -4678,22 +4681,22 @@ static void irate_function(SQLFunctionCtx *pCtx) { ///////////////////////////////////////////////////////////////////////////////////////////////////////////// -void blockInfo_func(SQLFunctionCtx* pCtx) { +void blockInfo_func(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - STableBlockDist* pDist = (STableBlockDist*) GET_ROWCELL_INTERBUF(pResInfo); + STableBlockDist *pDist = (STableBlockDist *)GET_ROWCELL_INTERBUF(pResInfo); - int32_t len = *(int32_t*) pCtx->pInput; - blockDistInfoFromBinary((char*)pCtx->pInput + sizeof(int32_t), len, pDist); + int32_t len = *(int32_t *)pCtx->pInput; + blockDistInfoFromBinary((char *)pCtx->pInput + sizeof(int32_t), len, pDist); pDist->rowSize = (uint16_t)pCtx->param[0].i64; memcpy(pCtx->pOutput, pCtx->pInput, sizeof(int32_t) + len); - pResInfo->numOfRes = 1; + pResInfo->numOfRes = 1; pResInfo->hasResult = DATA_SET_FLAG; } -static void mergeTableBlockDist(SResultRowCellInfo* pResInfo, const STableBlockDist* pSrc) { - STableBlockDist* pDist = (STableBlockDist*) GET_ROWCELL_INTERBUF(pResInfo); +static void mergeTableBlockDist(SResultRowCellInfo *pResInfo, const STableBlockDist *pSrc) { + STableBlockDist *pDist = (STableBlockDist *)GET_ROWCELL_INTERBUF(pResInfo); assert(pDist != NULL && pSrc != NULL); pDist->numOfTables += pSrc->numOfTables; @@ -4710,7 +4713,7 @@ static void mergeTableBlockDist(SResultRowCellInfo* pResInfo, const STableBlockD pDist->maxRows = pSrc->maxRows; pDist->minRows = pSrc->minRows; - int32_t maxSteps = TSDB_MAX_MAX_ROW_FBLOCK/TSDB_BLOCK_DIST_STEP_ROWS; + int32_t maxSteps = TSDB_MAX_MAX_ROW_FBLOCK / TSDB_BLOCK_DIST_STEP_ROWS; if (TSDB_MAX_MAX_ROW_FBLOCK % TSDB_BLOCK_DIST_STEP_ROWS != 0) { ++maxSteps; } @@ -4720,16 +4723,16 @@ static void mergeTableBlockDist(SResultRowCellInfo* pResInfo, const STableBlockD size_t steps = taosArrayGetSize(pSrc->dataBlockInfos); for (int32_t i = 0; i < steps; ++i) { - int32_t srcNumBlocks = ((SFileBlockInfo*)taosArrayGet(pSrc->dataBlockInfos, i))->numBlocksOfStep; - SFileBlockInfo* blockInfo = (SFileBlockInfo*)taosArrayGet(pDist->dataBlockInfos, i); + int32_t srcNumBlocks = ((SFileBlockInfo *)taosArrayGet(pSrc->dataBlockInfos, i))->numBlocksOfStep; + SFileBlockInfo *blockInfo = (SFileBlockInfo *)taosArrayGet(pDist->dataBlockInfos, i); blockInfo->numBlocksOfStep += srcNumBlocks; } } -void block_func_merge(SQLFunctionCtx* pCtx) { +void block_func_merge(SQLFunctionCtx *pCtx) { STableBlockDist info = {0}; - int32_t len = *(int32_t*) pCtx->pInput; - blockDistInfoFromBinary(((char*)pCtx->pInput) + sizeof(int32_t), len, &info); + int32_t len = *(int32_t *)pCtx->pInput; + blockDistInfoFromBinary(((char *)pCtx->pInput) + sizeof(int32_t), len, &info); SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); mergeTableBlockDist(pResInfo, &info); taosArrayDestroy(&info.dataBlockInfos); @@ -4738,8 +4741,8 @@ void block_func_merge(SQLFunctionCtx* pCtx) { pResInfo->hasResult = DATA_SET_FLAG; } -void getPercentiles(STableBlockDist *pTableBlockDist, int64_t totalBlocks, int32_t numOfPercents, - double* percents, int32_t* percentiles) { +void getPercentiles(STableBlockDist *pTableBlockDist, int64_t totalBlocks, int32_t numOfPercents, double *percents, + int32_t *percentiles) { if (totalBlocks == 0) { for (int32_t i = 0; i < numOfPercents; ++i) { percentiles[i] = 0; @@ -4769,28 +4772,28 @@ void getPercentiles(STableBlockDist *pTableBlockDist, int64_t totalBlocks, int32 } for (int32_t i = 0; i < numOfPercents; ++i) { - percentiles[i] = (percentiles[i]+1) * TSDB_BLOCK_DIST_STEP_ROWS - TSDB_BLOCK_DIST_STEP_ROWS/2; + percentiles[i] = (percentiles[i] + 1) * TSDB_BLOCK_DIST_STEP_ROWS - TSDB_BLOCK_DIST_STEP_ROWS / 2; } } -void generateBlockDistResult(STableBlockDist *pTableBlockDist, char* result) { +void generateBlockDistResult(STableBlockDist *pTableBlockDist, char *result) { if (pTableBlockDist == NULL) { return; } - SArray* blockInfos = pTableBlockDist->dataBlockInfos; + SArray *blockInfos = pTableBlockDist->dataBlockInfos; uint64_t totalRows = pTableBlockDist->totalRows; size_t numSteps = taosArrayGetSize(blockInfos); - int64_t totalBlocks = 0; - int64_t min = -1, max = -1, avg = 0; + int64_t totalBlocks = 0; + int64_t min = -1, max = -1, avg = 0; for (int32_t i = 0; i < numSteps; i++) { SFileBlockInfo *blockInfo = taosArrayGet(blockInfos, i); - int64_t blocks = blockInfo->numBlocksOfStep; + int64_t blocks = blockInfo->numBlocksOfStep; totalBlocks += blocks; } - avg = totalBlocks > 0 ? (int64_t)(totalRows/totalBlocks) : 0; + avg = totalBlocks > 0 ? (int64_t)(totalRows / totalBlocks) : 0; min = totalBlocks > 0 ? pTableBlockDist->minRows : 0; max = totalBlocks > 0 ? pTableBlockDist->maxRows : 0; @@ -4807,34 +4810,35 @@ void generateBlockDistResult(STableBlockDist *pTableBlockDist, char* result) { stdDev = sqrt(variance); } - double percents[] = {0.05, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 0.95, 0.99}; + double percents[] = {0.05, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 0.95, 0.99}; int32_t percentiles[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; - assert(sizeof(percents)/sizeof(double) == sizeof(percentiles)/sizeof(int32_t)); - getPercentiles(pTableBlockDist, totalBlocks, sizeof(percents)/sizeof(double), percents, percentiles); + assert(sizeof(percents) / sizeof(double) == sizeof(percentiles) / sizeof(int32_t)); + getPercentiles(pTableBlockDist, totalBlocks, sizeof(percents) / sizeof(double), percents, percentiles); uint64_t totalLen = pTableBlockDist->totalSize; - int32_t rowSize = pTableBlockDist->rowSize; - int32_t smallBlocks = pTableBlockDist->numOfSmallBlocks; - double compRatio = (totalRows>0) ? ((double)(totalLen)/(rowSize*totalRows)) : 1; - int sz = sprintf(result + VARSTR_HEADER_SIZE, - "summary: \n\t " - "5th=[%d], 10th=[%d], 20th=[%d], 30th=[%d], 40th=[%d], 50th=[%d]\n\t " - "60th=[%d], 70th=[%d], 80th=[%d], 90th=[%d], 95th=[%d], 99th=[%d]\n\t " - "Min=[%"PRId64"(Rows)] Max=[%"PRId64"(Rows)] Avg=[%"PRId64"(Rows)] Stddev=[%.2f] \n\t " - "Rows=[%"PRIu64"], Blocks=[%"PRId64"], SmallBlocks=[%d], Size=[%.3f(Kb)] Comp=[%.5g]\n\t " - "RowsInMem=[%d] \n\t", - percentiles[0], percentiles[1], percentiles[2], percentiles[3], percentiles[4], percentiles[5], - percentiles[6], percentiles[7], percentiles[8], percentiles[9], percentiles[10], percentiles[11], - min, max, avg, stdDev, - totalRows, totalBlocks, smallBlocks, totalLen/1024.0, compRatio, - pTableBlockDist->numOfRowsInMemTable); + int32_t rowSize = pTableBlockDist->rowSize; + int32_t smallBlocks = pTableBlockDist->numOfSmallBlocks; + double compRatio = (totalRows > 0) ? ((double)(totalLen) / (rowSize * totalRows)) : 1; + int sz = sprintf(result + VARSTR_HEADER_SIZE, + "summary: \n\t " + "5th=[%d], 10th=[%d], 20th=[%d], 30th=[%d], 40th=[%d], 50th=[%d]\n\t " + "60th=[%d], 70th=[%d], 80th=[%d], 90th=[%d], 95th=[%d], 99th=[%d]\n\t " + "Min=[%" PRId64 "(Rows)] Max=[%" PRId64 "(Rows)] Avg=[%" PRId64 + "(Rows)] Stddev=[%.2f] \n\t " + "Rows=[%" PRIu64 "], Blocks=[%" PRId64 + "], SmallBlocks=[%d], Size=[%.3f(Kb)] Comp=[%.5g]\n\t " + "RowsInMem=[%d] \n\t", + percentiles[0], percentiles[1], percentiles[2], percentiles[3], percentiles[4], percentiles[5], + percentiles[6], percentiles[7], percentiles[8], percentiles[9], percentiles[10], percentiles[11], + min, max, avg, stdDev, totalRows, totalBlocks, smallBlocks, totalLen / 1024.0, compRatio, + pTableBlockDist->numOfRowsInMemTable); varDataSetLen(result, sz); UNUSED(sz); } -void blockinfo_func_finalizer(SQLFunctionCtx* pCtx) { +void blockinfo_func_finalizer(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - STableBlockDist* pDist = (STableBlockDist*) GET_ROWCELL_INTERBUF(pResInfo); + STableBlockDist *pDist = (STableBlockDist *)GET_ROWCELL_INTERBUF(pResInfo); pDist->rowSize = (uint16_t)pCtx->param[0].i64; generateBlockDistResult(pDist, pCtx->pOutput); @@ -4845,38 +4849,38 @@ void blockinfo_func_finalizer(SQLFunctionCtx* pCtx) { } // cannot set the numOfIteratedElems again since it is set during previous iteration - pResInfo->numOfRes = 1; + pResInfo->numOfRes = 1; pResInfo->hasResult = DATA_SET_FLAG; doFinalizer(pCtx); } ////////////////////////////////////////////////////////////////////////////////// -//cumulative_sum function +// cumulative_sum function -static bool csum_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool csum_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } - SCumSumInfo* pCumSumInfo = GET_ROWCELL_INTERBUF(pResInfo); + SCumSumInfo *pCumSumInfo = GET_ROWCELL_INTERBUF(pResInfo); pCumSumInfo->i64CumSum = 0; return true; } static void csum_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SCumSumInfo* pCumSumInfo = GET_ROWCELL_INTERBUF(pResInfo); + SCumSumInfo *pCumSumInfo = GET_ROWCELL_INTERBUF(pResInfo); int32_t notNullElems = 0; int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->order); - int32_t i = (pCtx->order == TSDB_ORDER_ASC) ? 0 : pCtx->size -1; + int32_t i = (pCtx->order == TSDB_ORDER_ASC) ? 0 : pCtx->size - 1; - TSKEY* pTimestamp = pCtx->ptsOutputBuf; - TSKEY* tsList = GET_TS_LIST(pCtx); + TSKEY *pTimestamp = pCtx->ptsOutputBuf; + TSKEY *tsList = GET_TS_LIST(pCtx); for (; i < pCtx->size && i >= 0; i += step) { - char* pData = GET_INPUT_DATA(pCtx, i); + char *pData = GET_INPUT_DATA(pCtx, i); if (pCtx->hasNull && isNull(pData, pCtx->inputType)) { qDebug("%p csum_function() index of null data:%d", pCtx, i); continue; @@ -4904,7 +4908,7 @@ static void csum_function(SQLFunctionCtx *pCtx) { uint64_t *retVal = (uint64_t *)pCtx->pOutput; *retVal = (uint64_t)(pCumSumInfo->u64CumSum); } else if (IS_FLOAT_TYPE(pCtx->inputType)) { - double *retVal = (double*) pCtx->pOutput; + double *retVal = (double *)pCtx->pOutput; SET_DOUBLE_VAL(retVal, pCumSumInfo->d64CumSum); } @@ -4917,7 +4921,7 @@ static void csum_function(SQLFunctionCtx *pCtx) { assert(pCtx->hasNull); } else { for (int t = 0; t < pCtx->tagInfo.numOfTagCols; ++t) { - SQLFunctionCtx* tagCtx = pCtx->tagInfo.pTagCtxList[t]; + SQLFunctionCtx *tagCtx = pCtx->tagInfo.pTagCtxList[t]; if (tagCtx->functionId == TSDB_FUNC_TAG_DUMMY) { aAggs[TSDB_FUNC_TAGPRJ].xFunction(tagCtx); } @@ -4930,34 +4934,34 @@ static void csum_function(SQLFunctionCtx *pCtx) { ////////////////////////////////////////////////////////////////////////////////// // Simple Moving_average function -static bool mavg_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool mavg_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } - SMovingAvgInfo* mavgInfo = GET_ROWCELL_INTERBUF(pResInfo); + SMovingAvgInfo *mavgInfo = GET_ROWCELL_INTERBUF(pResInfo); mavgInfo->pos = 0; mavgInfo->kPointsMeet = false; mavgInfo->sum = 0; mavgInfo->numPointsK = (int32_t)pCtx->param[0].i64; - mavgInfo->points = (double*)((char*)mavgInfo + sizeof(SMovingAvgInfo)); + mavgInfo->points = (double *)((char *)mavgInfo + sizeof(SMovingAvgInfo)); return true; } static void mavg_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SMovingAvgInfo* mavgInfo = GET_ROWCELL_INTERBUF(pResInfo); + SMovingAvgInfo *mavgInfo = GET_ROWCELL_INTERBUF(pResInfo); int32_t notNullElems = 0; int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->order); - int32_t i = (pCtx->order == TSDB_ORDER_ASC) ? 0 : pCtx->size -1; + int32_t i = (pCtx->order == TSDB_ORDER_ASC) ? 0 : pCtx->size - 1; - TSKEY* pTimestamp = pCtx->ptsOutputBuf; - char* pOutput = pCtx->pOutput; - TSKEY* tsList = GET_TS_LIST(pCtx); + TSKEY *pTimestamp = pCtx->ptsOutputBuf; + char *pOutput = pCtx->pOutput; + TSKEY *tsList = GET_TS_LIST(pCtx); for (; i < pCtx->size && i >= 0; i += step) { - char* pData = GET_INPUT_DATA(pCtx, i); + char *pData = GET_INPUT_DATA(pCtx, i); if (pCtx->hasNull && isNull(pData, pCtx->inputType)) { qDebug("%p mavg_function() index of null data:%d", pCtx, i); continue; @@ -4970,7 +4974,7 @@ static void mavg_function(SQLFunctionCtx *pCtx) { mavgInfo->points[mavgInfo->pos] = v; mavgInfo->sum += v; } else { - if (!mavgInfo->kPointsMeet && mavgInfo->pos == mavgInfo->numPointsK - 1){ + if (!mavgInfo->kPointsMeet && mavgInfo->pos == mavgInfo->numPointsK - 1) { mavgInfo->sum += v; mavgInfo->kPointsMeet = true; } else { @@ -4992,9 +4996,9 @@ static void mavg_function(SQLFunctionCtx *pCtx) { } } - { + { for (int t = 0; t < pCtx->tagInfo.numOfTagCols; ++t) { - SQLFunctionCtx* tagCtx = pCtx->tagInfo.pTagCtxList[t]; + SQLFunctionCtx *tagCtx = pCtx->tagInfo.pTagCtxList[t]; if (tagCtx->functionId == TSDB_FUNC_TAG_DUMMY) { aAggs[TSDB_FUNC_TAGPRJ].xFunction(tagCtx); } @@ -5007,20 +5011,21 @@ static void mavg_function(SQLFunctionCtx *pCtx) { ////////////////////////////////////////////////////////////////////////////////// // Sample function with reservoir sampling algorithm -static void assignResultSample(SQLFunctionCtx *pCtx, SSampleFuncInfo *pInfo, int32_t index, int64_t ts, void *pData, uint16_t type, int16_t bytes, char *inputTags) { - assignVal(pInfo->values + index*bytes, pData, bytes, type); +static void assignResultSample(SQLFunctionCtx *pCtx, SSampleFuncInfo *pInfo, int32_t index, int64_t ts, void *pData, + uint16_t type, int16_t bytes, char *inputTags) { + assignVal(pInfo->values + index * bytes, pData, bytes, type); *(pInfo->timeStamps + index) = ts; - SExtTagsInfo* pTagInfo = &pCtx->tagInfo; - int32_t posTag = 0; - char* tags = pInfo->taglists + index*pTagInfo->tagsLen; + SExtTagsInfo *pTagInfo = &pCtx->tagInfo; + int32_t posTag = 0; + char *tags = pInfo->taglists + index * pTagInfo->tagsLen; if (pCtx->currentStage == MERGE_STAGE) { assert(inputTags != NULL); memcpy(tags, inputTags, (size_t)pTagInfo->tagsLen); } else { assert(inputTags == NULL); for (int32_t i = 0; i < pTagInfo->numOfTagCols; ++i) { - SQLFunctionCtx* ctx = pTagInfo->pTagCtxList[i]; + SQLFunctionCtx *ctx = pTagInfo->pTagCtxList[i]; if (ctx->functionId == TSDB_FUNC_TS_DUMMY) { ctx->tag.nType = TSDB_DATA_TYPE_BIGINT; ctx->tag.i64 = ts; @@ -5032,7 +5037,8 @@ static void assignResultSample(SQLFunctionCtx *pCtx, SSampleFuncInfo *pInfo, int } } -static void do_reservoir_sample(SQLFunctionCtx *pCtx, SSampleFuncInfo *pInfo, int32_t samplesK, int64_t ts, void *pData, uint16_t type, int16_t bytes) { +static void do_reservoir_sample(SQLFunctionCtx *pCtx, SSampleFuncInfo *pInfo, int32_t samplesK, int64_t ts, void *pData, + uint16_t type, int16_t bytes) { pInfo->totalPoints++; if (pInfo->numSampled < samplesK) { assignResultSample(pCtx, pInfo, pInfo->numSampled, ts, pData, type, bytes, NULL); @@ -5047,19 +5053,19 @@ static void do_reservoir_sample(SQLFunctionCtx *pCtx, SSampleFuncInfo *pInfo, in static void copySampleFuncRes(SQLFunctionCtx *pCtx, int32_t type) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SSampleFuncInfo *pRes = GET_ROWCELL_INTERBUF(pResInfo); + SSampleFuncInfo *pRes = GET_ROWCELL_INTERBUF(pResInfo); - TSKEY* pTimestamp = pCtx->ptsOutputBuf; - char* pOutput = pCtx->pOutput; + TSKEY *pTimestamp = pCtx->ptsOutputBuf; + char *pOutput = pCtx->pOutput; for (int32_t i = 0; i < pRes->numSampled; ++i) { - assignVal(pOutput, pRes->values + i*pRes->colBytes, pRes->colBytes, type); + assignVal(pOutput, pRes->values + i * pRes->colBytes, pRes->colBytes, type); *pTimestamp = *(pRes->timeStamps + i); pOutput += pCtx->outputBytes; pTimestamp++; } if (pCtx->tagInfo.numOfTagCols == 0) { - return ; + return; } char **tagOutputs = calloc(pCtx->tagInfo.numOfTagCols, POINTER_BYTES); @@ -5070,7 +5076,8 @@ static void copySampleFuncRes(SQLFunctionCtx *pCtx, int32_t type) { for (int32_t i = 0; i < pRes->numSampled; ++i) { int16_t tagOffset = 0; for (int32_t j = 0; j < pCtx->tagInfo.numOfTagCols; ++j) { - memcpy(tagOutputs[j], pRes->taglists + i*pCtx->tagInfo.tagsLen + tagOffset, (size_t)pCtx->tagInfo.pTagCtxList[j]->outputBytes); + memcpy(tagOutputs[j], pRes->taglists + i * pCtx->tagInfo.tagsLen + tagOffset, + (size_t)pCtx->tagInfo.pTagCtxList[j]->outputBytes); tagOffset += pCtx->tagInfo.pTagCtxList[j]->outputBytes; tagOutputs[j] += pCtx->tagInfo.pTagCtxList[j]->outputBytes; } @@ -5079,7 +5086,7 @@ static void copySampleFuncRes(SQLFunctionCtx *pCtx, int32_t type) { tfree(tagOutputs); } -static bool sample_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool sample_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } @@ -5089,10 +5096,10 @@ static bool sample_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pRes SSampleFuncInfo *pRes = getOutputInfo(pCtx); pRes->totalPoints = 0; pRes->numSampled = 0; - pRes->values = ((char*)pRes + sizeof(SSampleFuncInfo)); + pRes->values = ((char *)pRes + sizeof(SSampleFuncInfo)); pRes->colBytes = (pCtx->currentStage != MERGE_STAGE) ? pCtx->inputBytes : pCtx->outputBytes; pRes->timeStamps = (int64_t *)((char *)pRes->values + pRes->colBytes * pCtx->param[0].i64); - pRes->taglists = (char*)pRes->timeStamps + sizeof(int64_t) * pCtx->param[0].i64; + pRes->taglists = (char *)pRes->timeStamps + sizeof(int64_t) * pCtx->param[0].i64; return true; } @@ -5100,12 +5107,12 @@ static void sample_function(SQLFunctionCtx *pCtx) { int32_t notNullElems = 0; SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SSampleFuncInfo *pRes = getOutputInfo(pCtx); + SSampleFuncInfo *pRes = getOutputInfo(pCtx); - if (pRes->values != ((char*)pRes + sizeof(SSampleFuncInfo))) { - pRes->values = ((char*)pRes + sizeof(SSampleFuncInfo)); - pRes->timeStamps = (int64_t*)((char*)pRes->values + pRes->colBytes * pCtx->param[0].i64); - pRes->taglists = (char*)pRes->timeStamps + sizeof(int64_t) * pCtx->param[0].i64; + if (pRes->values != ((char *)pRes + sizeof(SSampleFuncInfo))) { + pRes->values = ((char *)pRes + sizeof(SSampleFuncInfo)); + pRes->timeStamps = (int64_t *)((char *)pRes->values + pRes->colBytes * pCtx->param[0].i64); + pRes->taglists = (char *)pRes->timeStamps + sizeof(int64_t) * pCtx->param[0].i64; } for (int32_t i = 0; i < pCtx->size; ++i) { @@ -5116,7 +5123,7 @@ static void sample_function(SQLFunctionCtx *pCtx) { notNullElems++; - TSKEY ts = (pCtx->ptsList != NULL)? GET_TS_DATA(pCtx, i):0; + TSKEY ts = (pCtx->ptsList != NULL) ? GET_TS_DATA(pCtx, i) : 0; do_reservoir_sample(pCtx, pRes, (int32_t)pCtx->param[0].i64, ts, data, pCtx->inputType, pRes->colBytes); } @@ -5133,18 +5140,17 @@ static void sample_function(SQLFunctionCtx *pCtx) { } static void sample_func_merge(SQLFunctionCtx *pCtx) { - SSampleFuncInfo* pInput = (SSampleFuncInfo*)GET_INPUT_DATA_LIST(pCtx); - pInput->values = ((char*)pInput + sizeof(SSampleFuncInfo)); - pInput->timeStamps = (int64_t*)((char*)pInput->values + pInput->colBytes * pCtx->param[0].i64); - pInput->taglists = (char*)pInput->timeStamps + sizeof(int64_t)*pCtx->param[0].i64; + SSampleFuncInfo *pInput = (SSampleFuncInfo *)GET_INPUT_DATA_LIST(pCtx); + pInput->values = ((char *)pInput + sizeof(SSampleFuncInfo)); + pInput->timeStamps = (int64_t *)((char *)pInput->values + pInput->colBytes * pCtx->param[0].i64); + pInput->taglists = (char *)pInput->timeStamps + sizeof(int64_t) * pCtx->param[0].i64; SSampleFuncInfo *pOutput = getOutputInfo(pCtx); pOutput->totalPoints = pInput->totalPoints; pOutput->numSampled = pInput->numSampled; for (int32_t i = 0; i < pInput->numSampled; ++i) { - assignResultSample(pCtx, pOutput, i, pInput->timeStamps[i], - pInput->values + i * pInput->colBytes, pCtx->outputType, pInput->colBytes, - pInput->taglists + i*pCtx->tagInfo.tagsLen); + assignResultSample(pCtx, pOutput, i, pInput->timeStamps[i], pInput->values + i * pInput->colBytes, pCtx->outputType, + pInput->colBytes, pInput->taglists + i * pCtx->tagInfo.tagsLen); } SET_VAL(pCtx, pInput->numSampled, pOutput->numSampled); @@ -5156,7 +5162,7 @@ static void sample_func_merge(SQLFunctionCtx *pCtx) { static void sample_func_finalizer(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SSampleFuncInfo *pRes = GET_ROWCELL_INTERBUF(pResInfo); + SSampleFuncInfo *pRes = GET_ROWCELL_INTERBUF(pResInfo); if (pRes->numSampled == 0) { // no result assert(pResInfo->hasResult != DATA_SET_FLAG); @@ -5172,7 +5178,7 @@ static void sample_func_finalizer(SQLFunctionCtx *pCtx) { ////////////////////////////////////////////////////////////////////////////////// // elapsed function -static bool elapsedSetup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool elapsedSetup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } @@ -5185,16 +5191,14 @@ static bool elapsedSetup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { return true; } -static int32_t elapsedRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { - return BLK_DATA_NO_NEEDED; -} +static int32_t elapsedRequired(SQLFunctionCtx *pCtx, STimeWindow *w, int32_t colId) { return BLK_DATA_NO_NEEDED; } static void elapsedFunction(SQLFunctionCtx *pCtx) { SElapsedInfo *pInfo = getOutputInfo(pCtx); if (pCtx->preAggVals.isSet) { if (pInfo->min == MAX_TS_KEY) { - pInfo->min = pCtx->preAggVals.statis.min; - pInfo->max = pCtx->preAggVals.statis.max; + pInfo->min = pCtx->preAggVals.statis.min < pCtx->startTs ? pCtx->startTs : pCtx->preAggVals.statis.min; + pInfo->max = pCtx->preAggVals.statis.max > pCtx->endTs ? pCtx->endTs + 1 : pCtx->preAggVals.statis.max; } else { if (pCtx->order == TSDB_ORDER_ASC) { pInfo->max = pCtx->preAggVals.statis.max; @@ -5219,7 +5223,8 @@ static void elapsedFunction(SQLFunctionCtx *pCtx) { int64_t *ptsList = (int64_t *)GET_INPUT_DATA_LIST(pCtx); // pCtx->start.key == INT64_MIN mean this is first window or there is actual start point of current window. - // pCtx->end.key == INT64_MIN mean current window does not end in current data block or there is actual end point of current window. + // pCtx->end.key == INT64_MIN mean current window does not end in current data block or there is actual end point of + // current window. if (pCtx->order == TSDB_ORDER_DESC) { if (pCtx->start.key == INT64_MIN) { pInfo->max = (pInfo->max < ptsList[pCtx->size - 1]) ? ptsList[pCtx->size - 1] : pInfo->max; @@ -5249,7 +5254,7 @@ static void elapsedFunction(SQLFunctionCtx *pCtx) { elapsedOver: SET_VAL(pCtx, pCtx->size, 1); - + if (pCtx->size > 0) { GET_RES_INFO(pCtx)->hasResult = DATA_SET_FLAG; pInfo->hasResult = DATA_SET_FLAG; @@ -5269,7 +5274,7 @@ static void elapsedFinalizer(SQLFunctionCtx *pCtx) { } SElapsedInfo *pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); - double result = (double)pInfo->max - (double)pInfo->min; + double result = (double)pInfo->max - (double)pInfo->min; *(double *)pCtx->pOutput = result >= 0 ? result : -result; if (pCtx->numOfParams > 0 && pCtx->param[0].i64 > 0) { *(double *)pCtx->pOutput = *(double *)pCtx->pOutput / pCtx->param[0].i64; @@ -5280,7 +5285,7 @@ static void elapsedFinalizer(SQLFunctionCtx *pCtx) { } ////////////////////////////////////////////////////////////////////////////////// -static bool histogram_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool histogram_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } @@ -5291,11 +5296,11 @@ static bool histogram_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* p } int32_t numOfBins = (int32_t)pCtx->param[0].i64; - double* listBin = (double*) pCtx->param[1].pz; + double *listBin = (double *)pCtx->param[1].pz; int32_t normalized = (int32_t)pCtx->param[2].i64; pRes->numOfBins = numOfBins; pRes->normalized = normalized; - pRes->orderedBins = (SHistogramFuncBin*)((char*)pRes + sizeof(SHistogramFuncInfo)); + pRes->orderedBins = (SHistogramFuncBin *)((char *)pRes + sizeof(SHistogramFuncInfo)); for (int32_t i = 0; i < numOfBins; ++i) { double lower = listBin[i] < listBin[i + 1] ? listBin[i] : listBin[i + 1]; double upper = listBin[i + 1] > listBin[i] ? listBin[i + 1] : listBin[i]; @@ -5307,12 +5312,12 @@ static bool histogram_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* p } static void histogram_function(SQLFunctionCtx *pCtx) { - SResultRowCellInfo* pResInfo = GET_RES_INFO(pCtx); + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SHistogramFuncInfo* pRes = getOutputInfo(pCtx); + SHistogramFuncInfo *pRes = getOutputInfo(pCtx); - if (pRes->orderedBins != (SHistogramFuncBin*)((char*)pRes + sizeof(SHistogramFuncInfo))) { - pRes->orderedBins = (SHistogramFuncBin*)((char*)pRes + sizeof(SHistogramFuncInfo)); + if (pRes->orderedBins != (SHistogramFuncBin *)((char *)pRes + sizeof(SHistogramFuncInfo))) { + pRes->orderedBins = (SHistogramFuncBin *)((char *)pRes + sizeof(SHistogramFuncInfo)); } int32_t notNullElems = 0; @@ -5354,10 +5359,10 @@ static void histogram_function(SQLFunctionCtx *pCtx) { } static void histogram_func_merge(SQLFunctionCtx *pCtx) { - SHistogramFuncInfo* pInput = (SHistogramFuncInfo*) GET_INPUT_DATA_LIST(pCtx); - pInput->orderedBins = (SHistogramFuncBin*)((char*)pInput + sizeof(SHistogramFuncInfo)); + SHistogramFuncInfo *pInput = (SHistogramFuncInfo *)GET_INPUT_DATA_LIST(pCtx); + pInput->orderedBins = (SHistogramFuncBin *)((char *)pInput + sizeof(SHistogramFuncInfo)); - SHistogramFuncInfo* pRes = getOutputInfo(pCtx); + SHistogramFuncInfo *pRes = getOutputInfo(pCtx); for (int32_t i = 0; i < pInput->numOfBins; ++i) { pRes->orderedBins[i].count += pInput->orderedBins[i].count; } @@ -5378,7 +5383,7 @@ static void histogram_func_finalizer(SQLFunctionCtx *pCtx) { int sz; if (!pRes->normalized) { int64_t count = (int64_t)pRes->orderedBins[i].count; - sz = sprintf(pCtx->pOutput + VARSTR_HEADER_SIZE, "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%"PRId64"}", + sz = sprintf(pCtx->pOutput + VARSTR_HEADER_SIZE, "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%" PRId64 "}", pRes->orderedBins[i].lower, pRes->orderedBins[i].upper, count); } else { sz = sprintf(pCtx->pOutput + VARSTR_HEADER_SIZE, "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%lf}", @@ -5396,13 +5401,13 @@ static void histogram_func_finalizer(SQLFunctionCtx *pCtx) { // unique&tail copy static void copyRes(SQLFunctionCtx *pCtx, void *data, int32_t bytes) { - size_t size = sizeof(int64_t) + bytes + pCtx->tagInfo.tagsLen; + size_t size = sizeof(int64_t) + bytes + pCtx->tagInfo.tagsLen; int32_t len = (int32_t)(GET_RES_INFO(pCtx)->numOfRes); - char *tsOutput = pCtx->ptsOutputBuf; - char *output = pCtx->pOutput; + char *tsOutput = pCtx->ptsOutputBuf; + char *output = pCtx->pOutput; int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->param[3].i64); - char *tvp = (char*)data + (size * ((pCtx->param[3].i64 == TSDB_ORDER_ASC) ? 0 : len -1)); + char *tvp = (char *)data + (size * ((pCtx->param[3].i64 == TSDB_ORDER_ASC) ? 0 : len - 1)); for (int32_t i = 0; i < len; ++i) { memcpy(tsOutput, tvp, sizeof(int64_t)); memcpy(output, tvp + sizeof(int64_t), bytes); @@ -5414,7 +5419,7 @@ static void copyRes(SQLFunctionCtx *pCtx, void *data, int32_t bytes) { // set the corresponding tag data for each record // todo check malloc failure if (pCtx->tagInfo.numOfTagCols == 0) { - return ; + return; } char **pData = calloc(pCtx->tagInfo.numOfTagCols, POINTER_BYTES); @@ -5422,7 +5427,7 @@ static void copyRes(SQLFunctionCtx *pCtx, void *data, int32_t bytes) { pData[i] = pCtx->tagInfo.pTagCtxList[i]->pOutput; } - tvp = (char*)data + (size * ((pCtx->param[3].i64 == TSDB_ORDER_ASC) ? 0 : len -1)); + tvp = (char *)data + (size * ((pCtx->param[3].i64 == TSDB_ORDER_ASC) ? 0 : len - 1)); for (int32_t i = 0; i < len; ++i) { int32_t offset = (int32_t)sizeof(int64_t) + bytes; for (int32_t j = 0; j < pCtx->tagInfo.numOfTagCols; ++j) { @@ -5436,36 +5441,38 @@ static void copyRes(SQLFunctionCtx *pCtx, void *data, int32_t bytes) { tfree(pData); } -static bool unique_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool unique_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } - if(*pCtx->pUniqueSet != NULL){ + if (*pCtx->pUniqueSet != NULL) { taosHashClear(*pCtx->pUniqueSet); - }else{ + } else { *pCtx->pUniqueSet = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); } return true; } -static void do_unique_function(SQLFunctionCtx *pCtx, SUniqueFuncInfo *pInfo, TSKEY timestamp, char *pData, char *tag, int32_t bytes, int16_t type){ +static void do_unique_function(SQLFunctionCtx *pCtx, SUniqueFuncInfo *pInfo, TSKEY timestamp, char *pData, char *tag, + int32_t bytes, int16_t type) { int32_t hashKeyBytes = bytes; - if(IS_VAR_DATA_TYPE(type)){ // for var data, we can not use bytes, because there are dirty data in the back of var data + if (IS_VAR_DATA_TYPE( + type)) { // for var data, we can not use bytes, because there are dirty data in the back of var data hashKeyBytes = varDataTLen(pData); } UniqueUnit **unique = taosHashGet(*pCtx->pUniqueSet, pData, hashKeyBytes); if (unique == NULL) { size_t size = sizeof(UniqueUnit) + bytes + pCtx->tagInfo.tagsLen; - char *tmp = pInfo->res + pInfo->num * size; - ((UniqueUnit*)tmp)->timestamp = timestamp; + char *tmp = pInfo->res + pInfo->num * size; + ((UniqueUnit *)tmp)->timestamp = timestamp; char *data = tmp + sizeof(UniqueUnit); char *tags = tmp + sizeof(UniqueUnit) + bytes; memcpy(data, pData, bytes); if (pCtx->currentStage == MERGE_STAGE && tag != NULL) { memcpy(tags, tag, (size_t)pCtx->tagInfo.tagsLen); - }else{ + } else { int32_t offset = 0; for (int32_t j = 0; j < pCtx->tagInfo.numOfTagCols; ++j) { SQLFunctionCtx *tagCtx = pCtx->tagInfo.pTagCtxList[j]; @@ -5480,9 +5487,9 @@ static void do_unique_function(SQLFunctionCtx *pCtx, SUniqueFuncInfo *pInfo, TSK } } - taosHashPut(*pCtx->pUniqueSet, pData, hashKeyBytes, &tmp, sizeof(UniqueUnit*)); + taosHashPut(*pCtx->pUniqueSet, pData, hashKeyBytes, &tmp, sizeof(UniqueUnit *)); pInfo->num++; - }else if((*unique)->timestamp > timestamp){ + } else if ((*unique)->timestamp > timestamp) { (*unique)->timestamp = timestamp; } } @@ -5498,9 +5505,10 @@ static void unique_function(SQLFunctionCtx *pCtx) { } do_unique_function(pCtx, pInfo, k, pData, NULL, pCtx->inputBytes, pCtx->inputType); - if (sizeof(SUniqueFuncInfo) + pInfo->num * (sizeof(UniqueUnit) + pCtx->inputBytes + pCtx->tagInfo.tagsLen) >= MAX_UNIQUE_RESULT_SIZE - || (pInfo->num > pCtx->param[0].i64)){ - GET_RES_INFO(pCtx)->numOfRes = -1; // mark out of memory + if (sizeof(SUniqueFuncInfo) + pInfo->num * (sizeof(UniqueUnit) + pCtx->inputBytes + pCtx->tagInfo.tagsLen) >= + MAX_UNIQUE_RESULT_SIZE || + (pInfo->num > pCtx->param[0].i64)) { + GET_RES_INFO(pCtx)->numOfRes = -1; // mark out of memory return; } } @@ -5511,32 +5519,33 @@ static void unique_function(SQLFunctionCtx *pCtx) { static void unique_function_merge(SQLFunctionCtx *pCtx) { SUniqueFuncInfo *pInput = (SUniqueFuncInfo *)GET_INPUT_DATA_LIST(pCtx); SUniqueFuncInfo *pOutput = getOutputInfo(pCtx); - size_t size = sizeof(UniqueUnit) + pCtx->outputBytes + pCtx->tagInfo.tagsLen; + size_t size = sizeof(UniqueUnit) + pCtx->outputBytes + pCtx->tagInfo.tagsLen; for (int32_t i = 0; i < pInput->num; ++i) { - char *tmp = pInput->res + i* size; - TSKEY timestamp = ((UniqueUnit*)tmp)->timestamp; + char *tmp = pInput->res + i * size; + TSKEY timestamp = ((UniqueUnit *)tmp)->timestamp; char *data = tmp + sizeof(UniqueUnit); char *tags = tmp + sizeof(UniqueUnit) + pCtx->outputBytes; do_unique_function(pCtx, pOutput, timestamp, data, tags, pCtx->outputBytes, pCtx->outputType); - if (sizeof(SUniqueFuncInfo) + pOutput->num * (sizeof(UniqueUnit) + pCtx->outputBytes + pCtx->tagInfo.tagsLen) >= MAX_UNIQUE_RESULT_SIZE - || (pOutput->num > pCtx->param[0].i64)){ - GET_RES_INFO(pCtx)->numOfRes = -1; // mark out of memory + if (sizeof(SUniqueFuncInfo) + pOutput->num * (sizeof(UniqueUnit) + pCtx->outputBytes + pCtx->tagInfo.tagsLen) >= + MAX_UNIQUE_RESULT_SIZE || + (pOutput->num > pCtx->param[0].i64)) { + GET_RES_INFO(pCtx)->numOfRes = -1; // mark out of memory return; } } -// GET_RES_INFO(pCtx)->numOfRes = pOutput->num; + // GET_RES_INFO(pCtx)->numOfRes = pOutput->num; } -typedef struct{ - int32_t dataOffset; +typedef struct { + int32_t dataOffset; __compar_fn_t comparFn; } SortSupporter; static int32_t sortCompareFn(const void *p1, const void *p2, const void *param) { SortSupporter *support = (SortSupporter *)param; - return support->comparFn((const char*)p1 + support->dataOffset, (const char*)p2 + support->dataOffset); + return support->comparFn((const char *)p1 + support->dataOffset, (const char *)p2 + support->dataOffset); } static void unique_func_finalizer(SQLFunctionCtx *pCtx) { @@ -5558,7 +5567,7 @@ static void unique_func_finalizer(SQLFunctionCtx *pCtx) { if (pCtx->param[2].i64 == PRIMARYKEY_TIMESTAMP_COL_INDEX || pCtx->param[2].i64 == TSDB_RES_COL_ID) { support.dataOffset = 0; support.comparFn = compareInt64Val; - } else{ + } else { support.dataOffset = sizeof(int64_t); support.comparFn = getComparFunc(type, 0); } @@ -5569,35 +5578,37 @@ static void unique_func_finalizer(SQLFunctionCtx *pCtx) { doFinalizer(pCtx); } -static bool mode_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool mode_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } - if(*pCtx->pModeSet != NULL){ + if (*pCtx->pModeSet != NULL) { taosHashClear(*pCtx->pModeSet); - }else{ + } else { *pCtx->pModeSet = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); } return true; } -static void do_mode_function(SQLFunctionCtx *pCtx, SModeFuncInfo *pInfo, char *pData, int64_t count, int32_t bytes, int16_t type){ +static void do_mode_function(SQLFunctionCtx *pCtx, SModeFuncInfo *pInfo, char *pData, int64_t count, int32_t bytes, + int16_t type) { int32_t hashKeyBytes = bytes; - if(IS_VAR_DATA_TYPE(type)){ // for var data, we can not use bytes, because there are dirty data in the back of var data + if (IS_VAR_DATA_TYPE( + type)) { // for var data, we can not use bytes, because there are dirty data in the back of var data hashKeyBytes = varDataTLen(pData); } ModeUnit **mode = taosHashGet(*pCtx->pModeSet, pData, hashKeyBytes); if (mode == NULL) { size_t size = sizeof(ModeUnit) + bytes; - char *tmp = pInfo->res + pInfo->num * size; - ((ModeUnit*)tmp)->count = count; + char *tmp = pInfo->res + pInfo->num * size; + ((ModeUnit *)tmp)->count = count; char *data = tmp + sizeof(ModeUnit); memcpy(data, pData, bytes); - taosHashPut(*pCtx->pModeSet, pData, hashKeyBytes, &tmp, sizeof(ModeUnit*)); + taosHashPut(*pCtx->pModeSet, pData, hashKeyBytes, &tmp, sizeof(ModeUnit *)); pInfo->num++; - }else{ + } else { (*mode)->count += count; } } @@ -5613,8 +5624,8 @@ static void mode_function(SQLFunctionCtx *pCtx) { do_mode_function(pCtx, pInfo, pData, 1, pCtx->inputBytes, pCtx->inputType); - if (sizeof(SModeFuncInfo) + pInfo->num * (sizeof(ModeUnit) + pCtx->inputBytes) >= MAX_MODE_INNER_RESULT_SIZE){ - GET_RES_INFO(pCtx)->numOfRes = -1; // mark out of memory + if (sizeof(SModeFuncInfo) + pInfo->num * (sizeof(ModeUnit) + pCtx->inputBytes) >= MAX_MODE_INNER_RESULT_SIZE) { + GET_RES_INFO(pCtx)->numOfRes = -1; // mark out of memory return; } } @@ -5624,14 +5635,14 @@ static void mode_function(SQLFunctionCtx *pCtx) { static void mode_function_merge(SQLFunctionCtx *pCtx) { SModeFuncInfo *pInput = (SModeFuncInfo *)GET_INPUT_DATA_LIST(pCtx); SModeFuncInfo *pOutput = getOutputInfo(pCtx); - size_t size = sizeof(ModeUnit) + pCtx->outputBytes; + size_t size = sizeof(ModeUnit) + pCtx->outputBytes; for (int32_t i = 0; i < pInput->num; ++i) { - char *tmp = pInput->res + i* size; + char *tmp = pInput->res + i * size; char *data = tmp + sizeof(ModeUnit); - do_mode_function(pCtx, pOutput, data, ((ModeUnit*)tmp)->count, pCtx->outputBytes, pCtx->outputType); + do_mode_function(pCtx, pOutput, data, ((ModeUnit *)tmp)->count, pCtx->outputBytes, pCtx->outputType); - if (sizeof(SModeFuncInfo) + pOutput->num * (sizeof(ModeUnit) + pCtx->outputBytes) >= MAX_MODE_INNER_RESULT_SIZE){ - GET_RES_INFO(pCtx)->numOfRes = -1; // mark out of memory + if (sizeof(SModeFuncInfo) + pOutput->num * (sizeof(ModeUnit) + pCtx->outputBytes) >= MAX_MODE_INNER_RESULT_SIZE) { + GET_RES_INFO(pCtx)->numOfRes = -1; // mark out of memory return; } } @@ -5650,27 +5661,27 @@ static void mode_func_finalizer(SQLFunctionCtx *pCtx) { } SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SModeFuncInfo *pRes = GET_ROWCELL_INTERBUF(pResInfo); + SModeFuncInfo *pRes = GET_ROWCELL_INTERBUF(pResInfo); size_t size = sizeof(ModeUnit) + bytes; - char *tvp = pRes->res; - char *result = NULL; + char *tvp = pRes->res; + char *result = NULL; int64_t maxCount = 0; for (int32_t i = 0; i < pRes->num; ++i) { - int64_t count = ((ModeUnit*)tvp)->count; - if (count > maxCount){ + int64_t count = ((ModeUnit *)tvp)->count; + if (count > maxCount) { maxCount = count; result = tvp; - }else if(count == maxCount){ + } else if (count == maxCount) { result = NULL; } tvp += size; } - if (result){ + if (result) { memcpy(pCtx->pOutput, result + sizeof(ModeUnit), bytes); - }else{ + } else { setNull(pCtx->pOutput, type, 0); } pResInfo->numOfRes = 1; @@ -5679,7 +5690,7 @@ static void mode_func_finalizer(SQLFunctionCtx *pCtx) { static void buildTailStruct(STailInfo *pTailInfo, SQLFunctionCtx *pCtx) { char *tmp = (char *)pTailInfo + sizeof(STailInfo); - pTailInfo->res = (TailUnit**) tmp; + pTailInfo->res = (TailUnit **)tmp; tmp += POINTER_BYTES * pCtx->param[0].i64; int32_t bytes = 0; @@ -5691,13 +5702,13 @@ static void buildTailStruct(STailInfo *pTailInfo, SQLFunctionCtx *pCtx) { size_t size = sizeof(TailUnit) + bytes + pCtx->tagInfo.tagsLen; for (int32_t i = 0; i < pCtx->param[0].i64; ++i) { - pTailInfo->res[i] = (TailUnit*) tmp; + pTailInfo->res[i] = (TailUnit *)tmp; tmp += size; } } -static void valueTailAssign(TailUnit *dst, int32_t bytes, const char *val, int64_t tsKey, - SExtTagsInfo *pTagInfo, char *pTags, int16_t stage) { +static void valueTailAssign(TailUnit *dst, int32_t bytes, const char *val, int64_t tsKey, SExtTagsInfo *pTagInfo, + char *pTags, int16_t stage) { dst->timestamp = tsKey; memcpy(dst->data, val, bytes); @@ -5706,7 +5717,7 @@ static void valueTailAssign(TailUnit *dst, int32_t bytes, const char *val, int64 } else { // the tags are dumped from the ctx tag fields int32_t size = 0; for (int32_t i = 0; i < pTagInfo->numOfTagCols; ++i) { - SQLFunctionCtx* ctx = pTagInfo->pTagCtxList[i]; + SQLFunctionCtx *ctx = pTagInfo->pTagCtxList[i]; if (ctx->functionId == TSDB_FUNC_TS_DUMMY) { ctx->tag.nType = TSDB_DATA_TYPE_BIGINT; ctx->tag.i64 = tsKey; @@ -5720,38 +5731,37 @@ static void valueTailAssign(TailUnit *dst, int32_t bytes, const char *val, int64 } static int32_t tailComparFn(const void *p1, const void *p2, const void *param) { - TailUnit *d1 = *(TailUnit **) p1; - TailUnit *d2 = *(TailUnit **) p2; + TailUnit *d1 = *(TailUnit **)p1; + TailUnit *d2 = *(TailUnit **)p2; return compareInt64Val(d1, d2); } -static void tailSwapFn(void *dst, void *src, const void *param) -{ - TailUnit **vdst = (TailUnit **) dst; - TailUnit **vsrc = (TailUnit **) src; +static void tailSwapFn(void *dst, void *src, const void *param) { + TailUnit **vdst = (TailUnit **)dst; + TailUnit **vsrc = (TailUnit **)src; - TailUnit *tmp = *vdst; + TailUnit *tmp = *vdst; *vdst = *vsrc; *vsrc = tmp; } static void do_tail_function_add(STailInfo *pInfo, int32_t maxLen, void *pData, int64_t ts, int32_t bytes, - SExtTagsInfo *pTagInfo, char *pTags, int16_t stage) { + SExtTagsInfo *pTagInfo, char *pTags, int16_t stage) { TailUnit **pList = pInfo->res; if (pInfo->num < maxLen) { valueTailAssign(pList[pInfo->num], bytes, pData, ts, pTagInfo, pTags, stage); - taosheapsort((void *) pList, sizeof(TailUnit **), pInfo->num + 1, NULL, tailComparFn, NULL, tailSwapFn, 0); + taosheapsort((void *)pList, sizeof(TailUnit **), pInfo->num + 1, NULL, tailComparFn, NULL, tailSwapFn, 0); pInfo->num++; - } else if(pList[0]->timestamp < ts) { + } else if (pList[0]->timestamp < ts) { valueTailAssign(pList[0], bytes, pData, ts, pTagInfo, pTags, stage); - taosheapadjust((void *) pList, sizeof(TailUnit **), 0, maxLen - 1, NULL, tailComparFn, NULL, tailSwapFn, 0); + taosheapadjust((void *)pList, sizeof(TailUnit **), 0, maxLen - 1, NULL, tailComparFn, NULL, tailSwapFn, 0); } } -static bool tail_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResInfo) { +static bool tail_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo *pResInfo) { if (!function_setup(pCtx, pResInfo)) { return false; } @@ -5764,36 +5774,36 @@ static bool tail_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResIn static void tail_function(SQLFunctionCtx *pCtx) { STailInfo *pRes = getOutputInfo(pCtx); -// if (pCtx->stableQuery){ - for (int32_t i = 0; i < pCtx->size; ++i) { - char *data = GET_INPUT_DATA(pCtx, i); + // if (pCtx->stableQuery){ + for (int32_t i = 0; i < pCtx->size; ++i) { + char *data = GET_INPUT_DATA(pCtx, i); - TSKEY ts = (pCtx->ptsList != NULL)? GET_TS_DATA(pCtx, i):0; - do_tail_function_add(pRes, (int32_t)pCtx->param[0].i64, data, ts, - pCtx->inputBytes, &pCtx->tagInfo, NULL, pCtx->currentStage); - } -// }else{ -// for (int32_t i = pCtx->size - 1; i >= 0; --i) { -// if (pRes->offset++ < (int32_t)pCtx->param[1].i64){ -// continue; -// } -// if (pRes->num >= (int32_t)(pCtx->param[0].i64 - pCtx->param[1].i64)){ // query complete -// pCtx->resultInfo->complete = true; -// for (int32_t j = 0; j < pCtx->tagInfo.numOfTagCols; ++j) { -// SQLFunctionCtx *ctx = pCtx->tagInfo.pTagCtxList[j]; -// ctx->resultInfo->complete = true; -// } -// break; -// } -// char *data = GET_INPUT_DATA(pCtx, i); -// -// TSKEY ts = (pCtx->ptsList != NULL)? GET_TS_DATA(pCtx, i):0; -// -// valueTailAssign(pRes->res[pRes->num], pCtx->inputBytes, data, ts, &pCtx->tagInfo, NULL, pCtx->currentStage); -// -// pRes->num++; -// } -// } + TSKEY ts = (pCtx->ptsList != NULL) ? GET_TS_DATA(pCtx, i) : 0; + do_tail_function_add(pRes, (int32_t)pCtx->param[0].i64, data, ts, pCtx->inputBytes, &pCtx->tagInfo, NULL, + pCtx->currentStage); + } + // }else{ + // for (int32_t i = pCtx->size - 1; i >= 0; --i) { + // if (pRes->offset++ < (int32_t)pCtx->param[1].i64){ + // continue; + // } + // if (pRes->num >= (int32_t)(pCtx->param[0].i64 - pCtx->param[1].i64)){ // query complete + // pCtx->resultInfo->complete = true; + // for (int32_t j = 0; j < pCtx->tagInfo.numOfTagCols; ++j) { + // SQLFunctionCtx *ctx = pCtx->tagInfo.pTagCtxList[j]; + // ctx->resultInfo->complete = true; + // } + // break; + // } + // char *data = GET_INPUT_DATA(pCtx, i); + // + // TSKEY ts = (pCtx->ptsList != NULL)? GET_TS_DATA(pCtx, i):0; + // + // valueTailAssign(pRes->res[pRes->num], pCtx->inputBytes, data, ts, &pCtx->tagInfo, NULL, pCtx->currentStage); + // + // pRes->num++; + // } + // } // treat the result as only one result GET_RES_INFO(pCtx)->numOfRes = 1; @@ -5810,7 +5820,8 @@ static void tail_func_merge(SQLFunctionCtx *pCtx) { // the intermediate result is binary, we only use the output data type for (int32_t i = 0; i < pInput->num; ++i) { do_tail_function_add(pOutput, (int32_t)pCtx->param[0].i64, pInput->res[i]->data, pInput->res[i]->timestamp, - pCtx->outputBytes, &pCtx->tagInfo, pInput->res[i]->data + pCtx->outputBytes, pCtx->currentStage); + pCtx->outputBytes, &pCtx->tagInfo, pInput->res[i]->data + pCtx->outputBytes, + pCtx->currentStage); } GET_RES_INFO(pCtx)->numOfRes = pOutput->num; @@ -5833,11 +5844,11 @@ static void tail_func_finalizer(SQLFunctionCtx *pCtx) { type = pCtx->inputType; } -// if(pCtx->stableQuery){ - GET_RES_INFO(pCtx)->numOfRes = pRes->num - (int32_t)pCtx->param[1].i64; -// }else{ -// GET_RES_INFO(pCtx)->numOfRes = pRes->num; -// } + // if(pCtx->stableQuery){ + GET_RES_INFO(pCtx)->numOfRes = pRes->num - (int32_t)pCtx->param[1].i64; + // }else{ + // GET_RES_INFO(pCtx)->numOfRes = pRes->num; + // } if (GET_RES_INFO(pCtx)->numOfRes <= 0) { doFinalizer(pCtx); return; @@ -5846,14 +5857,14 @@ static void tail_func_finalizer(SQLFunctionCtx *pCtx) { taosqsort(pRes->res, pRes->num, POINTER_BYTES, NULL, tailComparFn); size_t size = sizeof(int64_t) + bytes + pCtx->tagInfo.tagsLen; - void *data = calloc(size, GET_RES_INFO(pCtx)->numOfRes); - if(!data){ + void *data = calloc(size, GET_RES_INFO(pCtx)->numOfRes); + if (!data) { qError("calloc error in tail_func_finalizer: size:%d, num:%d", (int32_t)size, GET_RES_INFO(pCtx)->numOfRes); doFinalizer(pCtx); return; } - for(int32_t i = 0; i < GET_RES_INFO(pCtx)->numOfRes; i++){ - memcpy((char*)data + i * size, pRes->res[i], size); + for (int32_t i = 0; i < GET_RES_INFO(pCtx)->numOfRes; i++) { + memcpy((char *)data + i * size, pRes->res[i], size); } SortSupporter support = {0}; @@ -5869,29 +5880,28 @@ static void tail_func_finalizer(SQLFunctionCtx *pCtx) { doFinalizer(pCtx); } - static void state_count_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SStateInfo *pStateInfo = GET_ROWCELL_INTERBUF(pResInfo); + SStateInfo *pStateInfo = GET_ROWCELL_INTERBUF(pResInfo); - char *data = GET_INPUT_DATA_LIST(pCtx); + char *data = GET_INPUT_DATA_LIST(pCtx); int64_t *pOutput = (int64_t *)pCtx->pOutput; - for (int32_t i = 0; i < pCtx->size; i++,pOutput++,data += pCtx->inputBytes) { + for (int32_t i = 0; i < pCtx->size; i++, pOutput++, data += pCtx->inputBytes) { if (pCtx->hasNull && isNull(data, pCtx->inputType)) { setNull(pOutput, TSDB_DATA_TYPE_BIGINT, 0); continue; } - if (isStateOperTrue(data, pCtx->inputType, &pCtx->param[0], &pCtx->param[1])){ + if (isStateOperTrue(data, pCtx->inputType, &pCtx->param[0], &pCtx->param[1])) { *pOutput = ++pStateInfo->countPrev; - }else{ + } else { *pOutput = -1; pStateInfo->countPrev = 0; } } for (int t = 0; t < pCtx->tagInfo.numOfTagCols; ++t) { - SQLFunctionCtx* tagCtx = pCtx->tagInfo.pTagCtxList[t]; + SQLFunctionCtx *tagCtx = pCtx->tagInfo.pTagCtxList[t]; if (tagCtx->functionId == TSDB_FUNC_TAG_DUMMY) { aAggs[TSDB_FUNC_TAGPRJ].xFunction(tagCtx); } @@ -5901,32 +5911,32 @@ static void state_count_function(SQLFunctionCtx *pCtx) { static void state_duration_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SStateInfo *pStateInfo = GET_ROWCELL_INTERBUF(pResInfo); + SStateInfo *pStateInfo = GET_ROWCELL_INTERBUF(pResInfo); - char *data = GET_INPUT_DATA_LIST(pCtx); - TSKEY* tsList = GET_TS_LIST(pCtx); + char *data = GET_INPUT_DATA_LIST(pCtx); + TSKEY *tsList = GET_TS_LIST(pCtx); int64_t *pOutput = (int64_t *)pCtx->pOutput; - for (int32_t i = 0; i < pCtx->size; i++,pOutput++,data += pCtx->inputBytes) { + for (int32_t i = 0; i < pCtx->size; i++, pOutput++, data += pCtx->inputBytes) { if (pCtx->hasNull && isNull(data, pCtx->inputType)) { setNull(pOutput, TSDB_DATA_TYPE_BIGINT, 0); continue; } - if (isStateOperTrue(data, pCtx->inputType, &pCtx->param[0], &pCtx->param[1])){ + if (isStateOperTrue(data, pCtx->inputType, &pCtx->param[0], &pCtx->param[1])) { if (pStateInfo->durationStart == 0) { *pOutput = 0; pStateInfo->durationStart = tsList[i]; } else { - *pOutput = (tsList[i] - pStateInfo->durationStart)/pCtx->param[2].i64; + *pOutput = (tsList[i] - pStateInfo->durationStart) / pCtx->param[2].i64; } - } else{ + } else { *pOutput = -1; pStateInfo->durationStart = 0; } } for (int t = 0; t < pCtx->tagInfo.numOfTagCols; ++t) { - SQLFunctionCtx* tagCtx = pCtx->tagInfo.pTagCtxList[t]; + SQLFunctionCtx *tagCtx = pCtx->tagInfo.pTagCtxList[t]; if (tagCtx->functionId == TSDB_FUNC_TAG_DUMMY) { aAggs[TSDB_FUNC_TAGPRJ].xFunction(tagCtx); } @@ -5963,10 +5973,10 @@ static void window_start_function(SQLFunctionCtx *pCtx) { if (pCtx->functionId == TSDB_FUNC_WSTART) { SET_VAL(pCtx, pCtx->size, 1); *(int64_t *)(pCtx->pOutput) = pCtx->startTs; - } else { //TSDB_FUNC_QSTART - int32_t size = MIN(pCtx->size, pCtx->allocRows); //size cannot exceeds allocated rows + } else { // TSDB_FUNC_QSTART + int32_t size = MIN(pCtx->size, pCtx->allocRows); // size cannot exceeds allocated rows SET_VAL(pCtx, pCtx->size, size); - //INC_INIT_VAL(pCtx, size); + // INC_INIT_VAL(pCtx, size); char *output = pCtx->pOutput; for (int32_t i = 0; i < size; ++i) { if (pCtx->qWindow.skey == INT64_MIN) { @@ -5983,10 +5993,10 @@ static void window_stop_function(SQLFunctionCtx *pCtx) { if (pCtx->functionId == TSDB_FUNC_WSTOP) { SET_VAL(pCtx, pCtx->size, 1); *(int64_t *)(pCtx->pOutput) = pCtx->endTs; - } else { //TSDB_FUNC_QSTOP - int32_t size = MIN(pCtx->size, pCtx->allocRows); //size cannot exceeds allocated rows + } else { // TSDB_FUNC_QSTOP + int32_t size = MIN(pCtx->size, pCtx->allocRows); // size cannot exceeds allocated rows SET_VAL(pCtx, pCtx->size, size); - //INC_INIT_VAL(pCtx, size); + // INC_INIT_VAL(pCtx, size); char *output = pCtx->pOutput; for (int32_t i = 0; i < size; ++i) { if (pCtx->qWindow.ekey == INT64_MAX) { @@ -6008,10 +6018,10 @@ static void window_duration_function(SQLFunctionCtx *pCtx) { duration = -duration; } *(int64_t *)(pCtx->pOutput) = duration; - } else { //TSDB_FUNC_QDURATION - int32_t size = MIN(pCtx->size, pCtx->allocRows); //size cannot exceeds allocated rows + } else { // TSDB_FUNC_QDURATION + int32_t size = MIN(pCtx->size, pCtx->allocRows); // size cannot exceeds allocated rows SET_VAL(pCtx, pCtx->size, size); - //INC_INIT_VAL(pCtx, size); + // INC_INIT_VAL(pCtx, size); duration = pCtx->qWindow.ekey - pCtx->qWindow.skey; if (duration < 0) { duration = -duration; @@ -6040,631 +6050,683 @@ static void window_duration_function(SQLFunctionCtx *pCtx) { * */ int32_t functionCompatList[] = { - // count, sum, avg, min, max, stddev, percentile, apercentile, first, last - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - // last_row, top, bottom, spread, twa, leastsqr, ts, ts_dummy, tag_dummy, ts_comp - 4, -1, -1, 1, 1, 1, 1, 1, 1, -1, - // tag, colprj, tagprj, arithm, diff, first_dist, last_dist, stddev_dst, interp rate, irate - 1, 1, 1, 1, -1, 1, 1, 1, 5, 1, 1, - // tid_tag, deriv, csum, mavg, sample, block_info, elapsed, histogram, unique, mode, tail - 6, 8, -1, -1, -1, 7, 1, -1, -1, 1, -1, - // stateCount, stateDuration, wstart, wstop, wduration, qstart, qstop, qduration, hyperloglog - 1, 1, 1, 1, 1, 1, 1, 1, 1, + // count, sum, avg, min, max, stddev, percentile, apercentile, first, + // last + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + // last_row, top, bottom, spread, twa, leastsqr, ts, ts_dummy, tag_dummy, + // ts_comp + 4, + -1, + -1, + 1, + 1, + 1, + 1, + 1, + 1, + -1, + // tag, colprj, tagprj, arithm, diff, first_dist, last_dist, stddev_dst, interp + // rate, irate + 1, + 1, + 1, + 1, + -1, + 1, + 1, + 1, + 5, + 1, + 1, + // tid_tag, deriv, csum, mavg, sample, block_info, elapsed, histogram, unique, + // mode, tail + 6, + 8, + -1, + -1, + -1, + 7, + 1, + -1, + -1, + 1, + -1, + // stateCount, stateDuration, wstart, wstop, wduration, qstart, qstop, qduration, hyperloglog + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, }; -SAggFunctionInfo aAggs[TSDB_FUNC_MAX_NUM] = {{ - // 0, count function does not invoke the finalize function - "count", - TSDB_FUNC_COUNT, - TSDB_FUNC_COUNT, - TSDB_BASE_FUNC_SO, - function_setup, - count_function, - doFinalizer, - count_func_merge, - countRequired, - }, - { - // 1 - "sum", - TSDB_FUNC_SUM, - TSDB_FUNC_SUM, - TSDB_BASE_FUNC_SO, - function_setup, - sum_function, - function_finalizer, - sum_func_merge, - statisRequired, - }, - { - // 2 - "avg", - TSDB_FUNC_AVG, - TSDB_FUNC_AVG, - TSDB_BASE_FUNC_SO, - function_setup, - avg_function, - avg_finalizer, - avg_func_merge, - statisRequired, - }, - { - // 3 - "min", - TSDB_FUNC_MIN, - TSDB_FUNC_MIN, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, - min_func_setup, - min_function, - function_finalizer, - min_func_merge, - statisRequired, - }, - { - // 4 - "max", - TSDB_FUNC_MAX, - TSDB_FUNC_MAX, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, - max_func_setup, - max_function, - function_finalizer, - max_func_merge, - statisRequired, - }, - { - // 5 - "stddev", - TSDB_FUNC_STDDEV, - TSDB_FUNC_STDDEV_DST, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, - function_setup, - stddev_function, - stddev_finalizer, - noop1, - dataBlockRequired, - }, - { - // 6 - "percentile", - TSDB_FUNC_PERCT, - TSDB_FUNC_INVALID_ID, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, - percentile_function_setup, - percentile_function, - percentile_finalizer, - noop1, - dataBlockRequired, - }, - { - // 7 - "apercentile", - TSDB_FUNC_APERCT, - TSDB_FUNC_APERCT, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_STABLE, - apercentile_function_setup, - apercentile_function, - apercentile_finalizer, - apercentile_func_merge, - dataBlockRequired, - }, - { - // 8 - "first", - TSDB_FUNC_FIRST, - TSDB_FUNC_FIRST_DST, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, - function_setup, - first_function, - function_finalizer, - noop1, - firstFuncRequired, - }, - { - // 9 - "last", - TSDB_FUNC_LAST, - TSDB_FUNC_LAST_DST, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, - function_setup, - last_function, - function_finalizer, - noop1, - lastFuncRequired, - }, - { - // 10 - "last_row", - TSDB_FUNC_LAST_ROW, - TSDB_FUNC_LAST_ROW, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | - TSDB_FUNCSTATE_SELECTIVITY, - first_last_function_setup, - last_row_function, - last_row_finalizer, - last_dist_func_merge, - dataBlockRequired, - }, - { - // 11 - "top", - TSDB_FUNC_TOP, - TSDB_FUNC_TOP, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_NEED_TS | - TSDB_FUNCSTATE_SELECTIVITY, - top_bottom_function_setup, - top_function, - top_bottom_func_finalizer, - top_func_merge, - dataBlockRequired, - }, - { - // 12 - "bottom", - TSDB_FUNC_BOTTOM, - TSDB_FUNC_BOTTOM, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_NEED_TS | - TSDB_FUNCSTATE_SELECTIVITY, - top_bottom_function_setup, - bottom_function, - top_bottom_func_finalizer, - bottom_func_merge, - dataBlockRequired, - }, - { - // 13 - "spread", - TSDB_FUNC_SPREAD, - TSDB_FUNC_SPREAD, - TSDB_BASE_FUNC_SO, - spread_function_setup, - spread_function, - spread_function_finalizer, - spread_func_merge, - countRequired, - }, - { - // 14 - "twa", - TSDB_FUNC_TWA, - TSDB_FUNC_TWA, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS, - twa_function_setup, - twa_function, - twa_function_finalizer, - twa_function_copy, - dataBlockRequired, - }, - { - // 15 - "leastsquares", - TSDB_FUNC_LEASTSQR, - TSDB_FUNC_INVALID_ID, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, - leastsquares_function_setup, - leastsquares_function, - leastsquares_finalizer, - noop1, - dataBlockRequired, - }, - { - // 16 - "ts", - TSDB_FUNC_TS, - TSDB_FUNC_TS, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, - function_setup, - date_col_output_function, - doFinalizer, - copy_function, - noDataRequired, - }, - { - // 17 - "ts", - TSDB_FUNC_TS_DUMMY, - TSDB_FUNC_TS_DUMMY, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, - function_setup, - noop1, - doFinalizer, - copy_function, - dataBlockRequired, - }, - { - // 18 - "tag_dummy", - TSDB_FUNC_TAG_DUMMY, - TSDB_FUNC_TAG_DUMMY, - TSDB_BASE_FUNC_SO, - function_setup, - tag_function, - doFinalizer, - copy_function, - noDataRequired, - }, - { - // 19 - "ts", - TSDB_FUNC_TS_COMP, - TSDB_FUNC_TS_COMP, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_NEED_TS, - ts_comp_function_setup, - ts_comp_function, - ts_comp_finalize, - copy_function, - dataBlockRequired, - }, - { - // 20 - "tag", - TSDB_FUNC_TAG, - TSDB_FUNC_TAG, - TSDB_BASE_FUNC_SO, - function_setup, - tag_function, - doFinalizer, - copy_function, - noDataRequired, - }, - { - // 21, column project sql function - "colprj", - TSDB_FUNC_PRJ, - TSDB_FUNC_PRJ, - TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_NEED_TS, - function_setup, - col_project_function, - doFinalizer, - copy_function, - dataBlockRequired, - }, - { - // 22, multi-output, tag function has only one result - "tagprj", - TSDB_FUNC_TAGPRJ, - TSDB_FUNC_TAGPRJ, - TSDB_BASE_FUNC_MO, - function_setup, - tag_project_function, - doFinalizer, - copy_function, - noDataRequired, - }, - { - // 23 - "arithmetic", - TSDB_FUNC_SCALAR_EXPR, - TSDB_FUNC_SCALAR_EXPR, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS, - function_setup, - scalar_expr_function, - doFinalizer, - copy_function, - dataBlockRequired, - }, - { - // 24 - "diff", - TSDB_FUNC_DIFF, - TSDB_FUNC_INVALID_ID, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, - diff_function_setup, - diff_function, - doFinalizer, - noop1, - dataBlockRequired, - }, +SAggFunctionInfo aAggs[TSDB_FUNC_MAX_NUM] = { + { + // 0, count function does not invoke the finalize function + "count", + TSDB_FUNC_COUNT, + TSDB_FUNC_COUNT, + TSDB_BASE_FUNC_SO, + function_setup, + count_function, + doFinalizer, + count_func_merge, + countRequired, + }, + { + // 1 + "sum", + TSDB_FUNC_SUM, + TSDB_FUNC_SUM, + TSDB_BASE_FUNC_SO, + function_setup, + sum_function, + function_finalizer, + sum_func_merge, + statisRequired, + }, + { + // 2 + "avg", + TSDB_FUNC_AVG, + TSDB_FUNC_AVG, + TSDB_BASE_FUNC_SO, + function_setup, + avg_function, + avg_finalizer, + avg_func_merge, + statisRequired, + }, + { + // 3 + "min", + TSDB_FUNC_MIN, + TSDB_FUNC_MIN, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, + min_func_setup, + min_function, + function_finalizer, + min_func_merge, + statisRequired, + }, + { + // 4 + "max", + TSDB_FUNC_MAX, + TSDB_FUNC_MAX, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, + max_func_setup, + max_function, + function_finalizer, + max_func_merge, + statisRequired, + }, + { + // 5 + "stddev", + TSDB_FUNC_STDDEV, + TSDB_FUNC_STDDEV_DST, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, + function_setup, + stddev_function, + stddev_finalizer, + noop1, + dataBlockRequired, + }, + { + // 6 + "percentile", + TSDB_FUNC_PERCT, + TSDB_FUNC_INVALID_ID, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, + percentile_function_setup, + percentile_function, + percentile_finalizer, + noop1, + dataBlockRequired, + }, + { + // 7 + "apercentile", + TSDB_FUNC_APERCT, + TSDB_FUNC_APERCT, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_STABLE, + apercentile_function_setup, + apercentile_function, + apercentile_finalizer, + apercentile_func_merge, + dataBlockRequired, + }, + { + // 8 + "first", + TSDB_FUNC_FIRST, + TSDB_FUNC_FIRST_DST, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, + function_setup, + first_function, + function_finalizer, + noop1, + firstFuncRequired, + }, + { + // 9 + "last", + TSDB_FUNC_LAST, + TSDB_FUNC_LAST_DST, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, + function_setup, + last_function, + function_finalizer, + noop1, + lastFuncRequired, + }, + { + // 10 + "last_row", + TSDB_FUNC_LAST_ROW, + TSDB_FUNC_LAST_ROW, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | + TSDB_FUNCSTATE_SELECTIVITY, + first_last_function_setup, + last_row_function, + last_row_finalizer, + last_dist_func_merge, + dataBlockRequired, + }, + { + // 11 + "top", + TSDB_FUNC_TOP, + TSDB_FUNC_TOP, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_NEED_TS | + TSDB_FUNCSTATE_SELECTIVITY, + top_bottom_function_setup, + top_function, + top_bottom_func_finalizer, + top_func_merge, + dataBlockRequired, + }, + { + // 12 + "bottom", + TSDB_FUNC_BOTTOM, + TSDB_FUNC_BOTTOM, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_NEED_TS | + TSDB_FUNCSTATE_SELECTIVITY, + top_bottom_function_setup, + bottom_function, + top_bottom_func_finalizer, + bottom_func_merge, + dataBlockRequired, + }, + { + // 13 + "spread", + TSDB_FUNC_SPREAD, + TSDB_FUNC_SPREAD, + TSDB_BASE_FUNC_SO, + spread_function_setup, + spread_function, + spread_function_finalizer, + spread_func_merge, + countRequired, + }, + { + // 14 + "twa", + TSDB_FUNC_TWA, + TSDB_FUNC_TWA, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS, + twa_function_setup, + twa_function, + twa_function_finalizer, + twa_function_copy, + dataBlockRequired, + }, + { + // 15 + "leastsquares", + TSDB_FUNC_LEASTSQR, + TSDB_FUNC_INVALID_ID, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, + leastsquares_function_setup, + leastsquares_function, + leastsquares_finalizer, + noop1, + dataBlockRequired, + }, + { + // 16 + "ts", + TSDB_FUNC_TS, + TSDB_FUNC_TS, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, + function_setup, + date_col_output_function, + doFinalizer, + copy_function, + noDataRequired, + }, + { + // 17 + "ts", + TSDB_FUNC_TS_DUMMY, + TSDB_FUNC_TS_DUMMY, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, + function_setup, + noop1, + doFinalizer, + copy_function, + dataBlockRequired, + }, + { + // 18 + "tag_dummy", + TSDB_FUNC_TAG_DUMMY, + TSDB_FUNC_TAG_DUMMY, + TSDB_BASE_FUNC_SO, + function_setup, + tag_function, + doFinalizer, + copy_function, + noDataRequired, + }, + { + // 19 + "ts", + TSDB_FUNC_TS_COMP, + TSDB_FUNC_TS_COMP, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_NEED_TS, + ts_comp_function_setup, + ts_comp_function, + ts_comp_finalize, + copy_function, + dataBlockRequired, + }, + { + // 20 + "tag", + TSDB_FUNC_TAG, + TSDB_FUNC_TAG, + TSDB_BASE_FUNC_SO, + function_setup, + tag_function, + doFinalizer, + copy_function, + noDataRequired, + }, + { + // 21, column project sql function + "colprj", + TSDB_FUNC_PRJ, + TSDB_FUNC_PRJ, + TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_NEED_TS, + function_setup, + col_project_function, + doFinalizer, + copy_function, + dataBlockRequired, + }, + { + // 22, multi-output, tag function has only one result + "tagprj", + TSDB_FUNC_TAGPRJ, + TSDB_FUNC_TAGPRJ, + TSDB_BASE_FUNC_MO, + function_setup, + tag_project_function, + doFinalizer, + copy_function, + noDataRequired, + }, + { + // 23 + "arithmetic", + TSDB_FUNC_SCALAR_EXPR, + TSDB_FUNC_SCALAR_EXPR, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS, + function_setup, + scalar_expr_function, + doFinalizer, + copy_function, + dataBlockRequired, + }, + { + // 24 + "diff", + TSDB_FUNC_DIFF, + TSDB_FUNC_INVALID_ID, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, + diff_function_setup, + diff_function, + doFinalizer, + noop1, + dataBlockRequired, + }, // distributed version used in two-stage aggregation processes - { - // 25 - "first_dist", - TSDB_FUNC_FIRST_DST, - TSDB_FUNC_FIRST_DST, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, - first_last_function_setup, - first_dist_function, - function_finalizer, - first_dist_func_merge, - firstDistFuncRequired, - }, - { - // 26 - "last_dist", - TSDB_FUNC_LAST_DST, - TSDB_FUNC_LAST_DST, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, - first_last_function_setup, - last_dist_function, - function_finalizer, - last_dist_func_merge, - lastDistFuncRequired, - }, - { - // 27 - "stddev", // return table id and the corresponding tags for join match and subscribe - TSDB_FUNC_STDDEV_DST, - TSDB_FUNC_AVG, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE, - function_setup, - stddev_dst_function, - stddev_dst_finalizer, - stddev_dst_merge, - dataBlockRequired, - }, - { - // 28 - "interp", - TSDB_FUNC_INTERP, - TSDB_FUNC_INTERP, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, - function_setup, - interp_function, - doFinalizer, - full_copy_function, - dataBlockRequired, - }, - { - // 29 - "rate", - TSDB_FUNC_RATE, - TSDB_FUNC_RATE, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, - rate_function_setup, - rate_function, - rate_finalizer, - rate_func_copy, - dataBlockRequired, - }, - { - // 30 - "irate", - TSDB_FUNC_IRATE, - TSDB_FUNC_IRATE, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, - rate_function_setup, - irate_function, - rate_finalizer, - rate_func_copy, - dataBlockRequired, - }, - { - // 31 - "tbid", // return table id and the corresponding tags for join match and subscribe - TSDB_FUNC_TID_TAG, - TSDB_FUNC_TID_TAG, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE, - function_setup, - noop1, - noop1, - noop1, - dataBlockRequired, - }, - { //32 - "derivative", // return table id and the corresponding tags for join match and subscribe - TSDB_FUNC_DERIVATIVE, - TSDB_FUNC_INVALID_ID, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, - deriv_function_setup, - deriv_function, - doFinalizer, - noop1, - dataBlockRequired, - }, - { - // 33 - "csum", - TSDB_FUNC_CSUM, - TSDB_FUNC_INVALID_ID, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, - csum_function_setup, - csum_function, - doFinalizer, - noop1, - dataBlockRequired, - }, - { - // 34 - "mavg", - TSDB_FUNC_MAVG, - TSDB_FUNC_INVALID_ID, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, - mavg_function_setup, - mavg_function, - doFinalizer, - noop1, - dataBlockRequired, - }, - { - // 35 - "sample", - TSDB_FUNC_SAMPLE, - TSDB_FUNC_SAMPLE, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, - sample_function_setup, - sample_function, - sample_func_finalizer, - sample_func_merge, - dataBlockRequired, - }, - { - // 36 - "_block_dist", - TSDB_FUNC_BLKINFO, - TSDB_FUNC_BLKINFO, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE, - function_setup, - blockInfo_func, - blockinfo_func_finalizer, - block_func_merge, - dataBlockRequired, - }, - { - // 37 - "elapsed", - TSDB_FUNC_ELAPSED, - TSDB_FUNC_ELAPSED, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE, - elapsedSetup, - elapsedFunction, - elapsedFinalizer, - elapsedMerge, - elapsedRequired, - }, - { - //38 - "histogram", - TSDB_FUNC_HISTOGRAM, - TSDB_FUNC_HISTOGRAM, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE, - histogram_function_setup, - histogram_function, - histogram_func_finalizer, - histogram_func_merge, - dataBlockRequired, - }, - { - // 39 - "unique", - TSDB_FUNC_UNIQUE, - TSDB_FUNC_UNIQUE, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_SELECTIVITY, - unique_function_setup, - unique_function, - unique_func_finalizer, - unique_function_merge, - dataBlockRequired, - }, - { - // 40 - "mode", - TSDB_FUNC_MODE, - TSDB_FUNC_MODE, - TSDB_BASE_FUNC_SO, - mode_function_setup, - mode_function, - mode_func_finalizer, - mode_function_merge, - dataBlockRequired, - }, - { - // 41 - "tail", - TSDB_FUNC_TAIL, - TSDB_FUNC_TAIL, - TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_SELECTIVITY, - tail_function_setup, - tail_function, - tail_func_finalizer, - tail_func_merge, - tailFuncRequired, - }, - { - // 42 - "stateCount", - TSDB_FUNC_STATE_COUNT, - TSDB_FUNC_INVALID_ID, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, - function_setup, - state_count_function, - doFinalizer, - noop1, - dataBlockRequired, - }, - { - // 43 - "stateDuration", - TSDB_FUNC_STATE_DURATION, - TSDB_FUNC_INVALID_ID, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, - function_setup, - state_duration_function, - doFinalizer, - noop1, - dataBlockRequired, - }, - { - // 44 - "_wstart", - TSDB_FUNC_WSTART, - TSDB_FUNC_WSTART, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, - function_setup, - window_start_function, - doFinalizer, - copy_function, - dataBlockRequired, - }, - { - // 45 - "_wstop", - TSDB_FUNC_WSTOP, - TSDB_FUNC_WSTOP, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, - function_setup, - window_stop_function, - doFinalizer, - copy_function, - dataBlockRequired, - }, - { - // 46 - "_wduration", - TSDB_FUNC_WDURATION, - TSDB_FUNC_WDURATION, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, - function_setup, - window_duration_function, - doFinalizer, - copy_function, - dataBlockRequired, - }, - { - // 47 - "_qstart", - TSDB_FUNC_QSTART, - TSDB_FUNC_QSTART, - TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_SELECTIVITY, - function_setup, - window_start_function, - doFinalizer, - copy_function, - dataBlockRequired, - }, - { - // 48 - "_qstop", - TSDB_FUNC_QSTOP, - TSDB_FUNC_QSTOP, - TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_SELECTIVITY, - function_setup, - window_stop_function, - doFinalizer, - copy_function, - dataBlockRequired, - }, - { - // 49 - "_qduration", - TSDB_FUNC_QDURATION, - TSDB_FUNC_QDURATION, - TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_SELECTIVITY, - function_setup, - window_duration_function, - doFinalizer, - copy_function, - dataBlockRequired, - }, - { - // 50 - "hyperloglog", - TSDB_FUNC_HYPERLOGLOG, - TSDB_FUNC_HYPERLOGLOG, - TSDB_BASE_FUNC_SO, - function_setup, - hll_function, - hll_func_finalizer, - hll_func_merge, - dataBlockRequired, - } -}; + { + // 25 + "first_dist", + TSDB_FUNC_FIRST_DST, + TSDB_FUNC_FIRST_DST, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, + first_last_function_setup, + first_dist_function, + function_finalizer, + first_dist_func_merge, + firstDistFuncRequired, + }, + { + // 26 + "last_dist", + TSDB_FUNC_LAST_DST, + TSDB_FUNC_LAST_DST, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, + first_last_function_setup, + last_dist_function, + function_finalizer, + last_dist_func_merge, + lastDistFuncRequired, + }, + { + // 27 + "stddev", // return table id and the corresponding tags for join match and subscribe + TSDB_FUNC_STDDEV_DST, + TSDB_FUNC_AVG, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE, + function_setup, + stddev_dst_function, + stddev_dst_finalizer, + stddev_dst_merge, + dataBlockRequired, + }, + { + // 28 + "interp", + TSDB_FUNC_INTERP, + TSDB_FUNC_INTERP, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | + TSDB_FUNCSTATE_SELECTIVITY, + function_setup, + interp_function, + doFinalizer, + full_copy_function, + dataBlockRequired, + }, + { + // 29 + "rate", + TSDB_FUNC_RATE, + TSDB_FUNC_RATE, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, + rate_function_setup, + rate_function, + rate_finalizer, + rate_func_copy, + dataBlockRequired, + }, + { + // 30 + "irate", + TSDB_FUNC_IRATE, + TSDB_FUNC_IRATE, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, + rate_function_setup, + irate_function, + rate_finalizer, + rate_func_copy, + dataBlockRequired, + }, + { + // 31 + "tbid", // return table id and the corresponding tags for join match and subscribe + TSDB_FUNC_TID_TAG, + TSDB_FUNC_TID_TAG, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE, + function_setup, + noop1, + noop1, + noop1, + dataBlockRequired, + }, + { + // 32 + "derivative", // return table id and the corresponding tags for join match and subscribe + TSDB_FUNC_DERIVATIVE, + TSDB_FUNC_INVALID_ID, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, + deriv_function_setup, + deriv_function, + doFinalizer, + noop1, + dataBlockRequired, + }, + { + // 33 + "csum", + TSDB_FUNC_CSUM, + TSDB_FUNC_INVALID_ID, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, + csum_function_setup, + csum_function, + doFinalizer, + noop1, + dataBlockRequired, + }, + { + // 34 + "mavg", + TSDB_FUNC_MAVG, + TSDB_FUNC_INVALID_ID, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, + mavg_function_setup, + mavg_function, + doFinalizer, + noop1, + dataBlockRequired, + }, + { + // 35 + "sample", + TSDB_FUNC_SAMPLE, + TSDB_FUNC_SAMPLE, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, + sample_function_setup, + sample_function, + sample_func_finalizer, + sample_func_merge, + dataBlockRequired, + }, + { + // 36 + "_block_dist", + TSDB_FUNC_BLKINFO, + TSDB_FUNC_BLKINFO, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE, + function_setup, + blockInfo_func, + blockinfo_func_finalizer, + block_func_merge, + dataBlockRequired, + }, + { + // 37 + "elapsed", + TSDB_FUNC_ELAPSED, + TSDB_FUNC_ELAPSED, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE, + elapsedSetup, + elapsedFunction, + elapsedFinalizer, + elapsedMerge, + elapsedRequired, + }, + { + // 38 + "histogram", + TSDB_FUNC_HISTOGRAM, + TSDB_FUNC_HISTOGRAM, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE, + histogram_function_setup, + histogram_function, + histogram_func_finalizer, + histogram_func_merge, + dataBlockRequired, + }, + { + // 39 + "unique", + TSDB_FUNC_UNIQUE, + TSDB_FUNC_UNIQUE, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_SELECTIVITY, + unique_function_setup, + unique_function, + unique_func_finalizer, + unique_function_merge, + dataBlockRequired, + }, + { + // 40 + "mode", + TSDB_FUNC_MODE, + TSDB_FUNC_MODE, + TSDB_BASE_FUNC_SO, + mode_function_setup, + mode_function, + mode_func_finalizer, + mode_function_merge, + dataBlockRequired, + }, + { + // 41 + "tail", + TSDB_FUNC_TAIL, + TSDB_FUNC_TAIL, + TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_SELECTIVITY, + tail_function_setup, + tail_function, + tail_func_finalizer, + tail_func_merge, + tailFuncRequired, + }, + { + // 42 + "stateCount", + TSDB_FUNC_STATE_COUNT, + TSDB_FUNC_INVALID_ID, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, + function_setup, + state_count_function, + doFinalizer, + noop1, + dataBlockRequired, + }, + { + // 43 + "stateDuration", + TSDB_FUNC_STATE_DURATION, + TSDB_FUNC_INVALID_ID, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, + function_setup, + state_duration_function, + doFinalizer, + noop1, + dataBlockRequired, + }, + { + // 44 + "_wstart", + TSDB_FUNC_WSTART, + TSDB_FUNC_WSTART, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, + function_setup, + window_start_function, + doFinalizer, + copy_function, + dataBlockRequired, + }, + { + // 45 + "_wstop", + TSDB_FUNC_WSTOP, + TSDB_FUNC_WSTOP, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, + function_setup, + window_stop_function, + doFinalizer, + copy_function, + dataBlockRequired, + }, + { + // 46 + "_wduration", + TSDB_FUNC_WDURATION, + TSDB_FUNC_WDURATION, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, + function_setup, + window_duration_function, + doFinalizer, + copy_function, + dataBlockRequired, + }, + { + // 47 + "_qstart", + TSDB_FUNC_QSTART, + TSDB_FUNC_QSTART, + TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_SELECTIVITY, + function_setup, + window_start_function, + doFinalizer, + copy_function, + dataBlockRequired, + }, + { + // 48 + "_qstop", + TSDB_FUNC_QSTOP, + TSDB_FUNC_QSTOP, + TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_SELECTIVITY, + function_setup, + window_stop_function, + doFinalizer, + copy_function, + dataBlockRequired, + }, + { + // 49 + "_qduration", + TSDB_FUNC_QDURATION, + TSDB_FUNC_QDURATION, + TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_SELECTIVITY, + function_setup, + window_duration_function, + doFinalizer, + copy_function, + dataBlockRequired, + }, + { + // 50 + "hyperloglog", + TSDB_FUNC_HYPERLOGLOG, + TSDB_FUNC_HYPERLOGLOG, + TSDB_BASE_FUNC_SO, + function_setup, + hll_function, + hll_func_finalizer, + hll_func_merge, + dataBlockRequired, + }};