diff --git a/Jenkinsfile b/Jenkinsfile index 8d2429c137414fdb63260f94c0c99a6d264d8c48..e7603b578bf2c7718e67fbd9fda1674db49c07c0 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -46,6 +46,7 @@ def pre_test(){ git fetch origin +refs/pull/${CHANGE_ID}/merge git checkout -qf FETCH_HEAD git --no-pager diff --name-only FETCH_HEAD $(git merge-base FETCH_HEAD develop)|grep -v -E '.*md|//src//connector|Jenkinsfile' || exit 0 + find ${WKC}/tests/pytest -name \'*\'.sql -exec rm -rf {} \\; cd ${WK} git reset --hard HEAD~10 git checkout develop @@ -115,7 +116,6 @@ pipeline { sh ''' date cd ${WKC}/tests - find pytest -name '*'sql|xargs rm -rf ./test-all.sh p1 date''' } @@ -131,7 +131,6 @@ pipeline { sh ''' date cd ${WKC}/tests - find pytest -name '*'sql|xargs rm -rf ./test-all.sh p2 date''' } diff --git a/cmake/install.inc b/cmake/install.inc index 0ea79589caef1bf3ec72f4234f99e87328759c33..1d50ca292d902461e0f77732da83076954224f06 100755 --- a/cmake/install.inc +++ b/cmake/install.inc @@ -32,7 +32,7 @@ ELSEIF (TD_WINDOWS) #INSTALL(TARGETS taos RUNTIME DESTINATION driver) #INSTALL(TARGETS shell RUNTIME DESTINATION .) IF (TD_MVN_INSTALLED) - INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos-jdbcdriver-2.0.21-dist.jar DESTINATION connector/jdbc) + INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos-jdbcdriver-2.0.22-dist.jar DESTINATION connector/jdbc) ENDIF () ELSEIF (TD_DARWIN) SET(TD_MAKE_INSTALL_SH "${TD_COMMUNITY_DIR}/packaging/tools/make_install.sh") diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index 502d044d7540169497b682df96424ca3304f4668..f69ee23222119b3bc9b983c8fb24b067f11ff996 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -123,6 +123,7 @@ int32_t tscGetDataBlockFromList(SHashObj* pHashList, int64_t id, int32_t size, i bool tscIsPointInterpQuery(SQueryInfo* pQueryInfo); bool tscIsTWAQuery(SQueryInfo* pQueryInfo); bool tscIsSecondStageQuery(SQueryInfo* pQueryInfo); +bool tscGroupbyColumn(SQueryInfo* pQueryInfo); bool tscNonOrderedProjectionQueryOnSTable(SQueryInfo *pQueryInfo, int32_t tableIndex); bool tscOrderedProjectionQueryOnSTable(SQueryInfo* pQueryInfo, int32_t tableIndex); @@ -133,6 +134,7 @@ bool tscIsProjectionQuery(SQueryInfo* pQueryInfo); bool tscIsTwoStageSTableQuery(SQueryInfo* pQueryInfo, int32_t tableIndex); bool tscQueryTags(SQueryInfo* pQueryInfo); bool tscMultiRoundQuery(SQueryInfo* pQueryInfo, int32_t tableIndex); +bool tscQueryBlockInfo(SQueryInfo* pQueryInfo); SSqlExpr* tscAddFuncInSelectClause(SQueryInfo* pQueryInfo, int32_t outputColIndex, int16_t functionId, SColumnIndex* pIndex, SSchema* pColSchema, int16_t colType); @@ -152,7 +154,6 @@ SInternalField* tscFieldInfoInsert(SFieldInfo* pFieldInfo, int32_t index, TAOS_F SInternalField* tscFieldInfoGetInternalField(SFieldInfo* pFieldInfo, int32_t index); TAOS_FIELD* tscFieldInfoGetField(SFieldInfo* pFieldInfo, int32_t index); -void tscFieldInfoUpdateOffset(SQueryInfo* pQueryInfo); void tscFieldInfoUpdateOffset(SQueryInfo* pQueryInfo); int16_t tscFieldInfoGetOffset(SQueryInfo* pQueryInfo, int32_t index); diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index 0bfbf3f946a4a1428549a493ab990a2be2173266..1740266518dd2c736feb47a5e9baa98cb66cbd94 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -198,9 +198,10 @@ typedef struct STableDataBlocks { typedef struct SQueryInfo { int16_t command; // the command may be different for each subclause, so keep it seperately. uint32_t type; // query/insert type + STimeWindow window; // the whole query time window - STimeWindow window; // query time window - SInterval interval; + SInterval interval; // tumble time window + SSessionWindow sessionWindow; // session time window SSqlGroupbyExpr groupbyExpr; // group by tags info SArray * colList; // SArray @@ -232,6 +233,7 @@ typedef struct SQueryInfo { typedef struct { int command; uint8_t msgType; + char reserve1[3]; // fix bus error on arm32 bool autoCreated; // create table if it is not existed during retrieve table meta in mnode union { @@ -244,8 +246,10 @@ typedef struct { char * curSql; // current sql, resume position of sql after parsing paused int8_t parseFinished; + char reserve2[3]; // fix bus error on arm32 int16_t numOfCols; + char reserve3[2]; // fix bus error on arm32 uint32_t allocSize; char * payload; int32_t payloadLen; @@ -255,7 +259,9 @@ typedef struct { int32_t numOfParams; int8_t dataSourceType; // load data from file or not + char reserve4[3]; // fix bus error on arm32 int8_t submitSchema; // submit block is built with table schema + char reserve5[3]; // fix bus error on arm32 STagData tagData; // NOTE: pTagData->data is used as a variant length array SName **pTableNameList; // all involved tableMeta list of current insert sql statement. @@ -397,7 +403,6 @@ typedef struct SSqlStream { void tscSetStreamDestTable(SSqlStream* pStream, const char* dstTable); - int tscAcquireRpc(const char *key, const char *user, const char *secret,void **pRpcObj); void tscReleaseRpc(void *param); void tscInitMsgsFp(); diff --git a/src/client/src/TSDBJNIConnector.c b/src/client/src/TSDBJNIConnector.c index a8829499a324605036beefad62d83eccf4d1c65b..56e155311e60dd0a30d3f6b5dce99c9d4c76ec7f 100644 --- a/src/client/src/TSDBJNIConnector.c +++ b/src/client/src/TSDBJNIConnector.c @@ -481,15 +481,19 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_fetchRowImp(JNIEn case TSDB_DATA_TYPE_BOOL: (*env)->CallVoidMethod(env, rowobj, g_rowdataSetBooleanFp, i, (jboolean)(*((char *)row[i]) == 1)); break; + case TSDB_DATA_TYPE_UTINYINT: case TSDB_DATA_TYPE_TINYINT: (*env)->CallVoidMethod(env, rowobj, g_rowdataSetByteFp, i, (jbyte) * ((int8_t *)row[i])); break; + case TSDB_DATA_TYPE_USMALLINT: case TSDB_DATA_TYPE_SMALLINT: (*env)->CallVoidMethod(env, rowobj, g_rowdataSetShortFp, i, (jshort) * ((int16_t *)row[i])); break; + case TSDB_DATA_TYPE_UINT: case TSDB_DATA_TYPE_INT: (*env)->CallVoidMethod(env, rowobj, g_rowdataSetIntFp, i, (jint) * (int32_t *)row[i]); break; + case TSDB_DATA_TYPE_UBIGINT: case TSDB_DATA_TYPE_BIGINT: (*env)->CallVoidMethod(env, rowobj, g_rowdataSetLongFp, i, (jlong) * ((int64_t *)row[i])); break; diff --git a/src/client/src/tscLocalMerge.c b/src/client/src/tscLocalMerge.c index 23fb0ab67cded77ff737fac4246343486e80eb95..a44b0c46ba7920c2483c4baa3b34bc37f06bdee7 100644 --- a/src/client/src/tscLocalMerge.c +++ b/src/client/src/tscLocalMerge.c @@ -100,6 +100,10 @@ static void tscInitSqlContext(SSqlCmd *pCmd, SLocalMerger *pReducer, tOrderDescr } else if (functionId == TSDB_FUNC_APERCT) { pCtx->param[0].i64 = pExpr->param[0].i64; pCtx->param[0].nType = pExpr->param[0].nType; + } else if (functionId == TSDB_FUNC_BLKINFO) { + pCtx->param[0].i64 = pExpr->param[0].i64; + pCtx->param[0].nType = pExpr->param[0].nType; + pCtx->numOfParams = 1; } pCtx->interBufBytes = pExpr->interBytes; @@ -951,10 +955,10 @@ static void doFillResult(SSqlObj *pSql, SLocalMerger *pLocalMerge, bool doneOutp // todo extract function int64_t actualETime = (pQueryInfo->order.order == TSDB_ORDER_ASC)? pQueryInfo->window.ekey: pQueryInfo->window.skey; - tFilePage **pResPages = malloc(POINTER_BYTES * pQueryInfo->fieldsInfo.numOfOutput); + void** pResPages = malloc(POINTER_BYTES * pQueryInfo->fieldsInfo.numOfOutput); for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) { TAOS_FIELD *pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, i); - pResPages[i] = calloc(1, sizeof(tFilePage) + pField->bytes * pLocalMerge->resColModel->capacity); + pResPages[i] = calloc(1, pField->bytes * pLocalMerge->resColModel->capacity); } while (1) { @@ -966,7 +970,7 @@ static void doFillResult(SSqlObj *pSql, SLocalMerger *pLocalMerge, bool doneOutp if (pQueryInfo->limit.offset > 0) { for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) { TAOS_FIELD *pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, i); - memmove(pResPages[i]->data, pResPages[i]->data + pField->bytes * pQueryInfo->limit.offset, + memmove(pResPages[i], ((char*)pResPages[i]) + pField->bytes * pQueryInfo->limit.offset, (size_t)(newRows * pField->bytes)); } } @@ -1010,7 +1014,7 @@ static void doFillResult(SSqlObj *pSql, SLocalMerger *pLocalMerge, bool doneOutp int32_t offset = 0; for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) { TAOS_FIELD *pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, i); - memcpy(pRes->data + offset * pRes->numOfRows, pResPages[i]->data, (size_t)(pField->bytes * pRes->numOfRows)); + memcpy(pRes->data + offset * pRes->numOfRows, pResPages[i], (size_t)(pField->bytes * pRes->numOfRows)); offset += pField->bytes; } diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index a5906f5539a8d40b055f580933feb4b1184331f1..41bfe3456173fe3c87eac7a09fcaf3ae42dea38d 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -307,7 +307,8 @@ int32_t tsParseOneColumnData(SSchema *pSchema, SStrToken *pToken, char *payload, return tscInvalidSQLErrMsg(msg, "illegal float data", pToken->z); } - *((float *)payload) = (float)dv; +// *((float *)payload) = (float)dv; + SET_FLOAT_VAL(payload, dv); } break; @@ -1359,7 +1360,7 @@ int tsParseSql(SSqlObj *pSql, bool initial) { } } } else { - SSqlInfo SQLInfo = qSQLParse(pSql->sqlstr); + SSqlInfo SQLInfo = qSqlParse(pSql->sqlstr); ret = tscToSQLCmd(pSql, &SQLInfo); if (ret == TSDB_CODE_TSC_INVALID_SQL && pSql->parseRetry == 0 && SQLInfo.type == TSDB_SQL_NULL) { tscResetSqlCmd(pCmd, true); diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 7219e56cb9c8cfb035e44b5337907688f90f29c8..8b2998c0e71c501ea0d13a08aa347662ca2e5ae4 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -75,32 +75,31 @@ static int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int3 static int32_t insertResultField(SQueryInfo* pQueryInfo, int32_t outputIndex, SColumnList* pIdList, int16_t bytes, int8_t type, char* fieldName, SSqlExpr* pSqlExpr); -static int32_t convertFunctionId(int32_t optr, int16_t* functionId); static uint8_t convertOptr(SStrToken *pToken); -static int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSelection, bool isSTable, bool joinQuery, bool intervalQuery); +static int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, SArray* pSelectList, bool isSTable, bool joinQuery, bool timeWindowQuery); static bool validateIpAddress(const char* ip, size_t size); static bool hasUnsupportFunctionsForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo); -static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool intervalQuery); +static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool twQuery); static int32_t parseGroupbyClause(SQueryInfo* pQueryInfo, SArray* pList, SSqlCmd* pCmd); -static int32_t parseIntervalClause(SSqlObj* pSql, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql); -static int32_t parseOffsetClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql); -static int32_t parseSlidingClause(SSqlObj* pSql, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql); +static int32_t parseIntervalClause(SSqlObj* pSql, SQueryInfo* pQueryInfo, SQuerySqlNode* pQuerySqlNode); +static int32_t parseIntervalOffset(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* offsetToken); +static int32_t parseSlidingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* pSliding); static int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExprItem* pItem); -static int32_t parseWhereClause(SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SSqlObj* pSql); -static int32_t parseFillClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySQL); -static int32_t parseOrderbyClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql, SSchema* pSchema); +static int32_t parseWhereClause(SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SSqlObj* pSql); +static int32_t parseFillClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySqlNode* pQuerySQL); +static int32_t parseOrderbyClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySqlNode* pQuerySqlNode, SSchema* pSchema); static int32_t tsRewriteFieldNameIfNecessary(SSqlCmd* pCmd, SQueryInfo* pQueryInfo); 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 validateArithmeticSQLExpr(SSqlCmd* pCmd, tSQLExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList, int32_t* type); +static int32_t validateArithmeticSQLExpr(SSqlCmd* pCmd, tSqlExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList, int32_t* type); static int32_t validateEp(char* ep); static int32_t validateDNodeConfig(SMiscInfo* pOptions); static int32_t validateLocalConfig(SMiscInfo* pOptions); @@ -111,15 +110,14 @@ static bool validateOneTags(SSqlCmd* pCmd, TAOS_FIELD* pTagField); static bool hasTimestampForPointInterpQuery(SQueryInfo* pQueryInfo); static bool hasNormalColumnFilter(SQueryInfo* pQueryInfo); -static int32_t parseLimitClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t index, SQuerySQL* pQuerySql, SSqlObj* pSql); +static int32_t parseLimitClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t index, SQuerySqlNode* pQuerySqlNode, SSqlObj* pSql); static int32_t parseCreateDBOptions(SSqlCmd* pCmd, SCreateDbInfo* pCreateDbSql); static int32_t getColumnIndexByName(SSqlCmd* pCmd, const SStrToken* pToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex); static int32_t getTableIndexByName(SStrToken* pToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex); -static int32_t optrToString(tSQLExpr* pExpr, char** exprString); static int32_t getTableIndexImpl(SStrToken* pTableToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex); static int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo); -static int32_t doLocalQueryProcess(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql); +static int32_t doLocalQueryProcess(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySqlNode* pQuerySqlNode); static int32_t tscCheckCreateDbParams(SSqlCmd* pCmd, SCreateDbMsg* pCreate); static SColumnList getColumnList(int32_t num, int16_t tableIndex, int32_t columnIndex); @@ -127,9 +125,13 @@ static SColumnList getColumnList(int32_t num, int16_t tableIndex, int32_t column static int32_t doCheckForCreateTable(SSqlObj* pSql, int32_t subClauseIndex, SSqlInfo* pInfo); static int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo); static int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo); -static int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index); -static int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSQLExpr* pSqlExpr, SQueryInfo* pQueryInfo, SArray* pCols, int64_t *uid); -static bool validateDebugFlag(int32_t flag); +static int32_t doValidateSqlNode(SSqlObj* pSql, SQuerySqlNode* pQuerySqlNode, int32_t index); +static int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pSqlExpr, SQueryInfo* pQueryInfo, SArray* pCols, int64_t *uid); +static bool validateDebugFlag(int32_t v); + +static bool isTimeWindowQuery(SQueryInfo* pQueryInfo) { + return pQueryInfo->interval.interval > 0 || pQueryInfo->sessionWindow.gap > 0; +} int16_t getNewResColId(SQueryInfo* pQueryInfo) { return pQueryInfo->resColumnId--; @@ -176,11 +178,11 @@ static uint8_t convertOptr(SStrToken *pToken) { static bool validateDebugFlag(int32_t v) { const static int validFlag[] = {131, 135, 143}; - + for (int i = 0; i < tListLen(validFlag); i++) { if (v == validFlag[i]) { return true; - } + } } return false; } @@ -584,12 +586,12 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { SStrToken* t1 = taosArrayGet(pMiscInfo->a, 1); pCmd->payload[t->n] = ' '; // add sep strncpy(&pCmd->payload[t->n + 1], t1->z, t1->n); - } - return TSDB_CODE_SUCCESS; + } + return TSDB_CODE_SUCCESS; } case TSDB_SQL_CREATE_TABLE: { - SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo; + SCreateTableSql* pCreateTable = pInfo->pCreateTableInfo; if (pCreateTable->type == TSQL_CREATE_TABLE || pCreateTable->type == TSQL_CREATE_STABLE) { if ((code = doCheckForCreateTable(pSql, 0, pInfo)) != TSDB_CODE_SUCCESS) { @@ -624,9 +626,9 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { assert(pCmd->numOfClause == pInfo->subclauseInfo.numOfClause); for (int32_t i = pCmd->clauseIndex; i < pInfo->subclauseInfo.numOfClause; ++i) { - SQuerySQL* pQuerySql = pInfo->subclauseInfo.pClause[i]; + SQuerySqlNode* pQuerySqlNode = pInfo->subclauseInfo.pClause[i]; tscTrace("%p start to parse %dth subclause, total:%d", pSql, i, pInfo->subclauseInfo.numOfClause); - if ((code = doCheckForQuery(pSql, pQuerySql, i)) != TSDB_CODE_SUCCESS) { + if ((code = doValidateSqlNode(pSql, pQuerySqlNode, i)) != TSDB_CODE_SUCCESS) { return code; } @@ -702,21 +704,86 @@ static bool isTopBottomQuery(SQueryInfo* pQueryInfo) { return false; } -int32_t parseIntervalClause(SSqlObj* pSql, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql) { +// need to add timestamp column in result set, if it is a time window query +static int32_t addPrimaryTsColumnForTimeWindowQuery(SQueryInfo* pQueryInfo) { + uint64_t uid = tscSqlExprGet(pQueryInfo, 0)->uid; + + int32_t tableIndex = COLUMN_INDEX_INITIAL_VAL; + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, i); + if (pTableMetaInfo->pTableMeta->id.uid == uid) { + tableIndex = i; + break; + } + } + + if (tableIndex == COLUMN_INDEX_INITIAL_VAL) { + return TSDB_CODE_TSC_INVALID_SQL; + } + + SSchema s = {.bytes = TSDB_KEYSIZE, .type = TSDB_DATA_TYPE_TIMESTAMP, .colId = PRIMARYKEY_TIMESTAMP_COL_INDEX}; + 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); + return TSDB_CODE_SUCCESS; +} + +static int32_t checkInvalidExprForTimeWindow(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { const char* msg1 = "invalid query expression"; + const char* msg2 = "top/bottom query does not support order by value in time window query"; + + // for top/bottom + interval query, we do not add additional timestamp column in the front + if (isTopBottomQuery(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 + int32_t colId = pQueryInfo->order.orderColId; + if (isTimeWindowQuery(pQueryInfo) && colId != PRIMARYKEY_TIMESTAMP_COL_INDEX) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); + } + + return TSDB_CODE_SUCCESS; + } + + /* + * invalid sql: + * select count(tbname)/count(tag1)/count(tag2) from super_table_name [interval(1d)|session(ts, 1d)]; + */ + size_t size = tscSqlExprNumOfExprs(pQueryInfo); + for (int32_t i = 0; i < size; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); + if (pExpr->functionId == TSDB_FUNC_COUNT && TSDB_COL_IS_TAG(pExpr->colInfo.flag)) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); + } + } + + /* + * invalid sql: + * select tbname, tags_fields from super_table_name [interval(1s)|session(ts,1s)] + */ + if (tscQueryTags(pQueryInfo) && isTimeWindowQuery(pQueryInfo)) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); + } + + return addPrimaryTsColumnForTimeWindowQuery(pQueryInfo); +} + +int32_t parseIntervalClause(SSqlObj* pSql, SQueryInfo* pQueryInfo, SQuerySqlNode* pQuerySqlNode) { const char* msg2 = "interval cannot be less than 10 ms"; const char* msg3 = "sliding cannot be used without interval"; - const char* msg4 = "top/bottom query does not support order by value in interval query"; SSqlCmd* pCmd = &pSql->cmd; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); - if (pQuerySql->interval.type == 0 || pQuerySql->interval.n == 0) { - if (pQuerySql->sliding.n > 0) { + if (!TPARSER_HAS_TOKEN(pQuerySqlNode->interval.interval)) { + if (TPARSER_HAS_TOKEN(pQuerySqlNode->sliding)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } + return TSDB_CODE_SUCCESS; } @@ -726,7 +793,7 @@ int32_t parseIntervalClause(SSqlObj* pSql, SQueryInfo* pQueryInfo, SQuerySQL* pQ } // interval is not null - SStrToken* t = &pQuerySql->interval; + SStrToken *t = &pQuerySqlNode->interval.interval; if (parseNatualDuration(t->z, t->n, &pQueryInfo->interval.interval, &pQueryInfo->interval.intervalUnit) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } @@ -743,78 +810,64 @@ int32_t parseIntervalClause(SSqlObj* pSql, SQueryInfo* pQueryInfo, SQuerySQL* pQ } } - // for top/bottom + interval query, we do not add additional timestamp column in the front - if (isTopBottomQuery(pQueryInfo)) { - if (parseOffsetClause(pCmd, pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_SQL; - } + if (parseIntervalOffset(pCmd, pQueryInfo, &pQuerySqlNode->interval.offset) != TSDB_CODE_SUCCESS) { + return TSDB_CODE_TSC_INVALID_SQL; + } - if (parseSlidingClause(pSql, pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_SQL; - } + if (parseSlidingClause(pCmd, pQueryInfo, &pQuerySqlNode->sliding) != TSDB_CODE_SUCCESS) { + return TSDB_CODE_TSC_INVALID_SQL; + } - int32_t colId = pQueryInfo->order.orderColId; - if (pQueryInfo->interval.interval > 0 && colId != PRIMARYKEY_TIMESTAMP_COL_INDEX) { - return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); - } + // The following part is used to check for the invalid query expression. + return checkInvalidExprForTimeWindow(pCmd, pQueryInfo); +} + +int32_t parseSessionClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySqlNode * pQuerySqlNode) { + const char* msg1 = "gap should be fixed time window"; + const char* msg2 = "only one type time window allowed"; + const char* msg3 = "invalid column name"; + const char* msg4 = "invalid time window"; + // no session window + if (!TPARSER_HAS_TOKEN(pQuerySqlNode->sessionVal.gap)) { return TSDB_CODE_SUCCESS; } - /* - * check invalid SQL: - * select count(tbname)/count(tag1)/count(tag2) from super_table_name interval(1d); - */ - size_t size = tscSqlExprNumOfExprs(pQueryInfo); - for (int32_t i = 0; i < size; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); - if (pExpr->functionId == TSDB_FUNC_COUNT && TSDB_COL_IS_TAG(pExpr->colInfo.flag)) { - return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); - } + SStrToken* col = &pQuerySqlNode->sessionVal.col; + SStrToken* gap = &pQuerySqlNode->sessionVal.gap; + + char timeUnit = 0; + if (parseNatualDuration(gap->z, gap->n, &pQueryInfo->sessionWindow.gap, &timeUnit) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); } - /* - * check invalid SQL: - * select tbname, tags_fields from super_table_name interval(1s) - */ - if (tscQueryTags(pQueryInfo) && pQueryInfo->interval.interval > 0) { + if (timeUnit == 'y' || timeUnit == 'n') { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } - // need to add timestamp column in result set, if interval is existed - uint64_t uid = tscSqlExprGet(pQueryInfo, 0)->uid; - - int32_t tableIndex = COLUMN_INDEX_INITIAL_VAL; - for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { - pTableMetaInfo = tscGetMetaInfo(pQueryInfo, i); - if (pTableMetaInfo->pTableMeta->id.uid == uid) { - tableIndex = i; - break; - } + // if the unit of time window value is millisecond, change the value from microsecond + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + if (tinfo.precision == TSDB_TIME_PRECISION_MILLI) { + pQueryInfo->sessionWindow.gap = pQueryInfo->sessionWindow.gap / 1000; } - if (tableIndex == COLUMN_INDEX_INITIAL_VAL) { - return TSDB_CODE_TSC_INVALID_SQL; + if (pQueryInfo->sessionWindow.gap != 0 && pQueryInfo->interval.interval != 0) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } - SSchema s = {.bytes = TSDB_KEYSIZE, .type = TSDB_DATA_TYPE_TIMESTAMP, .colId = PRIMARYKEY_TIMESTAMP_COL_INDEX}; - 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); - - if (parseOffsetClause(pCmd, pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_SQL; + SColumnIndex index = COLUMN_INDEX_INITIALIZER; + if (getColumnIndexByName(pCmd, col, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } - if (parseSlidingClause(pSql, pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_SQL; - } + pQueryInfo->sessionWindow.primaryColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; - return TSDB_CODE_SUCCESS; + // The following part is used to check for the invalid query expression. + return checkInvalidExprForTimeWindow(pCmd, pQueryInfo); } -int32_t parseOffsetClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql) { +int32_t parseIntervalOffset(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* offsetToken) { const char* msg1 = "interval offset cannot be negative"; const char* msg2 = "interval offset should be shorter than interval"; const char* msg3 = "cannot use 'year' as offset when interval is 'month'"; @@ -822,7 +875,7 @@ int32_t parseOffsetClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQue STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); - SStrToken* t = &pQuerySql->offset; + SStrToken* t = offsetToken; if (t->n == 0) { pQueryInfo->interval.offsetUnit = pQueryInfo->interval.intervalUnit; pQueryInfo->interval.offset = 0; @@ -865,20 +918,17 @@ int32_t parseOffsetClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQue return TSDB_CODE_SUCCESS; } -int32_t parseSlidingClause(SSqlObj* pSql, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql) { +int32_t parseSlidingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* pSliding) { const char* msg0 = "sliding value too small"; const char* msg1 = "sliding value no larger than the interval value"; const char* msg2 = "sliding value can not less than 1% of interval value"; const char* msg3 = "does not support sliding when interval is natural month/year"; -// const char* msg4 = "sliding not support yet in ordinary query"; const static int32_t INTERVAL_SLIDING_FACTOR = 100; - SSqlCmd* pCmd = &pSql->cmd; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); - SStrToken* pSliding = &pQuerySql->sliding; if (pSliding->n == 0) { pQueryInfo->interval.slidingUnit = pQueryInfo->interval.intervalUnit; pQueryInfo->interval.sliding = pQueryInfo->interval.interval; @@ -970,7 +1020,7 @@ static bool validateTableColumnInfo(SArray* pFieldList, SSqlCmd* pCmd) { const char* msg1 = "first column must be timestamp"; const char* msg2 = "row length exceeds max length"; const char* msg3 = "duplicated column names"; - const char* msg4 = "invalid data types"; + const char* msg4 = "invalid data type"; const char* msg5 = "invalid binary/nchar column length"; const char* msg6 = "invalid column name"; @@ -991,14 +1041,13 @@ static bool validateTableColumnInfo(SArray* pFieldList, SSqlCmd* pCmd) { int32_t nLen = 0; for (int32_t i = 0; i < numOfCols; ++i) { pField = taosArrayGet(pFieldList, i); - - if (pField->bytes == 0) { - invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); + if (!isValidDataType(pField->type)) { + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); return false; } - if (!isValidDataType(pField->type)) { - invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); + if (pField->bytes == 0) { + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); return false; } @@ -1191,7 +1240,7 @@ bool validateOneColumn(SSqlCmd* pCmd, TAOS_FIELD* pColField) { const char* msg1 = "too many columns"; const char* msg2 = "duplicated column names"; const char* msg3 = "column length too long"; - const char* msg4 = "invalid data types"; + const char* msg4 = "invalid data type"; const char* msg5 = "invalid column name"; const char* msg6 = "invalid column length"; @@ -1537,10 +1586,11 @@ bool isValidDistinctSql(SQueryInfo* pQueryInfo) { } return false; } -int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSelection, bool isSTable, bool joinQuery, bool intervalQuery) { - assert(pSelection != NULL && pCmd != NULL); - const char* msg2 = "functions can not be mixed up"; +int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, SArray* pSelectList, bool isSTable, bool joinQuery, bool timeWindowQuery) { + assert(pSelectList != NULL && pCmd != NULL); + + const char* msg2 = "functions or others can not be mixed up"; const char* msg3 = "not support query expression"; const char* msg5 = "invalid function name"; const char* msg6 = "only support distinct one tag"; @@ -1550,34 +1600,35 @@ int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSel if (pQueryInfo->colList == NULL) { pQueryInfo->colList = taosArrayInit(4, POINTER_BYTES); } + bool hasDistinct = false; - for (int32_t i = 0; i < pSelection->nExpr; ++i) { + size_t numOfExpr = taosArrayGetSize(pSelectList); + for (int32_t i = 0; i < numOfExpr; ++i) { int32_t outputIndex = (int32_t)tscSqlExprNumOfExprs(pQueryInfo); - tSqlExprItem* pItem = &pSelection->a[i]; + tSqlExprItem* pItem = taosArrayGet(pSelectList, i); if (hasDistinct == false) { hasDistinct = (pItem->distinct == true); } - // project on all fields - int32_t optr = pItem->pNode->nSQLOptr; - if (optr == TK_ALL || optr == TK_ID || optr == TK_STRING || optr == TK_INTEGER || optr == TK_FLOAT) { - // it is actually a function, but the function name is invalid - if (pItem->pNode->nSQLOptr == TK_ID && (pItem->pNode->colInfo.z == NULL && pItem->pNode->colInfo.n == 0)) { + int32_t type = pItem->pNode->type; + if (type == SQL_NODE_SQLFUNCTION) { + pItem->pNode->functionId = isValidFunction(pItem->pNode->operand.z, pItem->pNode->operand.n); + if (pItem->pNode->functionId < 0) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); } - // select table_name1.field_name1, table_name2.field_name2 from table_name1, table_name2 - if (addProjectionExprAndResultField(pCmd, pQueryInfo, pItem) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_SQL; - } - } else if (pItem->pNode->nSQLOptr >= TK_COUNT && pItem->pNode->nSQLOptr <= TK_TBID) { // sql function in selection clause, append sql function info in pSqlCmd structure sequentially if (addExprAndResultField(pCmd, pQueryInfo, outputIndex, pItem, true) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } - - } else if (pItem->pNode->nSQLOptr >= TK_PLUS && pItem->pNode->nSQLOptr <= TK_REM) { + } else if (type == SQL_NODE_TABLE_COLUMN || type == SQL_NODE_VALUE) { + // use the dynamic array list to decide if the function is valid or not + // select table_name1.field_name1, table_name2.field_name2 from table_name1, table_name2 + if (addProjectionExprAndResultField(pCmd, pQueryInfo, pItem) != TSDB_CODE_SUCCESS) { + return TSDB_CODE_TSC_INVALID_SQL; + } + } else if (type == SQL_NODE_EXPR) { int32_t code = handleArithmeticExpr(pCmd, clauseIndex, i, pItem); if (code != TSDB_CODE_SUCCESS) { return code; @@ -1600,11 +1651,11 @@ int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSel // 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 && !tscQueryTags(pQueryInfo)) { + if (numOfSrcCols <= 0 && !tscQueryTags(pQueryInfo) && !tscQueryBlockInfo(pQueryInfo)) { addPrimaryTsColIntoResult(pQueryInfo); } - if (!functionCompatibleCheck(pQueryInfo, joinQuery, intervalQuery)) { + if (!functionCompatibleCheck(pQueryInfo, joinQuery, timeWindowQuery)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -1727,7 +1778,7 @@ int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, t const char* msg1 = "tag for normal table query is not allowed"; int32_t startPos = (int32_t)tscSqlExprNumOfExprs(pQueryInfo); - int32_t optr = pItem->pNode->nSQLOptr; + int32_t optr = pItem->pNode->tokenId; if (optr == TK_ALL) { // project on all fields TSDB_QUERY_SET_TYPE(pQueryInfo->type, TSDB_QUERY_TYPE_PROJECTION_QUERY); @@ -1757,13 +1808,13 @@ int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, t index.columnIndex = (pQueryInfo->udColumnId--); index.tableIndex = 0; - SSchema colSchema = tGetUserSpecifiedColumnSchema(&pItem->pNode->val, &pItem->pNode->token, pItem->aliasName); + SSchema colSchema = tGetUserSpecifiedColumnSchema(&pItem->pNode->value, &pItem->pNode->token, pItem->aliasName); SSqlExpr* pExpr = tscAddFuncInSelectClause(pQueryInfo, startPos, TSDB_FUNC_PRJ, &index, &colSchema, TSDB_COL_UDC); // NOTE: the first parameter is reserved for the tag column id during join query process. pExpr->numOfParams = 2; - tVariantAssign(&pExpr->param[1], &pItem->pNode->val); + tVariantAssign(&pExpr->param[1], &pItem->pNode->value); } else if (optr == TK_ID) { SColumnIndex index = COLUMN_INDEX_INITIALIZER; @@ -1872,27 +1923,25 @@ void setResultColName(char* name, tSqlExprItem* pItem, int32_t functionId, SStrT } } - -void setLastOrderForGoupBy(SQueryInfo* pQueryInfo, STableMetaInfo* pTableMetaInfo) { // todo refactor - SSqlGroupbyExpr* pGroupBy = &pQueryInfo->groupbyExpr; - if (pGroupBy->numOfGroupCols > 0) { - size_t idx = taosArrayGetSize(pQueryInfo->exprList); - for(int32_t k = 0; k < pGroupBy->numOfGroupCols; ++k) { - SColIndex* pIndex = taosArrayGet(pGroupBy->columnInfo, k); - if (!TSDB_COL_IS_TAG(pIndex->flag) && pIndex->colIndex < tscGetNumOfColumns(pTableMetaInfo->pTableMeta)) { // group by normal columns - SSqlExpr* pExpr = taosArrayGetP(pQueryInfo->exprList, idx - 1); - pExpr->numOfParams = 1; - pExpr->param->i64 = TSDB_ORDER_ASC; - - break; +static void updateLastScanOrderIfNeeded(SQueryInfo* pQueryInfo) { + if (pQueryInfo->sessionWindow.gap > 0 || tscGroupbyColumn(pQueryInfo)) { + size_t numOfExpr = tscSqlExprNumOfExprs(pQueryInfo); + for (int32_t i = 0; i < numOfExpr; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); + if (pExpr->functionId != TSDB_FUNC_LAST && pExpr->functionId != TSDB_FUNC_LAST_DST) { + continue; } + + pExpr->numOfParams = 1; + pExpr->param->i64 = TSDB_ORDER_ASC; + pExpr->param->nType = TSDB_DATA_TYPE_INT; } } } int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t colIndex, tSqlExprItem* pItem, bool finalResult) { STableMetaInfo* pTableMetaInfo = NULL; - int32_t optr = pItem->pNode->nSQLOptr; + int32_t functionId = pItem->pNode->functionId; const char* msg1 = "not support column types"; const char* msg2 = "invalid parameters"; @@ -1902,28 +1951,22 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col const char* msg6 = "function applied to tags not allowed"; const char* msg7 = "normal table can not apply this function"; const char* msg8 = "multi-columns selection does not support alias column name"; - const char* msg9 = "invalid function"; - const char* msg10 = "diff can no be applied to unsigned numeric type"; + const char* msg9 = "diff can no be applied to unsigned numeric type"; - switch (optr) { - case TK_COUNT: { + switch (functionId) { + case TSDB_FUNC_COUNT: { /* more than one parameter for count() function */ - if (pItem->pNode->pParam != NULL && pItem->pNode->pParam->nExpr != 1) { + if (pItem->pNode->pParam != NULL && taosArrayGetSize(pItem->pNode->pParam) != 1) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } - int16_t functionID = 0; - if (convertFunctionId(optr, &functionID) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_SQL; - } - SSqlExpr* pExpr = NULL; SColumnIndex index = COLUMN_INDEX_INITIALIZER; if (pItem->pNode->pParam != NULL) { - tSqlExprItem* pParamElem = &pItem->pNode->pParam->a[0]; + tSqlExprItem* pParamElem = taosArrayGet(pItem->pNode->pParam, 0); SStrToken* pToken = &pParamElem->pNode->colInfo; - int16_t sqlOptr = pParamElem->pNode->nSQLOptr; + int16_t sqlOptr = pParamElem->pNode->tokenId; if ((pToken->z == NULL || pToken->n == 0) && (TK_INTEGER != sqlOptr)) /*select count(1) from table*/ { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); @@ -1939,11 +1982,11 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col index = (SColumnIndex){0, PRIMARYKEY_TIMESTAMP_COL_INDEX}; int32_t size = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes; - pExpr = tscSqlExprAppend(pQueryInfo, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, getNewResColId(pQueryInfo), size, false); + pExpr = tscSqlExprAppend(pQueryInfo, functionId, &index, TSDB_DATA_TYPE_BIGINT, size, getNewResColId(pQueryInfo), size, false); } else if (sqlOptr == TK_INTEGER) { // select count(1) from table1 char buf[8] = {0}; int64_t val = -1; - tVariant* pVariant = &pParamElem->pNode->val; + tVariant* pVariant = &pParamElem->pNode->value; if (pVariant->nType == TSDB_DATA_TYPE_BIGINT) { tVariantDump(pVariant, buf, TSDB_DATA_TYPE_BIGINT, true); val = GET_INT64_VAL(buf); @@ -1951,7 +1994,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col if (val == 1) { index = (SColumnIndex){0, PRIMARYKEY_TIMESTAMP_COL_INDEX}; int32_t size = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes; - pExpr = tscSqlExprAppend(pQueryInfo, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, getNewResColId(pQueryInfo), size, false); + pExpr = tscSqlExprAppend(pQueryInfo, functionId, &index, TSDB_DATA_TYPE_BIGINT, size, getNewResColId(pQueryInfo), size, false); } else { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } @@ -1971,12 +2014,12 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col } int32_t size = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes; - pExpr = tscSqlExprAppend(pQueryInfo, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, getNewResColId(pQueryInfo), size, isTag); + pExpr = tscSqlExprAppend(pQueryInfo, functionId, &index, TSDB_DATA_TYPE_BIGINT, size, getNewResColId(pQueryInfo), size, isTag); } } else { // count(*) is equalled to count(primary_timestamp_key) index = (SColumnIndex){0, PRIMARYKEY_TIMESTAMP_COL_INDEX}; int32_t size = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes; - pExpr = tscSqlExprAppend(pQueryInfo, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, getNewResColId(pQueryInfo), size, false); + pExpr = tscSqlExprAppend(pQueryInfo, functionId, &index, TSDB_DATA_TYPE_BIGINT, size, getNewResColId(pQueryInfo), size, false); } pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); @@ -2001,29 +2044,29 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col return TSDB_CODE_SUCCESS; } - case TK_SUM: - case TK_AVG: - case TK_RATE: - case TK_IRATE: - case TK_SUM_RATE: - case TK_SUM_IRATE: - case TK_AVG_RATE: - case TK_AVG_IRATE: - case TK_TWA: - case TK_MIN: - case TK_MAX: - case TK_DIFF: - case TK_STDDEV: - case TK_LEASTSQUARES: { + case TSDB_FUNC_SUM: + case TSDB_FUNC_AVG: + case TSDB_FUNC_RATE: + case TSDB_FUNC_IRATE: + case TSDB_FUNC_SUM_RATE: + case TSDB_FUNC_SUM_IRATE: + case TSDB_FUNC_AVG_RATE: + case TSDB_FUNC_AVG_IRATE: + case TSDB_FUNC_TWA: + case TSDB_FUNC_MIN: + case TSDB_FUNC_MAX: + case TSDB_FUNC_DIFF: + case TSDB_FUNC_STDDEV: + case TSDB_FUNC_LEASTSQR: { // 1. valid the number of parameters - if (pItem->pNode->pParam == NULL || (optr != TK_LEASTSQUARES && pItem->pNode->pParam->nExpr != 1) || - (optr == TK_LEASTSQUARES && pItem->pNode->pParam->nExpr != 3)) { + if (pItem->pNode->pParam == NULL || (functionId != TSDB_FUNC_LEASTSQR && taosArrayGetSize(pItem->pNode->pParam) != 1) || + (functionId == TSDB_FUNC_LEASTSQR && taosArrayGetSize(pItem->pNode->pParam) != 3)) { /* no parameters or more than one parameter for function */ return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } - tSqlExprItem* pParamElem = &(pItem->pNode->pParam->a[0]); - if (pParamElem->pNode->nSQLOptr != TK_ALL && pParamElem->pNode->nSQLOptr != TK_ID) { + tSqlExprItem* pParamElem = taosArrayGet(pItem->pNode->pParam, 0); + if (pParamElem->pNode->tokenId != TK_ALL && pParamElem->pNode->tokenId != TK_ID) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -2043,26 +2086,21 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col if (!IS_NUMERIC_TYPE(colType)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); - } else if (IS_UNSIGNED_NUMERIC_TYPE(colType) && optr == TK_DIFF) { - return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg10); + } else if (IS_UNSIGNED_NUMERIC_TYPE(colType) && functionId == TSDB_FUNC_DIFF) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg9); } int16_t resultType = 0; int16_t resultSize = 0; int32_t intermediateResSize = 0; - int16_t functionID = 0; - if (convertFunctionId(optr, &functionID) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_SQL; - } - - if (getResultDataInfo(pSchema->type, pSchema->bytes, functionID, 0, &resultType, &resultSize, + if (getResultDataInfo(pSchema->type, pSchema->bytes, functionId, 0, &resultType, &resultSize, &intermediateResSize, 0, false) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } // set the first column ts for diff query - if (optr == TK_DIFF) { + if (functionId == TSDB_FUNC_DIFF) { colIndex += 1; SColumnIndex indexTS = {.tableIndex = index.tableIndex, .columnIndex = 0}; SSqlExpr* pExpr = tscSqlExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &indexTS, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, @@ -2077,19 +2115,19 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6); } - SSqlExpr* pExpr = tscSqlExprAppend(pQueryInfo, functionID, &index, resultType, resultSize, getNewResColId(pQueryInfo), resultSize, false); + SSqlExpr* pExpr = tscSqlExprAppend(pQueryInfo, functionId, &index, resultType, resultSize, getNewResColId(pQueryInfo), resultSize, false); - if (optr == TK_LEASTSQUARES) { + if (functionId == TSDB_FUNC_LEASTSQR) { /* set the leastsquares parameters */ char val[8] = {0}; - if (tVariantDump(&pParamElem[1].pNode->val, val, TSDB_DATA_TYPE_DOUBLE, true) < 0) { + if (tVariantDump(&pParamElem[1].pNode->value, val, TSDB_DATA_TYPE_DOUBLE, true) < 0) { return TSDB_CODE_TSC_INVALID_SQL; } addExprParams(pExpr, val, TSDB_DATA_TYPE_DOUBLE, DOUBLE_BYTES); memset(val, 0, tListLen(val)); - if (tVariantDump(&pParamElem[2].pNode->val, val, TSDB_DATA_TYPE_DOUBLE, true) < 0) { + if (tVariantDump(&pParamElem[2].pNode->value, val, TSDB_DATA_TYPE_DOUBLE, true) < 0) { return TSDB_CODE_TSC_INVALID_SQL; } @@ -2115,43 +2153,38 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col tscInsertPrimaryTsSourceColumn(pQueryInfo, &index); return TSDB_CODE_SUCCESS; } - case TK_FIRST: - case TK_LAST: - case TK_SPREAD: - case TK_LAST_ROW: - case TK_INTERP: { + case TSDB_FUNC_FIRST: + case TSDB_FUNC_LAST: + case TSDB_FUNC_SPREAD: + case TSDB_FUNC_LAST_ROW: + case TSDB_FUNC_INTERP: { bool requireAllFields = (pItem->pNode->pParam == NULL); - int16_t functionID = 0; - if (convertFunctionId(optr, &functionID) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg9); - } - // 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)))) { + SConvertFunc cvtFunc = {.originFuncId = functionId, .execFuncId = functionId}; + if (functionId == TSDB_FUNC_LAST_ROW && ((!TSWINDOW_IS_EQUAL(pQueryInfo->window, TSWINDOW_INITIALIZER)) || (hasNormalColumnFilter(pQueryInfo)))) { cvtFunc.execFuncId = TSDB_FUNC_LAST; } if (!requireAllFields) { - if (pItem->pNode->pParam->nExpr < 1) { + if (taosArrayGetSize(pItem->pNode->pParam) < 1) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } - if (pItem->pNode->pParam->nExpr > 1 && (pItem->aliasName != NULL && strlen(pItem->aliasName) > 0)) { + if (taosArrayGetSize(pItem->pNode->pParam) > 1 && (pItem->aliasName != NULL && strlen(pItem->aliasName) > 0)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg8); } /* in first/last function, multiple columns can be add to resultset */ - for (int32_t i = 0; i < pItem->pNode->pParam->nExpr; ++i) { - tSqlExprItem* pParamElem = &(pItem->pNode->pParam->a[i]); - if (pParamElem->pNode->nSQLOptr != TK_ALL && pParamElem->pNode->nSQLOptr != TK_ID) { + for (int32_t i = 0; i < taosArrayGetSize(pItem->pNode->pParam); ++i) { + tSqlExprItem* pParamElem = taosArrayGet(pItem->pNode->pParam, i); + if (pParamElem->pNode->tokenId != TK_ALL && pParamElem->pNode->tokenId != TK_ID) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (pParamElem->pNode->nSQLOptr == TK_ALL) { // select table.* + if (pParamElem->pNode->tokenId == TK_ALL) { // select table.* SStrToken tmpToken = pParamElem->pNode->colInfo; if (getTableIndexByName(&tmpToken, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { @@ -2170,10 +2203,6 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col if (setExprInfoForFunctions(pCmd, pQueryInfo, &pSchema[j], cvtFunc, name, colIndex++, &index, finalResult) != 0) { return TSDB_CODE_TSC_INVALID_SQL; } - - if (optr == TK_LAST) { - setLastOrderForGoupBy(pQueryInfo, pTableMetaInfo); - } } } else { @@ -2189,19 +2218,14 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col } char name[TSDB_COL_NAME_LEN] = {0}; - SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, index.columnIndex); - bool multiColOutput = pItem->pNode->pParam->nExpr > 1; + bool multiColOutput = taosArrayGetSize(pItem->pNode->pParam) > 1; setResultColName(name, pItem, cvtFunc.originFuncId, &pParamElem->pNode->colInfo, multiColOutput); - if (setExprInfoForFunctions(pCmd, pQueryInfo, pSchema, cvtFunc, name, colIndex++, &index, finalResult) != 0) { + if (setExprInfoForFunctions(pCmd, pQueryInfo, pSchema, cvtFunc, name, colIndex + i, &index, finalResult) != 0) { return TSDB_CODE_TSC_INVALID_SQL; } - - if (optr == TK_LAST) { - setLastOrderForGoupBy(pQueryInfo, pTableMetaInfo); - } } } @@ -2228,33 +2252,28 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col if (setExprInfoForFunctions(pCmd, pQueryInfo, &pSchema[index.columnIndex], cvtFunc, name, colIndex, &index, finalResult) != 0) { return TSDB_CODE_TSC_INVALID_SQL; } - colIndex++; - - if (optr == TK_LAST) { - setLastOrderForGoupBy(pQueryInfo, pTableMetaInfo); - } } numOfFields += tscGetNumOfColumns(pTableMetaInfo->pTableMeta); } - return TSDB_CODE_SUCCESS; } } - case TK_TOP: - case TK_BOTTOM: - case TK_PERCENTILE: - case TK_APERCENTILE: { + + case TSDB_FUNC_TOP: + case TSDB_FUNC_BOTTOM: + case TSDB_FUNC_PERCT: + case TSDB_FUNC_APERCT: { // 1. valid the number of parameters - if (pItem->pNode->pParam == NULL || pItem->pNode->pParam->nExpr != 2) { + if (pItem->pNode->pParam == NULL || taosArrayGetSize(pItem->pNode->pParam) != 2) { /* no parameters or more than one parameter for function */ return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } - tSqlExprItem* pParamElem = &(pItem->pNode->pParam->a[0]); - if (pParamElem->pNode->nSQLOptr != TK_ID) { + tSqlExprItem* pParamElem = taosArrayGet(pItem->pNode->pParam, 0); + if (pParamElem->pNode->tokenId != TK_ID) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -2282,11 +2301,11 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col } // 3. valid the parameters - if (pParamElem[1].pNode->nSQLOptr == TK_ID) { + if (pParamElem[1].pNode->tokenId == TK_ID) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } - tVariant* pVariant = &pParamElem[1].pNode->val; + tVariant* pVariant = &pParamElem[1].pNode->value; int8_t resultType = pSchema[index.columnIndex].type; int16_t resultSize = pSchema[index.columnIndex].bytes; @@ -2294,7 +2313,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col char val[8] = {0}; SSqlExpr* pExpr = NULL; - if (optr == TK_PERCENTILE || optr == TK_APERCENTILE) { + if (functionId == TSDB_FUNC_PERCT || functionId == TSDB_FUNC_APERCT) { tVariantDump(pVariant, val, TSDB_DATA_TYPE_DOUBLE, true); double dp = GET_DOUBLE_VAL(val); @@ -2310,10 +2329,6 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col * for dp = 0, it is actually min, * for dp = 100, it is max, */ - int16_t functionId = 0; - if (convertFunctionId(optr, &functionId) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_SQL; - } tscInsertPrimaryTsSourceColumn(pQueryInfo, &index); colIndex += 1; // the first column is ts @@ -2327,11 +2342,6 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); } - int16_t functionId = 0; - if (convertFunctionId(optr, &functionId) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_SQL; - } - // todo REFACTOR // set the first column ts for top/bottom query SColumnIndex index1 = {0, PRIMARYKEY_TIMESTAMP_COL_INDEX}; @@ -2365,19 +2375,20 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col return TSDB_CODE_SUCCESS; }; - case TK_TBID: { + case TSDB_FUNC_TID_TAG: { pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); if (UTIL_TABLE_IS_NORMAL_TABLE(pTableMetaInfo)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7); } // no parameters or more than one parameter for function - if (pItem->pNode->pParam == NULL || pItem->pNode->pParam->nExpr != 1) { + if (pItem->pNode->pParam == NULL || taosArrayGetSize(pItem->pNode->pParam) != 1) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } - tSQLExpr* pParam = pItem->pNode->pParam->a[0].pNode; - + tSqlExprItem* pParamItem = taosArrayGet(pItem->pNode->pParam, 0); + tSqlExpr* pParam = pParamItem->pNode; + SColumnIndex index = COLUMN_INDEX_INITIALIZER; if (getColumnIndexByName(pCmd, &pParam->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); @@ -2433,7 +2444,31 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col return TSDB_CODE_SUCCESS; } - + case TSDB_FUNC_BLKINFO: { + // no parameters or more than one parameter for function + if (pItem->pNode->pParam != NULL && taosArrayGetSize(pItem->pNode->pParam) != 0) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); + } + + SColumnIndex index = {.tableIndex = 0, .columnIndex = TSDB_BLOCK_DIST_COLUMN_INDEX,}; + pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); + + SSchema s = {.name = "block_dist", .type = TSDB_DATA_TYPE_BINARY}; + int32_t inter = 0; + int16_t resType = 0; + int16_t bytes = 0; + getResultDataInfo(TSDB_DATA_TYPE_INT, 4, TSDB_FUNC_BLKINFO, 0, &resType, &bytes, &inter, 0, 0); + + s.bytes = bytes; + s.type = (uint8_t)resType; + SSqlExpr* pExpr = tscAddFuncInSelectClause(pQueryInfo, 0, TSDB_FUNC_BLKINFO, &index, &s, TSDB_COL_TAG); + pExpr->numOfParams = 1; + pExpr->param[0].i64 = pTableMetaInfo->pTableMeta->tableInfo.rowSize; + pExpr->param[0].nType = TSDB_DATA_TYPE_BIGINT; + + return TSDB_CODE_SUCCESS; + } + default: return TSDB_CODE_TSC_INVALID_SQL; } @@ -2458,8 +2493,8 @@ void getColumnName(tSqlExprItem* pItem, char* resultFieldName, int32_t nameLengt if (pItem->aliasName != NULL) { strncpy(resultFieldName, pItem->aliasName, nameLength); } else { - int32_t len = ((int32_t)pItem->pNode->operand.n < nameLength) ? (int32_t)pItem->pNode->operand.n : nameLength; - strncpy(resultFieldName, pItem->pNode->operand.z, len); + int32_t len = ((int32_t)pItem->pNode->token.n < nameLength) ? (int32_t)pItem->pNode->token.n : nameLength; + strncpy(resultFieldName, pItem->pNode->token.z, len); } } @@ -2599,87 +2634,6 @@ int32_t getColumnIndexByName(SSqlCmd* pCmd, const SStrToken* pToken, SQueryInfo* return doGetColumnIndexByName(pCmd, &tmpToken, pQueryInfo, pIndex); } -int32_t convertFunctionId(int32_t optr, int16_t* functionId) { - switch (optr) { - case TK_COUNT: - *functionId = TSDB_FUNC_COUNT; - break; - case TK_SUM: - *functionId = TSDB_FUNC_SUM; - break; - case TK_AVG: - *functionId = TSDB_FUNC_AVG; - break; - case TK_RATE: - *functionId = TSDB_FUNC_RATE; - break; - case TK_IRATE: - *functionId = TSDB_FUNC_IRATE; - break; - case TK_SUM_RATE: - *functionId = TSDB_FUNC_SUM_RATE; - break; - case TK_SUM_IRATE: - *functionId = TSDB_FUNC_SUM_IRATE; - break; - case TK_AVG_RATE: - *functionId = TSDB_FUNC_AVG_RATE; - break; - case TK_AVG_IRATE: - *functionId = TSDB_FUNC_AVG_IRATE; - break; - case TK_MIN: - *functionId = TSDB_FUNC_MIN; - break; - case TK_MAX: - *functionId = TSDB_FUNC_MAX; - break; - case TK_STDDEV: - *functionId = TSDB_FUNC_STDDEV; - break; - case TK_PERCENTILE: - *functionId = TSDB_FUNC_PERCT; - break; - case TK_APERCENTILE: - *functionId = TSDB_FUNC_APERCT; - break; - case TK_FIRST: - *functionId = TSDB_FUNC_FIRST; - break; - case TK_LAST: - *functionId = TSDB_FUNC_LAST; - break; - case TK_LEASTSQUARES: - *functionId = TSDB_FUNC_LEASTSQR; - break; - case TK_TOP: - *functionId = TSDB_FUNC_TOP; - break; - case TK_BOTTOM: - *functionId = TSDB_FUNC_BOTTOM; - break; - case TK_DIFF: - *functionId = TSDB_FUNC_DIFF; - break; - case TK_SPREAD: - *functionId = TSDB_FUNC_SPREAD; - break; - case TK_TWA: - *functionId = TSDB_FUNC_TWA; - break; - case TK_INTERP: - *functionId = TSDB_FUNC_INTERP; - break; - case TK_LAST_ROW: - *functionId = TSDB_FUNC_LAST_ROW; - break; - default: - return -1; - } - - return TSDB_CODE_SUCCESS; -} - int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { SSqlCmd* pCmd = &pSql->cmd; STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0); @@ -2916,23 +2870,40 @@ bool hasUnsupportFunctionsForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) return false; } -static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool intervalQuery) { +static bool groupbyTagsOrNull(SQueryInfo* pQueryInfo) { + if (pQueryInfo->groupbyExpr.columnInfo == NULL || + taosArrayGetSize(pQueryInfo->groupbyExpr.columnInfo) == 0) { + return true; + } + + size_t s = taosArrayGetSize(pQueryInfo->groupbyExpr.columnInfo); + for (int32_t i = 0; i < s; i++) { + SColIndex* colIndex = taosArrayGet(pQueryInfo->groupbyExpr.columnInfo, i); + if (colIndex->flag != TSDB_COL_TAG) { + return false; + } + } + + return true; +} + +static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool twQuery) { int32_t startIdx = 0; size_t numOfExpr = tscSqlExprNumOfExprs(pQueryInfo); assert(numOfExpr > 0); SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, startIdx); - int32_t functionID = pExpr->functionId; // ts function can be simultaneously used with any other functions. + int32_t functionID = pExpr->functionId; if (functionID == TSDB_FUNC_TS || functionID == TSDB_FUNC_TS_DUMMY) { startIdx++; } int32_t factor = functionCompatList[tscSqlExprGet(pQueryInfo, startIdx)->functionId]; - if (tscSqlExprGet(pQueryInfo, 0)->functionId == TSDB_FUNC_LAST_ROW && (joinQuery || intervalQuery)) { + if (tscSqlExprGet(pQueryInfo, 0)->functionId == TSDB_FUNC_LAST_ROW && (joinQuery || twQuery || !groupbyTagsOrNull(pQueryInfo))) { return false; } @@ -2960,7 +2931,7 @@ static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool } } - if (functionId == TSDB_FUNC_LAST_ROW && (joinQuery || intervalQuery)) { + if (functionId == TSDB_FUNC_LAST_ROW && (joinQuery || twQuery || !groupbyTagsOrNull(pQueryInfo))) { return false; } } @@ -3100,10 +3071,10 @@ static SColumnFilterInfo* addColumnFilterInfo(SColumn* pColumn) { } static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SColumnFilterInfo* pColumnFilter, - SColumnIndex* columnIndex, tSQLExpr* pExpr) { + SColumnIndex* columnIndex, tSqlExpr* pExpr) { const char* msg = "not supported filter condition"; - tSQLExpr* pRight = pExpr->pRight; + tSqlExpr* pRight = pExpr->pRight; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, columnIndex->tableIndex); SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, columnIndex->columnIndex); @@ -3113,39 +3084,39 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, colType = TSDB_DATA_TYPE_BIGINT; } else if (colType == TSDB_DATA_TYPE_FLOAT || colType == TSDB_DATA_TYPE_DOUBLE) { colType = TSDB_DATA_TYPE_DOUBLE; - } else if ((colType == TSDB_DATA_TYPE_TIMESTAMP) && (TSDB_DATA_TYPE_BINARY == pRight->val.nType)) { - int retVal = setColumnFilterInfoForTimestamp(pCmd, pQueryInfo, &pRight->val); + } else if ((colType == TSDB_DATA_TYPE_TIMESTAMP) && (TSDB_DATA_TYPE_BINARY == pRight->value.nType)) { + int retVal = setColumnFilterInfoForTimestamp(pCmd, pQueryInfo, &pRight->value); if (TSDB_CODE_SUCCESS != retVal) { return retVal; } } int32_t retVal = TSDB_CODE_SUCCESS; - if (pExpr->nSQLOptr == TK_LE || pExpr->nSQLOptr == TK_LT) { - retVal = tVariantDump(&pRight->val, (char*)&pColumnFilter->upperBndd, colType, false); + 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 } else if (colType == TSDB_DATA_TYPE_BINARY) { - pColumnFilter->pz = (int64_t)calloc(1, pRight->val.nLen + TSDB_NCHAR_SIZE); - pColumnFilter->len = pRight->val.nLen; - retVal = tVariantDump(&pRight->val, (char*)pColumnFilter->pz, colType, false); + pColumnFilter->pz = (int64_t)calloc(1, pRight->value.nLen + TSDB_NCHAR_SIZE); + pColumnFilter->len = pRight->value.nLen; + retVal = tVariantDump(&pRight->value, (char*)pColumnFilter->pz, colType, false); } else if (colType == TSDB_DATA_TYPE_NCHAR) { - // pRight->val.nLen + 1 is larger than the actual nchar string length - pColumnFilter->pz = (int64_t)calloc(1, (pRight->val.nLen + 1) * TSDB_NCHAR_SIZE); - retVal = tVariantDump(&pRight->val, (char*)pColumnFilter->pz, colType, false); + // pRight->value.nLen + 1 is larger than the actual nchar string length + pColumnFilter->pz = (int64_t)calloc(1, (pRight->value.nLen + 1) * TSDB_NCHAR_SIZE); + retVal = tVariantDump(&pRight->value, (char*)pColumnFilter->pz, colType, false); size_t len = twcslen((wchar_t*)pColumnFilter->pz); pColumnFilter->len = len * TSDB_NCHAR_SIZE; } else { - retVal = tVariantDump(&pRight->val, (char*)&pColumnFilter->lowerBndd, colType, false); + retVal = tVariantDump(&pRight->value, (char*)&pColumnFilter->lowerBndd, colType, false); } if (retVal != TSDB_CODE_SUCCESS) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } - switch (pExpr->nSQLOptr) { + switch (pExpr->tokenId) { case TK_LE: pColumnFilter->upperRelOptr = TSDB_RELATION_LESS_EQUAL; break; @@ -3181,162 +3152,45 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, } typedef struct SCondExpr { - tSQLExpr* pTagCond; - tSQLExpr* pTimewindow; + tSqlExpr* pTagCond; + tSqlExpr* pTimewindow; - tSQLExpr* pColumnCond; + tSqlExpr* pColumnCond; - tSQLExpr* pTableCond; + tSqlExpr* pTableCond; int16_t relType; // relation between table name in expression and other tag // filter condition expression, TK_AND or TK_OR int16_t tableCondIndex; - tSQLExpr* pJoinExpr; // join condition + tSqlExpr* pJoinExpr; // join condition bool tsJoin; } SCondExpr; -static int32_t getTimeRange(STimeWindow* win, tSQLExpr* pRight, int32_t optr, int16_t timePrecision); - -static int32_t tSQLExprNodeToString(tSQLExpr* pExpr, char** str) { - if (pExpr->nSQLOptr == TK_ID) { // column name - strncpy(*str, pExpr->colInfo.z, pExpr->colInfo.n); - *str += pExpr->colInfo.n; - - } else if (pExpr->nSQLOptr >= TK_BOOL && pExpr->nSQLOptr <= TK_STRING) { // value - *str += tVariantToString(&pExpr->val, *str); - - } else if (pExpr->nSQLOptr >= TK_COUNT && pExpr->nSQLOptr <= TK_AVG_IRATE) { - /* - * arithmetic expression of aggregation, such as count(ts) + count(ts) *2 - */ - strncpy(*str, pExpr->operand.z, pExpr->operand.n); - *str += pExpr->operand.n; - } else { // not supported operation - assert(false); - } - - return TSDB_CODE_SUCCESS; -} - -// pExpr->nSQLOptr == 0 while handling "is null" query -static bool isExprLeafNode(tSQLExpr* pExpr) { - return (pExpr->pRight == NULL && pExpr->pLeft == NULL) && - (pExpr->nSQLOptr == 0 || pExpr->nSQLOptr == TK_ID || (pExpr->nSQLOptr >= TK_BOOL && pExpr->nSQLOptr <= TK_NCHAR) || pExpr->nSQLOptr == TK_SET); -} - -static bool isExprDirectParentOfLeafNode(tSQLExpr* pExpr) { - return (pExpr->pLeft != NULL && pExpr->pRight != NULL) && - (isExprLeafNode(pExpr->pLeft) && isExprLeafNode(pExpr->pRight)); -} - -static int32_t tSQLExprLeafToString(tSQLExpr* pExpr, bool addParentheses, char** output) { - if (!isExprDirectParentOfLeafNode(pExpr)) { - return TSDB_CODE_TSC_INVALID_SQL; - } - - tSQLExpr* pLeft = pExpr->pLeft; - tSQLExpr* pRight = pExpr->pRight; - - if (addParentheses) { - *(*output) = '('; - *output += 1; - } - - tSQLExprNodeToString(pLeft, output); - if (optrToString(pExpr, output) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_TSC_INVALID_SQL; - } - - tSQLExprNodeToString(pRight, output); - - if (addParentheses) { - *(*output) = ')'; - *output += 1; - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t optrToString(tSQLExpr* pExpr, char** exprString) { - const char* le = "<="; - const char* ge = ">="; - const char* ne = "<>"; - const char* likeOptr = "LIKE"; - - switch (pExpr->nSQLOptr) { - case TK_LE: { - *(int16_t*)(*exprString) = *(int16_t*)le; - *exprString += 1; - break; - } - case TK_GE: { - *(int16_t*)(*exprString) = *(int16_t*)ge; - *exprString += 1; - break; - } - case TK_NE: { - *(int16_t*)(*exprString) = *(int16_t*)ne; - *exprString += 1; - break; - } - - case TK_LT: - *(*exprString) = '<'; - break; - case TK_GT: - *(*exprString) = '>'; - break; - case TK_EQ: - *(*exprString) = '='; - break; - case TK_PLUS: - *(*exprString) = '+'; - break; - case TK_MINUS: - *(*exprString) = '-'; - break; - case TK_STAR: - *(*exprString) = '*'; - break; - case TK_DIVIDE: - *(*exprString) = '/'; - break; - case TK_REM: - *(*exprString) = '%'; - break; - case TK_LIKE: { - int32_t len = sprintf(*exprString, " %s ", likeOptr); - *exprString += (len - 1); - break; - } - default: - return TSDB_CODE_TSC_INVALID_SQL; - } - - *exprString += 1; +static int32_t getTimeRange(STimeWindow* win, tSqlExpr* pRight, int32_t optr, int16_t timePrecision); - return TSDB_CODE_SUCCESS; -} +static int32_t tablenameListToString(tSqlExpr* pExpr, SStringBuilder* sb) { + SArray* pList = pExpr->pParam; -static int32_t tablenameListToString(tSQLExpr* pExpr, SStringBuilder* sb) { - tSQLExprList* pList = pExpr->pParam; - if (pList->nExpr <= 0) { + int32_t size = (int32_t) taosArrayGetSize(pList); + if (size <= 0) { return TSDB_CODE_TSC_INVALID_SQL; } - if (pList->nExpr > 0) { + if (size > 0) { taosStringBuilderAppendStringLen(sb, QUERY_COND_REL_PREFIX_IN, QUERY_COND_REL_PREFIX_IN_LEN); } - for (int32_t i = 0; i < pList->nExpr; ++i) { - tSQLExpr* pSub = pList->a[i].pNode; - taosStringBuilderAppendStringLen(sb, pSub->val.pz, pSub->val.nLen); + for (int32_t i = 0; i < size; ++i) { + tSqlExprItem* pSub = taosArrayGet(pList, i); + tVariant* pVar = &pSub->pNode->value; + + taosStringBuilderAppendStringLen(sb, pVar->pz, pVar->nLen); - if (i < pList->nExpr - 1) { + if (i < size - 1) { taosStringBuilderAppendString(sb, TBNAME_LIST_SEP); } - if (pSub->val.nLen <= 0 || !tscValidateTableNameLength(pSub->val.nLen)) { + if (pVar->nLen <= 0 || !tscValidateTableNameLength(pVar->nLen)) { return TSDB_CODE_TSC_INVALID_SQL; } } @@ -3344,9 +3198,9 @@ static int32_t tablenameListToString(tSQLExpr* pExpr, SStringBuilder* sb) { return TSDB_CODE_SUCCESS; } -static int32_t tablenameCondToString(tSQLExpr* pExpr, SStringBuilder* sb) { +static int32_t tablenameCondToString(tSqlExpr* pExpr, SStringBuilder* sb) { taosStringBuilderAppendStringLen(sb, QUERY_COND_REL_PREFIX_LIKE, QUERY_COND_REL_PREFIX_LIKE_LEN); - taosStringBuilderAppendString(sb, pExpr->val.pz); + taosStringBuilderAppendString(sb, pExpr->value.pz); return TSDB_CODE_SUCCESS; } @@ -3358,7 +3212,7 @@ enum { TSQL_EXPR_TBNAME = 3, }; -static int32_t extractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SColumnIndex* pIndex, tSQLExpr* pExpr, int32_t sqlOptr) { +static int32_t extractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SColumnIndex* pIndex, tSqlExpr* pExpr, int32_t sqlOptr) { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, pIndex->tableIndex); STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; @@ -3400,21 +3254,21 @@ static int32_t extractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SC ((pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) ? 1 : 0); if (pColFilter->filterstr) { - if (pExpr->nSQLOptr != TK_EQ - && pExpr->nSQLOptr != TK_NE - && pExpr->nSQLOptr != TK_ISNULL - && pExpr->nSQLOptr != TK_NOTNULL - && pExpr->nSQLOptr != TK_LIKE + if (pExpr->tokenId != TK_EQ + && pExpr->tokenId != TK_NE + && pExpr->tokenId != TK_ISNULL + && pExpr->tokenId != TK_NOTNULL + && pExpr->tokenId != TK_LIKE ) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } } else { - if (pExpr->nSQLOptr == TK_LIKE) { + if (pExpr->tokenId == TK_LIKE) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } if (pSchema->type == TSDB_DATA_TYPE_BOOL) { - if (pExpr->nSQLOptr != TK_EQ && pExpr->nSQLOptr != TK_NE) { + if (pExpr->tokenId != TK_EQ && pExpr->tokenId != TK_NE) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } } @@ -3424,51 +3278,7 @@ static int32_t extractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SC return doExtractColumnFilterInfo(pCmd, pQueryInfo, pColFilter, pIndex, pExpr); } -static void relToString(tSQLExpr* pExpr, char** str) { - assert(pExpr->nSQLOptr == TK_AND || pExpr->nSQLOptr == TK_OR); - - const char* or = "OR"; - const char*and = "AND"; - - // if (pQueryInfo->tagCond.relType == TSQL_STABLE_QTYPE_COND) { - if (pExpr->nSQLOptr == TK_AND) { - strcpy(*str, and); - *str += strlen(and); - } else { - strcpy(*str, or); - *str += strlen(or); - } -} - -UNUSED_FUNC -static int32_t getTagCondString(tSQLExpr* pExpr, char** str) { - if (pExpr == NULL) { - return TSDB_CODE_SUCCESS; - } - - if (!isExprDirectParentOfLeafNode(pExpr)) { - *(*str) = '('; - *str += 1; - - int32_t ret = getTagCondString(pExpr->pLeft, str); - if (ret != TSDB_CODE_SUCCESS) { - return ret; - } - - relToString(pExpr, str); - - ret = getTagCondString(pExpr->pRight, str); - - *(*str) = ')'; - *str += 1; - - return ret; - } - - return tSQLExprLeafToString(pExpr, true, str); -} - -static int32_t getTablenameCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr* pTableCond, SStringBuilder* sb) { +static int32_t getTablenameCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr* pTableCond, SStringBuilder* sb) { const char* msg0 = "invalid table name list"; const char* msg1 = "not string following like"; @@ -3476,8 +3286,8 @@ static int32_t getTablenameCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr* return TSDB_CODE_SUCCESS; } - tSQLExpr* pLeft = pTableCond->pLeft; - tSQLExpr* pRight = pTableCond->pRight; + tSqlExpr* pLeft = pTableCond->pLeft; + tSqlExpr* pRight = pTableCond->pRight; if (!isTablenameToken(&pLeft->colInfo)) { return TSDB_CODE_TSC_INVALID_SQL; @@ -3485,10 +3295,10 @@ static int32_t getTablenameCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr* int32_t ret = TSDB_CODE_SUCCESS; - if (pTableCond->nSQLOptr == TK_IN) { + if (pTableCond->tokenId == TK_IN) { ret = tablenameListToString(pRight, sb); - } else if (pTableCond->nSQLOptr == TK_LIKE) { - if (pRight->nSQLOptr != TK_STRING) { + } else if (pTableCond->tokenId == TK_LIKE) { + if (pRight->tokenId != TK_STRING) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } @@ -3502,18 +3312,18 @@ static int32_t getTablenameCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr* return ret; } -static int32_t getColumnQueryCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr* pExpr, int32_t relOptr) { +static int32_t getColumnQueryCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr* pExpr, int32_t relOptr) { if (pExpr == NULL) { return TSDB_CODE_SUCCESS; } - if (!isExprDirectParentOfLeafNode(pExpr)) { // internal node - int32_t ret = getColumnQueryCondInfo(pCmd, pQueryInfo, pExpr->pLeft, pExpr->nSQLOptr); + if (!tSqlExprIsParentOfLeaf(pExpr)) { // internal node + int32_t ret = getColumnQueryCondInfo(pCmd, pQueryInfo, pExpr->pLeft, pExpr->tokenId); if (ret != TSDB_CODE_SUCCESS) { return ret; } - return getColumnQueryCondInfo(pCmd, pQueryInfo, pExpr->pRight, pExpr->nSQLOptr); + return getColumnQueryCondInfo(pCmd, pQueryInfo, pExpr->pRight, pExpr->tokenId); } else { // handle leaf node SColumnIndex index = COLUMN_INDEX_INITIALIZER; if (getColumnIndexByName(pCmd, &pExpr->pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { @@ -3524,7 +3334,7 @@ static int32_t getColumnQueryCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQ } } -static int32_t getJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr* pExpr) { +static int32_t getJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr* pExpr) { const char* msg1 = "invalid join query condition"; const char* msg2 = "invalid table name in join query"; const char* msg3 = "type of join columns must be identical"; @@ -3534,7 +3344,7 @@ static int32_t getJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr* return TSDB_CODE_SUCCESS; } - if (!isExprDirectParentOfLeafNode(pExpr)) { + if (!tSqlExprIsParentOfLeaf(pExpr)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } @@ -3582,9 +3392,9 @@ static int32_t getJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr* return TSDB_CODE_SUCCESS; } -static int32_t validateSQLExpr(SSqlCmd* pCmd, tSQLExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList, - int32_t* type, uint64_t* uid) { - if (pExpr->nSQLOptr == TK_ID) { +static int32_t validateSQLExpr(SSqlCmd* pCmd, tSqlExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList, + int32_t* type, uint64_t* uid) { + if (pExpr->type == SQL_NODE_TABLE_COLUMN) { if (*type == NON_ARITHMEIC_EXPR) { *type = NORMAL_ARITHMETIC; } else if (*type == AGG_ARIGHTMEIC) { @@ -3606,9 +3416,9 @@ static int32_t validateSQLExpr(SSqlCmd* pCmd, tSQLExpr* pExpr, SQueryInfo* pQuer } pList->ids[pList->num++] = index; - } else if (pExpr->nSQLOptr == TK_FLOAT && (isnan(pExpr->val.dKey) || isinf(pExpr->val.dKey))) { + } else if (pExpr->tokenId == TK_FLOAT && (isnan(pExpr->value.dKey) || isinf(pExpr->value.dKey))) { return TSDB_CODE_TSC_INVALID_SQL; - } else if (pExpr->nSQLOptr >= TK_COUNT && pExpr->nSQLOptr <= TK_AVG_IRATE) { + } else if (pExpr->type == SQL_NODE_SQLFUNCTION) { if (*type == NON_ARITHMEIC_EXPR) { *type = AGG_ARIGHTMEIC; } else if (*type == NORMAL_ARITHMETIC) { @@ -3621,6 +3431,11 @@ static int32_t validateSQLExpr(SSqlCmd* pCmd, tSQLExpr* pExpr, SQueryInfo* pQuer // sql function list in selection clause. // Append the sqlExpr into exprList of pQueryInfo structure sequentially + pExpr->functionId = isValidFunction(pExpr->operand.z, pExpr->operand.n); + if (pExpr->functionId < 0) { + return TSDB_CODE_TSC_INVALID_SQL; + } + if (addExprAndResultField(pCmd, pQueryInfo, outputIndex, &item, false) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } @@ -3653,16 +3468,16 @@ static int32_t validateSQLExpr(SSqlCmd* pCmd, tSQLExpr* pExpr, SQueryInfo* pQuer return TSDB_CODE_SUCCESS; } -static int32_t validateArithmeticSQLExpr(SSqlCmd* pCmd, tSQLExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList, int32_t* type) { +static int32_t validateArithmeticSQLExpr(SSqlCmd* pCmd, tSqlExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList, int32_t* type) { if (pExpr == NULL) { return TSDB_CODE_SUCCESS; } - tSQLExpr* pLeft = pExpr->pLeft; + tSqlExpr* pLeft = pExpr->pLeft; uint64_t uidLeft = 0; uint64_t uidRight = 0; - if (pLeft->nSQLOptr >= TK_PLUS && pLeft->nSQLOptr <= TK_REM) { + if (pLeft->type == SQL_NODE_EXPR) { int32_t ret = validateArithmeticSQLExpr(pCmd, pLeft, pQueryInfo, pList, type); if (ret != TSDB_CODE_SUCCESS) { return ret; @@ -3674,8 +3489,8 @@ static int32_t validateArithmeticSQLExpr(SSqlCmd* pCmd, tSQLExpr* pExpr, SQueryI } } - tSQLExpr* pRight = pExpr->pRight; - if (pRight->nSQLOptr >= TK_PLUS && pRight->nSQLOptr <= TK_REM) { + tSqlExpr* pRight = pExpr->pRight; + if (pRight->type == SQL_NODE_EXPR) { int32_t ret = validateArithmeticSQLExpr(pCmd, pRight, pQueryInfo, pList, type); if (ret != TSDB_CODE_SUCCESS) { return ret; @@ -3695,7 +3510,7 @@ static int32_t validateArithmeticSQLExpr(SSqlCmd* pCmd, tSQLExpr* pExpr, SQueryI return TSDB_CODE_SUCCESS; } -static bool isValidExpr(tSQLExpr* pLeft, tSQLExpr* pRight, int32_t optr) { +static bool isValidExpr(tSqlExpr* pLeft, tSqlExpr* pRight, int32_t optr) { if (pLeft == NULL || (pRight == NULL && optr != TK_IN)) { return false; } @@ -3708,34 +3523,27 @@ static bool isValidExpr(tSQLExpr* pLeft, tSQLExpr* pRight, int32_t optr) { * * However, columnA < 4+12 is valid */ - if (pLeft->nSQLOptr >= TK_COUNT && pLeft->nSQLOptr <= TK_AVG_IRATE) { + if (pLeft->type == SQL_NODE_SQLFUNCTION) { return false; } if (pRight == NULL) { return true; } - - if (pRight->nSQLOptr >= TK_COUNT && pRight->nSQLOptr <= TK_AVG_IRATE) { - return false; - } - if (pLeft->nSQLOptr >= TK_BOOL - && pLeft->nSQLOptr <= TK_BINARY - && pRight->nSQLOptr >= TK_BOOL - && pRight->nSQLOptr <= TK_BINARY) { + if (pLeft->tokenId >= TK_BOOL && pLeft->tokenId <= TK_BINARY && pRight->tokenId >= TK_BOOL && pRight->tokenId <= TK_BINARY) { return false; } return true; } -static void exchangeExpr(tSQLExpr* pExpr) { - tSQLExpr* pLeft = pExpr->pLeft; - tSQLExpr* pRight = pExpr->pRight; +static void exchangeExpr(tSqlExpr* pExpr) { + tSqlExpr* pLeft = pExpr->pLeft; + tSqlExpr* pRight = pExpr->pRight; - if (pRight->nSQLOptr == TK_ID && (pLeft->nSQLOptr == TK_INTEGER || pLeft->nSQLOptr == TK_FLOAT || - pLeft->nSQLOptr == TK_STRING || pLeft->nSQLOptr == TK_BOOL)) { + if (pRight->tokenId == TK_ID && (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 @@ -3743,7 +3551,7 @@ static void exchangeExpr(tSQLExpr* pExpr) { * the column-id is at the left handside. */ uint32_t optr = 0; - switch (pExpr->nSQLOptr) { + switch (pExpr->tokenId) { case TK_LE: optr = TK_GE; break; @@ -3757,28 +3565,28 @@ static void exchangeExpr(tSQLExpr* pExpr) { optr = TK_LE; break; default: - optr = pExpr->nSQLOptr; + optr = pExpr->tokenId; } - pExpr->nSQLOptr = optr; + pExpr->tokenId = optr; SWAP(pExpr->pLeft, pExpr->pRight, void*); } } -static bool validateJoinExprNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr* pExpr, SColumnIndex* pLeftIndex) { +static bool validateJoinExprNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr* pExpr, SColumnIndex* pLeftIndex) { const char* msg1 = "illegal column name"; const char* msg2 = "= is expected in join expression"; const char* msg3 = "join column must have same type"; const char* msg4 = "self join is not allowed"; const char* msg5 = "join table must be the same type(table to table, super table to super table)"; - tSQLExpr* pRight = pExpr->pRight; + tSqlExpr* pRight = pExpr->pRight; - if (pRight->nSQLOptr != TK_ID) { + if (pRight->tokenId != TK_ID) { return true; } - if (pExpr->nSQLOptr != TK_EQ) { + if (pExpr->tokenId != TK_EQ) { invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); return false; } @@ -3816,11 +3624,11 @@ static bool validateJoinExprNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr return true; } -static bool validTableNameOptr(tSQLExpr* pExpr) { +static bool validTableNameOptr(tSqlExpr* pExpr) { const char nameFilterOptr[] = {TK_IN, TK_LIKE}; for (int32_t i = 0; i < tListLen(nameFilterOptr); ++i) { - if (pExpr->nSQLOptr == nameFilterOptr[i]) { + if (pExpr->tokenId == nameFilterOptr[i]) { return true; } } @@ -3828,7 +3636,7 @@ static bool validTableNameOptr(tSQLExpr* pExpr) { return false; } -static int32_t setExprToCond(tSQLExpr** parent, tSQLExpr* pExpr, const char* msg, int32_t parentOptr, char* msgBuf) { +static int32_t setExprToCond(tSqlExpr** parent, tSqlExpr* pExpr, const char* msg, int32_t parentOptr, char* msgBuf) { if (*parent != NULL) { if (parentOptr == TK_OR && msg != NULL) { return invalidSqlErrMsg(msgBuf, msg); @@ -3842,7 +3650,7 @@ static int32_t setExprToCond(tSQLExpr** parent, tSQLExpr* pExpr, const char* msg return TSDB_CODE_SUCCESS; } -static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SCondExpr* pCondExpr, +static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SCondExpr* pCondExpr, int32_t* type, int32_t parentOptr) { const char* msg1 = "table query cannot use tags filter"; const char* msg2 = "illegal column name"; @@ -3853,8 +3661,8 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQL const char* msg7 = "only in/like allowed in filter table name"; const char* msg8 = "wildcard string should be less than 20 characters"; - tSQLExpr* pLeft = (*pExpr)->pLeft; - tSQLExpr* pRight = (*pExpr)->pRight; + tSqlExpr* pLeft = (*pExpr)->pLeft; + tSqlExpr* pRight = (*pExpr)->pRight; int32_t ret = TSDB_CODE_SUCCESS; @@ -3863,7 +3671,7 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQL return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } - assert(isExprDirectParentOfLeafNode(*pExpr)); + assert(tSqlExprIsParentOfLeaf(*pExpr)); STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); STableMeta* pTableMeta = pTableMetaInfo->pTableMeta; @@ -3874,7 +3682,7 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQL } // set join query condition - if (pRight->nSQLOptr == TK_ID) { // no need to keep the timestamp join condition + if (pRight->tokenId == TK_ID) { // no need to keep the timestamp join condition TSDB_QUERY_SET_TYPE(pQueryInfo->type, TSDB_QUERY_TYPE_JOIN_QUERY); pCondExpr->tsJoin = true; @@ -3896,8 +3704,8 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQL } // check for like expression - if ((*pExpr)->nSQLOptr == TK_LIKE) { - if (pRight->val.nLen > TSDB_PATTERN_STRING_MAX_LEN) { + if ((*pExpr)->tokenId == TK_LIKE) { + if (pRight->value.nLen > TSDB_PATTERN_STRING_MAX_LEN) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg8); } @@ -3930,7 +3738,7 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQL *type = TSQL_EXPR_TBNAME; *pExpr = NULL; } else { - if (pRight != NULL && pRight->nSQLOptr == TK_ID) { // join on tag columns for stable query + if (pRight != NULL && pRight->tokenId == TK_ID) { // join on tag columns for stable query if (!validateJoinExprNode(pCmd, pQueryInfo, *pExpr, &index)) { return TSDB_CODE_TSC_INVALID_SQL; } @@ -3954,7 +3762,7 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQL } else { // query on other columns *type = TSQL_EXPR_COLUMN; - if (pRight->nSQLOptr == TK_ID) { // other column cannot be served as the join column + if (pRight->tokenId == TK_ID) { // other column cannot be served as the join column return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); } @@ -3965,7 +3773,7 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQL return ret; } -int32_t getQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SCondExpr* pCondExpr, +int32_t getQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SCondExpr* pCondExpr, int32_t* type, int32_t parentOptr) { if (pExpr == NULL) { return TSDB_CODE_SUCCESS; @@ -3973,23 +3781,23 @@ int32_t getQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr** pExpr const char* msg1 = "query condition between different columns must use 'AND'"; - tSQLExpr* pLeft = (*pExpr)->pLeft; - tSQLExpr* pRight = (*pExpr)->pRight; + tSqlExpr* pLeft = (*pExpr)->pLeft; + tSqlExpr* pRight = (*pExpr)->pRight; - if (!isValidExpr(pLeft, pRight, (*pExpr)->nSQLOptr)) { + if (!isValidExpr(pLeft, pRight, (*pExpr)->tokenId)) { return TSDB_CODE_TSC_INVALID_SQL; } int32_t leftType = -1; int32_t rightType = -1; - if (!isExprDirectParentOfLeafNode(*pExpr)) { - int32_t ret = getQueryCondExpr(pCmd, pQueryInfo, &(*pExpr)->pLeft, pCondExpr, &leftType, (*pExpr)->nSQLOptr); + if (!tSqlExprIsParentOfLeaf(*pExpr)) { + int32_t ret = getQueryCondExpr(pCmd, pQueryInfo, &(*pExpr)->pLeft, pCondExpr, &leftType, (*pExpr)->tokenId); if (ret != TSDB_CODE_SUCCESS) { return ret; } - ret = getQueryCondExpr(pCmd, pQueryInfo, &(*pExpr)->pRight, pCondExpr, &rightType, (*pExpr)->nSQLOptr); + ret = getQueryCondExpr(pCmd, pQueryInfo, &(*pExpr)->pRight, pCondExpr, &rightType, (*pExpr)->tokenId); if (ret != TSDB_CODE_SUCCESS) { return ret; } @@ -3999,7 +3807,7 @@ int32_t getQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr** pExpr * expression is not valid for parent node, it must be TK_AND operator. */ if (leftType != rightType) { - if ((*pExpr)->nSQLOptr == TK_OR && (leftType + rightType != TSQL_EXPR_TBNAME + TSQL_EXPR_TAG)) { + if ((*pExpr)->tokenId == TK_OR && (leftType + rightType != TSQL_EXPR_TBNAME + TSQL_EXPR_TAG)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } } @@ -4013,40 +3821,9 @@ int32_t getQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr** pExpr return handleExprInQueryCond(pCmd, pQueryInfo, pExpr, pCondExpr, type, parentOptr); } -static void doCompactQueryExpr(tSQLExpr** pExpr) { - if (*pExpr == NULL || isExprDirectParentOfLeafNode(*pExpr)) { - return; - } - - if ((*pExpr)->pLeft) { - doCompactQueryExpr(&(*pExpr)->pLeft); - } - - if ((*pExpr)->pRight) { - doCompactQueryExpr(&(*pExpr)->pRight); - } - - if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight == NULL && - ((*pExpr)->nSQLOptr == TK_OR || (*pExpr)->nSQLOptr == TK_AND)) { - tSqlExprNodeDestroy(*pExpr); - *pExpr = NULL; - - } else if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight != NULL) { - tSQLExpr* tmpPtr = (*pExpr)->pRight; - tSqlExprNodeDestroy(*pExpr); - - (*pExpr) = tmpPtr; - } else if ((*pExpr)->pRight == NULL && (*pExpr)->pLeft != NULL) { - tSQLExpr* tmpPtr = (*pExpr)->pLeft; - tSqlExprNodeDestroy(*pExpr); - - (*pExpr) = tmpPtr; - } -} - -static void doExtractExprForSTable(SSqlCmd* pCmd, tSQLExpr** pExpr, SQueryInfo* pQueryInfo, tSQLExpr** pOut, int32_t tableIndex) { - if (isExprDirectParentOfLeafNode(*pExpr)) { - tSQLExpr* pLeft = (*pExpr)->pLeft; +static void doExtractExprForSTable(SSqlCmd* pCmd, tSqlExpr** pExpr, SQueryInfo* pQueryInfo, tSqlExpr** pOut, int32_t tableIndex) { + if (tSqlExprIsParentOfLeaf(*pExpr)) { + tSqlExpr* pLeft = (*pExpr)->pLeft; SColumnIndex index = COLUMN_INDEX_INITIALIZER; if (getColumnIndexByName(pCmd, &pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { @@ -4061,19 +3838,19 @@ static void doExtractExprForSTable(SSqlCmd* pCmd, tSQLExpr** pExpr, SQueryInfo* (*pExpr) = NULL; } else { - *pOut = tSqlExprCreate(NULL, NULL, (*pExpr)->nSQLOptr); + *pOut = tSqlExprCreate(NULL, NULL, (*pExpr)->tokenId); doExtractExprForSTable(pCmd, &(*pExpr)->pLeft, pQueryInfo, &((*pOut)->pLeft), tableIndex); doExtractExprForSTable(pCmd, &(*pExpr)->pRight, pQueryInfo, &((*pOut)->pRight), tableIndex); } } -static tSQLExpr* extractExprForSTable(SSqlCmd* pCmd, tSQLExpr** pExpr, SQueryInfo* pQueryInfo, int32_t tableIndex) { - tSQLExpr* pResExpr = NULL; +static tSqlExpr* extractExprForSTable(SSqlCmd* pCmd, tSqlExpr** pExpr, SQueryInfo* pQueryInfo, int32_t tableIndex) { + tSqlExpr* pResExpr = NULL; if (*pExpr != NULL) { doExtractExprForSTable(pCmd, pExpr, pQueryInfo, &pResExpr, tableIndex); - doCompactQueryExpr(&pResExpr); + tSqlExprCompact(&pResExpr); } return pResExpr; @@ -4093,7 +3870,7 @@ int tableNameCompar(const void* lhs, const void* rhs) { } static int32_t setTableCondForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, const char* account, - tSQLExpr* pExpr, int16_t tableCondIndex, SStringBuilder* sb) { + tSqlExpr* pExpr, int16_t tableCondIndex, SStringBuilder* sb) { const char* msg = "table name too long"; if (pExpr == NULL) { @@ -4105,9 +3882,9 @@ static int32_t setTableCondForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, STagCond* pTagCond = &pQueryInfo->tagCond; pTagCond->tbnameCond.uid = pTableMetaInfo->pTableMeta->id.uid; - assert(pExpr->nSQLOptr == TK_LIKE || pExpr->nSQLOptr == TK_IN); + assert(pExpr->tokenId == TK_LIKE || pExpr->tokenId == TK_IN); - if (pExpr->nSQLOptr == TK_LIKE) { + if (pExpr->tokenId == TK_LIKE) { char* str = taosStringBuilderGetResult(sb, NULL); pQueryInfo->tagCond.tbnameCond.cond = strdup(str); pQueryInfo->tagCond.tbnameCond.len = (int32_t) strlen(str); @@ -4194,7 +3971,7 @@ static bool validateFilterExpr(SQueryInfo* pQueryInfo) { return true; } -static int32_t getTimeRangeFromExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLExpr* pExpr) { +static int32_t getTimeRangeFromExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr* pExpr) { const char* msg0 = "invalid timestamp"; const char* msg1 = "only one time stamp window allowed"; @@ -4202,8 +3979,8 @@ static int32_t getTimeRangeFromExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLE return TSDB_CODE_SUCCESS; } - if (!isExprDirectParentOfLeafNode(pExpr)) { - if (pExpr->nSQLOptr == TK_OR) { + if (!tSqlExprIsParentOfLeaf(pExpr)) { + if (pExpr->tokenId == TK_OR) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } @@ -4219,10 +3996,10 @@ static int32_t getTimeRangeFromExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSQLE STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); - tSQLExpr* pRight = pExpr->pRight; + tSqlExpr* pRight = pExpr->pRight; STimeWindow win = {.skey = INT64_MIN, .ekey = INT64_MAX}; - if (getTimeRange(&win, pRight, pExpr->nSQLOptr, tinfo.precision) != TSDB_CODE_SUCCESS) { + if (getTimeRange(&win, pRight, pExpr->tokenId, tinfo.precision) != TSDB_CODE_SUCCESS) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg0); } @@ -4366,7 +4143,7 @@ static int32_t validateTagCondExpr(SSqlCmd* pCmd, tExprNode *p) { retVal = tVariantDump(vVariant, tmp, schemaType, false); free(tmp); } else if (schemaType == TSDB_DATA_TYPE_NCHAR) { - // pRight->val.nLen + 1 is larger than the actual nchar string length + // pRight->value.nLen + 1 is larger than the actual nchar string length char *tmp = calloc(1, (vVariant->nLen + 1) * TSDB_NCHAR_SIZE); retVal = tVariantDump(vVariant, tmp, schemaType, false); free(tmp); @@ -4383,7 +4160,7 @@ static int32_t validateTagCondExpr(SSqlCmd* pCmd, tExprNode *p) { return TSDB_CODE_SUCCESS; } -static int32_t getTagQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCondExpr* pCondExpr, tSQLExpr** pExpr) { +static int32_t getTagQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCondExpr* pCondExpr, tSqlExpr** pExpr) { int32_t ret = TSDB_CODE_SUCCESS; if (pCondExpr->pTagCond == NULL) { @@ -4391,7 +4168,7 @@ static int32_t getTagQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCondE } for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { - tSQLExpr* p1 = extractExprForSTable(pCmd, pExpr, pQueryInfo, i); + tSqlExpr* p1 = extractExprForSTable(pCmd, pExpr, pQueryInfo, i); if (p1 == NULL) { // no query condition on this table continue; } @@ -4423,7 +4200,7 @@ static int32_t getTagQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCondE } tsSetSTableQueryCond(&pQueryInfo->tagCond, uid, &bw); - doCompactQueryExpr(pExpr); + tSqlExprCompact(pExpr); if (ret == TSDB_CODE_SUCCESS) { ret = validateTagCondExpr(pCmd, p); @@ -4446,7 +4223,7 @@ static int32_t getTagQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCondE return ret; } -int32_t parseWhereClause(SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SSqlObj* pSql) { +int32_t parseWhereClause(SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SSqlObj* pSql) { if (pExpr == NULL) { return TSDB_CODE_SUCCESS; } @@ -4465,11 +4242,11 @@ int32_t parseWhereClause(SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SSqlObj* pSql } int32_t type = 0; - if ((ret = getQueryCondExpr(&pSql->cmd, pQueryInfo, pExpr, &condExpr, &type, (*pExpr)->nSQLOptr)) != TSDB_CODE_SUCCESS) { + if ((ret = getQueryCondExpr(&pSql->cmd, pQueryInfo, pExpr, &condExpr, &type, (*pExpr)->tokenId)) != TSDB_CODE_SUCCESS) { return ret; } - doCompactQueryExpr(pExpr); + tSqlExprCompact(pExpr); // after expression compact, the expression tree is only include tag query condition condExpr.pTagCond = (*pExpr); @@ -4520,9 +4297,9 @@ int32_t parseWhereClause(SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SSqlObj* pSql return ret; } -int32_t getTimeRange(STimeWindow* win, tSQLExpr* pRight, int32_t optr, int16_t timePrecision) { +int32_t getTimeRange(STimeWindow* win, tSqlExpr* pRight, int32_t optr, int16_t timePrecision) { // this is join condition, do nothing - if (pRight->nSQLOptr == TK_ID) { + if (pRight->tokenId == TK_ID) { return TSDB_CODE_SUCCESS; } @@ -4530,42 +4307,42 @@ int32_t getTimeRange(STimeWindow* win, tSQLExpr* pRight, int32_t optr, int16_t t * filter primary ts filter expression like: * where ts in ('2015-12-12 4:8:12') */ - if (pRight->nSQLOptr == TK_SET || optr == TK_IN) { + if (pRight->tokenId == TK_SET || optr == TK_IN) { return TSDB_CODE_TSC_INVALID_SQL; } int64_t val = 0; bool parsed = false; - if (pRight->val.nType == TSDB_DATA_TYPE_BINARY) { - pRight->val.nLen = strdequote(pRight->val.pz); + if (pRight->value.nType == TSDB_DATA_TYPE_BINARY) { + pRight->value.nLen = strdequote(pRight->value.pz); - char* seg = strnchr(pRight->val.pz, '-', pRight->val.nLen, false); + char* seg = strnchr(pRight->value.pz, '-', pRight->value.nLen, false); if (seg != NULL) { - if (taosParseTime(pRight->val.pz, &val, pRight->val.nLen, TSDB_TIME_PRECISION_MICRO, tsDaylight) == TSDB_CODE_SUCCESS) { + if (taosParseTime(pRight->value.pz, &val, pRight->value.nLen, TSDB_TIME_PRECISION_MICRO, tsDaylight) == TSDB_CODE_SUCCESS) { parsed = true; } else { return TSDB_CODE_TSC_INVALID_SQL; } } else { - SStrToken token = {.z = pRight->val.pz, .n = pRight->val.nLen, .type = TK_ID}; - int32_t len = tSQLGetToken(pRight->val.pz, &token.type); + SStrToken token = {.z = pRight->value.pz, .n = pRight->value.nLen, .type = TK_ID}; + int32_t len = tSQLGetToken(pRight->value.pz, &token.type); - if ((token.type != TK_INTEGER && token.type != TK_FLOAT) || len != pRight->val.nLen) { + if ((token.type != TK_INTEGER && token.type != TK_FLOAT) || len != pRight->value.nLen) { return TSDB_CODE_TSC_INVALID_SQL; } } - } else if (pRight->nSQLOptr == TK_INTEGER && timePrecision == TSDB_TIME_PRECISION_MILLI) { + } else if (pRight->tokenId == TK_INTEGER && timePrecision == TSDB_TIME_PRECISION_MILLI) { /* - * if the pRight->nSQLOptr == TK_INTEGER/TK_FLOAT, the value is adaptive, we + * if the pRight->tokenId == TK_INTEGER/TK_FLOAT, the value is adaptive, we * need the time precision in metermeta to transfer the value in MICROSECOND * * Additional check to avoid data overflow */ - if (pRight->val.i64 <= INT64_MAX / 1000) { - pRight->val.i64 *= 1000; + if (pRight->value.i64 <= INT64_MAX / 1000) { + pRight->value.i64 *= 1000; } - } else if (pRight->nSQLOptr == TK_FLOAT && timePrecision == TSDB_TIME_PRECISION_MILLI) { - pRight->val.dKey *= 1000; + } else if (pRight->tokenId == TK_FLOAT && timePrecision == TSDB_TIME_PRECISION_MILLI) { + pRight->value.dKey *= 1000; } if (!parsed) { @@ -4573,7 +4350,7 @@ int32_t getTimeRange(STimeWindow* win, tSQLExpr* pRight, int32_t optr, int16_t t * failed to parse timestamp in regular formation, try next * it may be a epoch time in string format */ - tVariantDump(&pRight->val, (char*)&val, TSDB_DATA_TYPE_BIGINT, true); + tVariantDump(&pRight->value, (char*)&val, TSDB_DATA_TYPE_BIGINT, true); /* * transfer it into MICROSECOND format if it is a string, since for @@ -4581,7 +4358,7 @@ int32_t getTimeRange(STimeWindow* win, tSQLExpr* pRight, int32_t optr, int16_t t * * additional check to avoid data overflow */ - if (pRight->nSQLOptr == TK_STRING && timePrecision == TSDB_TIME_PRECISION_MILLI) { + if (pRight->tokenId == TK_STRING && timePrecision == TSDB_TIME_PRECISION_MILLI) { if (val <= INT64_MAX / 1000) { val *= 1000; } @@ -4640,7 +4417,7 @@ int32_t tsRewriteFieldNameIfNecessary(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { return TSDB_CODE_SUCCESS; } -int32_t parseFillClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySQL) { +int32_t parseFillClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySqlNode* pQuerySQL) { SArray* pFillToken = pQuerySQL->fillType; tVariantListItem* pItem = taosArrayGet(pFillToken, 0); @@ -4762,7 +4539,7 @@ static void setDefaultOrderInfo(SQueryInfo* pQueryInfo) { } } -int32_t parseOrderbyClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql, SSchema* pSchema) { +int32_t parseOrderbyClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySqlNode* pQuerySqlNode, SSchema* pSchema) { const char* msg0 = "only support order by primary timestamp"; const char* msg1 = "invalid column name"; const char* msg2 = "order by primary timestamp or first tag in groupby clause allowed"; @@ -4777,11 +4554,11 @@ int32_t parseOrderbyClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQu pQueryInfo->order.orderColId = 0; return TSDB_CODE_SUCCESS; } - if (pQuerySql->pSortOrder == NULL) { + if (pQuerySqlNode->pSortOrder == NULL) { return TSDB_CODE_SUCCESS; } - SArray* pSortorder = pQuerySql->pSortOrder; + SArray* pSortorder = pQuerySqlNode->pSortOrder; /* * for table query, there is only one or none order option is allowed, which is the @@ -4849,7 +4626,7 @@ int32_t parseOrderbyClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQu if (orderByTags) { pQueryInfo->groupbyExpr.orderIndex = index.columnIndex - tscGetNumOfColumns(pTableMetaInfo->pTableMeta); - tVariantListItem* p1 = taosArrayGet(pQuerySql->pSortOrder, 0); + tVariantListItem* p1 = taosArrayGet(pQuerySqlNode->pSortOrder, 0); pQueryInfo->groupbyExpr.orderType = p1->sortOrder; } else if (isTopBottomQuery(pQueryInfo)) { /* order of top/bottom query in interval is not valid */ @@ -4861,12 +4638,12 @@ int32_t parseOrderbyClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQu return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } - tVariantListItem* p1 = taosArrayGet(pQuerySql->pSortOrder, 0); + tVariantListItem* p1 = taosArrayGet(pQuerySqlNode->pSortOrder, 0); pQueryInfo->order.order = p1->sortOrder; pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; return TSDB_CODE_SUCCESS; } else { - tVariantListItem* p1 = taosArrayGet(pQuerySql->pSortOrder, 0); + tVariantListItem* p1 = taosArrayGet(pQuerySqlNode->pSortOrder, 0); pQueryInfo->order.order = p1->sortOrder; pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; @@ -4879,7 +4656,7 @@ int32_t parseOrderbyClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQu } if (s == 2) { - tVariantListItem *pItem = taosArrayGet(pQuerySql->pSortOrder, 0); + tVariantListItem *pItem = taosArrayGet(pQuerySqlNode->pSortOrder, 0); if (orderByTags) { pQueryInfo->groupbyExpr.orderIndex = index.columnIndex - tscGetNumOfColumns(pTableMetaInfo->pTableMeta); pQueryInfo->groupbyExpr.orderType = pItem->sortOrder; @@ -4888,7 +4665,7 @@ int32_t parseOrderbyClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQu pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; } - pItem = taosArrayGet(pQuerySql->pSortOrder, 1); + pItem = taosArrayGet(pQuerySqlNode->pSortOrder, 1); tVariant* pVar2 = &pItem->pVar; SStrToken cname = {pVar2->nLen, pVar2->nType, pVar2->pz}; if (getColumnIndexByName(pCmd, &cname, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { @@ -4923,13 +4700,13 @@ int32_t parseOrderbyClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQu return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } - tVariantListItem* pItem = taosArrayGet(pQuerySql->pSortOrder, 0); + tVariantListItem* pItem = taosArrayGet(pQuerySqlNode->pSortOrder, 0); pQueryInfo->order.order = pItem->sortOrder; pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; return TSDB_CODE_SUCCESS; } - tVariantListItem* pItem = taosArrayGet(pQuerySql->pSortOrder, 0); + tVariantListItem* pItem = taosArrayGet(pQuerySqlNode->pSortOrder, 0); pQueryInfo->order.order = pItem->sortOrder; } @@ -5392,7 +5169,7 @@ int32_t validateLocalConfig(SMiscInfo* pOptions) { // 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) && + if ((pOption->len == pOptionToken->n) && (strncasecmp(pOption->name, pOptionToken->z, pOptionToken->n) == 0)) { return TSDB_CODE_SUCCESS; } @@ -5407,7 +5184,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) + if ((pOption->len == pOptionToken->n) && (strncasecmp(pOption->name, pOptionToken->z, pOptionToken->n) == 0)) { return TSDB_CODE_SUCCESS; } @@ -5457,7 +5234,7 @@ bool hasTimestampForPointInterpQuery(SQueryInfo* pQueryInfo) { return (pQueryInfo->window.skey == pQueryInfo->window.ekey) && (pQueryInfo->window.skey != 0); } -int32_t parseLimitClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t clauseIndex, SQuerySQL* pQuerySql, SSqlObj* pSql) { +int32_t parseLimitClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t clauseIndex, SQuerySqlNode* pQuerySqlNode, SSqlObj* pSql) { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); const char* msg0 = "soffset/offset can not be less than 0"; @@ -5465,9 +5242,9 @@ int32_t parseLimitClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t clauseIn const char* msg2 = "slimit/soffset can not apply to projection query"; // handle the limit offset value, validate the limit - pQueryInfo->limit = pQuerySql->limit; + pQueryInfo->limit = pQuerySqlNode->limit; pQueryInfo->clauseLimit = pQueryInfo->limit.limit; - pQueryInfo->slimit = pQuerySql->slimit; + pQueryInfo->slimit = pQuerySqlNode->slimit; tscDebug("%p limit:%" PRId64 ", offset:%" PRId64 " slimit:%" PRId64 ", soffset:%" PRId64, pSql, pQueryInfo->limit.limit, pQueryInfo->limit.offset, pQueryInfo->slimit.limit, pQueryInfo->slimit.offset); @@ -5992,8 +5769,8 @@ static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd, SQueryInfo* pQueryInfo SColumnList ids = getColumnList(1, 0, pColIndex->colIndex); insertResultField(pQueryInfo, (int32_t)size, &ids, bytes, (int8_t)type, name, pExpr); } else { - // if this query is "group by" normal column, interval is not allowed - if (pQueryInfo->interval.interval > 0) { + // if this query is "group by" normal column, time window query is not allowed + if (isTimeWindowQuery(pQueryInfo)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -6055,7 +5832,7 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); } - if (pQueryInfo->groupbyExpr.numOfGroupCols > 0 || pQueryInfo->interval.interval > 0) { + if (pQueryInfo->groupbyExpr.numOfGroupCols > 0 || isTimeWindowQuery(pQueryInfo)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); } else { return TSDB_CODE_SUCCESS; @@ -6126,17 +5903,20 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { return checkUpdateTagPrjFunctions(pQueryInfo, pCmd); } } -int32_t doLocalQueryProcess(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql) { +int32_t doLocalQueryProcess(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySqlNode* pQuerySqlNode) { const char* msg1 = "only one expression allowed"; const char* msg2 = "invalid expression in select clause"; const char* msg3 = "invalid function"; - tSQLExprList* pExprList = pQuerySql->pSelection; - if (pExprList->nExpr != 1) { + SArray* pExprList = pQuerySqlNode->pSelectList; + size_t size = taosArrayGetSize(pExprList); + if (size != 1) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } + bool server_status = false; - tSQLExpr* pExpr = pExprList->a[0].pNode; + tSqlExprItem* pExprItem = taosArrayGet(pExprList, 0); + tSqlExpr* pExpr = pExprItem->pNode; if (pExpr->operand.z == NULL) { //handle 'select 1' if (pExpr->token.n == 1 && 0 == strncasecmp(pExpr->token.z, "1", 1)) { @@ -6157,8 +5937,8 @@ int32_t doLocalQueryProcess(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQ index = 2; } else { for (int32_t i = 0; i < tListLen(functionsInfo); ++i) { - if (strncasecmp(functionsInfo[i].name, pExpr->operand.z, functionsInfo[i].len) == 0 && - functionsInfo[i].len == pExpr->operand.n) { + if (strncasecmp(functionsInfo[i].name, pExpr->token.z, functionsInfo[i].len) == 0 && + functionsInfo[i].len == pExpr->token.n) { index = i; break; } @@ -6182,8 +5962,9 @@ int32_t doLocalQueryProcess(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SQuerySQL* pQ SColumnIndex ind = {0}; SSqlExpr* pExpr1 = tscSqlExprAppend(pQueryInfo, TSDB_FUNC_TAG_DUMMY, &ind, TSDB_DATA_TYPE_INT, tDataTypes[TSDB_DATA_TYPE_INT].bytes, getNewResColId(pQueryInfo), tDataTypes[TSDB_DATA_TYPE_INT].bytes, false); - - const char* name = (pExprList->a[0].aliasName != NULL)? pExprList->a[0].aliasName:functionsInfo[index].name; + + tSqlExprItem* item = taosArrayGet(pExprList, 0); + const char* name = (item->aliasName != NULL)? item->aliasName:functionsInfo[index].name; tstrncpy(pExpr1->aliasName, name, tListLen(pExpr1->aliasName)); return TSDB_CODE_SUCCESS; @@ -6318,7 +6099,7 @@ int32_t doCheckForCreateTable(SSqlObj* pSql, int32_t subClauseIndex, SSqlInfo* p SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, subClauseIndex); STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo; + SCreateTableSql* pCreateTable = pInfo->pCreateTableInfo; SArray* pFieldList = pCreateTable->colInfo.pColumns; SArray* pTagList = pCreateTable->colInfo.pTagColumns; @@ -6373,7 +6154,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { SSqlCmd* pCmd = &pSql->cmd; - SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo; + SCreateTableSql* pCreateTable = pInfo->pCreateTableInfo; SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); // two table: the first one is for current table, and the secondary is for the super table. @@ -6453,12 +6234,12 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { tVariantListItem* pItem = taosArrayGet(pValList, i); findColumnIndex = false; - + // todo speedup by using hash list for (int32_t t = 0; t < schemaSize; ++t) { if (strncmp(sToken->z, pTagSchema[t].name, sToken->n) == 0 && strlen(pTagSchema[t].name) == sToken->n) { SSchema* pSchema = &pTagSchema[t]; - + char tagVal[TSDB_MAX_TAGS_LEN]; if (pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) { if (pItem->pVar.nLen > pSchema->bytes) { @@ -6466,9 +6247,9 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } } - + ret = tVariantDump(&(pItem->pVar), tagVal, pSchema->type, true); - + // check again after the convert since it may be converted from binary to nchar. if (pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) { int16_t len = varDataTLen(tagVal); @@ -6477,12 +6258,12 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } } - + if (ret != TSDB_CODE_SUCCESS) { tdDestroyKVRowBuilder(&kvRowBuilder); return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); } - + tdAddColToKVRow(&kvRowBuilder, pSchema->colId, pSchema->type, tagVal); findColumnIndex = true; @@ -6493,7 +6274,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { if (!findColumnIndex) { tdDestroyKVRowBuilder(&kvRowBuilder); return tscInvalidSQLErrMsg(pCmd->payload, "invalid tag name", sToken->z); - } + } } } else { if (schemaSize != valSize) { @@ -6504,7 +6285,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { for (int32_t i = 0; i < valSize; ++i) { SSchema* pSchema = &pTagSchema[i]; tVariantListItem* pItem = taosArrayGet(pValList, i); - + char tagVal[TSDB_MAX_TAGS_LEN]; if (pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) { if (pItem->pVar.nLen > pSchema->bytes) { @@ -6512,9 +6293,9 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } } - + ret = tVariantDump(&(pItem->pVar), tagVal, pSchema->type, true); - + // check again after the convert since it may be converted from binary to nchar. if (pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) { int16_t len = varDataTLen(tagVal); @@ -6523,12 +6304,12 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } } - + if (ret != TSDB_CODE_SUCCESS) { tdDestroyKVRowBuilder(&kvRowBuilder); return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); } - + tdAddColToKVRow(&kvRowBuilder, pSchema->colId, pSchema->type, tagVal); } } @@ -6582,24 +6363,24 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); assert(pQueryInfo->numOfTables == 1); - SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo; + SCreateTableSql* pCreateTable = pInfo->pCreateTableInfo; STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); // if sql specifies db, use it, otherwise use default db SStrToken* pName = &(pCreateTable->name); - SQuerySQL* pQuerySql = pCreateTable->pSelect; + SQuerySqlNode* pQuerySqlNode = pCreateTable->pSelect; if (tscValidateName(pName) != TSDB_CODE_SUCCESS) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } - SArray* pSrcMeterName = pInfo->pCreateTableInfo->pSelect->from; - if (pSrcMeterName == NULL || taosArrayGetSize(pSrcMeterName) == 0) { + SFromInfo* pFromInfo = pInfo->pCreateTableInfo->pSelect->from; + if (pFromInfo == NULL || taosArrayGetSize(pFromInfo->tableList) == 0) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6); } - tVariantListItem* p1 = taosArrayGet(pSrcMeterName, 0); - SStrToken srcToken = {.z = p1->pVar.pz, .n = p1->pVar.nLen, .type = TK_STRING}; + STableNamePair* p1 = taosArrayGet(pFromInfo->tableList, 0); + SStrToken srcToken = {.z = p1->name.z, .n = p1->name.n, .type = TK_STRING}; if (tscValidateName(&srcToken) != TSDB_CODE_SUCCESS) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } @@ -6615,23 +6396,22 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { } bool isSTable = UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo); - if (parseSelectClause(&pSql->cmd, 0, pQuerySql->pSelection, isSTable, false, false) != TSDB_CODE_SUCCESS) { + if (parseSelectClause(&pSql->cmd, 0, pQuerySqlNode->pSelectList, isSTable, false, false) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } - if (pQuerySql->pWhere != NULL) { // query condition in stream computing - if (parseWhereClause(pQueryInfo, &pQuerySql->pWhere, pSql) != TSDB_CODE_SUCCESS) { + if (pQuerySqlNode->pWhere != NULL) { // query condition in stream computing + if (parseWhereClause(pQueryInfo, &pQuerySqlNode->pWhere, pSql) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } } // set interval value - if (parseIntervalClause(pSql, pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) { + if (parseIntervalClause(pSql, pQueryInfo, pQuerySqlNode) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } - if ((pQueryInfo->interval.interval > 0) && - (validateFunctionsInIntervalOrGroupbyQuery(pCmd, pQueryInfo) != TSDB_CODE_SUCCESS)) { + if (isTimeWindowQuery(pQueryInfo) && (validateFunctionsInIntervalOrGroupbyQuery(pCmd, pQueryInfo) != TSDB_CODE_SUCCESS)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -6645,7 +6425,7 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { return code; } - if (pQuerySql->selectToken.n > TSDB_MAX_SAVED_SQL_LEN) { + if (pQuerySqlNode->sqlstr.n > TSDB_MAX_SAVED_SQL_LEN) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); } @@ -6663,12 +6443,12 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { * check if fill operation is available, the fill operation is parsed and executed during query execution, * not here. */ - if (pQuerySql->fillType != NULL) { + if (pQuerySqlNode->fillType != NULL) { if (pQueryInfo->interval.interval == 0) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } - tVariantListItem* pItem = taosArrayGet(pQuerySql->fillType, 0); + tVariantListItem* pItem = taosArrayGet(pQuerySqlNode->fillType, 0); if (pItem->pVar.nType == TSDB_DATA_TYPE_BINARY) { if (!((strncmp(pItem->pVar.pz, "none", 4) == 0 && pItem->pVar.nLen == 4) || (strncmp(pItem->pVar.pz, "null", 4) == 0 && pItem->pVar.nLen == 4))) { @@ -6717,10 +6497,10 @@ static int32_t checkQueryRangeForFill(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { return TSDB_CODE_SUCCESS; } -int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { - assert(pQuerySql != NULL && (pQuerySql->from == NULL || taosArrayGetSize(pQuerySql->from) > 0)); +int32_t doValidateSqlNode(SSqlObj* pSql, SQuerySqlNode* pQuerySqlNode, int32_t index) { + assert(pQuerySqlNode != NULL && (pQuerySqlNode->from == NULL || taosArrayGetSize(pQuerySqlNode->from->tableList) > 0)); - const char* msg0 = "invalid table name"; + const char* msg0 = "invalid table name"; const char* msg1 = "point interpolation query needs timestamp"; const char* msg2 = "fill only available for interval query"; const char* msg3 = "start(end) time of query range required or time range too large"; @@ -6728,6 +6508,7 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { const char* msg5 = "too many columns in selection clause"; const char* msg6 = "too many tables in from clause"; const char* msg7 = "invalid table alias name"; + const char* msg8 = "alias name too long"; int32_t code = TSDB_CODE_SUCCESS; @@ -6742,7 +6523,7 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { assert(pCmd->clauseIndex == index); // too many result columns not support order by in query - if (pQuerySql->pSelection->nExpr > TSDB_MAX_COLUMNS) { + if (taosArrayGetSize(pQuerySqlNode->pSelectList) > TSDB_MAX_COLUMNS) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); } @@ -6753,77 +6534,77 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { * select client_version(); * select server_state(); */ - if (pQuerySql->from == NULL) { - assert(pQuerySql->fillType == NULL && pQuerySql->pGroupby == NULL && pQuerySql->pWhere == NULL && - pQuerySql->pSortOrder == NULL); - return doLocalQueryProcess(pCmd, pQueryInfo, pQuerySql); + if (pQuerySqlNode->from == NULL) { + assert(pQuerySqlNode->fillType == NULL && pQuerySqlNode->pGroupby == NULL && pQuerySqlNode->pWhere == NULL && + pQuerySqlNode->pSortOrder == NULL); + return doLocalQueryProcess(pCmd, pQueryInfo, pQuerySqlNode); } - size_t fromSize = taosArrayGetSize(pQuerySql->from); - if (fromSize > TSDB_MAX_JOIN_TABLE_NUM * 2) { + size_t fromSize = taosArrayGetSize(pQuerySqlNode->from->tableList); + if (fromSize > TSDB_MAX_JOIN_TABLE_NUM) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); } pQueryInfo->command = TSDB_SQL_SELECT; - - if (fromSize > 4) { + if (fromSize > 2) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6); } // set all query tables, which are maybe more than one. - for (int32_t i = 0; i < fromSize; ) { - tVariantListItem* item = taosArrayGet(pQuerySql->from, i); - tVariant* pTableItem = &item->pVar; + for (int32_t i = 0; i < fromSize; ++i) { + STableNamePair* item = taosArrayGet(pQuerySqlNode->from->tableList, i); + SStrToken* pTableItem = &item->name; - if (pTableItem->nType != TSDB_DATA_TYPE_BINARY) { + if (pTableItem->type != TSDB_DATA_TYPE_BINARY) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg0); } - pTableItem->nLen = strdequote(pTableItem->pz); + tscDequoteAndTrimToken(pTableItem); - SStrToken tableName = {.z = pTableItem->pz, .n = pTableItem->nLen, .type = TK_STRING}; + SStrToken tableName = {.z = pTableItem->z, .n = pTableItem->n, .type = TK_STRING}; if (tscValidateName(&tableName) != TSDB_CODE_SUCCESS) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg0); } - if (pQueryInfo->numOfTables <= i/2) { // more than one table + if (pQueryInfo->numOfTables <= i) { // more than one table tscAddEmptyMetaInfo(pQueryInfo); } - STableMetaInfo* pTableMetaInfo1 = tscGetMetaInfo(pQueryInfo, i/2); - - SStrToken t = {.type = TSDB_DATA_TYPE_BINARY, .n = pTableItem->nLen, .z = pTableItem->pz}; - code = tscSetTableFullName(pTableMetaInfo1, &t, pSql); + STableMetaInfo* pTableMetaInfo1 = tscGetMetaInfo(pQueryInfo, i); + code = tscSetTableFullName(pTableMetaInfo1, pTableItem, pSql); if (code != TSDB_CODE_SUCCESS) { return code; } - tVariantListItem* p1 = taosArrayGet(pQuerySql->from, i + 1); - if (p1->pVar.nType != TSDB_DATA_TYPE_BINARY) { - return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7); - } + SStrToken* aliasName = &item->aliasName; + if (TPARSER_HAS_TOKEN(*aliasName)) { + if (aliasName->type != TSDB_DATA_TYPE_BINARY) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7); + } - SStrToken aliasName = {.z = p1->pVar.pz, .n = p1->pVar.nLen, .type = TK_STRING}; - if (tscValidateName(&aliasName) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7); - } + tscDequoteAndTrimToken(aliasName); + + SStrToken aliasName1 = {.z = aliasName->z, .n = aliasName->n, .type = TK_STRING}; + if (tscValidateName(&aliasName1) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7); + } + + if (aliasName1.n >= TSDB_TABLE_NAME_LEN) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg8); + } - // has no table alias name - if (memcmp(pTableItem->pz, p1->pVar.pz, p1->pVar.nLen) == 0) { - strncpy(pTableMetaInfo1->aliasName, tNameGetTableName(&pTableMetaInfo1->name), tListLen(pTableMetaInfo->aliasName)); + strncpy(pTableMetaInfo1->aliasName, aliasName1.z, aliasName1.n); } else { - tstrncpy(pTableMetaInfo1->aliasName, p1->pVar.pz, sizeof(pTableMetaInfo1->aliasName)); + strncpy(pTableMetaInfo1->aliasName, tNameGetTableName(&pTableMetaInfo1->name), tListLen(pTableMetaInfo1->aliasName)); } code = tscGetTableMeta(pSql, pTableMetaInfo1); if (code != TSDB_CODE_SUCCESS) { return code; } - - i += 2; } - assert(pQueryInfo->numOfTables == taosArrayGetSize(pQuerySql->from) / 2); + assert(pQueryInfo->numOfTables == taosArrayGetSize(pQuerySqlNode->from->tableList)); bool isSTable = false; if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { @@ -6839,52 +6620,56 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { } // parse the group by clause in the first place - if (parseGroupbyClause(pQueryInfo, pQuerySql->pGroupby, pCmd) != TSDB_CODE_SUCCESS) { + if (parseGroupbyClause(pQueryInfo, pQuerySqlNode->pGroupby, pCmd) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } // set where info STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); - if (pQuerySql->pWhere != NULL) { - if (parseWhereClause(pQueryInfo, &pQuerySql->pWhere, pSql) != TSDB_CODE_SUCCESS) { + if (pQuerySqlNode->pWhere != NULL) { + if (parseWhereClause(pQueryInfo, &pQuerySqlNode->pWhere, pSql) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } - pQuerySql->pWhere = NULL; + pQuerySqlNode->pWhere = NULL; if (tinfo.precision == TSDB_TIME_PRECISION_MILLI) { pQueryInfo->window.skey = pQueryInfo->window.skey / 1000; pQueryInfo->window.ekey = pQueryInfo->window.ekey / 1000; } } else { // set the time rang - if (taosArrayGetSize(pQuerySql->from) > 2) { // it is a join query, no wher clause is not allowed. + if (taosArrayGetSize(pQuerySqlNode->from->tableList) > 1) { // it is a join query, no where clause is not allowed. return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), "condition missing for join query "); } } - int32_t joinQuery = (pQuerySql->from != NULL && taosArrayGetSize(pQuerySql->from) > 2); + int32_t joinQuery = (pQuerySqlNode->from != NULL && taosArrayGetSize(pQuerySqlNode->from->tableList) > 1); + int32_t timeWindowQuery = + (TPARSER_HAS_TOKEN(pQuerySqlNode->interval.interval) || TPARSER_HAS_TOKEN(pQuerySqlNode->sessionVal.gap)); - int32_t intervalQuery = !(pQuerySql->interval.type == 0 || pQuerySql->interval.n == 0); - - if (parseSelectClause(pCmd, index, pQuerySql->pSelection, isSTable, joinQuery, intervalQuery) != TSDB_CODE_SUCCESS) { + if (parseSelectClause(pCmd, index, pQuerySqlNode->pSelectList, isSTable, joinQuery, timeWindowQuery) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } // set order by info - if (parseOrderbyClause(pCmd, pQueryInfo, pQuerySql, tscGetTableSchema(pTableMetaInfo->pTableMeta)) != TSDB_CODE_SUCCESS) { + if (parseOrderbyClause(pCmd, pQueryInfo, pQuerySqlNode, tscGetTableSchema(pTableMetaInfo->pTableMeta)) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } // set interval value - if (parseIntervalClause(pSql, pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) { + if (parseIntervalClause(pSql, pQueryInfo, pQuerySqlNode) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } else { - if ((pQueryInfo->interval.interval > 0) && + if (isTimeWindowQuery(pQueryInfo) && (validateFunctionsInIntervalOrGroupbyQuery(pCmd, pQueryInfo) != TSDB_CODE_SUCCESS)) { return TSDB_CODE_TSC_INVALID_SQL; } } + if (parseSessionClause(pCmd, pQueryInfo, pQuerySqlNode) != TSDB_CODE_SUCCESS) { + return TSDB_CODE_TSC_INVALID_SQL; + } + // no result due to invalid query time range if (pQueryInfo->window.skey > pQueryInfo->window.ekey) { pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; @@ -6898,13 +6683,12 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { // in case of join query, time range is required. if (QUERY_IS_JOIN_QUERY(pQueryInfo->type)) { int64_t timeRange = ABS(pQueryInfo->window.skey - pQueryInfo->window.ekey); - if (timeRange == 0 && pQueryInfo->window.skey == 0) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } } - if ((code = parseLimitClause(pCmd, pQueryInfo, index, pQuerySql, pSql)) != TSDB_CODE_SUCCESS) { + if ((code = parseLimitClause(pCmd, pQueryInfo, index, pQuerySqlNode, pSql)) != TSDB_CODE_SUCCESS) { return code; } @@ -6912,9 +6696,10 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { return code; } + updateLastScanOrderIfNeeded(pQueryInfo); tscFieldInfoUpdateOffset(pQueryInfo); - if (pQuerySql->fillType != NULL) { + if (pQuerySqlNode->fillType != NULL) { if (pQueryInfo->interval.interval == 0 && (!tscIsPointInterpQuery(pQueryInfo))) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -6927,7 +6712,7 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { return code; } - if ((code = parseFillClause(pCmd, pQueryInfo, pQuerySql)) != TSDB_CODE_SUCCESS) { + if ((code = parseFillClause(pCmd, pQueryInfo, pQuerySqlNode)) != TSDB_CODE_SUCCESS) { return code; } } @@ -6935,7 +6720,7 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { return TSDB_CODE_SUCCESS; // Does not build query message here } -int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSQLExpr* pSqlExpr, SQueryInfo* pQueryInfo, SArray* pCols, int64_t *uid) { +int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pSqlExpr, SQueryInfo* pQueryInfo, SArray* pCols, int64_t *uid) { tExprNode* pLeft = NULL; tExprNode* pRight= NULL; @@ -6953,25 +6738,25 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSQLExpr* pS } } - if (pSqlExpr->pLeft == NULL && pSqlExpr->pRight == NULL && pSqlExpr->nSQLOptr == 0) { + if (pSqlExpr->pLeft == NULL && pSqlExpr->pRight == NULL && pSqlExpr->tokenId == 0) { *pExpr = calloc(1, sizeof(tExprNode)); return TSDB_CODE_SUCCESS; } if (pSqlExpr->pLeft == NULL) { - if (pSqlExpr->nSQLOptr >= TK_BOOL && pSqlExpr->nSQLOptr <= TK_STRING) { + if (pSqlExpr->type == SQL_NODE_VALUE) { *pExpr = calloc(1, sizeof(tExprNode)); (*pExpr)->nodeType = TSQL_NODE_VALUE; (*pExpr)->pVal = calloc(1, sizeof(tVariant)); - tVariantAssign((*pExpr)->pVal, &pSqlExpr->val); + tVariantAssign((*pExpr)->pVal, &pSqlExpr->value); return TSDB_CODE_SUCCESS; - } else if (pSqlExpr->nSQLOptr >= TK_COUNT && pSqlExpr->nSQLOptr <= TK_AVG_IRATE) { + } else if (pSqlExpr->type == SQL_NODE_SQLFUNCTION) { // arithmetic expression on the results of aggregation functions *pExpr = calloc(1, sizeof(tExprNode)); (*pExpr)->nodeType = TSQL_NODE_COL; (*pExpr)->pSchema = calloc(1, sizeof(SSchema)); - strncpy((*pExpr)->pSchema->name, pSqlExpr->operand.z, pSqlExpr->operand.n); + strncpy((*pExpr)->pSchema->name, pSqlExpr->token.z, pSqlExpr->token.n); // set the input column data byte and type. size_t size = taosArrayGetSize(pQueryInfo->exprList); @@ -6991,7 +6776,7 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSQLExpr* pS break; } } - } else if (pSqlExpr->nSQLOptr == TK_ID) { // column name, normal column arithmetic expression + } else if (pSqlExpr->type == SQL_NODE_TABLE_COLUMN) { // column name, normal column arithmetic expression SColumnIndex index = COLUMN_INDEX_INITIALIZER; int32_t ret = getColumnIndexByName(pCmd, &pSqlExpr->colInfo, pQueryInfo, &index); if (ret != TSDB_CODE_SUCCESS) { @@ -7031,7 +6816,7 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSQLExpr* pS (*pExpr)->_node.pLeft = pLeft; (*pExpr)->_node.pRight = pRight; - SStrToken t = {.type = pSqlExpr->nSQLOptr}; + SStrToken t = {.type = pSqlExpr->tokenId}; (*pExpr)->_node.optr = convertOptr(&t); assert((*pExpr)->_node.optr != 0); diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 9e43ae674dfb4a1c845b7ca4db82b1cf6be4d0dd..73a324f5213085cd1720023eb70ac403ae0051d4 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -497,8 +497,6 @@ int tscProcessSql(SSqlObj *pSql) { return pSql->res.code; } } else if (pCmd->command >= TSDB_SQL_LOCAL) { - //pSql->epSet = tscMgmtEpSet; -// } else { // local handler return (*tscProcessMsgRsp[pCmd->command])(pSql); } @@ -645,7 +643,6 @@ static char *doSerializeTableInfo(SQueryTableMsg* pQueryMsg, SSqlObj *pSql, char } pSql->epSet.inUse = rand()%pSql->epSet.numOfEps; - pQueryMsg->head.vgId = htonl(vgId); STableIdInfo *pTableIdInfo = (STableIdInfo *)pMsg; @@ -660,8 +657,6 @@ static char *doSerializeTableInfo(SQueryTableMsg* pQueryMsg, SSqlObj *pSql, char int32_t numOfVgroups = (int32_t)taosArrayGetSize(pTableMetaInfo->pVgroupTables); assert(index >= 0 && index < numOfVgroups); - tscDebug("%p query on stable, vgIndex:%d, numOfVgroups:%d", pSql, index, numOfVgroups); - SVgroupTableInfo* pTableIdList = taosArrayGet(pTableMetaInfo->pVgroupTables, index); // set the vgroup info @@ -670,7 +665,10 @@ static char *doSerializeTableInfo(SQueryTableMsg* pQueryMsg, SSqlObj *pSql, char int32_t numOfTables = (int32_t)taosArrayGetSize(pTableIdList->itemList); pQueryMsg->numOfTables = htonl(numOfTables); // set the number of tables - + + tscDebug("%p query on stable, vgId:%d, numOfTables:%d, vgIndex:%d, numOfVgroups:%d", pSql, + pTableIdList->vgInfo.vgId, numOfTables, index, numOfVgroups); + // serialize each table id info for(int32_t i = 0; i < numOfTables; ++i) { STableIdInfo* pItem = taosArrayGet(pTableIdList->itemList, i); @@ -705,7 +703,7 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { STableMeta * pTableMeta = pTableMetaInfo->pTableMeta; size_t numOfSrcCols = taosArrayGetSize(pQueryInfo->colList); - if (numOfSrcCols <= 0 && !tscQueryTags(pQueryInfo)) { + if (numOfSrcCols <= 0 && !tscQueryTags(pQueryInfo) && !tscQueryBlockInfo(pQueryInfo)) { tscError("%p illegal value of numOfCols in query msg: %" PRIu64 ", table cols:%d", pSql, (uint64_t)numOfSrcCols, tscGetNumOfColumns(pTableMeta)); @@ -756,6 +754,8 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pQueryMsg->vgroupLimit = htobe64(pQueryInfo->vgroupLimit); pQueryMsg->sqlstrLen = htonl(sqlLen); pQueryMsg->prevResultLen = htonl(pQueryInfo->bufLen); + pQueryMsg->sw.gap = htobe64(pQueryInfo->sessionWindow.gap); + pQueryMsg->sw.primaryColId = htonl(PRIMARYKEY_TIMESTAMP_COL_INDEX); size_t numOfOutput = tscSqlExprNumOfExprs(pQueryInfo); pQueryMsg->numOfOutput = htons((int16_t)numOfOutput); // this is the stage one output column number @@ -835,13 +835,31 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pSqlFuncExpr->colInfo.colIndex = htons(pExpr->colInfo.colIndex); pSqlFuncExpr->colInfo.flag = htons(pExpr->colInfo.flag); + if (TSDB_COL_IS_UD_COL(pExpr->colInfo.flag)) { + pSqlFuncExpr->colType = htons(pExpr->resType); + pSqlFuncExpr->colBytes = htons(pExpr->resBytes); + } else if (pExpr->colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) { + SSchema *s = tGetTbnameColumnSchema(); + + pSqlFuncExpr->colType = htons(s->type); + pSqlFuncExpr->colBytes = htons(s->bytes); + } else if (pExpr->colInfo.colId == TSDB_BLOCK_DIST_COLUMN_INDEX) { + SSchema s = tGetBlockDistColumnSchema(); + + pSqlFuncExpr->colType = htons(s.type); + pSqlFuncExpr->colBytes = htons(s.bytes); + } else { + SSchema* s = tscGetColumnSchemaById(pTableMeta, pExpr->colInfo.colId); + pSqlFuncExpr->colType = htons(s->type); + pSqlFuncExpr->colBytes = htons(s->bytes); + } + pSqlFuncExpr->functionId = htons(pExpr->functionId); pSqlFuncExpr->numOfParams = htons(pExpr->numOfParams); pSqlFuncExpr->resColId = htons(pExpr->resColId); pMsg += sizeof(SSqlFuncMsg); - for (int32_t j = 0; j < pExpr->numOfParams; ++j) { - // todo add log + for (int32_t j = 0; j < pExpr->numOfParams; ++j) { // todo add log pSqlFuncExpr->arg[j].argType = htons((uint16_t)pExpr->param[j].nType); pSqlFuncExpr->arg[j].argBytes = htons(pExpr->param[j].nLen); @@ -866,6 +884,8 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { for (int32_t i = 0; i < output; ++i) { SInternalField* pField = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, i); SSqlExpr *pExpr = pField->pSqlExpr; + + // this should be switched to projection query if (pExpr != NULL) { // the queried table has been removed and a new table with the same name has already been created already // return error msg @@ -879,33 +899,31 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { return TSDB_CODE_TSC_INVALID_SQL; } - pSqlFuncExpr1->colInfo.colId = htons(pExpr->colInfo.colId); - pSqlFuncExpr1->colInfo.colIndex = htons(pExpr->colInfo.colIndex); - pSqlFuncExpr1->colInfo.flag = htons(pExpr->colInfo.flag); - - pSqlFuncExpr1->functionId = htons(pExpr->functionId); - pSqlFuncExpr1->numOfParams = htons(pExpr->numOfParams); - pMsg += sizeof(SSqlFuncMsg); - - for (int32_t j = 0; j < pExpr->numOfParams; ++j) { - // todo add log - pSqlFuncExpr1->arg[j].argType = htons((uint16_t)pExpr->param[j].nType); - pSqlFuncExpr1->arg[j].argBytes = htons(pExpr->param[j].nLen); - - if (pExpr->param[j].nType == TSDB_DATA_TYPE_BINARY) { - memcpy(pMsg, pExpr->param[j].pz, pExpr->param[j].nLen); - pMsg += pExpr->param[j].nLen; - } else { - pSqlFuncExpr1->arg[j].argValue.i64 = htobe64(pExpr->param[j].i64); + pSqlFuncExpr1->numOfParams = 0; // no params for projection query + pSqlFuncExpr1->functionId = htons(TSDB_FUNC_PRJ); + pSqlFuncExpr1->colInfo.colId = htons(pExpr->resColId); + pSqlFuncExpr1->colInfo.flag = htons(TSDB_COL_NORMAL); + + bool assign = false; + for (int32_t f = 0; f < tscSqlExprNumOfExprs(pQueryInfo); ++f) { + SSqlExpr *pe = tscSqlExprGet(pQueryInfo, f); + if (pe == pExpr) { + pSqlFuncExpr1->colInfo.colIndex = htons(f); + pSqlFuncExpr1->colType = htons(pe->resType); + pSqlFuncExpr1->colBytes = htons(pe->resBytes); + assign = true; + break; } } + assert(assign); + pMsg += sizeof(SSqlFuncMsg); pSqlFuncExpr1 = (SSqlFuncMsg *)pMsg; } else { assert(pField->pArithExprInfo != NULL); SExprInfo* pExprInfo = pField->pArithExprInfo; - pSqlFuncExpr1->colInfo.colId = htons(pExprInfo->base.colInfo.colId); + pSqlFuncExpr1->colInfo.colId = htons(pExprInfo->base.colInfo.colId); pSqlFuncExpr1->functionId = htons(pExprInfo->base.functionId); pSqlFuncExpr1->numOfParams = htons(pExprInfo->base.numOfParams); pMsg += sizeof(SSqlFuncMsg); @@ -1332,7 +1350,7 @@ int tscEstimateCreateTableMsgLength(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlCmd *pCmd = &(pSql->cmd); int32_t size = minMsgSize() + sizeof(SCMCreateTableMsg) + sizeof(SCreateTableMsg); - SCreateTableSQL *pCreateTableInfo = pInfo->pCreateTableInfo; + SCreateTableSql *pCreateTableInfo = pInfo->pCreateTableInfo; if (pCreateTableInfo->type == TSQL_CREATE_TABLE_FROM_STABLE) { int32_t numOfTables = (int32_t)taosArrayGetSize(pInfo->pCreateTableInfo->childTableInfo); size += numOfTables * (sizeof(SCreateTableMsg) + TSDB_MAX_TAGS_LEN); @@ -1341,7 +1359,7 @@ int tscEstimateCreateTableMsgLength(SSqlObj *pSql, SSqlInfo *pInfo) { } if (pCreateTableInfo->pSelect != NULL) { - size += (pCreateTableInfo->pSelect->selectToken.n + 1); + size += (pCreateTableInfo->pSelect->sqlstr.n + 1); } return size + TSDB_EXTRA_PAYLOAD_SIZE; @@ -1399,7 +1417,7 @@ int tscBuildCreateTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int32_t code = tNameExtractFullName(&pTableMetaInfo->name, pCreateMsg->tableName); assert(code == 0); - SCreateTableSQL *pCreateTable = pInfo->pCreateTableInfo; + SCreateTableSql *pCreateTable = pInfo->pCreateTableInfo; pCreateMsg->igExists = pCreateTable->existCheck ? 1 : 0; pCreateMsg->numOfColumns = htons(pCmd->numOfCols); @@ -1422,11 +1440,11 @@ int tscBuildCreateTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pMsg = (char *)pSchema; if (type == TSQL_CREATE_STREAM) { // check if it is a stream sql - SQuerySQL *pQuerySql = pInfo->pCreateTableInfo->pSelect; + SQuerySqlNode *pQuerySql = pInfo->pCreateTableInfo->pSelect; - strncpy(pMsg, pQuerySql->selectToken.z, pQuerySql->selectToken.n + 1); - pCreateMsg->sqlLen = htons(pQuerySql->selectToken.n + 1); - pMsg += pQuerySql->selectToken.n + 1; + strncpy(pMsg, pQuerySql->sqlstr.z, pQuerySql->sqlstr.n + 1); + pCreateMsg->sqlLen = htons(pQuerySql->sqlstr.n + 1); + pMsg += pQuerySql->sqlstr.n + 1; } } diff --git a/src/client/src/tscSub.c b/src/client/src/tscSub.c index f3d7ef28c05fc3128622f8217341d52327d79ec0..32257f5a7c723d04ec0f8200c889a68d78cf7824 100644 --- a/src/client/src/tscSub.c +++ b/src/client/src/tscSub.c @@ -503,9 +503,19 @@ TAOS_RES *taos_consume(TAOS_SUB *tsub) { SSqlCmd *pCmd = &pSql->cmd; STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0); SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); - if (taosArrayGetSize(pSub->progress) > 0) { // fix crash in single tabel subscription - pQueryInfo->window.skey = ((SSubscriptionProgress*)taosArrayGet(pSub->progress, 0))->key; - tscDebug("subscribe:%s set subscribe skey:%"PRId64, pSub->topic, pQueryInfo->window.skey); + if (taosArrayGetSize(pSub->progress) > 0) { // fix crash in single table subscription + + size_t size = taosArrayGetSize(pSub->progress); + TSKEY s = INT64_MAX; + for(int32_t i = 0; i < size; ++i) { + TSKEY k = ((SSubscriptionProgress*)taosArrayGet(pSub->progress, i))->key; + if (s > k) { + s = k; + } + } + + pQueryInfo->window.skey = s; + tscDebug("subscribe:%s set next round subscribe skey:%"PRId64, pSub->topic, pQueryInfo->window.skey); } if (pSub->pTimer == NULL) { diff --git a/src/client/src/tscSubquery.c b/src/client/src/tscSubquery.c index e577291966318e659848a42173f5a94f58473550..22cb5809510e0863c8aaeb71e0072a3146b31610 100644 --- a/src/client/src/tscSubquery.c +++ b/src/client/src/tscSubquery.c @@ -74,14 +74,14 @@ static bool allSubqueryDone(SSqlObj *pParentSql) { SSubqueryState *subState = &pParentSql->subState; //lock in caller - + tscDebug("%p total subqueries: %d", pParentSql, subState->numOfSub); for (int i = 0; i < subState->numOfSub; i++) { if (0 == subState->states[i]) { - tscDebug("%p subquery:%p,%d is NOT finished, total:%d", pParentSql, pParentSql->pSubs[i], i, subState->numOfSub); + tscDebug("%p subquery:%p, index: %d NOT finished, abort query completion check", pParentSql, pParentSql->pSubs[i], i); done = false; break; } else { - tscDebug("%p subquery:%p,%d is finished, total:%d", pParentSql, pParentSql->pSubs[i], i, subState->numOfSub); + tscDebug("%p subquery:%p, index: %d finished", pParentSql, pParentSql->pSubs[i], i); } } @@ -453,7 +453,7 @@ static int32_t tscLaunchRealSubqueries(SSqlObj* pSql) { pSubQueryInfo->tsBuf = NULL; // free result for async object will also free sqlObj - assert(tscSqlExprNumOfExprs(pSubQueryInfo) == 1); // ts_comp query only requires one resutl columns + assert(tscSqlExprNumOfExprs(pSubQueryInfo) == 1); // ts_comp query only requires one result columns taos_free_result(pPrevSub); SSqlObj *pNew = createSubqueryObj(pSql, (int16_t) i, tscJoinQueryCallback, pSupporter, TSDB_SQL_SELECT, NULL); @@ -507,6 +507,7 @@ static int32_t tscLaunchRealSubqueries(SSqlObj* pSql) { SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, 0); int16_t funcId = pExpr->functionId; + // add the invisible timestamp column if ((pExpr->colInfo.colId != PRIMARYKEY_TIMESTAMP_COL_INDEX) || (funcId != TSDB_FUNC_TS && funcId != TSDB_FUNC_TS_DUMMY && funcId != TSDB_FUNC_PRJ)) { @@ -847,6 +848,8 @@ static void tidTagRetrieveCallback(void* param, TAOS_RES* tres, int32_t numOfRow SSqlRes* pRes = &pSql->res; SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex); + + // todo, the type may not include TSDB_QUERY_TYPE_TAG_FILTER_QUERY assert(TSDB_QUERY_HAS_TYPE(pQueryInfo->type, TSDB_QUERY_TYPE_TAG_FILTER_QUERY)); if (pParentSql->res.code != TSDB_CODE_SUCCESS) { @@ -1059,7 +1062,6 @@ static void tsCompRetrieveCallback(void* param, TAOS_RES* tres, int32_t numOfRow tscError("%p invalid ts comp file from vnode, abort subquery, file size:%d", pSql, numOfRows); pParentSql->res.code = TAOS_SYSTEM_ERROR(errno); - if (quitAllSubquery(pSql, pParentSql, pSupporter)){ return; } @@ -1880,6 +1882,13 @@ void doAppendData(SInterResult* pInterResult, TAOS_ROW row, int32_t numOfCols, S } } + if (p && taosArrayGetSize(p) > 0) { + SResPair *l = taosArrayGetLast(p); + if (l->key == key && key == INT64_MIN) { + continue; + } + } + //append a new column if (p == NULL) { SStddevInterResult t = {.colId = id, .pResult = taosArrayInit(10, sizeof(SResPair)),}; @@ -2643,12 +2652,17 @@ static SSqlObj *tscCreateSTableSubquery(SSqlObj *pSql, SRetrieveSupport *trsuppo SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(&pNew->cmd, 0); pQueryInfo->type |= TSDB_QUERY_TYPE_STABLE_SUBQUERY; + + // clear the limit/offset info, since it should not be sent to vnode to be executed. + pQueryInfo->limit.limit = -1; + pQueryInfo->limit.offset = 0; + assert(pQueryInfo->numOfTables == 1 && pNew->cmd.numOfClause == 1 && trsupport->subqueryIndex < pSql->subState.numOfSub); // launch subquery for each vnode, so the subquery index equals to the vgroupIndex. STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pQueryInfo, table_index); pTableMetaInfo->vgroupIndex = trsupport->subqueryIndex; - + pSql->pSubs[trsupport->subqueryIndex] = pNew; } @@ -3102,30 +3116,6 @@ void tscBuildResFromSubqueries(SSqlObj *pSql) { } } -static UNUSED_FUNC void transferNcharData(SSqlObj *pSql, int32_t columnIndex, TAOS_FIELD *pField) { - SSqlRes *pRes = &pSql->res; - - if (pRes->tsrow[columnIndex] != NULL && pField->type == TSDB_DATA_TYPE_NCHAR) { - // convert unicode to native code in a temporary buffer extra one byte for terminated symbol - if (pRes->buffer[columnIndex] == NULL) { - pRes->buffer[columnIndex] = malloc(pField->bytes + TSDB_NCHAR_SIZE); - } - - /* string terminated char for binary data*/ - memset(pRes->buffer[columnIndex], 0, pField->bytes + TSDB_NCHAR_SIZE); - - int32_t length = taosUcs4ToMbs(pRes->tsrow[columnIndex], pRes->length[columnIndex], pRes->buffer[columnIndex]); - if ( length >= 0 ) { - pRes->tsrow[columnIndex] = (unsigned char*)pRes->buffer[columnIndex]; - pRes->length[columnIndex] = length; - } else { - tscError("%p charset:%s to %s. val:%s convert failed.", pSql, DEFAULT_UNICODE_ENCODEC, tsCharset, (char*)pRes->tsrow[columnIndex]); - pRes->tsrow[columnIndex] = NULL; - pRes->length[columnIndex] = 0; - } - } -} - char *getArithmeticInputSrc(void *param, const char *name, int32_t colId) { SArithmeticSupport *pSupport = (SArithmeticSupport *) param; diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 578a7df1493b23792791a29bf0acf43c6e38b719..e8994502610653c01b9a6f93247f9ab4b42792af 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -97,6 +97,22 @@ bool tscQueryTags(SQueryInfo* pQueryInfo) { return true; } +bool tscQueryBlockInfo(SQueryInfo* pQueryInfo) { + int32_t numOfCols = (int32_t) tscSqlExprNumOfExprs(pQueryInfo); + + for (int32_t i = 0; i < numOfCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); + int32_t functId = pExpr->functionId; + + // "select count(tbname)" query + if (functId == TSDB_FUNC_BLKINFO) { + return true; + } + } + + return false; +} + bool tscIsTwoStageSTableQuery(SQueryInfo* pQueryInfo, int32_t tableIndex) { if (pQueryInfo == NULL) { return false; @@ -223,6 +239,21 @@ bool tscIsSecondStageQuery(SQueryInfo* pQueryInfo) { return false; } +bool tscGroupbyColumn(SQueryInfo* pQueryInfo) { + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); + int32_t numOfCols = tscGetNumOfColumns(pTableMetaInfo->pTableMeta); + + SSqlGroupbyExpr* pGroupbyExpr = &pQueryInfo->groupbyExpr; + for (int32_t k = 0; k < pGroupbyExpr->numOfGroupCols; ++k) { + SColIndex* pIndex = taosArrayGet(pGroupbyExpr->columnInfo, k); + if (!TSDB_COL_IS_TAG(pIndex->flag) && pIndex->colIndex < numOfCols) { // group by normal columns + return true; + } + } + + return false; +} + bool tscIsTWAQuery(SQueryInfo* pQueryInfo) { size_t numOfExprs = tscSqlExprNumOfExprs(pQueryInfo); for (int32_t i = 0; i < numOfExprs; ++i) { @@ -1128,7 +1159,7 @@ SSqlExpr* tscSqlExprInsert(SQueryInfo* pQueryInfo, int32_t index, int16_t functi } SSqlExpr* tscSqlExprAppend(SQueryInfo* pQueryInfo, int16_t functionId, SColumnIndex* pColIndex, int16_t type, - int16_t size, int16_t resColId, int16_t interSize, bool isTagCol) { + int16_t size, int16_t resColId, int16_t interSize, bool isTagCol) { SSqlExpr* pExpr = doCreateSqlExpr(pQueryInfo, functionId, pColIndex, type, size, resColId, interSize, isTagCol); taosArrayPush(pQueryInfo->exprList, &pExpr); return pExpr; @@ -1722,10 +1753,15 @@ void tscInitQueryInfo(SQueryInfo* pQueryInfo) { pQueryInfo->fieldsInfo.internalField = taosArrayInit(4, sizeof(SInternalField)); assert(pQueryInfo->exprList == NULL); - pQueryInfo->exprList = taosArrayInit(4, POINTER_BYTES); - pQueryInfo->colList = taosArrayInit(4, POINTER_BYTES); - pQueryInfo->udColumnId = TSDB_UD_COLUMN_INDEX; - pQueryInfo->resColumnId= -1000; + pQueryInfo->exprList = taosArrayInit(4, POINTER_BYTES); + pQueryInfo->colList = taosArrayInit(4, POINTER_BYTES); + pQueryInfo->udColumnId = TSDB_UD_COLUMN_INDEX; + pQueryInfo->resColumnId = -1000; + pQueryInfo->limit.limit = -1; + pQueryInfo->limit.offset = 0; + + pQueryInfo->slimit.limit = -1; + pQueryInfo->slimit.offset = 0; } int32_t tscAddSubqueryInfo(SSqlCmd* pCmd) { diff --git a/src/common/inc/tdataformat.h b/src/common/inc/tdataformat.h index 959654d1585b13c31e96ab7972d0aa2782a51901..e8c076099759ffe683c78b55a54848cf765daada 100644 --- a/src/common/inc/tdataformat.h +++ b/src/common/inc/tdataformat.h @@ -283,12 +283,37 @@ typedef struct { #define keyCol(pCols) (&((pCols)->cols[0])) // Key column #define dataColsTKeyAt(pCols, idx) ((TKEY *)(keyCol(pCols)->pData))[(idx)] #define dataColsKeyAt(pCols, idx) tdGetKey(dataColsTKeyAt(pCols, idx)) -#define dataColsTKeyFirst(pCols) (((pCols)->numOfRows == 0) ? TKEY_INVALID : dataColsTKeyAt(pCols, 0)) -#define dataColsKeyFirst(pCols) (((pCols)->numOfRows == 0) ? TSDB_DATA_TIMESTAMP_NULL : dataColsKeyAt(pCols, 0)) -#define dataColsTKeyLast(pCols) \ - (((pCols)->numOfRows == 0) ? TKEY_INVALID : dataColsTKeyAt(pCols, (pCols)->numOfRows - 1)) -#define dataColsKeyLast(pCols) \ - (((pCols)->numOfRows == 0) ? TSDB_DATA_TIMESTAMP_NULL : dataColsKeyAt(pCols, (pCols)->numOfRows - 1)) +static FORCE_INLINE TKEY dataColsTKeyFirst(SDataCols *pCols) { + if (pCols->numOfRows) { + return dataColsTKeyAt(pCols, 0); + } else { + return TKEY_INVALID; + } +} + +static FORCE_INLINE TSKEY dataColsKeyFirst(SDataCols *pCols) { + if (pCols->numOfRows) { + return dataColsKeyAt(pCols, 0); + } else { + return TSDB_DATA_TIMESTAMP_NULL; + } +} + +static FORCE_INLINE TKEY dataColsTKeyLast(SDataCols *pCols) { + if (pCols->numOfRows) { + return dataColsTKeyAt(pCols, pCols->numOfRows - 1); + } else { + return TKEY_INVALID; + } +} + +static FORCE_INLINE TSKEY dataColsKeyLast(SDataCols *pCols) { + if (pCols->numOfRows) { + return dataColsKeyAt(pCols, pCols->numOfRows - 1); + } else { + return TSDB_DATA_TIMESTAMP_NULL; + } +} SDataCols *tdNewDataCols(int maxRowSize, int maxCols, int maxRows); void tdResetDataCols(SDataCols *pCols); diff --git a/src/common/inc/tname.h b/src/common/inc/tname.h index b651913d7384bac75eea7c9644b501b781c47e33..465b298973cbccdf380150bdce46a09b56594801 100644 --- a/src/common/inc/tname.h +++ b/src/common/inc/tname.h @@ -33,7 +33,7 @@ typedef struct SDataStatis { typedef struct SColumnInfoData { SColumnInfo info; - void* pData; // the corresponding block data in memory + char* pData; // the corresponding block data in memory } SColumnInfoData; typedef struct SResPair { diff --git a/src/connector/jdbc/CMakeLists.txt b/src/connector/jdbc/CMakeLists.txt index 3c50ac566b5fe49a619a50c9eac446285a2b431c..86ddfcb022f4467f7378b85dda5760b66366bbbc 100644 --- a/src/connector/jdbc/CMakeLists.txt +++ b/src/connector/jdbc/CMakeLists.txt @@ -8,7 +8,7 @@ IF (TD_MVN_INSTALLED) ADD_CUSTOM_COMMAND(OUTPUT ${JDBC_CMD_NAME} POST_BUILD COMMAND mvn -Dmaven.test.skip=true install -f ${CMAKE_CURRENT_SOURCE_DIR}/pom.xml - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/target/taos-jdbcdriver-2.0.21-dist.jar ${LIBRARY_OUTPUT_PATH} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/target/taos-jdbcdriver-2.0.22-dist.jar ${LIBRARY_OUTPUT_PATH} COMMAND mvn -Dmaven.test.skip=true clean -f ${CMAKE_CURRENT_SOURCE_DIR}/pom.xml COMMENT "build jdbc driver") ADD_CUSTOM_TARGET(${JDBC_TARGET_NAME} ALL WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} DEPENDS ${JDBC_CMD_NAME}) diff --git a/src/connector/jdbc/deploy-pom.xml b/src/connector/jdbc/deploy-pom.xml index 1c24b621efe52e152ac8ea70ef3c2afdbc72d5b0..fe93e54c69e969cd8f54b41b6275423157f56197 100755 --- a/src/connector/jdbc/deploy-pom.xml +++ b/src/connector/jdbc/deploy-pom.xml @@ -5,7 +5,7 @@ com.taosdata.jdbc taos-jdbcdriver - 2.0.21 + 2.0.22 jar JDBCDriver diff --git a/src/connector/jdbc/pom.xml b/src/connector/jdbc/pom.xml index f1e013e864f4617e04be0c8c5b892c4396c66e79..51cb0b380822c81137c1a5514c898b51ae0d85be 100755 --- a/src/connector/jdbc/pom.xml +++ b/src/connector/jdbc/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.taosdata.jdbc taos-jdbcdriver - 2.0.21 + 2.0.22 jar JDBCDriver https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc @@ -102,6 +102,8 @@ **/*Test.java + **/TSDBJNIConnectorTest.java + **/UnsignedNumberJniTest.java **/DatetimeBefore1970Test.java **/AppMemoryLeakTest.java **/AuthenticationTest.java diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDriver.java index 28b7bd6a5ff0eef90dee8d97767f71a44ed1552d..5eaac1cd3ba7283b019a1d294c1a33334a3d9fa7 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDriver.java @@ -12,7 +12,7 @@ public abstract class AbstractDriver implements Driver { hostProp.required = false; hostProp.description = "Hostname"; - DriverPropertyInfo portProp = new DriverPropertyInfo(TSDBDriver.PROPERTY_KEY_PORT, info.getProperty(TSDBDriver.PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT)); + DriverPropertyInfo portProp = new DriverPropertyInfo(TSDBDriver.PROPERTY_KEY_PORT, info.getProperty(TSDBDriver.PROPERTY_KEY_PORT)); portProp.required = false; portProp.description = "Port"; @@ -40,11 +40,11 @@ public abstract class AbstractDriver implements Driver { protected Properties parseURL(String url, Properties defaults) { Properties urlProps = (defaults != null) ? defaults : new Properties(); - // parse properties + // parse properties in url int beginningOfSlashes = url.indexOf("//"); int index = url.indexOf("?"); if (index != -1) { - String paramString = url.substring(index + 1, url.length()); + String paramString = url.substring(index + 1); url = url.substring(0, index); StringTokenizer queryParams = new StringTokenizer(paramString, "&"); while (queryParams.hasMoreElements()) { @@ -68,6 +68,7 @@ public abstract class AbstractDriver implements Driver { String dbProductName = url.substring(0, beginningOfSlashes); dbProductName = dbProductName.substring(dbProductName.indexOf(":") + 1); dbProductName = dbProductName.substring(0, dbProductName.indexOf(":")); + urlProps.setProperty(TSDBDriver.PROPERTY_KEY_PRODUCT_NAME,dbProductName); // parse dbname url = url.substring(beginningOfSlashes + 2); int indexOfSlash = url.indexOf("/"); diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractResultSet.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractResultSet.java index 14bd2929f17e344381510897528bc479c50a4d36..238d18039de8ca6bf2f178263827bc4f1d6634f2 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractResultSet.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractResultSet.java @@ -29,12 +29,7 @@ public abstract class AbstractResultSet extends WrapperImpl implements ResultSet public abstract boolean getBoolean(int columnIndex) throws SQLException; @Override - public byte getByte(int columnIndex) throws SQLException { - if (isClosed()) - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESULTSET_CLOSED); - - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } + public abstract byte getByte(int columnIndex) throws SQLException; @Override public abstract short getShort(int columnIndex) throws SQLException; @@ -1205,6 +1200,7 @@ public abstract class AbstractResultSet extends WrapperImpl implements ResultSet public T getObject(String columnLabel, Class type) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); + } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/SavedPreparedStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/SavedPreparedStatement.java index a0aa7ec584ee351185d10b023e5142d00d60d66b..512fcd26b8ce01b9e673fd7ed9a266ed0372ba52 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/SavedPreparedStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/SavedPreparedStatement.java @@ -122,8 +122,7 @@ public class SavedPreparedStatement { initPreparedParam = initDefaultParam(tableName, middleParamSize, valueListSize); } else { - // not match - throw new SQLException(TSDBConstants.WrapErrMsg("the sql is not complete!")); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_SQL); } } @@ -189,7 +188,7 @@ public class SavedPreparedStatement { String errorMsg = String.format("the parameterIndex %s out of the range [1, %s]", parameterIndex, paramSize); if (parameterIndex < 1 || parameterIndex > paramSize) { - throw new SQLException(TSDBConstants.WrapErrMsg(errorMsg)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE,errorMsg); } this.isAddBatch = false; //set isAddBatch to false @@ -212,7 +211,7 @@ public class SavedPreparedStatement { return; } - throw new SQLException(TSDBConstants.WrapErrMsg(errorMsg)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE,errorMsg); } public void addBatch() { diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java index 6179b47da1041a3461f1be43625f8c7d4e284e64..37073e243fef99176c6d3d16d87d7cdaabb0e1b4 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java @@ -16,16 +16,11 @@ package com.taosdata.jdbc; import java.sql.SQLException; import java.sql.Types; -import java.util.HashMap; -import java.util.Map; public abstract class TSDBConstants { - public static final String DEFAULT_PORT = "6200"; - public static Map DATATYPE_MAP = null; - public static final long JNI_NULL_POINTER = 0L; - + // JNI_ERROR_NUMBER public static final int JNI_SUCCESS = 0; public static final int JNI_TDENGINE_ERROR = -1; public static final int JNI_CONNECTION_NULL = -2; @@ -34,8 +29,7 @@ public abstract class TSDBConstants { public static final int JNI_SQL_NULL = -5; public static final int JNI_FETCH_END = -6; public static final int JNI_OUT_OF_MEMORY = -7; - - public static final int TSDB_DATA_TYPE_NULL = 0; + // TSDB Data Types public static final int TSDB_DATA_TYPE_BOOL = 1; public static final int TSDB_DATA_TYPE_TINYINT = 2; public static final int TSDB_DATA_TYPE_SMALLINT = 3; @@ -46,46 +40,36 @@ public abstract class TSDBConstants { public static final int TSDB_DATA_TYPE_BINARY = 8; public static final int TSDB_DATA_TYPE_TIMESTAMP = 9; public static final int TSDB_DATA_TYPE_NCHAR = 10; - - // nchar field's max length + /* + 系统增加新的无符号数据类型,分别是: + unsigned tinyint, 数值范围:0-254, NULL 为255 + unsigned smallint,数值范围: 0-65534, NULL 为65535 + unsigned int,数值范围:0-4294967294,NULL 为4294967295u + unsigned bigint,数值范围:0-18446744073709551614u,NULL 为18446744073709551615u。 + example: + create table tb(ts timestamp, a tinyint unsigned, b smallint unsigned, c int unsigned, d bigint unsigned); + */ + public static final int TSDB_DATA_TYPE_UTINYINT = 11; //unsigned tinyint + public static final int TSDB_DATA_TYPE_USMALLINT = 12; //unsigned smallint + public static final int TSDB_DATA_TYPE_UINT = 13; //unsigned int + public static final int TSDB_DATA_TYPE_UBIGINT = 14; //unsigned bigint + // nchar column max length public static final int maxFieldSize = 16 * 1024; - public static String WrapErrMsg(String msg) { - return "TDengine Error: " + msg; - } - - public static String FixErrMsg(int code) { - switch (code) { - case JNI_TDENGINE_ERROR: - return WrapErrMsg("internal error of database!"); - case JNI_CONNECTION_NULL: - return WrapErrMsg("invalid tdengine connection!"); - case JNI_RESULT_SET_NULL: - return WrapErrMsg("invalid resultset pointer!"); - case JNI_NUM_OF_FIELDS_0: - return WrapErrMsg("invalid num of fields!"); - case JNI_SQL_NULL: - return WrapErrMsg("can't execute empty sql!"); - case JNI_FETCH_END: - return WrapErrMsg("fetch to the end of resultset"); - default: - break; - } - return WrapErrMsg("unkown error!"); - } - public static int taosType2JdbcType(int taosType) throws SQLException { switch (taosType) { - case TSDBConstants.TSDB_DATA_TYPE_NULL: - return Types.NULL; case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Types.BOOLEAN; case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: return Types.TINYINT; + case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: return Types.SMALLINT; + case TSDBConstants.TSDB_DATA_TYPE_UINT: case TSDBConstants.TSDB_DATA_TYPE_INT: return Types.INTEGER; + case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return Types.BIGINT; case TSDBConstants.TSDB_DATA_TYPE_FLOAT: @@ -99,13 +83,42 @@ public abstract class TSDBConstants { case TSDBConstants.TSDB_DATA_TYPE_NCHAR: return Types.NCHAR; } - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN_SQL_TYPE_IN_TDENGINE); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN_TAOS_TYPE_IN_TDENGINE); + } + + public static String taosType2JdbcTypeName(int taosType) throws SQLException { + switch (taosType){ + case TSDBConstants.TSDB_DATA_TYPE_BOOL: + return "BOOL"; + case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + return "TINYINT"; + case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: + return "SMALLINT"; + case TSDBConstants.TSDB_DATA_TYPE_UINT: + case TSDBConstants.TSDB_DATA_TYPE_INT: + return "INT"; + case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + return "BIGINT"; + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + return "FLOAT"; + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: + return "DOUBLE"; + case TSDBConstants.TSDB_DATA_TYPE_BINARY: + return "BINARY"; + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: + return "TIMESTAMP"; + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + return "NCHAR"; + default: + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN_TAOS_TYPE_IN_TDENGINE); + } } public static int jdbcType2TaosType(int jdbcType) throws SQLException { switch (jdbcType){ - case Types.NULL: - return TSDBConstants.TSDB_DATA_TYPE_NULL; case Types.BOOLEAN: return TSDBConstants.TSDB_DATA_TYPE_BOOL; case Types.TINYINT: @@ -130,22 +143,31 @@ public abstract class TSDBConstants { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN_SQL_TYPE_IN_TDENGINE); } - static { - DATATYPE_MAP = new HashMap<>(); - DATATYPE_MAP.put(0, "NULL"); - DATATYPE_MAP.put(1, "BOOL"); - DATATYPE_MAP.put(2, "TINYINT"); - DATATYPE_MAP.put(3, "SMALLINT"); - DATATYPE_MAP.put(4, "INT"); - DATATYPE_MAP.put(5, "BIGINT"); - DATATYPE_MAP.put(6, "FLOAT"); - DATATYPE_MAP.put(7, "DOUBLE"); - DATATYPE_MAP.put(8, "BINARY"); - DATATYPE_MAP.put(9, "TIMESTAMP"); - DATATYPE_MAP.put(10, "NCHAR"); + public static String jdbcType2TaosTypeName(int jdbcType) throws SQLException { + switch (jdbcType){ + case Types.BOOLEAN: + return "BOOL"; + case Types.TINYINT: + return "TINYINT"; + case Types.SMALLINT: + return "SMALLINT"; + case Types.INTEGER: + return "INT"; + case Types.BIGINT: + return "BIGINT"; + case Types.FLOAT: + return "FLOAT"; + case Types.DOUBLE: + return "DOUBLE"; + case Types.BINARY: + return "BINARY"; + case Types.TIMESTAMP: + return "TIMESTAMP"; + case Types.NCHAR: + return "NCHAR"; + default: + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN_SQL_TYPE_IN_TDENGINE); + } } - public static String jdbcType2TaosTypeName(int type) throws SQLException { - return DATATYPE_MAP.get(jdbcType2TaosType(type)); - } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java index 530ece6bcb6d1e75609ceee0a333dcd1aeb39bde..5f599df1300b3d6959e9a7b58c00a720d722e07b 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java @@ -44,6 +44,10 @@ public class TSDBDriver extends AbstractDriver { private static final String URL_PREFIX = "jdbc:TAOS://"; + /** + * PRODUCT_NAME + */ + public static final String PROPERTY_KEY_PRODUCT_NAME = "productName"; /** * Key used to retrieve the host value from the properties instance passed to * the driver. @@ -96,38 +100,34 @@ public class TSDBDriver extends AbstractDriver { static { try { java.sql.DriverManager.registerDriver(new TSDBDriver()); - } catch (SQLException E) { - throw new RuntimeException(TSDBConstants.WrapErrMsg("can't register tdengine jdbc driver!")); + } catch (SQLException e) { + throw TSDBError.createRuntimeException(TSDBErrorNumbers.ERROR_CANNOT_REGISTER_JNI_DRIVER, e); } } public Connection connect(String url, Properties info) throws SQLException { if (url == null) - throw new SQLException(TSDBConstants.WrapErrMsg("url is not set!")); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_URL_NOT_SET); if (!acceptsURL(url)) return null; - Properties props = null; - if ((props = parseURL(url, info)) == null) { + Properties props = parseURL(url, info); + if (props == null) { return null; } try { TSDBJNIConnector.init((String) props.get(PROPERTY_KEY_CONFIG_DIR), (String) props.get(PROPERTY_KEY_LOCALE), (String) props.get(PROPERTY_KEY_CHARSET), (String) props.get(PROPERTY_KEY_TIME_ZONE)); - Connection newConn = new TSDBConnection(props, this.dbMetaData); - return newConn; + return new TSDBConnection(props, this.dbMetaData); } catch (SQLWarning sqlWarning) { sqlWarning.printStackTrace(); - Connection newConn = new TSDBConnection(props, this.dbMetaData); - return newConn; + return new TSDBConnection(props, this.dbMetaData); } catch (SQLException sqlEx) { throw sqlEx; } catch (Exception ex) { - SQLException sqlEx = new SQLException("SQLException:" + ex.toString()); - sqlEx.initCause(ex); - throw sqlEx; + throw new SQLException("SQLException:" + ex.toString(), ex); } } @@ -139,8 +139,8 @@ public class TSDBDriver extends AbstractDriver { */ public boolean acceptsURL(String url) throws SQLException { if (url == null) - throw new SQLException(TSDBConstants.WrapErrMsg("url is null")); - return (url != null && url.length() > 0 && url.trim().length() > 0) && (url.startsWith(URL_PREFIX) || url.startsWith(URL_PREFIX1)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_URL_NOT_SET); + return url.length() > 0 && url.trim().length() > 0 && (url.startsWith(URL_PREFIX) || url.startsWith(URL_PREFIX1)); } public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBError.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBError.java index c7717e331b39247542d1a022cafd1cab2ac65627..90967b3620fdaccf69f253c8f8927c067b6221fd 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBError.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBError.java @@ -3,6 +3,7 @@ package com.taosdata.jdbc; import java.sql.SQLClientInfoException; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLWarning; import java.util.HashMap; import java.util.Map; @@ -18,18 +19,25 @@ public class TSDBError { TSDBErrorMap.put(TSDBErrorNumbers.ERROR_BATCH_IS_EMPTY, "Batch is empty!"); TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_WITH_EXECUTEQUERY, "Can not issue data manipulation statements with executeQuery()"); TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_WITH_EXECUTEUPDATE, "Can not issue SELECT via executeUpdate()"); - TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_FOR_EXECUTE_QUERY, "not a valid sql for executeQuery: (?)"); + TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_FOR_EXECUTE_QUERY, "invalid sql for executeQuery: (?)"); TSDBErrorMap.put(TSDBErrorNumbers.ERROR_DATABASE_NOT_SPECIFIED_OR_AVAILABLE, "Database not specified or available"); - TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_FOR_EXECUTE_UPDATE, "not a valid sql for executeUpdate: (?)"); - TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_FOR_EXECUTE, "not a valid sql for execute: (?)"); + TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_FOR_EXECUTE_UPDATE, "invalid sql for executeUpdate: (?)"); + TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_FOR_EXECUTE, "invalid sql for execute: (?)"); TSDBErrorMap.put(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE, "parameter index out of range"); TSDBErrorMap.put(TSDBErrorNumbers.ERROR_SQLCLIENT_EXCEPTION_ON_CONNECTION_CLOSED, "connection already closed"); + TSDBErrorMap.put(TSDBErrorNumbers.ERROR_UNKNOWN_SQL_TYPE_IN_TDENGINE, "unknown sql type in tdengine"); + TSDBErrorMap.put(TSDBErrorNumbers.ERROR_CANNOT_REGISTER_JNI_DRIVER, "can't register JDBC-JNI driver"); + TSDBErrorMap.put(TSDBErrorNumbers.ERROR_CANNOT_REGISTER_RESTFUL_DRIVER, "can't register JDBC-RESTful driver"); + TSDBErrorMap.put(TSDBErrorNumbers.ERROR_URL_NOT_SET, "url is not set"); + TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_SQL, "invalid sql"); + TSDBErrorMap.put(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE, "numeric value out of range"); + TSDBErrorMap.put(TSDBErrorNumbers.ERROR_UNKNOWN_TAOS_TYPE_IN_TDENGINE, "unknown taos type in tdengine"); + /**************************************************/ TSDBErrorMap.put(TSDBErrorNumbers.ERROR_UNKNOWN, "unknown error"); /**************************************************/ TSDBErrorMap.put(TSDBErrorNumbers.ERROR_SUBSCRIBE_FAILED, "failed to create subscription"); TSDBErrorMap.put(TSDBErrorNumbers.ERROR_UNSUPPORTED_ENCODING, "Unsupported encoding"); - TSDBErrorMap.put(TSDBErrorNumbers.ERROR_JNI_TDENGINE_ERROR, "internal error of database"); TSDBErrorMap.put(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL, "JNI connection is NULL"); TSDBErrorMap.put(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL, "JNI result set is NULL"); @@ -65,4 +73,12 @@ public class TSDBError { return new SQLException("TDengine ERROR (" + Integer.toHexString(errorCode) + "): " + message, "", errorCode); } -} + public static RuntimeException createRuntimeException(int errorCode, Throwable t) { + String message = TSDBErrorMap.get(errorCode); + return new RuntimeException("ERROR (" + Integer.toHexString(errorCode) + "): " + message, t); + } + + public static SQLWarning createSQLWarning(String message) { + return new SQLWarning(message); + } +} \ No newline at end of file diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBErrorNumbers.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBErrorNumbers.java index 78e7aec79c252a2e6b2eeafcba37ab1c66f8674d..c978bb3a2e4a1b9dbd264268f057f2b6c735250a 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBErrorNumbers.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBErrorNumbers.java @@ -16,15 +16,20 @@ public class TSDBErrorNumbers { public static final int ERROR_DATABASE_NOT_SPECIFIED_OR_AVAILABLE = 0x230a; //Database not specified or available public static final int ERROR_INVALID_FOR_EXECUTE_UPDATE = 0x230b; //not a valid sql for executeUpdate: (SQL) public static final int ERROR_INVALID_FOR_EXECUTE = 0x230c; //not a valid sql for execute: (SQL) - public static final int ERROR_PARAMETER_INDEX_OUT_RANGE = 0x230d; // parameter index out of range + public static final int ERROR_PARAMETER_INDEX_OUT_RANGE = 0x230d; // parameter index out of range public static final int ERROR_SQLCLIENT_EXCEPTION_ON_CONNECTION_CLOSED = 0x230e; // connection already closed public static final int ERROR_UNKNOWN_SQL_TYPE_IN_TDENGINE = 0x230f; //unknown sql type in tdengine + public static final int ERROR_CANNOT_REGISTER_JNI_DRIVER = 0x2310; // can't register JDBC-JNI driver + public static final int ERROR_CANNOT_REGISTER_RESTFUL_DRIVER = 0x2311; // can't register JDBC-RESTful driver + public static final int ERROR_URL_NOT_SET = 0x2312; // url is not set + public static final int ERROR_INVALID_SQL = 0x2313; // invalid sql + public static final int ERROR_NUMERIC_VALUE_OUT_OF_RANGE = 0x2314; // numeric value out of range + public static final int ERROR_UNKNOWN_TAOS_TYPE_IN_TDENGINE = 0x2315; //unknown taos type in tdengine public static final int ERROR_UNKNOWN = 0x2350; //unknown error public static final int ERROR_SUBSCRIBE_FAILED = 0x2351; // failed to create subscription public static final int ERROR_UNSUPPORTED_ENCODING = 0x2352; // Unsupported encoding - public static final int ERROR_JNI_TDENGINE_ERROR = 0x2353; // internal error of database public static final int ERROR_JNI_CONNECTION_NULL = 0x2354; // JNI connection is NULL public static final int ERROR_JNI_RESULT_SET_NULL = 0x2355; // invalid JNI result set @@ -51,11 +56,16 @@ public class TSDBErrorNumbers { errorNumbers.add(ERROR_PARAMETER_INDEX_OUT_RANGE); errorNumbers.add(ERROR_SQLCLIENT_EXCEPTION_ON_CONNECTION_CLOSED); errorNumbers.add(ERROR_UNKNOWN_SQL_TYPE_IN_TDENGINE); + errorNumbers.add(ERROR_CANNOT_REGISTER_JNI_DRIVER); + errorNumbers.add(ERROR_CANNOT_REGISTER_RESTFUL_DRIVER); + errorNumbers.add(ERROR_URL_NOT_SET); + errorNumbers.add(ERROR_INVALID_SQL); + errorNumbers.add(ERROR_NUMERIC_VALUE_OUT_OF_RANGE); + errorNumbers.add(ERROR_UNKNOWN_TAOS_TYPE_IN_TDENGINE); /*****************************************************/ errorNumbers.add(ERROR_SUBSCRIBE_FAILED); errorNumbers.add(ERROR_UNSUPPORTED_ENCODING); - errorNumbers.add(ERROR_JNI_TDENGINE_ERROR); errorNumbers.add(ERROR_JNI_CONNECTION_NULL); errorNumbers.add(ERROR_JNI_RESULT_SET_NULL); @@ -63,7 +73,6 @@ public class TSDBErrorNumbers { errorNumbers.add(ERROR_JNI_SQL_NULL); errorNumbers.add(ERROR_JNI_FETCH_END); errorNumbers.add(ERROR_JNI_OUT_OF_MEMORY); - } private TSDBErrorNumbers() { diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java index b27bc63db17e2f7268ee9f1c8ae3c074db15c0e5..7d3741917cb9166a5b29e69ad2b4d3f5023bd43d 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java @@ -1,18 +1,19 @@ /** * ************************************************************************* * Copyright (c) 2019 TAOS Data, Inc. - * + *

* This program is free software: you can use, redistribute, and/or modify * it under the terms of the GNU Affero General Public License, version 3 * or later ("AGPL"), as published by the Free Software Foundation. - * + *

* This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. - * + *

* You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - **************************************************************************** */ + * *************************************************************************** + */ package com.taosdata.jdbc; import com.taosdata.jdbc.utils.TaosInfo; @@ -23,7 +24,7 @@ import java.util.List; /** * JNI connector - * */ + */ public class TSDBJNIConnector { private static volatile Boolean isInitialized = false; @@ -72,13 +73,13 @@ public class TSDBJNIConnector { if (!isInitialized) { initImp(configDir); if (setOptions(0, locale) < 0) { - throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set locale: " + locale + ". System default will be used.")); + throw TSDBError.createSQLWarning("Failed to set locale: " + locale + ". System default will be used."); } if (setOptions(1, charset) < 0) { - throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set charset: " + charset + ". System default will be used.")); + throw TSDBError.createSQLWarning("Failed to set charset: " + charset + ". System default will be used."); } if (setOptions(2, timezone) < 0) { - throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set timezone: " + timezone + ". System default will be used.")); + throw TSDBError.createSQLWarning("Failed to set timezone: " + timezone + ". System default will be used."); } isInitialized = true; TaosGlobalConfig.setCharset(getTsCharset()); diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java index 5d68ffad6c35697bdc96d4b1919f23be5aa88e4a..7c2940fdc2c1a0cb15a5a0f1557598cb5b062e61 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java @@ -27,8 +27,8 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { private final TSDBResultSetRowData rowData; private final TSDBResultSetBlockData blockData; - private boolean batchFetch = false; - private boolean lastWasNull = false; + private boolean batchFetch; + private boolean lastWasNull; private boolean isClosed; public void setBatchFetch(boolean batchFetch) { @@ -86,7 +86,6 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (rowData != null) { this.rowData.clear(); } - int code = this.jniConnector.fetchRow(this.resultSetPointer, this.rowData); if (code == TSDBConstants.JNI_CONNECTION_NULL) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); @@ -124,30 +123,27 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { String res = null; int colIndex = getTrueColumnIndex(columnIndex); - if (!this.getBatchFetch()) { - this.lastWasNull = this.rowData.wasNull(colIndex); - if (!lastWasNull) { - res = this.rowData.getString(colIndex, this.columnMetaDataList.get(colIndex).getColType()); - } - return res; - } else { + if (this.getBatchFetch()) return this.blockData.getString(colIndex); + + this.lastWasNull = this.rowData.wasNull(colIndex); + if (!lastWasNull) { + res = this.rowData.getString(colIndex, this.columnMetaDataList.get(colIndex).getColType()); } + return res; } public boolean getBoolean(int columnIndex) throws SQLException { boolean res = false; int colIndex = getTrueColumnIndex(columnIndex); - if (!this.getBatchFetch()) { - this.lastWasNull = this.rowData.wasNull(colIndex); - if (!lastWasNull) { - res = this.rowData.getBoolean(colIndex, this.columnMetaDataList.get(colIndex).getColType()); - } - } else { + if (this.getBatchFetch()) return this.blockData.getBoolean(colIndex); - } + this.lastWasNull = this.rowData.wasNull(colIndex); + if (!lastWasNull) { + res = this.rowData.getBoolean(colIndex, this.columnMetaDataList.get(colIndex).getColType()); + } return res; } @@ -155,91 +151,84 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { byte res = 0; int colIndex = getTrueColumnIndex(columnIndex); - if (!this.getBatchFetch()) { - this.lastWasNull = this.rowData.wasNull(colIndex); - if (!lastWasNull) { - res = (byte) this.rowData.getInt(colIndex, this.columnMetaDataList.get(colIndex).getColType()); - } - return res; - } else { + if (this.getBatchFetch()) return (byte) this.blockData.getInt(colIndex); + + this.lastWasNull = this.rowData.wasNull(colIndex); + if (!lastWasNull) { + res = (byte) this.rowData.getInt(colIndex, this.columnMetaDataList.get(colIndex).getColType()); } + return res; } public short getShort(int columnIndex) throws SQLException { short res = 0; int colIndex = getTrueColumnIndex(columnIndex); - if (!this.getBatchFetch()) { - this.lastWasNull = this.rowData.wasNull(colIndex); - if (!lastWasNull) { - res = (short) this.rowData.getInt(colIndex, this.columnMetaDataList.get(colIndex).getColType()); - } - return res; - } else { + if (this.getBatchFetch()) return (short) this.blockData.getInt(colIndex); + + this.lastWasNull = this.rowData.wasNull(colIndex); + if (!lastWasNull) { + res = (short) this.rowData.getInt(colIndex, this.columnMetaDataList.get(colIndex).getColType()); } + return res; } public int getInt(int columnIndex) throws SQLException { int res = 0; int colIndex = getTrueColumnIndex(columnIndex); - if (!this.getBatchFetch()) { - this.lastWasNull = this.rowData.wasNull(colIndex); - if (!lastWasNull) { - res = this.rowData.getInt(colIndex, this.columnMetaDataList.get(colIndex).getColType()); - } - return res; - } else { + if (this.getBatchFetch()) return this.blockData.getInt(colIndex); - } + this.lastWasNull = this.rowData.wasNull(colIndex); + if (!lastWasNull) { + res = this.rowData.getInt(colIndex, this.columnMetaDataList.get(colIndex).getColType()); + } + return res; } public long getLong(int columnIndex) throws SQLException { long res = 0L; int colIndex = getTrueColumnIndex(columnIndex); - if (!this.getBatchFetch()) { - this.lastWasNull = this.rowData.wasNull(colIndex); - if (!lastWasNull) { - res = this.rowData.getLong(colIndex, this.columnMetaDataList.get(colIndex).getColType()); - } - return res; - } else { + if (this.getBatchFetch()) return this.blockData.getLong(colIndex); + + this.lastWasNull = this.rowData.wasNull(colIndex); + if (!lastWasNull) { + res = this.rowData.getLong(colIndex, this.columnMetaDataList.get(colIndex).getColType()); } + return res; } public float getFloat(int columnIndex) throws SQLException { float res = 0; int colIndex = getTrueColumnIndex(columnIndex); - if (!this.getBatchFetch()) { - this.lastWasNull = this.rowData.wasNull(colIndex); - if (!lastWasNull) { - res = this.rowData.getFloat(colIndex, this.columnMetaDataList.get(colIndex).getColType()); - } - return res; - } else { + if (this.getBatchFetch()) return (float) this.blockData.getDouble(colIndex); - } + + this.lastWasNull = this.rowData.wasNull(colIndex); + if (!lastWasNull) + res = this.rowData.getFloat(colIndex, this.columnMetaDataList.get(colIndex).getColType()); + + return res; } public double getDouble(int columnIndex) throws SQLException { double res = 0; int colIndex = getTrueColumnIndex(columnIndex); - if (!this.getBatchFetch()) { - this.lastWasNull = this.rowData.wasNull(colIndex); - if (!lastWasNull) { - res = this.rowData.getDouble(colIndex, this.columnMetaDataList.get(colIndex).getColType()); - } - return res; - } else { + if (this.getBatchFetch()) return this.blockData.getDouble(colIndex); + + this.lastWasNull = this.rowData.wasNull(colIndex); + if (!lastWasNull) { + res = this.rowData.getDouble(colIndex, this.columnMetaDataList.get(colIndex).getColType()); } + return res; } @Deprecated @@ -255,15 +244,14 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { Timestamp res = null; int colIndex = getTrueColumnIndex(columnIndex); - if (!this.getBatchFetch()) { - this.lastWasNull = this.rowData.wasNull(colIndex); - if (!lastWasNull) { - res = this.rowData.getTimestamp(colIndex); - } - return res; - } else { + if (this.getBatchFetch()) return this.blockData.getTimestamp(columnIndex); + + this.lastWasNull = this.rowData.wasNull(colIndex); + if (!lastWasNull) { + res = this.rowData.getTimestamp(colIndex); } + return res; } public ResultSetMetaData getMetaData() throws SQLException { @@ -274,12 +262,11 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { public Object getObject(int columnIndex) throws SQLException { int colIndex = getTrueColumnIndex(columnIndex); - if (!this.getBatchFetch()) { - this.lastWasNull = this.rowData.wasNull(colIndex); - return this.rowData.get(colIndex); - } else { + if (this.getBatchFetch()) return this.blockData.get(colIndex); - } + + this.lastWasNull = this.rowData.wasNull(colIndex); + return this.rowData.get(colIndex); } @Override diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetBlockData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetBlockData.java index 9352cf525350ff57525680f405d61c6b00c0cf55..ce5290de6616dfcc367434cc2e24899e992ddd63 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetBlockData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetBlockData.java @@ -30,468 +30,472 @@ import java.util.Collections; import java.util.List; public class TSDBResultSetBlockData { - private int numOfRows = 0; - private int rowIndex = 0; - - private List columnMetaDataList; - private ArrayList colData = null; - - public TSDBResultSetBlockData(List colMeta, int numOfCols) { - this.columnMetaDataList = colMeta; - this.colData = new ArrayList(numOfCols); - } - - public TSDBResultSetBlockData() { - this.colData = new ArrayList(); - } - - public void clear() { - int size = this.colData.size(); - if (this.colData != null) { - this.colData.clear(); - } - - setNumOfCols(size); - } - - public int getNumOfRows() { - return this.numOfRows; - } - - public void setNumOfRows(int numOfRows) { - this.numOfRows = numOfRows; - } - - public int getNumOfCols() { - return this.colData.size(); - } - - public void setNumOfCols(int numOfCols) { - this.colData = new ArrayList(numOfCols); - this.colData.addAll(Collections.nCopies(numOfCols, null)); - } - - public boolean hasMore() { - return this.rowIndex < this.numOfRows; - } - - public boolean forward() { - if (this.rowIndex > this.numOfRows) { - return false; - } - - return ((++this.rowIndex) < this.numOfRows); - } - - public void reset() { - this.rowIndex = 0; - } - - public void setBoolean(int col, boolean value) { - colData.set(col, value); - } - - public void setByteArray(int col, int length, byte[] value) { - try { - switch (this.columnMetaDataList.get(col).getColType()) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: { - ByteBuffer buf = ByteBuffer.wrap(value, 0, length); - buf.order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(); - this.colData.set(col, buf); - break; - } - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: { - ByteBuffer buf = ByteBuffer.wrap(value, 0, length); - buf.order(ByteOrder.LITTLE_ENDIAN); - this.colData.set(col, buf); - break; - } - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: { - ByteBuffer buf = ByteBuffer.wrap(value, 0, length); - ShortBuffer sb = buf.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); - this.colData.set(col, sb); - break; - } - case TSDBConstants.TSDB_DATA_TYPE_INT: { - ByteBuffer buf = ByteBuffer.wrap(value, 0, length); - IntBuffer ib = buf.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer(); - this.colData.set(col, ib); - break; - } - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: { - ByteBuffer buf = ByteBuffer.wrap(value, 0, length); - LongBuffer lb = buf.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer(); - this.colData.set(col, lb); - break; - } - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: { - ByteBuffer buf = ByteBuffer.wrap(value, 0, length); - FloatBuffer fb = buf.order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer(); - this.colData.set(col, fb); - break; - } - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { - ByteBuffer buf = ByteBuffer.wrap(value, 0, length); - DoubleBuffer db = buf.order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer(); - this.colData.set(col, db); - break; - } - case TSDBConstants.TSDB_DATA_TYPE_BINARY: { - ByteBuffer buf = ByteBuffer.wrap(value, 0, length); - buf.order(ByteOrder.LITTLE_ENDIAN); - this.colData.set(col, buf); - break; - } - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: { - ByteBuffer buf = ByteBuffer.wrap(value, 0, length); - LongBuffer lb = buf.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer(); - this.colData.set(col, lb); - break; - } - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: { - ByteBuffer buf = ByteBuffer.wrap(value, 0, length); - buf.order(ByteOrder.LITTLE_ENDIAN); - this.colData.set(col, buf); - break; - } - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - private static class NullType { - private static final byte NULL_BOOL_VAL = 0x2; - private static final String NULL_STR = "null"; - - public String toString() { - return NullType.NULL_STR; - } - - public static boolean isBooleanNull(byte val) { - return val == NullType.NULL_BOOL_VAL; - } - - private static boolean isTinyIntNull(byte val) { - return val == Byte.MIN_VALUE; - } - - private static boolean isSmallIntNull(short val) { - return val == Short.MIN_VALUE; - } - - private static boolean isIntNull(int val) { - return val == Integer.MIN_VALUE; - } - - private static boolean isBigIntNull(long val) { - return val == Long.MIN_VALUE; - } - - private static boolean isFloatNull(float val) { - return Float.isNaN(val); - } - - private static boolean isDoubleNull(double val) { - return Double.isNaN(val); - } - - private static boolean isBinaryNull(byte[] val, int length) { - if (length != Byte.BYTES) { - return false; - } - - return val[0] == 0xFF; - } - - private static boolean isNcharNull(byte[] val, int length) { - if (length != Integer.BYTES) { - return false; - } - - return (val[0] & val[1] & val[2] & val[3]) == 0xFF; - } - - } - - /** - * The original type may not be a string type, but will be converted to by - * calling this method - * - * @param col column index - * @return - * @throws SQLException - */ - public String getString(int col) throws SQLException { - Object obj = get(col); - if (obj == null) { - return new NullType().toString(); - } - - return obj.toString(); - } - - public int getInt(int col) { - Object obj = get(col); - if (obj == null) { - return 0; - } - - int type = this.columnMetaDataList.get(col).getColType(); - switch (type) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: - case TSDBConstants.TSDB_DATA_TYPE_INT: { - return (int) obj; - } - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: { - return ((Long) obj).intValue(); - } - - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { - return ((Double) obj).intValue(); - } - - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: - case TSDBConstants.TSDB_DATA_TYPE_BINARY: { - return Integer.parseInt((String) obj); - } - } - - return 0; - } - - public boolean getBoolean(int col) throws SQLException { - Object obj = get(col); - if (obj == null) { - return Boolean.FALSE; - } - - int type = this.columnMetaDataList.get(col).getColType(); - switch (type) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: - case TSDBConstants.TSDB_DATA_TYPE_INT: { - return ((int) obj == 0L) ? Boolean.FALSE : Boolean.TRUE; - } - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: { - return (((Long) obj) == 0L) ? Boolean.FALSE : Boolean.TRUE; - } - - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { - return (((Double) obj) == 0) ? Boolean.FALSE : Boolean.TRUE; - } - - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: - case TSDBConstants.TSDB_DATA_TYPE_BINARY: { - if ("TRUE".compareToIgnoreCase((String) obj) == 0) { - return Boolean.TRUE; - } else if ("FALSE".compareToIgnoreCase((String) obj) == 0) { - return Boolean.TRUE; - } else { - throw new SQLDataException(); - } - } - } - - return Boolean.FALSE; - } - - public long getLong(int col) throws SQLException { - Object obj = get(col); - if (obj == null) { - return 0; - } - - int type = this.columnMetaDataList.get(col).getColType(); - switch (type) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: - case TSDBConstants.TSDB_DATA_TYPE_INT: { - return (int) obj; - } - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: { - return (long) obj; - } - - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { - return ((Double) obj).longValue(); - } - - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: - case TSDBConstants.TSDB_DATA_TYPE_BINARY: { - return Long.parseLong((String) obj); - } - } - - return 0; - } - - public Timestamp getTimestamp(int col) { - try { - return new Timestamp(getLong(col)); - } catch (SQLException e) { - e.printStackTrace(); - } - - return null; - } - - public double getDouble(int col) { - Object obj = get(col); - if (obj == null) { - return 0; - } - - int type = this.columnMetaDataList.get(col).getColType(); - switch (type) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: - case TSDBConstants.TSDB_DATA_TYPE_INT: { - return (int) obj; - } - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: { - return (long) obj; - } - - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { - return (double) obj; - } - - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: - case TSDBConstants.TSDB_DATA_TYPE_BINARY: { - return Double.parseDouble((String) obj); - } - } - - return 0; - } - - public Object get(int col) { - int fieldSize = this.columnMetaDataList.get(col).getColSize(); - - switch (this.columnMetaDataList.get(col).getColType()) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: { - ByteBuffer bb = (ByteBuffer) this.colData.get(col); - - byte val = bb.get(this.rowIndex); - if (NullType.isBooleanNull(val)) { - return null; - } - - return (val == 0x0) ? Boolean.FALSE : Boolean.TRUE; - } - - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: { - ByteBuffer bb = (ByteBuffer) this.colData.get(col); - - byte val = bb.get(this.rowIndex); - if (NullType.isTinyIntNull(val)) { - return null; - } - - return val; - } - - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: { - ShortBuffer sb = (ShortBuffer) this.colData.get(col); - short val = sb.get(this.rowIndex); - if (NullType.isSmallIntNull(val)) { - return null; - } - - return val; - } - - case TSDBConstants.TSDB_DATA_TYPE_INT: { - IntBuffer ib = (IntBuffer) this.colData.get(col); - int val = ib.get(this.rowIndex); - if (NullType.isIntNull(val)) { - return null; - } - - return val; - } - - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: { - LongBuffer lb = (LongBuffer) this.colData.get(col); - long val = lb.get(this.rowIndex); - if (NullType.isBigIntNull(val)) { - return null; - } - - return (long) val; - } - - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: { - FloatBuffer fb = (FloatBuffer) this.colData.get(col); - float val = fb.get(this.rowIndex); - if (NullType.isFloatNull(val)) { - return null; - } - - return val; - } - - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { - DoubleBuffer lb = (DoubleBuffer) this.colData.get(col); - double val = lb.get(this.rowIndex); - if (NullType.isDoubleNull(val)) { - return null; - } - - return val; - } - - case TSDBConstants.TSDB_DATA_TYPE_BINARY: { - ByteBuffer bb = (ByteBuffer) this.colData.get(col); - bb.position(fieldSize * this.rowIndex); - - int length = bb.getShort(); - - byte[] dest = new byte[length]; - bb.get(dest, 0, length); - if (NullType.isBinaryNull(dest, length)) { - return null; - } - - return new String(dest); - } - - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: { - ByteBuffer bb = (ByteBuffer) this.colData.get(col); - bb.position(fieldSize * this.rowIndex); - - int length = bb.getShort(); - - byte[] dest = new byte[length]; - bb.get(dest, 0, length); - if (NullType.isNcharNull(dest, length)) { - return null; - } - - try { - String ss = TaosGlobalConfig.getCharset(); - return new String(dest, ss); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - } - } - - return 0; - } + private int numOfRows = 0; + private int rowIndex = 0; + + private List columnMetaDataList; + private ArrayList colData = null; + + public TSDBResultSetBlockData(List colMeta, int numOfCols) { + this.columnMetaDataList = colMeta; + this.colData = new ArrayList(numOfCols); + } + + public TSDBResultSetBlockData() { + this.colData = new ArrayList(); + } + + public void clear() { + int size = this.colData.size(); + if (this.colData != null) { + this.colData.clear(); + } + + setNumOfCols(size); + } + + public int getNumOfRows() { + return this.numOfRows; + } + + public void setNumOfRows(int numOfRows) { + this.numOfRows = numOfRows; + } + + public int getNumOfCols() { + return this.colData.size(); + } + + public void setNumOfCols(int numOfCols) { + this.colData = new ArrayList(numOfCols); + this.colData.addAll(Collections.nCopies(numOfCols, null)); + } + + public boolean hasMore() { + return this.rowIndex < this.numOfRows; + } + + public boolean forward() { + if (this.rowIndex > this.numOfRows) { + return false; + } + + return ((++this.rowIndex) < this.numOfRows); + } + + public void reset() { + this.rowIndex = 0; + } + + public void setBoolean(int col, boolean value) { + colData.set(col, value); + } + + public void setByteArray(int col, int length, byte[] value) { + try { + switch (this.columnMetaDataList.get(col).getColType()) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: { + ByteBuffer buf = ByteBuffer.wrap(value, 0, length); + buf.order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(); + this.colData.set(col, buf); + break; + } + case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: { + ByteBuffer buf = ByteBuffer.wrap(value, 0, length); + buf.order(ByteOrder.LITTLE_ENDIAN); + this.colData.set(col, buf); + break; + } + case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: { + ByteBuffer buf = ByteBuffer.wrap(value, 0, length); + ShortBuffer sb = buf.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); + this.colData.set(col, sb); + break; + } + case TSDBConstants.TSDB_DATA_TYPE_UINT: + case TSDBConstants.TSDB_DATA_TYPE_INT: { + ByteBuffer buf = ByteBuffer.wrap(value, 0, length); + IntBuffer ib = buf.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer(); + this.colData.set(col, ib); + break; + } + case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: { + ByteBuffer buf = ByteBuffer.wrap(value, 0, length); + LongBuffer lb = buf.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer(); + this.colData.set(col, lb); + break; + } + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: { + ByteBuffer buf = ByteBuffer.wrap(value, 0, length); + FloatBuffer fb = buf.order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer(); + this.colData.set(col, fb); + break; + } + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { + ByteBuffer buf = ByteBuffer.wrap(value, 0, length); + DoubleBuffer db = buf.order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer(); + this.colData.set(col, db); + break; + } + case TSDBConstants.TSDB_DATA_TYPE_BINARY: { + ByteBuffer buf = ByteBuffer.wrap(value, 0, length); + buf.order(ByteOrder.LITTLE_ENDIAN); + this.colData.set(col, buf); + break; + } + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: { + ByteBuffer buf = ByteBuffer.wrap(value, 0, length); + LongBuffer lb = buf.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer(); + this.colData.set(col, lb); + break; + } + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: { + ByteBuffer buf = ByteBuffer.wrap(value, 0, length); + buf.order(ByteOrder.LITTLE_ENDIAN); + this.colData.set(col, buf); + break; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static class NullType { + private static final byte NULL_BOOL_VAL = 0x2; + private static final String NULL_STR = "null"; + + public String toString() { + return NullType.NULL_STR; + } + + public static boolean isBooleanNull(byte val) { + return val == NullType.NULL_BOOL_VAL; + } + + private static boolean isTinyIntNull(byte val) { + return val == Byte.MIN_VALUE; + } + + private static boolean isSmallIntNull(short val) { + return val == Short.MIN_VALUE; + } + + private static boolean isIntNull(int val) { + return val == Integer.MIN_VALUE; + } + + private static boolean isBigIntNull(long val) { + return val == Long.MIN_VALUE; + } + + private static boolean isFloatNull(float val) { + return Float.isNaN(val); + } + + private static boolean isDoubleNull(double val) { + return Double.isNaN(val); + } + + private static boolean isBinaryNull(byte[] val, int length) { + if (length != Byte.BYTES) { + return false; + } + + return val[0] == 0xFF; + } + + private static boolean isNcharNull(byte[] val, int length) { + if (length != Integer.BYTES) { + return false; + } + + return (val[0] & val[1] & val[2] & val[3]) == 0xFF; + } + + } + + /** + * The original type may not be a string type, but will be converted to by + * calling this method + * + * @param col column index + * @return + * @throws SQLException + */ + public String getString(int col) throws SQLException { + Object obj = get(col); + if (obj == null) { + return new NullType().toString(); + } + + return obj.toString(); + } + + public int getInt(int col) { + Object obj = get(col); + if (obj == null) { + return 0; + } + + int type = this.columnMetaDataList.get(col).getColType(); + switch (type) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: + case TSDBConstants.TSDB_DATA_TYPE_INT: { + return (int) obj; + } + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: { + return ((Long) obj).intValue(); + } + + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { + return ((Double) obj).intValue(); + } + + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + case TSDBConstants.TSDB_DATA_TYPE_BINARY: { + return Integer.parseInt((String) obj); + } + } + + return 0; + } + + public boolean getBoolean(int col) throws SQLException { + Object obj = get(col); + if (obj == null) { + return Boolean.FALSE; + } + + int type = this.columnMetaDataList.get(col).getColType(); + switch (type) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: + case TSDBConstants.TSDB_DATA_TYPE_INT: { + return ((int) obj == 0L) ? Boolean.FALSE : Boolean.TRUE; + } + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: { + return (((Long) obj) == 0L) ? Boolean.FALSE : Boolean.TRUE; + } + + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { + return (((Double) obj) == 0) ? Boolean.FALSE : Boolean.TRUE; + } + + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + case TSDBConstants.TSDB_DATA_TYPE_BINARY: { + if ("TRUE".compareToIgnoreCase((String) obj) == 0) { + return Boolean.TRUE; + } else if ("FALSE".compareToIgnoreCase((String) obj) == 0) { + return Boolean.TRUE; + } else { + throw new SQLDataException(); + } + } + } + + return Boolean.FALSE; + } + + public long getLong(int col) throws SQLException { + Object obj = get(col); + if (obj == null) { + return 0; + } + + int type = this.columnMetaDataList.get(col).getColType(); + switch (type) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: + case TSDBConstants.TSDB_DATA_TYPE_INT: { + return (int) obj; + } + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: { + return (long) obj; + } + + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { + return ((Double) obj).longValue(); + } + + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + case TSDBConstants.TSDB_DATA_TYPE_BINARY: { + return Long.parseLong((String) obj); + } + } + + return 0; + } + + public Timestamp getTimestamp(int col) { + try { + return new Timestamp(getLong(col)); + } catch (SQLException e) { + e.printStackTrace(); + } + + return null; + } + + public double getDouble(int col) { + Object obj = get(col); + if (obj == null) { + return 0; + } + + int type = this.columnMetaDataList.get(col).getColType(); + switch (type) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: + case TSDBConstants.TSDB_DATA_TYPE_INT: { + return (int) obj; + } + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: { + return (long) obj; + } + + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { + return (double) obj; + } + + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + case TSDBConstants.TSDB_DATA_TYPE_BINARY: { + return Double.parseDouble((String) obj); + } + } + + return 0; + } + + public Object get(int col) { + int fieldSize = this.columnMetaDataList.get(col).getColSize(); + + switch (this.columnMetaDataList.get(col).getColType()) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: { + ByteBuffer bb = (ByteBuffer) this.colData.get(col); + + byte val = bb.get(this.rowIndex); + if (NullType.isBooleanNull(val)) { + return null; + } + + return (val == 0x0) ? Boolean.FALSE : Boolean.TRUE; + } + + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: { + ByteBuffer bb = (ByteBuffer) this.colData.get(col); + + byte val = bb.get(this.rowIndex); + if (NullType.isTinyIntNull(val)) { + return null; + } + + return val; + } + + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: { + ShortBuffer sb = (ShortBuffer) this.colData.get(col); + short val = sb.get(this.rowIndex); + if (NullType.isSmallIntNull(val)) { + return null; + } + + return val; + } + + case TSDBConstants.TSDB_DATA_TYPE_INT: { + IntBuffer ib = (IntBuffer) this.colData.get(col); + int val = ib.get(this.rowIndex); + if (NullType.isIntNull(val)) { + return null; + } + + return val; + } + + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: { + LongBuffer lb = (LongBuffer) this.colData.get(col); + long val = lb.get(this.rowIndex); + if (NullType.isBigIntNull(val)) { + return null; + } + + return (long) val; + } + + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: { + FloatBuffer fb = (FloatBuffer) this.colData.get(col); + float val = fb.get(this.rowIndex); + if (NullType.isFloatNull(val)) { + return null; + } + + return val; + } + + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { + DoubleBuffer lb = (DoubleBuffer) this.colData.get(col); + double val = lb.get(this.rowIndex); + if (NullType.isDoubleNull(val)) { + return null; + } + + return val; + } + + case TSDBConstants.TSDB_DATA_TYPE_BINARY: { + ByteBuffer bb = (ByteBuffer) this.colData.get(col); + bb.position(fieldSize * this.rowIndex); + + int length = bb.getShort(); + + byte[] dest = new byte[length]; + bb.get(dest, 0, length); + if (NullType.isBinaryNull(dest, length)) { + return null; + } + + return new String(dest); + } + + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: { + ByteBuffer bb = (ByteBuffer) this.colData.get(col); + bb.position(fieldSize * this.rowIndex); + + int length = bb.getShort(); + + byte[] dest = new byte[length]; + bb.get(dest, 0, length); + if (NullType.isNcharNull(dest, length)) { + return null; + } + + try { + String ss = TaosGlobalConfig.getCharset(); + return new String(dest, ss); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + } + + return 0; + } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetMetaData.java index e0b1c246cbea246f885ab4c56c7ece0be212ff66..7abd997459fd8478a0a3bd06aa3eb0f51d1584ae 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetMetaData.java @@ -126,34 +126,12 @@ public class TSDBResultSetMetaData extends WrapperImpl implements ResultSetMetaD public int getColumnType(int column) throws SQLException { ColumnMetaData meta = this.colMetaDataList.get(column - 1); - switch (meta.getColType()) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: - return Types.BOOLEAN; - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: - return java.sql.Types.TINYINT; - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: - return java.sql.Types.SMALLINT; - case TSDBConstants.TSDB_DATA_TYPE_INT: - return java.sql.Types.INTEGER; - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: - return java.sql.Types.BIGINT; - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: - return java.sql.Types.FLOAT; - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: - return java.sql.Types.DOUBLE; - case TSDBConstants.TSDB_DATA_TYPE_BINARY: - return Types.BINARY; - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: - return java.sql.Types.TIMESTAMP; - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: - return Types.NCHAR; - } - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); + return TSDBConstants.taosType2JdbcType(meta.getColType()); } public String getColumnTypeName(int column) throws SQLException { ColumnMetaData meta = this.colMetaDataList.get(column - 1); - return TSDBConstants.DATATYPE_MAP.get(meta.getColType()); + return TSDBConstants.taosType2JdbcTypeName(meta.getColType()); } public boolean isReadOnly(int column) throws SQLException { diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetRowData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetRowData.java index 6518bf10e444a05073206e1ef72b8f21a87e26b1..44d760897b2cdac20c510e0c3f98910f25ac40d8 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetRowData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetRowData.java @@ -14,208 +14,322 @@ *****************************************************************************/ package com.taosdata.jdbc; +import java.math.BigDecimal; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collections; public class TSDBResultSetRowData { - private ArrayList data = null; - private int colSize = 0; - - public TSDBResultSetRowData(int colSize) { - this.setColSize(colSize); - } - - public TSDBResultSetRowData() { - this.data = new ArrayList<>(); - this.setColSize(0); - } - - public void clear() { - if(this.data != null) { - this.data.clear(); - } - if (this.colSize == 0) { - return; - } - this.data = new ArrayList<>(colSize); - this.data.addAll(Collections.nCopies(this.colSize, null)); - } - - public boolean wasNull(int col) { - return data.get(col) == null; - } - - public void setBoolean(int col, boolean value) { - data.set(col, value); - } - - public boolean getBoolean(int col, int srcType) throws SQLException { - Object obj = data.get(col); - - switch(srcType) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: return (Boolean) obj; - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return ((Float) obj) == 1.0? Boolean.TRUE:Boolean.FALSE; - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: return ((Double) obj) == 1.0? Boolean.TRUE:Boolean.FALSE; - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: return ((Byte) obj) == 1? Boolean.TRUE:Boolean.FALSE; - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:return ((Short)obj) == 1? Boolean.TRUE:Boolean.FALSE; - case TSDBConstants.TSDB_DATA_TYPE_INT: return ((Integer)obj) == 1? Boolean.TRUE:Boolean.FALSE; - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return ((Long) obj) == 1L? Boolean.TRUE:Boolean.FALSE; - } - - return Boolean.TRUE; - } - - public void setByte(int col, byte value) { - data.set(col, value); - } - - public void setShort(int col, short value) { - data.set(col, value); - } - - public void setInt(int col, int value) { - data.set(col, value); - } - - public int getInt(int col, int srcType) throws SQLException { - Object obj = data.get(col); - - switch(srcType) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj)? 1:0; - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return ((Float) obj).intValue(); - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: return ((Double)obj).intValue(); - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: return (Byte) obj; - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:return (Short) obj; - case TSDBConstants.TSDB_DATA_TYPE_INT: return (Integer) obj; - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return ((Long) obj).intValue(); - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: - case TSDBConstants.TSDB_DATA_TYPE_BINARY: return Integer.parseInt((String) obj); - } - - return 0; - } - - public void setLong(int col, long value) { - data.set(col, value); - } - - public long getLong(int col, int srcType) throws SQLException { - Object obj = data.get(col); - - switch(srcType) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj)? 1:0; - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return ((Float) obj).longValue(); - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: return ((Double) obj).longValue(); - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: return (Byte) obj; - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:return (Short) obj; - case TSDBConstants.TSDB_DATA_TYPE_INT: return (Integer) obj; - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return (Long) obj; - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: - case TSDBConstants.TSDB_DATA_TYPE_BINARY: return Long.parseLong((String) obj); - } - - return 0; - } - - public void setFloat(int col, float value) { - data.set(col, value); - } - - public float getFloat(int col, int srcType) throws SQLException { - Object obj = data.get(col); - - switch(srcType) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj)? 1:0; - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return (Float) obj; - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: return ((Double) obj).floatValue(); - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: return (Byte) obj; - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: return (Short) obj; - case TSDBConstants.TSDB_DATA_TYPE_INT: return (Integer) obj; - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return (Long) obj; - } - - return 0; - } - - public void setDouble(int col, double value) { - data.set(col, value); - } - - public double getDouble(int col, int srcType) throws SQLException { - Object obj = data.get(col); - - switch(srcType) { - case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj)? 1:0; - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return (Float) obj; - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: return (Double) obj; - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: return (Byte) obj; - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:return (Short) obj; - case TSDBConstants.TSDB_DATA_TYPE_INT: return (Integer) obj; - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return (Long) obj; - } - - return 0; - } - - public void setString(int col, String value) { - data.set(col, value); - } - - public void setByteArray(int col, byte[] value) { + private ArrayList data = null; + private int colSize = 0; + + public TSDBResultSetRowData(int colSize) { + this.setColSize(colSize); + } + + public TSDBResultSetRowData() { + this.data = new ArrayList<>(); + this.setColSize(0); + } + + public void clear() { + if (this.data != null) { + this.data.clear(); + } + if (this.colSize == 0) { + return; + } + this.data = new ArrayList<>(colSize); + this.data.addAll(Collections.nCopies(this.colSize, null)); + } + + public boolean wasNull(int col) { + return data.get(col) == null; + } + + public void setBoolean(int col, boolean value) { + data.set(col, value); + } + + public boolean getBoolean(int col, int srcType) throws SQLException { + Object obj = data.get(col); + + switch (srcType) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: + return (Boolean) obj; + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + return ((Float) obj) == 1.0 ? Boolean.TRUE : Boolean.FALSE; + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: + return ((Double) obj) == 1.0 ? Boolean.TRUE : Boolean.FALSE; + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + return ((Byte) obj) == 1 ? Boolean.TRUE : Boolean.FALSE; + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: + return ((Short) obj) == 1 ? Boolean.TRUE : Boolean.FALSE; + case TSDBConstants.TSDB_DATA_TYPE_INT: + return ((Integer) obj) == 1 ? Boolean.TRUE : Boolean.FALSE; + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + return ((Long) obj) == 1L ? Boolean.TRUE : Boolean.FALSE; + } + + return Boolean.TRUE; + } + + public void setByte(int col, byte value) { + data.set(col, value); + } + + public void setShort(int col, short value) { + data.set(col, value); + } + + public void setInt(int col, int value) { + data.set(col, value); + } + + public int getInt(int col, int srcType) throws SQLException { + Object obj = data.get(col); + + switch (srcType) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: + return Boolean.TRUE.equals(obj) ? 1 : 0; + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + return ((Float) obj).intValue(); + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: + return ((Double) obj).intValue(); + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + return (Byte) obj; + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: + return (Short) obj; + case TSDBConstants.TSDB_DATA_TYPE_INT: + return (Integer) obj; + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + return ((Long) obj).intValue(); + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + case TSDBConstants.TSDB_DATA_TYPE_BINARY: + return Integer.parseInt((String) obj); + case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: { + Byte value = (byte) obj; + if (value < 0) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); + return value; + } + case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: { + short value = (short) obj; + if (value < 0) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); + return value; + } + case TSDBConstants.TSDB_DATA_TYPE_UINT: { + int value = (int) obj; + if (value < 0) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); + return value; + } + case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: { + long value = (long) obj; + if (value < 0) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); + return new Long(value).intValue(); + } + } + + return 0; + } + + public void setLong(int col, long value) { + data.set(col, value); + } + + public long getLong(int col, int srcType) throws SQLException { + Object obj = data.get(col); + + switch (srcType) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: + return Boolean.TRUE.equals(obj) ? 1 : 0; + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + return ((Float) obj).longValue(); + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: + return ((Double) obj).longValue(); + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + return (Byte) obj; + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: + return (Short) obj; + case TSDBConstants.TSDB_DATA_TYPE_INT: + return (Integer) obj; + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + return (Long) obj; + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + case TSDBConstants.TSDB_DATA_TYPE_BINARY: + return Long.parseLong((String) obj); + case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: { + Byte value = (byte) obj; + if (value < 0) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); + return value; + } + case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: { + short value = (short) obj; + if (value < 0) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); + return value; + } + case TSDBConstants.TSDB_DATA_TYPE_UINT: { + int value = (int) obj; + if (value < 0) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); + return value; + } + case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: { + long value = (long) obj; + if (value < 0) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); + return value; + } + } + + return 0; + } + + public void setFloat(int col, float value) { + data.set(col, value); + } + + public float getFloat(int col, int srcType) throws SQLException { + Object obj = data.get(col); + + switch (srcType) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: + return Boolean.TRUE.equals(obj) ? 1 : 0; + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + return (Float) obj; + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: + return ((Double) obj).floatValue(); + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + return (Byte) obj; + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: + return (Short) obj; + case TSDBConstants.TSDB_DATA_TYPE_INT: + return (Integer) obj; + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + return (Long) obj; + } + + return 0; + } + + public void setDouble(int col, double value) { + data.set(col, value); + } + + public double getDouble(int col, int srcType) throws SQLException { + Object obj = data.get(col); + + switch (srcType) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: + return Boolean.TRUE.equals(obj) ? 1 : 0; + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + return (Float) obj; + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: + return (Double) obj; + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + return (Byte) obj; + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: + return (Short) obj; + case TSDBConstants.TSDB_DATA_TYPE_INT: + return (Integer) obj; + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + return (Long) obj; + } + + return 0; + } + + public void setString(int col, String value) { + data.set(col, value); + } + + public void setByteArray(int col, byte[] value) { try { data.set(col, new String(value, TaosGlobalConfig.getCharset())); } catch (Exception e) { e.printStackTrace(); } - } - - /** - * The original type may not be a string type, but will be converted to by calling this method - * @param col column index - * @return - * @throws SQLException - */ - public String getString(int col, int srcType) throws SQLException { - if (srcType == TSDBConstants.TSDB_DATA_TYPE_BINARY || srcType == TSDBConstants.TSDB_DATA_TYPE_NCHAR) { - return (String) data.get(col); - } else { - return String.valueOf(data.get(col)); - } - } - - public void setTimestamp(int col, long ts) { - data.set(col, ts); - } - - public Timestamp getTimestamp(int col) { - return new Timestamp((Long) data.get(col)); - } - - public Object get(int col) { - return data.get(col); - } - - public int getColSize() { - return colSize; - } - - public void setColSize(int colSize) { - this.colSize = colSize; - this.clear(); - } - - public ArrayList getData() { - return data; - } - - public void setData(ArrayList data) { - this.data = (ArrayList) data.clone(); - } + } + + /** + * The original type may not be a string type, but will be converted to by calling this method + * + * @param col column index + * @return + * @throws SQLException + */ + public String getString(int col, int srcType) throws SQLException { + switch (srcType) { + case TSDBConstants.TSDB_DATA_TYPE_BINARY: + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + return (String) data.get(col); + case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: { + Byte value = new Byte(String.valueOf(data.get(col))); + if (value >= 0) + return value.toString(); + return Integer.toString(value & 0xff); + } + case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: { + Short value = new Short(String.valueOf(data.get(col))); + if (value >= 0) + return value.toString(); + return Integer.toString(value & 0xffff); + } + case TSDBConstants.TSDB_DATA_TYPE_UINT: { + Integer value = new Integer(String.valueOf(data.get(col))); + if (value >= 0) + return value.toString(); + return Long.toString(value & 0xffffffffl); + } + case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: { + Long value = new Long(String.valueOf(data.get(col))); + if (value >= 0) + return value.toString(); + long lowValue = value & 0x7fffffffffffffffL; + return BigDecimal.valueOf(lowValue).add(BigDecimal.valueOf(Long.MAX_VALUE)).add(BigDecimal.valueOf(1)).toString(); + } + default: + return String.valueOf(data.get(col)); + } + } + + public void setTimestamp(int col, long ts) { + data.set(col, ts); + } + + public Timestamp getTimestamp(int col) { + return new Timestamp((Long) data.get(col)); + } + + public Object get(int col) { + return data.get(col); + } + + public int getColSize() { + return colSize; + } + + public void setColSize(int colSize) { + this.colSize = colSize; + this.clear(); + } + + public ArrayList getData() { + return data; + } + + public void setData(ArrayList data) { + this.data = (ArrayList) data.clone(); + } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java index 29e849049e8ec0206ca3fe906f86534ecf7fd53a..fb20a621b0f11413e13e4234be357f6456a2a4fa 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java @@ -51,7 +51,6 @@ public class TSDBStatement extends AbstractStatement { this.connector.freeResultSet(pSql); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_WITH_EXECUTEQUERY); } - TSDBResultSet res = new TSDBResultSet(this, this.connector, pSql); res.setBatchFetch(this.connection.getBatchFetch()); return res; diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBSubscribe.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBSubscribe.java index dd0d0d5b7b2744e0e6d28a3d73ec2b29fab37743..c5fd497ca3dfed8bc8555110660ff70a9fd23447 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBSubscribe.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBSubscribe.java @@ -21,27 +21,23 @@ public class TSDBSubscribe { private final long id; TSDBSubscribe(TSDBJNIConnector connecter, long id) throws SQLException { - if (null != connecter) { - this.connecter = connecter; - this.id = id; - } else { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - } + if (connecter == null) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); + + this.connecter = connecter; + this.id = id; } /** * consume - * */ public TSDBResultSet consume() throws SQLException { - if (this.connecter.isClosed()) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - } + if (this.connecter.isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); long resultSetPointer = this.connecter.consume(this.id); - if (resultSetPointer == TSDBConstants.JNI_CONNECTION_NULL) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); } else if (resultSetPointer == TSDBConstants.JNI_NULL_POINTER) { return null; } else { @@ -56,9 +52,9 @@ public class TSDBSubscribe { * @throws SQLException */ public void close(boolean keepProgress) throws SQLException { - if (this.connecter.isClosed()) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - } + if (this.connecter.isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); + this.connecter.unsubscribe(this.id, keepProgress); } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java index 0484701f68f317dab2a1415b321d093d488c5b6a..c1834a7b804428ab3875b8e6937223b651cd217f 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java @@ -27,7 +27,6 @@ public class RestfulConnection extends AbstractConnection { public Statement createStatement() throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_CONNECTION_CLOSED); - ; return new RestfulStatement(this, database); } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java index d45ca0c3a0bc4ea596b9e9577db33e8aa1bee696..a94cfa6e07c62ee9b163823aaad03bcc402bbffc 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java @@ -2,9 +2,7 @@ package com.taosdata.jdbc.rs; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.taosdata.jdbc.AbstractDriver; -import com.taosdata.jdbc.TSDBConstants; -import com.taosdata.jdbc.TSDBDriver; +import com.taosdata.jdbc.*; import com.taosdata.jdbc.utils.HttpClientPoolUtil; import java.io.UnsupportedEncodingException; @@ -21,15 +19,16 @@ public class RestfulDriver extends AbstractDriver { try { DriverManager.registerDriver(new RestfulDriver()); } catch (SQLException e) { - throw new RuntimeException(TSDBConstants.WrapErrMsg("can not register Restful JDBC driver"), e); + throw TSDBError.createRuntimeException(TSDBErrorNumbers.ERROR_URL_NOT_SET, e); } } @Override public Connection connect(String url, Properties info) throws SQLException { // throw SQLException if url is null - if (url == null) - throw new SQLException(TSDBConstants.WrapErrMsg("url is not set!")); + if (url == null || url.trim().isEmpty() || url.trim().replaceAll("\\s", "").isEmpty()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_URL_NOT_SET); + // return null if url is not be accepted if (!acceptsURL(url)) return null; @@ -61,14 +60,20 @@ public class RestfulDriver extends AbstractDriver { throw new SQLException(jsonResult.getString("desc")); } - return new RestfulConnection(host, port, props, database, url); + RestfulConnection conn = new RestfulConnection(host, port, props, database, url); + if (database != null && !database.trim().replaceAll("\\s", "").isEmpty()) { + Statement stmt = conn.createStatement(); + stmt.execute("use " + database); + stmt.close(); + } + return conn; } @Override public boolean acceptsURL(String url) throws SQLException { if (url == null) - throw new SQLException(TSDBConstants.WrapErrMsg("url is null")); - return (url != null && url.length() > 0 && url.trim().length() > 0) && url.startsWith(URL_PREFIX); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_URL_NOT_SET); + return url.length() > 0 && url.trim().length() > 0 && url.startsWith(URL_PREFIX); } @Override diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSet.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSet.java index 9d394b8b038917d637ab43826d57e4d79b1746c7..30081a63c3e8a77208b29ddbf1fc34079d4c2818 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSet.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSet.java @@ -9,7 +9,6 @@ import com.taosdata.jdbc.TSDBErrorNumbers; import java.sql.*; import java.util.ArrayList; -import java.util.List; public class RestfulResultSet extends AbstractResultSet implements ResultSet { private volatile boolean isClosed; @@ -17,8 +16,9 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { private final String database; private final Statement statement; +// private final JSONObject resultJson; // data - private ArrayList> resultSet; + private final ArrayList> resultSet; // meta private ArrayList columnNames; private ArrayList columns; @@ -32,6 +32,8 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { public RestfulResultSet(String database, Statement statement, JSONObject resultJson) throws SQLException { this.database = database; this.statement = statement; +// this.resultJson = resultJson; + // column metadata JSONArray columnMeta = resultJson.getJSONArray("column_meta"); columnNames = new ArrayList<>(); @@ -39,10 +41,11 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { for (int colIndex = 0; colIndex < columnMeta.size(); colIndex++) { JSONArray col = columnMeta.getJSONArray(colIndex); String col_name = col.getString(0); - int col_type = TSDBConstants.taosType2JdbcType(col.getInteger(1)); + int taos_type = col.getInteger(1); + int col_type = TSDBConstants.taosType2JdbcType(taos_type); int col_length = col.getInteger(2); columnNames.add(col_name); - columns.add(new Field(col_name, col_type, col_length, "")); + columns.add(new Field(col_name, col_type, col_length, "", taos_type)); } this.metaData = new RestfulResultSetMetaData(this.database, columns, this); @@ -53,105 +56,50 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { ArrayList row = new ArrayList(); JSONArray jsonRow = data.getJSONArray(rowIndex); for (int colIndex = 0; colIndex < jsonRow.size(); colIndex++) { - row.add(parseColumnData(jsonRow, colIndex, columns.get(colIndex).type)); + row.add(parseColumnData(jsonRow, colIndex, columns.get(colIndex).taos_type)); } resultSet.add(row); } - - /* - int columnIndex = 0; - for (; columnIndex < data.size(); columnIndex++) { - ArrayList oneRow = new ArrayList<>(); - JSONArray one = data.getJSONArray(columnIndex); - for (int j = 0; j < one.size(); j++) { - oneRow.add(one.getString(j)); - } - resultSet.add(oneRow); - } - - // column only names - JSONArray head = resultJson.getJSONArray("head"); - for (int i = 0; i < head.size(); i++) { - String name = head.getString(i); - columnNames.add(name); - columns.add(new Field(name, "", 0, "")); - } - this.metaData = new RestfulResultSetMetaData(this.database, columns, this); - */ } - private Object parseColumnData(JSONArray row, int colIndex, int sqlType) { - switch (sqlType) { - case Types.NULL: - return null; - case Types.BOOLEAN: + private Object parseColumnData(JSONArray row, int colIndex, int taosType) { + switch (taosType) { + case TSDBConstants.TSDB_DATA_TYPE_BOOL: return row.getBoolean(colIndex); - case Types.TINYINT: - case Types.SMALLINT: + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: + return row.getByte(colIndex); + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: return row.getShort(colIndex); - case Types.INTEGER: + case TSDBConstants.TSDB_DATA_TYPE_INT: return row.getInteger(colIndex); - case Types.BIGINT: + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return row.getBigInteger(colIndex); - case Types.FLOAT: + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return row.getFloat(colIndex); - case Types.DOUBLE: + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: return row.getDouble(colIndex); - case Types.TIMESTAMP: + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: return new Timestamp(row.getDate(colIndex).getTime()); - case Types.BINARY: - case Types.NCHAR: + case TSDBConstants.TSDB_DATA_TYPE_BINARY: + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: default: return row.getString(colIndex); } } -// /** -// * 由多个resultSet的JSON构造结果集 -// * -// * @param resultJson: 包含data信息的结果集,有sql返回的结果集 -// * @param fieldJson: 包含多个(最多2个)meta信息的结果集,有describe xxx -// **/ -// public RestfulResultSet(String database, Statement statement, JSONObject resultJson, List fieldJson) throws SQLException { -// this(database, statement, resultJson); -// ArrayList newColumns = new ArrayList<>(); -// -// for (Field column : columns) { -// Field field = findField(column.name, fieldJson); -// if (field != null) { -// newColumns.add(field); -// } else { -// newColumns.add(column); -// } -// } -// this.columns = newColumns; -// this.metaData = new RestfulResultSetMetaData(this.database, this.columns, this); -// } - -// public Field findField(String columnName, List fieldJsonList) { -// for (JSONObject fieldJSON : fieldJsonList) { -// JSONArray fieldDataJson = fieldJSON.getJSONArray("data"); -// for (int i = 0; i < fieldDataJson.size(); i++) { -// JSONArray field = fieldDataJson.getJSONArray(i); -// if (columnName.equalsIgnoreCase(field.getString(0))) { -// return new Field(field.getString(0), field.getString(1), field.getInteger(2), field.getString(3)); -// } -// } -// } -// return null; -// } - public class Field { String name; int type; int length; String note; + int taos_type; - public Field(String name, int type, int length, String note) { + public Field(String name, int type, int length, String note, int taos_type) { this.name = name; this.type = type; this.length = length; this.note = note; + this.taos_type = taos_type; } } @@ -184,10 +132,8 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { public String getString(int columnIndex) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESULTSET_CLOSED); - - if (columnIndex > resultSet.get(pos).size()) { - throw new SQLException(TSDBConstants.WrapErrMsg("Column Index out of range, " + columnIndex + " > " + resultSet.get(pos).size())); - } + if (columnIndex > resultSet.get(pos).size()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE, "Column Index out of range, " + columnIndex + " > " + resultSet.get(pos).size()); columnIndex = getTrueColumnIndex(columnIndex); return resultSet.get(pos).get(columnIndex).toString(); @@ -197,40 +143,107 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { public boolean getBoolean(int columnIndex) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESULTSET_CLOSED); + if (columnIndex > resultSet.get(pos).size()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE, "Column Index out of range, " + columnIndex + " > " + resultSet.get(pos).size()); columnIndex = getTrueColumnIndex(columnIndex); int result = getInt(columnIndex); return result == 0 ? false : true; } + @Override + public byte getByte(int columnIndex) throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESULTSET_CLOSED); + if (columnIndex > resultSet.get(pos).size()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE, "Column Index out of range, " + columnIndex + " > " + resultSet.get(pos).size()); + + columnIndex = getTrueColumnIndex(columnIndex); + Object value = resultSet.get(pos).get(columnIndex); + if (value == null) + return 0; + long valueAsLong = Long.parseLong(value.toString()); + if (valueAsLong == Byte.MIN_VALUE) + return 0; + if (valueAsLong < Byte.MIN_VALUE || valueAsLong > Byte.MAX_VALUE) + throwRangeException(value.toString(), columnIndex, Types.TINYINT); + + return (byte) valueAsLong; + } + + private void throwRangeException(String valueAsString, int columnIndex, int jdbcType) throws SQLException { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE, + "'" + valueAsString + "' in column '" + columnIndex + "' is outside valid range for the jdbcType " + TSDBConstants.jdbcType2TaosTypeName(jdbcType)); + } + @Override public short getShort(int columnIndex) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESULTSET_CLOSED); + if (columnIndex > resultSet.get(pos).size()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE, "Column Index out of range, " + columnIndex + " > " + resultSet.get(pos).size()); + columnIndex = getTrueColumnIndex(columnIndex); - return Short.parseShort(resultSet.get(pos).get(columnIndex).toString()); + Object value = resultSet.get(pos).get(columnIndex); + if (value == null) + return 0; + long valueAsLong = Long.parseLong(value.toString()); + if (valueAsLong == Short.MIN_VALUE) + return 0; + if (valueAsLong < Short.MIN_VALUE || valueAsLong > Short.MAX_VALUE) + throwRangeException(value.toString(), columnIndex, Types.SMALLINT); + return (short) valueAsLong; } @Override public int getInt(int columnIndex) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESULTSET_CLOSED); + if (columnIndex > resultSet.get(pos).size()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE, "Column Index out of range, " + columnIndex + " > " + resultSet.get(pos).size()); + columnIndex = getTrueColumnIndex(columnIndex); - return Integer.parseInt(resultSet.get(pos).get(columnIndex).toString()); + Object value = resultSet.get(pos).get(columnIndex); + if (value == null) + return 0; + long valueAsLong = Long.parseLong(value.toString()); + if (valueAsLong == Integer.MIN_VALUE) + return 0; + if (valueAsLong < Integer.MIN_VALUE || valueAsLong > Integer.MAX_VALUE) + throwRangeException(value.toString(), columnIndex, Types.INTEGER); + return (int) valueAsLong; } @Override public long getLong(int columnIndex) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESULTSET_CLOSED); + if (columnIndex > resultSet.get(pos).size()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE, "Column Index out of range, " + columnIndex + " > " + resultSet.get(pos).size()); + columnIndex = getTrueColumnIndex(columnIndex); - return Long.parseLong(resultSet.get(pos).get(columnIndex).toString()); + Object value = resultSet.get(pos).get(columnIndex); + if (value == null) + return 0; + + long valueAsLong = 0; + try { + valueAsLong = Long.parseLong(value.toString()); + if (valueAsLong == Long.MIN_VALUE) + return 0; + } catch (NumberFormatException e) { + throwRangeException(value.toString(), columnIndex, Types.BIGINT); + } + return valueAsLong; } @Override public float getFloat(int columnIndex) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESULTSET_CLOSED); + if (columnIndex > resultSet.get(pos).size()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE, "Column Index out of range, " + columnIndex + " > " + resultSet.get(pos).size()); + columnIndex = getTrueColumnIndex(columnIndex); return Float.parseFloat(resultSet.get(pos).get(columnIndex).toString()); } @@ -239,6 +252,8 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { public double getDouble(int columnIndex) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESULTSET_CLOSED); + if (columnIndex > resultSet.get(pos).size()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE, "Column Index out of range, " + columnIndex + " > " + resultSet.get(pos).size()); columnIndex = getTrueColumnIndex(columnIndex); return Double.parseDouble(resultSet.get(pos).get(columnIndex).toString()); @@ -246,12 +261,14 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { private int getTrueColumnIndex(int columnIndex) throws SQLException { if (columnIndex < 1) { - throw new SQLException("Column Index out of range, " + columnIndex + " < 1"); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE + , "Column Index out of range, " + columnIndex + " < 1"); } int numOfCols = resultSet.get(pos).size(); if (columnIndex > numOfCols) { - throw new SQLException("Column Index out of range, " + columnIndex + " > " + numOfCols); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE + , "Column Index out of range, " + columnIndex + " > " + numOfCols); } return columnIndex - 1; diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSetMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSetMetaData.java index 29ba13bec107be4564a95d27e800fce53b114f96..7ead8bd1bbaeeb8fe1f24c951656888f6bba6b6f 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSetMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSetMetaData.java @@ -1,6 +1,7 @@ package com.taosdata.jdbc.rs; import com.taosdata.jdbc.TSDBConstants; +import com.taosdata.jdbc.WrapperImpl; import java.sql.ResultSetMetaData; import java.sql.SQLException; @@ -8,7 +9,7 @@ import java.sql.Timestamp; import java.sql.Types; import java.util.ArrayList; -public class RestfulResultSetMetaData implements ResultSetMetaData { +public class RestfulResultSetMetaData extends WrapperImpl implements ResultSetMetaData { private final String database; private ArrayList fields; @@ -20,6 +21,10 @@ public class RestfulResultSetMetaData implements ResultSetMetaData { this.resultSet = resultSet; } + public ArrayList getFields() { + return fields; + } + @Override public int getColumnCount() throws SQLException { return fields.size(); @@ -134,8 +139,8 @@ public class RestfulResultSetMetaData implements ResultSetMetaData { @Override public String getColumnTypeName(int column) throws SQLException { - int type = fields.get(column - 1).type; - return TSDBConstants.jdbcType2TaosTypeName(type); + int taos_type = fields.get(column - 1).taos_type; + return TSDBConstants.taosType2JdbcTypeName(taos_type); } @Override @@ -180,18 +185,4 @@ public class RestfulResultSetMetaData implements ResultSetMetaData { return columnClassName; } - @Override - public T unwrap(Class iface) throws SQLException { - try { - return iface.cast(this); - } catch (ClassCastException cce) { - throw new SQLException("Unable to unwrap to " + iface.toString()); - } - } - - @Override - public boolean isWrapperFor(Class iface) throws SQLException { - return iface.isInstance(this); - } - } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java index d60940d87762340203e54fb7df23579c2f93d510..9071c046726a0ef7df3f712ac3ddbb10d3afafba 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java @@ -4,17 +4,14 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.taosdata.jdbc.AbstractStatement; -import com.taosdata.jdbc.TSDBConstants; import com.taosdata.jdbc.TSDBError; import com.taosdata.jdbc.TSDBErrorNumbers; import com.taosdata.jdbc.utils.HttpClientPoolUtil; import com.taosdata.jdbc.utils.SqlSyntaxValidator; -import java.sql.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; public class RestfulStatement extends AbstractStatement { @@ -30,39 +27,6 @@ public class RestfulStatement extends AbstractStatement { this.database = database; } - protected String[] parseTableIdentifier(String sql) { - sql = sql.trim().toLowerCase(); - String[] ret = null; - if (sql.contains("where")) - sql = sql.substring(0, sql.indexOf("where")); - if (sql.contains("interval")) - sql = sql.substring(0, sql.indexOf("interval")); - if (sql.contains("fill")) - sql = sql.substring(0, sql.indexOf("fill")); - if (sql.contains("sliding")) - sql = sql.substring(0, sql.indexOf("sliding")); - if (sql.contains("group by")) - sql = sql.substring(0, sql.indexOf("group by")); - if (sql.contains("order by")) - sql = sql.substring(0, sql.indexOf("order by")); - if (sql.contains("slimit")) - sql = sql.substring(0, sql.indexOf("slimit")); - if (sql.contains("limit")) - sql = sql.substring(0, sql.indexOf("limit")); - // parse - if (sql.contains("from")) { - sql = sql.substring(sql.indexOf("from") + 4).trim(); - return Arrays.asList(sql.split(",")).stream() - .map(tableIdentifier -> { - tableIdentifier = tableIdentifier.trim(); - if (tableIdentifier.contains(" ")) - tableIdentifier = tableIdentifier.substring(0, tableIdentifier.indexOf(" ")); - return tableIdentifier; - }).collect(Collectors.joining(",")).split(","); - } - return ret; - } - @Override public ResultSet executeQuery(String sql) throws SQLException { if (isClosed()) @@ -75,9 +39,8 @@ public class RestfulStatement extends AbstractStatement { return executeOneQuery(url, sql); } -// if (this.database == null || this.database.isEmpty()) -// throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_DATABASE_NOT_SPECIFIED_OR_AVAILABLE); - HttpClientPoolUtil.execute(url, "use " + this.database); +// if (this.database != null && !this.database.trim().replaceAll("\\s","").isEmpty()) +// HttpClientPoolUtil.execute(url, "use " + this.database); return executeOneQuery(url, sql); } @@ -93,10 +56,8 @@ public class RestfulStatement extends AbstractStatement { return executeOneUpdate(url, sql); } -// if (this.database == null || this.database.isEmpty()) -// throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_DATABASE_NOT_SPECIFIED_OR_AVAILABLE); - - HttpClientPoolUtil.execute(url, "use " + this.database); +// if (this.database != null && !this.database.trim().replaceAll("\\s", "").isEmpty()) +// HttpClientPoolUtil.execute(url, "use " + this.database); return executeOneUpdate(url, sql); } @@ -148,24 +109,9 @@ public class RestfulStatement extends AbstractStatement { String result = HttpClientPoolUtil.execute(url, sql); JSONObject resultJson = JSON.parseObject(result); if (resultJson.getString("status").equals("error")) { - throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + resultJson.getString("desc") + "\n" + "error code: " + resultJson.getString("code"))); + throw TSDBError.createSQLException(resultJson.getInteger("code"), resultJson.getString("desc")); } - // parse table name from sql -// String[] tableIdentifiers = parseTableIdentifier(sql); -// if (tableIdentifiers != null) { -// List fieldJsonList = new ArrayList<>(); -// for (String tableIdentifier : tableIdentifiers) { -// String fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + tableIdentifier); -// JSONObject fieldJson = JSON.parseObject(fields); -// if (fieldJson.getString("status").equals("error")) { -// throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + fieldJson.getString("desc") + "\n" + "error code: " + fieldJson.getString("code"))); -// } -// fieldJsonList.add(fieldJson); -// } -// this.resultSet = new RestfulResultSet(database, this, resultJson, fieldJsonList); -// } else { this.resultSet = new RestfulResultSet(database, this, resultJson); -// } this.affectedRows = 0; return resultSet; } @@ -177,7 +123,7 @@ public class RestfulStatement extends AbstractStatement { String result = HttpClientPoolUtil.execute(url, sql); JSONObject jsonObject = JSON.parseObject(result); if (jsonObject.getString("status").equals("error")) { - throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + jsonObject.getString("desc") + "\n" + "error code: " + jsonObject.getString("code"))); + throw TSDBError.createSQLException(jsonObject.getInteger("code"), jsonObject.getString("desc")); } this.resultSet = null; this.affectedRows = checkJsonResultSet(jsonObject); diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java index aba95f65b7d618d8f008cad986dd879f5113724c..161539962da5e26e928a669083954cc0817483a2 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java @@ -15,12 +15,12 @@ public class TSDBJNIConnectorTest { public void test() { try { // init - TSDBJNIConnector.init(null, null, null, null); + TSDBJNIConnector.init("/etc/taos/taos.cfg", null, null, null); // connect TSDBJNIConnector connector = new TSDBJNIConnector(); - connector.connect("127.0.0.1", 6030, null, "root", "taosdata"); + connector.connect("127.0.0.1", 6030, "unsign_jni", "root", "taosdata"); // executeQuery - long pSql = connector.executeQuery("show variables"); + long pSql = connector.executeQuery("select * from unsign_jni.us_table"); if (connector.isUpdateQuery(pSql)) { connector.freeResultSet(pSql); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_WITH_EXECUTEQUERY); @@ -29,13 +29,13 @@ public class TSDBJNIConnectorTest { List columnMetaDataList = new ArrayList<>(); int code = connector.getSchemaMetaData(pSql, columnMetaDataList); if (code == TSDBConstants.JNI_CONNECTION_NULL) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); } if (code == TSDBConstants.JNI_RESULT_SET_NULL) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_RESULT_SET_NULL)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL); } if (code == TSDBConstants.JNI_NUM_OF_FIELDS_0) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_NUM_OF_FIELDS_0)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_NUM_OF_FIELDS_0); } int columnSize = columnMetaDataList.size(); // print metadata diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/InsertDbwithoutUseDbTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/InsertDbwithoutUseDbTest.java new file mode 100644 index 0000000000000000000000000000000000000000..54ee8fd6ebafbb18dfcfbab99503c1c30f148dc8 --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/InsertDbwithoutUseDbTest.java @@ -0,0 +1,92 @@ +package com.taosdata.jdbc.cases; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import java.sql.*; +import java.util.Properties; +import java.util.Random; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class InsertDbwithoutUseDbTest { + + private static String host = "127.0.0.1"; + // private static String host = "master"; + private static Properties properties; + private static Random random = new Random(System.currentTimeMillis()); + + @Test + public void case001() throws ClassNotFoundException, SQLException { + // prepare schema + Class.forName("com.taosdata.jdbc.TSDBDriver"); + String url = "jdbc:TAOS://127.0.0.1:6030/?user=root&password=taosdata"; + Connection conn = DriverManager.getConnection(url, properties); + try (Statement stmt = conn.createStatement()) { + stmt.execute("drop database if exists inWithoutDb"); + stmt.execute("create database if not exists inWithoutDb"); + stmt.execute("create table inWithoutDb.weather(ts timestamp, f1 int)"); + } + conn.close(); + + // execute insert + url = "jdbc:TAOS://127.0.0.1:6030/inWithoutDb?user=root&password=taosdata"; + conn = DriverManager.getConnection(url, properties); + try (Statement stmt = conn.createStatement()) { + int affectedRow = stmt.executeUpdate("insert into weather(ts, f1) values(now," + random.nextInt(100) + ")"); + Assert.assertEquals(1, affectedRow); + boolean flag = stmt.execute("insert into weather(ts, f1) values(now + 10s," + random.nextInt(100) + ")"); + Assert.assertEquals(false, flag); + ResultSet rs = stmt.executeQuery("select count(*) from weather"); + rs.next(); + int count = rs.getInt("count(*)"); + Assert.assertEquals(2, count); + + } catch (SQLException e) { + e.printStackTrace(); + } + + conn.close(); + } + + @Test + public void case002() throws ClassNotFoundException, SQLException { + // prepare the schema + Class.forName("com.taosdata.jdbc.rs.RestfulDriver"); + final String url = "jdbc:TAOS-RS://" + host + ":6041/inWithoutDb?user=root&password=taosdata"; + Connection conn = DriverManager.getConnection(url, properties); + try (Statement stmt = conn.createStatement()) { + stmt.execute("drop database if exists inWithoutDb"); + stmt.execute("create database if not exists inWithoutDb"); + stmt.execute("create table inWithoutDb.weather(ts timestamp, f1 int)"); + } + conn.close(); + + // execute + conn = DriverManager.getConnection(url, properties); + try (Statement stmt = conn.createStatement()) { + int affectedRow = stmt.executeUpdate("insert into weather(ts, f1) values(now," + random.nextInt(100) + ")"); + Assert.assertEquals(1, affectedRow); + boolean flag = stmt.execute("insert into weather(ts, f1) values(now + 10s," + random.nextInt(100) + ")"); + Assert.assertEquals(false, flag); + ResultSet rs = stmt.executeQuery("select count(*) from weather"); + rs.next(); + int count = rs.getInt("count(*)"); + Assert.assertEquals(2, count); + + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @BeforeClass + public static void beforeClass() { + properties = new Properties(); + properties.setProperty("charset", "UTF-8"); + properties.setProperty("locale", "en_US.UTF-8"); + properties.setProperty("timezone", "UTC-8"); + } + +} diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/UnsignedNumberJniTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/UnsignedNumberJniTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d1816a3e7c84703e76a28e6b0e5db842de9c0ac4 --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/UnsignedNumberJniTest.java @@ -0,0 +1,191 @@ +package com.taosdata.jdbc.cases; + +import com.taosdata.jdbc.TSDBDriver; +import org.junit.*; +import org.junit.runners.MethodSorters; + +import java.sql.*; +import java.util.Properties; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class UnsignedNumberJniTest { + private static final String host = "127.0.0.1"; + private static Connection conn; + + @Test + public void testCase001() { + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from us_table"); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + for (int i = 1; i <= meta.getColumnCount(); i++) { + System.out.print(meta.getColumnLabel(i) + ": " + rs.getString(i) + "\t"); + } + System.out.println(); + Assert.assertEquals("127", rs.getString(2)); + Assert.assertEquals("32767", rs.getString(3)); + Assert.assertEquals("2147483647", rs.getString(4)); + Assert.assertEquals("9223372036854775807", rs.getString(5)); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Test + public void testCase002() { + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from us_table"); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + System.out.print(meta.getColumnLabel(1) + ": " + rs.getTimestamp(1) + "\t"); + System.out.print(meta.getColumnLabel(2) + ": " + rs.getByte(2) + "\t"); + System.out.print(meta.getColumnLabel(3) + ": " + rs.getShort(3) + "\t"); + System.out.print(meta.getColumnLabel(4) + ": " + rs.getInt(4) + "\t"); + System.out.print(meta.getColumnLabel(5) + ": " + rs.getLong(5) + "\t"); + System.out.println(); + Assert.assertEquals(127, rs.getByte(2)); + Assert.assertEquals(32767, rs.getShort(3)); + Assert.assertEquals(2147483647, rs.getInt(4)); + Assert.assertEquals(9223372036854775807l, rs.getLong(5)); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Test(expected = SQLException.class) + public void testCase003() throws SQLException { + try (Statement stmt = conn.createStatement()) { + long now = System.currentTimeMillis(); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + now + ", 127, 32767,2147483647, 18446744073709551614)"); + ResultSet rs = stmt.executeQuery("select * from us_table where ts = " + now); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + System.out.print(meta.getColumnLabel(1) + ": " + rs.getTimestamp(1) + "\t"); + System.out.print(meta.getColumnLabel(2) + ": " + rs.getByte(2) + "\t"); + System.out.print(meta.getColumnLabel(3) + ": " + rs.getShort(3) + "\t"); + System.out.print(meta.getColumnLabel(4) + ": " + rs.getInt(4) + "\t"); + System.out.print(meta.getColumnLabel(5) + ": " + rs.getLong(5) + "\t"); + System.out.println(); + Assert.assertEquals(127, rs.getByte(2)); + Assert.assertEquals(32767, rs.getShort(3)); + Assert.assertEquals(2147483647, rs.getInt(4)); + } + } + } + + @Test(expected = SQLException.class) + public void testCase004() throws SQLException { + try (Statement stmt = conn.createStatement()) { + long now = System.currentTimeMillis(); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + now + ", 127, 32767,4294967294, 18446744073709551614)"); + ResultSet rs = stmt.executeQuery("select * from us_table where ts = " + now); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + System.out.print(meta.getColumnLabel(1) + ": " + rs.getTimestamp(1) + "\t"); + System.out.print(meta.getColumnLabel(2) + ": " + rs.getByte(2) + "\t"); + System.out.print(meta.getColumnLabel(3) + ": " + rs.getShort(3) + "\t"); + System.out.print(meta.getColumnLabel(4) + ": " + rs.getInt(4) + "\t"); + System.out.print(meta.getColumnLabel(5) + ": " + rs.getLong(5) + "\t"); + System.out.println(); + Assert.assertEquals(127, rs.getByte(2)); + Assert.assertEquals(32767, rs.getShort(3)); + } + } + } + + @Test(expected = SQLException.class) + public void testCase005() throws SQLException { + try (Statement stmt = conn.createStatement()) { + long now = System.currentTimeMillis(); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + now + ", 127, 65534,4294967294, 18446744073709551614)"); + ResultSet rs = stmt.executeQuery("select * from us_table where ts = " + now); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + System.out.print(meta.getColumnLabel(1) + ": " + rs.getTimestamp(1) + "\t"); + System.out.print(meta.getColumnLabel(2) + ": " + rs.getByte(2) + "\t"); + System.out.print(meta.getColumnLabel(3) + ": " + rs.getShort(3) + "\t"); + System.out.print(meta.getColumnLabel(4) + ": " + rs.getInt(4) + "\t"); + System.out.print(meta.getColumnLabel(5) + ": " + rs.getLong(5) + "\t"); + System.out.println(); + + Assert.assertEquals(127, rs.getByte(2)); + } + } + } + + @Test(expected = SQLException.class) + public void testCase006() throws SQLException { + try (Statement stmt = conn.createStatement()) { + long now = System.currentTimeMillis(); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + now + ", 254, 65534,4294967294, 18446744073709551614)"); + ResultSet rs = stmt.executeQuery("select * from us_table where ts = " + now); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + System.out.print(meta.getColumnLabel(1) + ": " + rs.getTimestamp(1) + "\t"); + System.out.print(meta.getColumnLabel(2) + ": " + rs.getByte(2) + "\t"); + System.out.print(meta.getColumnLabel(3) + ": " + rs.getShort(3) + "\t"); + System.out.print(meta.getColumnLabel(4) + ": " + rs.getInt(4) + "\t"); + System.out.print(meta.getColumnLabel(5) + ": " + rs.getLong(5) + "\t"); + System.out.println(); + } + } + } + + @Test + public void testCase007() throws SQLException { + try (Statement stmt = conn.createStatement()) { + long now = System.currentTimeMillis(); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + now + ", 254, 65534,4294967294, 18446744073709551614)"); + ResultSet rs = stmt.executeQuery("select * from us_table where ts = " + now); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + for (int i = 1; i <= meta.getColumnCount(); i++) { + System.out.print(meta.getColumnLabel(i) + ": " + rs.getString(i) + "\t"); + } + System.out.println(); + Assert.assertEquals("254", rs.getString(2)); + Assert.assertEquals("65534", rs.getString(3)); + Assert.assertEquals("4294967294", rs.getString(4)); + Assert.assertEquals("18446744073709551614", rs.getString(5)); + } + } + } + + + @BeforeClass + public static void beforeClass() { + Properties properties = new Properties(); + properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); + properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); + properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); + + try { + Class.forName("com.taosdata.jdbc.TSDBDriver"); + final String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata"; + conn = DriverManager.getConnection(url, properties); + + Statement stmt = conn.createStatement(); + stmt.execute("drop database if exists unsign_jni"); + stmt.execute("create database if not exists unsign_jni"); + stmt.execute("use unsign_jni"); + stmt.execute("create table us_table(ts timestamp, f1 tinyint unsigned, f2 smallint unsigned, f3 int unsigned, f4 bigint unsigned)"); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(now, 127, 32767,2147483647, 9223372036854775807)"); + stmt.close(); + } catch (ClassNotFoundException | SQLException e) { + e.printStackTrace(); + } + } + + @AfterClass + public static void afterClass() { + try { + if (conn != null) + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/UnsignedNumberRestfulTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/UnsignedNumberRestfulTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d0e63067049d0cf739b4e9764af5d352f64f409c --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/UnsignedNumberRestfulTest.java @@ -0,0 +1,177 @@ +package com.taosdata.jdbc.cases; + +import com.taosdata.jdbc.TSDBDriver; +import org.junit.*; +import org.junit.runners.MethodSorters; + +import java.sql.*; +import java.util.Properties; + + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class UnsignedNumberRestfulTest { + private static final String host = "127.0.0.1"; +// private static final String host = "master"; + private static Connection conn; + + @Test + public void testCase001() { + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from us_table"); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + for (int i = 1; i <= meta.getColumnCount(); i++) { + System.out.print(meta.getColumnLabel(i) + ": " + rs.getString(i) + "\t"); + } + System.out.println(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Test + public void testCase002() { + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from us_table"); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + System.out.print(meta.getColumnLabel(1) + ": " + rs.getTimestamp(1) + "\t"); + System.out.print(meta.getColumnLabel(2) + ": " + rs.getByte(2) + "\t"); + System.out.print(meta.getColumnLabel(3) + ": " + rs.getShort(3) + "\t"); + System.out.print(meta.getColumnLabel(4) + ": " + rs.getInt(4) + "\t"); + System.out.print(meta.getColumnLabel(5) + ": " + rs.getLong(5) + "\t"); + System.out.println(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Test(expected = SQLException.class) + public void testCase003() throws SQLException { + try (Statement stmt = conn.createStatement()) { + long now = System.currentTimeMillis(); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + now + ", 127, 32767,2147483647, 18446744073709551614)"); + ResultSet rs = stmt.executeQuery("select * from us_table where ts = " + now); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + System.out.print(meta.getColumnLabel(1) + ": " + rs.getTimestamp(1) + "\t"); + System.out.print(meta.getColumnLabel(2) + ": " + rs.getByte(2) + "\t"); + System.out.print(meta.getColumnLabel(3) + ": " + rs.getShort(3) + "\t"); + System.out.print(meta.getColumnLabel(4) + ": " + rs.getInt(4) + "\t"); + System.out.print(meta.getColumnLabel(5) + ": " + rs.getLong(5) + "\t"); + System.out.println(); + } + } + } + + @Test(expected = SQLException.class) + public void testCase004() throws SQLException { + try (Statement stmt = conn.createStatement()) { + long now = System.currentTimeMillis(); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + now + ", 127, 32767,4294967294, 18446744073709551614)"); + ResultSet rs = stmt.executeQuery("select * from us_table where ts = " + now); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + System.out.print(meta.getColumnLabel(1) + ": " + rs.getTimestamp(1) + "\t"); + System.out.print(meta.getColumnLabel(2) + ": " + rs.getByte(2) + "\t"); + System.out.print(meta.getColumnLabel(3) + ": " + rs.getShort(3) + "\t"); + System.out.print(meta.getColumnLabel(4) + ": " + rs.getInt(4) + "\t"); + System.out.print(meta.getColumnLabel(5) + ": " + rs.getLong(5) + "\t"); + System.out.println(); + } + } + } + + @Test(expected = SQLException.class) + public void testCase005() throws SQLException { + try (Statement stmt = conn.createStatement()) { + long now = System.currentTimeMillis(); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + now + ", 127, 65534,4294967294, 18446744073709551614)"); + ResultSet rs = stmt.executeQuery("select * from us_table where ts = " + now); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + System.out.print(meta.getColumnLabel(1) + ": " + rs.getTimestamp(1) + "\t"); + System.out.print(meta.getColumnLabel(2) + ": " + rs.getByte(2) + "\t"); + System.out.print(meta.getColumnLabel(3) + ": " + rs.getShort(3) + "\t"); + System.out.print(meta.getColumnLabel(4) + ": " + rs.getInt(4) + "\t"); + System.out.print(meta.getColumnLabel(5) + ": " + rs.getLong(5) + "\t"); + System.out.println(); + } + } + } + + @Test(expected = SQLException.class) + public void testCase006() throws SQLException { + try (Statement stmt = conn.createStatement()) { + long now = System.currentTimeMillis(); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + now + ", 254, 65534,4294967294, 18446744073709551614)"); + ResultSet rs = stmt.executeQuery("select * from us_table where ts = " + now); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + System.out.print(meta.getColumnLabel(1) + ": " + rs.getTimestamp(1) + "\t"); + System.out.print(meta.getColumnLabel(2) + ": " + rs.getByte(2) + "\t"); + System.out.print(meta.getColumnLabel(3) + ": " + rs.getShort(3) + "\t"); + System.out.print(meta.getColumnLabel(4) + ": " + rs.getInt(4) + "\t"); + System.out.print(meta.getColumnLabel(5) + ": " + rs.getLong(5) + "\t"); + System.out.println(); + } + } + } + + @Test + public void testCase007() throws SQLException { + try (Statement stmt = conn.createStatement()) { + long now = System.currentTimeMillis(); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + now + ", 254, 65534,4294967294, 18446744073709551614)"); + ResultSet rs = stmt.executeQuery("select * from us_table where ts = " + now); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + for (int i = 1; i <= meta.getColumnCount(); i++) { + System.out.print(meta.getColumnLabel(i) + ": " + rs.getString(i) + "\t"); + } + System.out.println(); + Assert.assertEquals("254", rs.getString(2)); + Assert.assertEquals("65534", rs.getString(3)); + Assert.assertEquals("4294967294", rs.getString(4)); + Assert.assertEquals("18446744073709551614", rs.getString(5)); + } + } + } + + @BeforeClass + public static void beforeClass() { + Properties properties = new Properties(); + properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); + properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); + properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); + + try { + Class.forName("com.taosdata.jdbc.rs.RestfulDriver"); + final String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata"; + conn = DriverManager.getConnection(url, properties); + + Statement stmt = conn.createStatement(); + stmt.execute("drop database if exists unsign_restful"); + stmt.execute("create database if not exists unsign_restful"); + stmt.execute("use unsign_restful"); + stmt.execute("create table us_table(ts timestamp, f1 tinyint unsigned, f2 smallint unsigned, f3 int unsigned, f4 bigint unsigned)"); + stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(now, 127, 32767,2147483647, 9223372036854775807)"); + stmt.close(); + } catch (ClassNotFoundException | SQLException e) { + e.printStackTrace(); + } + } + + @AfterClass + public static void afterClass() { + try { + if (conn != null) + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulResultSetTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulResultSetTest.java index b199eff1baab53d9c4a8c65f7e0bb58157657d33..a15b1964ea8301b5153a548b53c9fc8763cd65f5 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulResultSetTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulResultSetTest.java @@ -40,9 +40,12 @@ public class RestfulResultSetTest { Assert.assertEquals(true, f9); } - @Test(expected = SQLFeatureNotSupportedException.class) + @Test public void getByte() throws SQLException { - rs.getByte(1); + byte f8 = rs.getByte("f8"); + Assert.assertEquals(10, f8); + f8 = rs.getByte(8); + Assert.assertEquals(10, f8); } @Test diff --git a/src/connector/python/linux/python2/setup.py b/src/connector/python/linux/python2/setup.py index 4a829f36c4bf0d6e680ed923573509cc1fad39db..d1fca047c67861706e6e6afc4bc05cd888bff755 100644 --- a/src/connector/python/linux/python2/setup.py +++ b/src/connector/python/linux/python2/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as fh: setuptools.setup( name="taos", - version="2.0.6", + version="2.0.7", author="Taosdata Inc.", author_email="support@taosdata.com", description="TDengine python client package", diff --git a/src/connector/python/linux/python2/taos/cinterface.py b/src/connector/python/linux/python2/taos/cinterface.py index 555cc3435bcbea302b34cbde09772ac5f6fe32b2..4367947341edad7b0a9bdbcaec69a7de9801e267 100644 --- a/src/connector/python/linux/python2/taos/cinterface.py +++ b/src/connector/python/linux/python2/taos/cinterface.py @@ -22,10 +22,10 @@ def _crow_timestamp_to_python(data, num_of_rows, nbytes=None, micro=False): if num_of_rows > 0: return list(map(_timestamp_converter, ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)])) + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)])) else: return list(map(_timestamp_converter, ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)])) + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)])) def _crow_bool_to_python(data, num_of_rows, nbytes=None, micro=False): @@ -145,10 +145,10 @@ def _crow_bigint_to_python(data, num_of_rows, nbytes=None, micro=False): """ if num_of_rows > 0: return [None if ele == FieldType.C_BIGINT_NULL else ele for ele in ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)]] + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)]] else: return [None if ele == FieldType.C_BIGINT_NULL else ele for ele in ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)]] + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)]] def _crow_bigint_unsigned_to_python( @@ -162,13 +162,13 @@ def _crow_bigint_unsigned_to_python( return [ None if ele == FieldType.C_BIGINT_UNSIGNED_NULL else ele for ele in ctypes.cast( data, ctypes.POINTER( - ctypes.c_ulong))[ + ctypes.c_uint64))[ :abs(num_of_rows)]] else: return [ None if ele == FieldType.C_BIGINT_UNSIGNED_NULL else ele for ele in ctypes.cast( data, ctypes.POINTER( - ctypes.c_ulong))[ + ctypes.c_uint64))[ :abs(num_of_rows)]] @@ -600,7 +600,7 @@ class CTaosInterface(object): # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_INT): # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_BIGINT): - # return ctypes.cast(data, ctypes.POINTER(ctypes.c_long))[0] + # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int64))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_FLOAT): # return ctypes.cast(data, ctypes.POINTER(ctypes.c_float))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_DOUBLE): @@ -608,7 +608,7 @@ class CTaosInterface(object): # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_BINARY): # return (ctypes.cast(data, ctypes.POINTER(ctypes.c_char))[0:byte]).rstrip('\x00') # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_TIMESTAMP): - # return ctypes.cast(data, ctypes.POINTER(ctypes.c_long))[0] + # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int64))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_NCHAR): # return (ctypes.cast(data, ctypes.c_char_p).value).rstrip('\x00') diff --git a/src/connector/python/linux/python3/setup.py b/src/connector/python/linux/python3/setup.py index a865f5df856d1b416d7f78da8b1f857a967f5e61..296e79b973fde7c86493565940d63f64e7c6fca3 100644 --- a/src/connector/python/linux/python3/setup.py +++ b/src/connector/python/linux/python3/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as fh: setuptools.setup( name="taos", - version="2.0.5", + version="2.0.7", author="Taosdata Inc.", author_email="support@taosdata.com", description="TDengine python client package", diff --git a/src/connector/python/linux/python3/taos/cinterface.py b/src/connector/python/linux/python3/taos/cinterface.py index 555cc3435bcbea302b34cbde09772ac5f6fe32b2..4367947341edad7b0a9bdbcaec69a7de9801e267 100644 --- a/src/connector/python/linux/python3/taos/cinterface.py +++ b/src/connector/python/linux/python3/taos/cinterface.py @@ -22,10 +22,10 @@ def _crow_timestamp_to_python(data, num_of_rows, nbytes=None, micro=False): if num_of_rows > 0: return list(map(_timestamp_converter, ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)])) + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)])) else: return list(map(_timestamp_converter, ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)])) + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)])) def _crow_bool_to_python(data, num_of_rows, nbytes=None, micro=False): @@ -145,10 +145,10 @@ def _crow_bigint_to_python(data, num_of_rows, nbytes=None, micro=False): """ if num_of_rows > 0: return [None if ele == FieldType.C_BIGINT_NULL else ele for ele in ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)]] + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)]] else: return [None if ele == FieldType.C_BIGINT_NULL else ele for ele in ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)]] + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)]] def _crow_bigint_unsigned_to_python( @@ -162,13 +162,13 @@ def _crow_bigint_unsigned_to_python( return [ None if ele == FieldType.C_BIGINT_UNSIGNED_NULL else ele for ele in ctypes.cast( data, ctypes.POINTER( - ctypes.c_ulong))[ + ctypes.c_uint64))[ :abs(num_of_rows)]] else: return [ None if ele == FieldType.C_BIGINT_UNSIGNED_NULL else ele for ele in ctypes.cast( data, ctypes.POINTER( - ctypes.c_ulong))[ + ctypes.c_uint64))[ :abs(num_of_rows)]] @@ -600,7 +600,7 @@ class CTaosInterface(object): # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_INT): # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_BIGINT): - # return ctypes.cast(data, ctypes.POINTER(ctypes.c_long))[0] + # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int64))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_FLOAT): # return ctypes.cast(data, ctypes.POINTER(ctypes.c_float))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_DOUBLE): @@ -608,7 +608,7 @@ class CTaosInterface(object): # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_BINARY): # return (ctypes.cast(data, ctypes.POINTER(ctypes.c_char))[0:byte]).rstrip('\x00') # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_TIMESTAMP): - # return ctypes.cast(data, ctypes.POINTER(ctypes.c_long))[0] + # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int64))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_NCHAR): # return (ctypes.cast(data, ctypes.c_char_p).value).rstrip('\x00') diff --git a/src/connector/python/linux/python3/taos/cursor.py b/src/connector/python/linux/python3/taos/cursor.py index 2e7c362d547973bc3e78f2eb57a33b7fa2d0635e..32dc0ea3c3676c61c65bf92b4fb04a5373314126 100644 --- a/src/connector/python/linux/python3/taos/cursor.py +++ b/src/connector/python/linux/python3/taos/cursor.py @@ -1,7 +1,6 @@ from .cinterface import CTaosInterface from .error import * from .constants import FieldType -import threading # querySeqNum = 0 @@ -38,7 +37,6 @@ class TDengineCursor(object): self._block_iter = 0 self._affected_rows = 0 self._logfile = "" - self._threadId = threading.get_ident() if connection is not None: self._connection = connection @@ -105,12 +103,6 @@ class TDengineCursor(object): def execute(self, operation, params=None): """Prepare and execute a database operation (query or command). """ - # if threading.get_ident() != self._threadId: - # info ="Cursor execute:Thread ID not match,creater:"+str(self._threadId)+" caller:"+str(threading.get_ident()) - # raise OperationalError(info) - # print(info) - # return None - if not operation: return None @@ -280,12 +272,6 @@ class TDengineCursor(object): def _handle_result(self): """Handle the return result from query. """ - # if threading.get_ident() != self._threadId: - # info = "Cursor handleresult:Thread ID not match,creater:"+str(self._threadId)+" caller:"+str(threading.get_ident()) - # raise OperationalError(info) - # print(info) - # return None - self._description = [] for ele in self._fields: self._description.append( diff --git a/src/connector/python/osx/python3/setup.py b/src/connector/python/osx/python3/setup.py index a6b97f753c7ee3bc303be0db2217e87e889ef4df..9bce1a976febcec457c2b3f8ade0bc6d546307c4 100644 --- a/src/connector/python/osx/python3/setup.py +++ b/src/connector/python/osx/python3/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as fh: setuptools.setup( name="taos", - version="2.0.5", + version="2.0.7", author="Taosdata Inc.", author_email="support@taosdata.com", description="TDengine python client package", diff --git a/src/connector/python/osx/python3/taos/cinterface.py b/src/connector/python/osx/python3/taos/cinterface.py index 6f56cf0c5e09c14fdc9d1296c80e434ab672ef44..dca9bd42e8733616f34654542ba8c2c3ea3ece9d 100644 --- a/src/connector/python/osx/python3/taos/cinterface.py +++ b/src/connector/python/osx/python3/taos/cinterface.py @@ -22,10 +22,10 @@ def _crow_timestamp_to_python(data, num_of_rows, nbytes=None, micro=False): if num_of_rows > 0: return list(map(_timestamp_converter, ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)])) + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)])) else: return list(map(_timestamp_converter, ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)])) + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)])) def _crow_bool_to_python(data, num_of_rows, nbytes=None, micro=False): @@ -145,10 +145,10 @@ def _crow_bigint_to_python(data, num_of_rows, nbytes=None, micro=False): """ if num_of_rows > 0: return [None if ele == FieldType.C_BIGINT_NULL else ele for ele in ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)]] + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)]] else: return [None if ele == FieldType.C_BIGINT_NULL else ele for ele in ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)]] + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)]] def _crow_bigint_unsigned_to_python( @@ -162,13 +162,13 @@ def _crow_bigint_unsigned_to_python( return [ None if ele == FieldType.C_BIGINT_UNSIGNED_NULL else ele for ele in ctypes.cast( data, ctypes.POINTER( - ctypes.c_ulong))[ + ctypes.c_uint64))[ :abs(num_of_rows)]] else: return [ None if ele == FieldType.C_BIGINT_UNSIGNED_NULL else ele for ele in ctypes.cast( data, ctypes.POINTER( - ctypes.c_ulong))[ + ctypes.c_uint64))[ :abs(num_of_rows)]] @@ -600,7 +600,7 @@ class CTaosInterface(object): # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_INT): # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_BIGINT): - # return ctypes.cast(data, ctypes.POINTER(ctypes.c_long))[0] + # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int64))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_FLOAT): # return ctypes.cast(data, ctypes.POINTER(ctypes.c_float))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_DOUBLE): @@ -608,7 +608,7 @@ class CTaosInterface(object): # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_BINARY): # return (ctypes.cast(data, ctypes.POINTER(ctypes.c_char))[0:byte]).rstrip('\x00') # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_TIMESTAMP): - # return ctypes.cast(data, ctypes.POINTER(ctypes.c_long))[0] + # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int64))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_NCHAR): # return (ctypes.cast(data, ctypes.c_char_p).value).rstrip('\x00') diff --git a/src/connector/python/osx/python3/taos/cursor.py b/src/connector/python/osx/python3/taos/cursor.py index 2e7c362d547973bc3e78f2eb57a33b7fa2d0635e..32dc0ea3c3676c61c65bf92b4fb04a5373314126 100644 --- a/src/connector/python/osx/python3/taos/cursor.py +++ b/src/connector/python/osx/python3/taos/cursor.py @@ -1,7 +1,6 @@ from .cinterface import CTaosInterface from .error import * from .constants import FieldType -import threading # querySeqNum = 0 @@ -38,7 +37,6 @@ class TDengineCursor(object): self._block_iter = 0 self._affected_rows = 0 self._logfile = "" - self._threadId = threading.get_ident() if connection is not None: self._connection = connection @@ -105,12 +103,6 @@ class TDengineCursor(object): def execute(self, operation, params=None): """Prepare and execute a database operation (query or command). """ - # if threading.get_ident() != self._threadId: - # info ="Cursor execute:Thread ID not match,creater:"+str(self._threadId)+" caller:"+str(threading.get_ident()) - # raise OperationalError(info) - # print(info) - # return None - if not operation: return None @@ -280,12 +272,6 @@ class TDengineCursor(object): def _handle_result(self): """Handle the return result from query. """ - # if threading.get_ident() != self._threadId: - # info = "Cursor handleresult:Thread ID not match,creater:"+str(self._threadId)+" caller:"+str(threading.get_ident()) - # raise OperationalError(info) - # print(info) - # return None - self._description = [] for ele in self._fields: self._description.append( diff --git a/src/connector/python/windows/python2/setup.py b/src/connector/python/windows/python2/setup.py index 333f5bedad7edeefab66e1c1ba90424085e2aa1c..47d374fe67673172596824f68ed495f68941bb51 100644 --- a/src/connector/python/windows/python2/setup.py +++ b/src/connector/python/windows/python2/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as fh: setuptools.setup( name="taos", - version="2.0.4", + version="2.0.7", author="Taosdata Inc.", author_email="support@taosdata.com", description="TDengine python client package", diff --git a/src/connector/python/windows/python2/taos/cinterface.py b/src/connector/python/windows/python2/taos/cinterface.py index d8cdce2ad138c34db5193e3972ba51d46c693254..ec72474df93a4d13aa13256611044c5bc140f2d3 100644 --- a/src/connector/python/windows/python2/taos/cinterface.py +++ b/src/connector/python/windows/python2/taos/cinterface.py @@ -22,10 +22,10 @@ def _crow_timestamp_to_python(data, num_of_rows, nbytes=None, micro=False): if num_of_rows > 0: return list(map(_timestamp_converter, ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)])) + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)])) else: return list(map(_timestamp_converter, ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)])) + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)])) def _crow_bool_to_python(data, num_of_rows, nbytes=None, micro=False): @@ -145,10 +145,10 @@ def _crow_bigint_to_python(data, num_of_rows, nbytes=None, micro=False): """ if num_of_rows > 0: return [None if ele == FieldType.C_BIGINT_NULL else ele for ele in ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)]] + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)]] else: return [None if ele == FieldType.C_BIGINT_NULL else ele for ele in ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)]] + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)]] def _crow_bigint_unsigned_to_python( @@ -162,13 +162,13 @@ def _crow_bigint_unsigned_to_python( return [ None if ele == FieldType.C_BIGINT_UNSIGNED_NULL else ele for ele in ctypes.cast( data, ctypes.POINTER( - ctypes.c_ulong))[ + ctypes.c_uint64))[ :abs(num_of_rows)]] else: return [ None if ele == FieldType.C_BIGINT_UNSIGNED_NULL else ele for ele in ctypes.cast( data, ctypes.POINTER( - ctypes.c_ulong))[ + ctypes.c_uint64))[ :abs(num_of_rows)]] @@ -600,7 +600,7 @@ class CTaosInterface(object): # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_INT): # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_BIGINT): - # return ctypes.cast(data, ctypes.POINTER(ctypes.c_long))[0] + # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int64))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_FLOAT): # return ctypes.cast(data, ctypes.POINTER(ctypes.c_float))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_DOUBLE): @@ -608,7 +608,7 @@ class CTaosInterface(object): # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_BINARY): # return (ctypes.cast(data, ctypes.POINTER(ctypes.c_char))[0:byte]).rstrip('\x00') # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_TIMESTAMP): - # return ctypes.cast(data, ctypes.POINTER(ctypes.c_long))[0] + # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int64))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_NCHAR): # return (ctypes.cast(data, ctypes.c_char_p).value).rstrip('\x00') diff --git a/src/connector/python/windows/python2/taos/cursor.py b/src/connector/python/windows/python2/taos/cursor.py index 0656b6326e173b111eb8293c6e3b76678eccc0e2..5f4666b59396d4dbd7df6daaf62beb814e3b53f7 100644 --- a/src/connector/python/windows/python2/taos/cursor.py +++ b/src/connector/python/windows/python2/taos/cursor.py @@ -1,7 +1,6 @@ from .cinterface import CTaosInterface from .error import * from .constants import FieldType -import threading # querySeqNum = 0 @@ -38,7 +37,6 @@ class TDengineCursor(object): self._block_iter = 0 self._affected_rows = 0 self._logfile = "" - self._threadId = threading.get_ident() if connection is not None: self._connection = connection diff --git a/src/connector/python/windows/python3/setup.py b/src/connector/python/windows/python3/setup.py index f29fec121b27f5e87ffd215bb4ff5006acf3bf1c..cdcec62a218e72adbda949c0fd666b5ec4abc340 100644 --- a/src/connector/python/windows/python3/setup.py +++ b/src/connector/python/windows/python3/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as fh: setuptools.setup( name="taos", - version="2.0.4", + version="2.0.7", author="Taosdata Inc.", author_email="support@taosdata.com", description="TDengine python client package", diff --git a/src/connector/python/windows/python3/taos/cinterface.py b/src/connector/python/windows/python3/taos/cinterface.py index d8cdce2ad138c34db5193e3972ba51d46c693254..ec72474df93a4d13aa13256611044c5bc140f2d3 100644 --- a/src/connector/python/windows/python3/taos/cinterface.py +++ b/src/connector/python/windows/python3/taos/cinterface.py @@ -22,10 +22,10 @@ def _crow_timestamp_to_python(data, num_of_rows, nbytes=None, micro=False): if num_of_rows > 0: return list(map(_timestamp_converter, ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)])) + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)])) else: return list(map(_timestamp_converter, ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)])) + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)])) def _crow_bool_to_python(data, num_of_rows, nbytes=None, micro=False): @@ -145,10 +145,10 @@ def _crow_bigint_to_python(data, num_of_rows, nbytes=None, micro=False): """ if num_of_rows > 0: return [None if ele == FieldType.C_BIGINT_NULL else ele for ele in ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)]] + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)]] else: return [None if ele == FieldType.C_BIGINT_NULL else ele for ele in ctypes.cast( - data, ctypes.POINTER(ctypes.c_long))[:abs(num_of_rows)]] + data, ctypes.POINTER(ctypes.c_int64))[:abs(num_of_rows)]] def _crow_bigint_unsigned_to_python( @@ -162,13 +162,13 @@ def _crow_bigint_unsigned_to_python( return [ None if ele == FieldType.C_BIGINT_UNSIGNED_NULL else ele for ele in ctypes.cast( data, ctypes.POINTER( - ctypes.c_ulong))[ + ctypes.c_uint64))[ :abs(num_of_rows)]] else: return [ None if ele == FieldType.C_BIGINT_UNSIGNED_NULL else ele for ele in ctypes.cast( data, ctypes.POINTER( - ctypes.c_ulong))[ + ctypes.c_uint64))[ :abs(num_of_rows)]] @@ -600,7 +600,7 @@ class CTaosInterface(object): # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_INT): # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_BIGINT): - # return ctypes.cast(data, ctypes.POINTER(ctypes.c_long))[0] + # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int64))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_FLOAT): # return ctypes.cast(data, ctypes.POINTER(ctypes.c_float))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_DOUBLE): @@ -608,7 +608,7 @@ class CTaosInterface(object): # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_BINARY): # return (ctypes.cast(data, ctypes.POINTER(ctypes.c_char))[0:byte]).rstrip('\x00') # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_TIMESTAMP): - # return ctypes.cast(data, ctypes.POINTER(ctypes.c_long))[0] + # return ctypes.cast(data, ctypes.POINTER(ctypes.c_int64))[0] # elif (dtype == CTaosInterface.TSDB_DATA_TYPE_NCHAR): # return (ctypes.cast(data, ctypes.c_char_p).value).rstrip('\x00') diff --git a/src/connector/python/windows/python3/taos/cursor.py b/src/connector/python/windows/python3/taos/cursor.py index 769cb7cf0f61fe850c16315bf552162f33536502..136cd42fe47a51a34b301730745d73e893861f7c 100644 --- a/src/connector/python/windows/python3/taos/cursor.py +++ b/src/connector/python/windows/python3/taos/cursor.py @@ -1,7 +1,6 @@ from .cinterface import CTaosInterface from .error import * from .constants import FieldType -import threading # querySeqNum = 0 @@ -38,7 +37,6 @@ class TDengineCursor(object): self._block_iter = 0 self._affected_rows = 0 self._logfile = "" - self._threadId = threading.get_ident() if connection is not None: self._connection = connection diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index f8a298bb2bc6e49f953f7f6e96136789369e353d..e069d0295ac5b08c0ff38cc09e90eb5b56817e7b 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -253,7 +253,7 @@ int32_t* taosGetErrno(); #define TSDB_CODE_QRY_IN_EXEC TAOS_DEF_ERROR_CODE(0, 0x0709) //"Multiple retrieval of this query") #define TSDB_CODE_QRY_TOO_MANY_TIMEWINDOW TAOS_DEF_ERROR_CODE(0, 0x070A) //"Too many time window in query") #define TSDB_CODE_QRY_NOT_ENOUGH_BUFFER TAOS_DEF_ERROR_CODE(0, 0x070B) //"Query buffer limit has reached") -#define TSDB_CODE_QRY_INCONSISTAN TAOS_DEF_ERROR_CODE(0, 0x070C) //"File inconsistance in replica") +#define TSDB_CODE_QRY_INCONSISTAN TAOS_DEF_ERROR_CODE(0, 0x070C) //"File inconsistency in replica") // grant diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index f740575b7a0a59c62cd5819dfac1831f26565bec..00f64eba77154fdb1217a7d132f0041906eeec20 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -394,7 +394,7 @@ typedef struct SColIndex { int16_t colId; // column id int16_t colIndex; // column index in colList if it is a normal column or index in tagColList if a tag uint16_t flag; // denote if it is a tag or a normal column - char name[TSDB_COL_NAME_LEN]; + char name[TSDB_COL_NAME_LEN]; // TODO remove it } SColIndex; /* sql function msg, to describe the message to vnode about sql function @@ -402,7 +402,10 @@ typedef struct SColIndex { typedef struct SSqlFuncMsg { int16_t functionId; int16_t numOfParams; + int16_t resColId; // result column id, id of the current output column + int16_t colType; + int16_t colBytes; SColIndex colInfo; struct ArgElem { @@ -482,12 +485,13 @@ typedef struct { int16_t orderColId; int16_t numOfCols; // the number of columns will be load from vnode SInterval interval; + SSessionWindow sw; // session window uint16_t tagCondLen; // tag length in current query uint32_t tbnameCondLen; // table name filter condition string length int16_t numOfGroupCols; // num of group by columns int16_t orderByIdx; int16_t orderType; // used in group by xx order by xxx - int64_t vgroupLimit; // limit the number of rows for each table, used in order by + limit in stable projection query. + int64_t vgroupLimit; // limit the number of rows for each table, used in order by + limit in stable projection query. int16_t prjOrder; // global order in super table projection query. int64_t limit; int64_t offset; @@ -625,6 +629,7 @@ typedef struct { int32_t maxtablesPerVnode; int32_t maxVgroupsPerDb; char arbitrator[TSDB_EP_LEN]; // tsArbitrator + char reserve[2]; // to solve arm32 bus error char timezone[64]; // tsTimezone int64_t checkTime; // 1970-01-01 00:00:00.000 char locale[TSDB_LOCALE_LEN]; // tsLocale diff --git a/src/inc/tsdb.h b/src/inc/tsdb.h index 78cd2927c7b85d39dd0a965971334e4c3b1cbddd..09444bb8e4b2c6cd45ce087a3444338d140a90ae 100644 --- a/src/inc/tsdb.h +++ b/src/inc/tsdb.h @@ -158,13 +158,18 @@ int32_t tsdbInsertData(STsdbRepo *repo, SSubmitMsg *pMsg, SShellSubmitRspMsg *pR typedef void *TsdbQueryHandleT; // Use void to hide implementation details -// query condition to build vnode iterator +#define BLOCK_LOAD_OFFSET_SEQ_ORDER 1 +#define BLOCK_LOAD_TABLE_SEQ_ORDER 2 +#define BLOCK_LOAD_TABLE_RR_ORDER 3 + +// query condition to build multi-table data block iterator typedef struct STsdbQueryCond { STimeWindow twindow; int32_t order; // desc|asc order to iterate the data block int32_t numOfCols; SColumnInfo *colList; bool loadExternalRows; // load external rows or not + int32_t type; // data block load type: } STsdbQueryCond; typedef struct SMemRef { @@ -181,17 +186,31 @@ typedef struct SDataBlockInfo { int32_t tid; } SDataBlockInfo; +typedef struct SFileBlockInfo { + int32_t numOfRows; +} SFileBlockInfo; + typedef struct { void *pTable; TSKEY lastKey; } STableKeyInfo; typedef struct { - size_t numOfTables; + uint32_t numOfTables; SArray * pGroupList; SHashObj *map; // speedup acquire the tableQueryInfo by table uid } STableGroupInfo; +typedef struct { + uint16_t rowSize; + uint16_t numOfFiles; + uint32_t numOfTables; + uint64_t totalSize; + int32_t firstSeekTimeUs; + uint32_t numOfRowsInMemTable; + SArray *dataBlockInfos; +} STableBlockDist; + /** * Get the data block iterator, starting from position according to the query condition * @@ -252,16 +271,7 @@ int64_t tsdbGetNumOfRowsInMemTable(TsdbQueryHandleT* pHandle); * @param pQueryHandle * @return */ -bool tsdbNextDataBlock(TsdbQueryHandleT *pQueryHandle); -/** - * move to next block if exists but not merge data in memtable - * - * @param pQueryHandle - * @return - */ -bool tsdbNextDataBlockWithoutMerge(TsdbQueryHandleT *pQueryHandle); - -SArray* tsdbGetExternalRow(TsdbQueryHandleT *pHandle, SMemRef* pMemRef, int16_t type); +bool tsdbNextDataBlock(TsdbQueryHandleT pQueryHandle); /** * Get current data block information @@ -306,7 +316,7 @@ int32_t tsdbQuerySTableByTagCond(STsdbRepo *tsdb, uint64_t uid, TSKEY key, const SColIndex *pColIndex, int32_t numOfCols); /** - * destory the created table group list, which is generated by tag query + * destroy the created table group list, which is generated by tag query * @param pGroupList */ void tsdbDestroyTableGroup(STableGroupInfo *pGroupList); @@ -336,6 +346,12 @@ int32_t tsdbGetTableGroupFromIdList(STsdbRepo *tsdb, SArray *pTableIdList, STabl */ void tsdbCleanupQueryHandle(TsdbQueryHandleT queryHandle); +void tsdbResetQueryHandle(TsdbQueryHandleT queryHandle, STsdbQueryCond *pCond); + +void tsdbResetQueryHandleForNewTable(TsdbQueryHandleT queryHandle, STsdbQueryCond *pCond, STableGroupInfo* groupList); + +int32_t tsdbGetFileBlocksDistInfo(TsdbQueryHandleT* queryHandle, STableBlockDist* pTableBlockInfo); + /** * get the statistics of repo usage * @param repo. point to the tsdbrepo diff --git a/src/inc/ttokendef.h b/src/inc/ttokendef.h index 5ee0ec376a292360d473e81f56ff1ed487a5e10e..31e38c1b50abd0013b7aa326b5f71cd884959b16 100644 --- a/src/inc/ttokendef.h +++ b/src/inc/ttokendef.h @@ -135,108 +135,75 @@ #define TK_FROM 116 #define TK_VARIABLE 117 #define TK_INTERVAL 118 -#define TK_FILL 119 -#define TK_SLIDING 120 -#define TK_ORDER 121 -#define TK_BY 122 -#define TK_ASC 123 -#define TK_DESC 124 -#define TK_GROUP 125 -#define TK_HAVING 126 -#define TK_LIMIT 127 -#define TK_OFFSET 128 -#define TK_SLIMIT 129 -#define TK_SOFFSET 130 -#define TK_WHERE 131 -#define TK_NOW 132 -#define TK_RESET 133 -#define TK_QUERY 134 -#define TK_ADD 135 -#define TK_COLUMN 136 -#define TK_TAG 137 -#define TK_CHANGE 138 -#define TK_SET 139 -#define TK_KILL 140 -#define TK_CONNECTION 141 -#define TK_STREAM 142 -#define TK_COLON 143 -#define TK_ABORT 144 -#define TK_AFTER 145 -#define TK_ATTACH 146 -#define TK_BEFORE 147 -#define TK_BEGIN 148 -#define TK_CASCADE 149 -#define TK_CLUSTER 150 -#define TK_CONFLICT 151 -#define TK_COPY 152 -#define TK_DEFERRED 153 -#define TK_DELIMITERS 154 -#define TK_DETACH 155 -#define TK_EACH 156 -#define TK_END 157 -#define TK_EXPLAIN 158 -#define TK_FAIL 159 -#define TK_FOR 160 -#define TK_IGNORE 161 -#define TK_IMMEDIATE 162 -#define TK_INITIALLY 163 -#define TK_INSTEAD 164 -#define TK_MATCH 165 -#define TK_KEY 166 -#define TK_OF 167 -#define TK_RAISE 168 -#define TK_REPLACE 169 -#define TK_RESTRICT 170 -#define TK_ROW 171 -#define TK_STATEMENT 172 -#define TK_TRIGGER 173 -#define TK_VIEW 174 -#define TK_COUNT 175 -#define TK_SUM 176 -#define TK_AVG 177 -#define TK_MIN 178 -#define TK_MAX 179 -#define TK_FIRST 180 -#define TK_LAST 181 -#define TK_TOP 182 -#define TK_BOTTOM 183 -#define TK_STDDEV 184 -#define TK_PERCENTILE 185 -#define TK_APERCENTILE 186 -#define TK_LEASTSQUARES 187 -#define TK_HISTOGRAM 188 -#define TK_DIFF 189 -#define TK_SPREAD 190 -#define TK_TWA 191 -#define TK_INTERP 192 -#define TK_LAST_ROW 193 -#define TK_RATE 194 -#define TK_IRATE 195 -#define TK_SUM_RATE 196 -#define TK_SUM_IRATE 197 -#define TK_AVG_RATE 198 -#define TK_AVG_IRATE 199 -#define TK_TBID 200 -#define TK_SEMI 201 -#define TK_NONE 202 -#define TK_PREV 203 -#define TK_LINEAR 204 -#define TK_IMPORT 205 -#define TK_METRIC 206 -#define TK_TBNAME 207 -#define TK_JOIN 208 -#define TK_METRICS 209 -#define TK_INSERT 210 -#define TK_INTO 211 -#define TK_VALUES 212 - - - - - - - - +#define TK_SESSION 119 +#define TK_FILL 120 +#define TK_SLIDING 121 +#define TK_ORDER 122 +#define TK_BY 123 +#define TK_ASC 124 +#define TK_DESC 125 +#define TK_GROUP 126 +#define TK_HAVING 127 +#define TK_LIMIT 128 +#define TK_OFFSET 129 +#define TK_SLIMIT 130 +#define TK_SOFFSET 131 +#define TK_WHERE 132 +#define TK_NOW 133 +#define TK_RESET 134 +#define TK_QUERY 135 +#define TK_ADD 136 +#define TK_COLUMN 137 +#define TK_TAG 138 +#define TK_CHANGE 139 +#define TK_SET 140 +#define TK_KILL 141 +#define TK_CONNECTION 142 +#define TK_STREAM 143 +#define TK_COLON 144 +#define TK_ABORT 145 +#define TK_AFTER 146 +#define TK_ATTACH 147 +#define TK_BEFORE 148 +#define TK_BEGIN 149 +#define TK_CASCADE 150 +#define TK_CLUSTER 151 +#define TK_CONFLICT 152 +#define TK_COPY 153 +#define TK_DEFERRED 154 +#define TK_DELIMITERS 155 +#define TK_DETACH 156 +#define TK_EACH 157 +#define TK_END 158 +#define TK_EXPLAIN 159 +#define TK_FAIL 160 +#define TK_FOR 161 +#define TK_IGNORE 162 +#define TK_IMMEDIATE 163 +#define TK_INITIALLY 164 +#define TK_INSTEAD 165 +#define TK_MATCH 166 +#define TK_KEY 167 +#define TK_OF 168 +#define TK_RAISE 169 +#define TK_REPLACE 170 +#define TK_RESTRICT 171 +#define TK_ROW 172 +#define TK_STATEMENT 173 +#define TK_TRIGGER 174 +#define TK_VIEW 175 +#define TK_SEMI 176 +#define TK_NONE 177 +#define TK_PREV 178 +#define TK_LINEAR 179 +#define TK_IMPORT 180 +#define TK_METRIC 181 +#define TK_TBNAME 182 +#define TK_JOIN 183 +#define TK_METRICS 184 +#define TK_INSERT 185 +#define TK_INTO 186 +#define TK_VALUES 187 #define TK_SPACE 300 diff --git a/src/inc/ttype.h b/src/inc/ttype.h index 2f6b80f89d024e3d286ae20b41f645a073ddc174..662a23bfdbc52e432d2bcfbdac5c43cfba5d60dc 100644 --- a/src/inc/ttype.h +++ b/src/inc/ttype.h @@ -171,10 +171,10 @@ extern tDataTypeDescriptor tDataTypes[15]; bool isValidDataType(int32_t type); -void setVardataNull(char* val, int32_t type); -void setNull(char *val, int32_t type, int32_t bytes); -void setNullN(char *val, int32_t type, int32_t bytes, int32_t numOfElems); -void* getNullValue(int32_t type); +void setVardataNull(char* val, int32_t type); +void setNull(char *val, int32_t type, int32_t bytes); +void setNullN(char *val, int32_t type, int32_t bytes, int32_t numOfElems); +void *getNullValue(int32_t type); void assignVal(char *val, const char *src, int32_t len, int32_t type); void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size, void* buf); diff --git a/src/kit/taosdemo/insert.json b/src/kit/taosdemo/insert.json index 5276b9fb61af9645578392a0035c8a95898c75e2..208b5fcb746aed052cdb8abefe64f5dadaa7d2d3 100644 --- a/src/kit/taosdemo/insert.json +++ b/src/kit/taosdemo/insert.json @@ -1,57 +1,58 @@ { - "filetype": "insert", - "cfgdir": "/etc/taos", - "host": "127.0.0.1", - "port": 6030, - "user": "root", - "password": "taosdata", - "thread_count": 4, - "thread_count_create_tbl": 4, - "result_file": "./insert_res.txt", + "filetype": "insert", + "cfgdir": "/etc/taos", + "host": "127.0.0.1", + "port": 6030, + "user": "root", + "password": "taosdata", + "thread_count": 4, + "thread_count_create_tbl": 4, + "result_file": "./insert_res.txt", "confirm_parameter_prompt": "no", "insert_interval": 0, "num_of_records_per_req": 100, - "databases": [{ - "dbinfo": { - "name": "db", - "drop": "yes", - "replica": 1, - "days": 10, - "cache": 16, - "blocks": 8, - "precision": "ms", - "keep": 365, - "minRows": 100, - "maxRows": 4096, - "comp":2, - "walLevel":1, - "cachelast":0, - "quorum":1, - "fsync":3000, - "update": 0 - }, - "super_tables": [{ - "name": "stb", + "max_sql_len": 1024000, + "databases": [{ + "dbinfo": { + "name": "db", + "drop": "yes", + "replica": 1, + "days": 10, + "cache": 16, + "blocks": 8, + "precision": "ms", + "keep": 365, + "minRows": 100, + "maxRows": 4096, + "comp":2, + "walLevel":1, + "cachelast":0, + "quorum":1, + "fsync":3000, + "update": 0 + }, + "super_tables": [{ + "name": "stb", "child_table_exists":"no", - "childtable_count": 100, - "childtable_prefix": "stb_", - "auto_create_table": "no", - "data_source": "rand", - "insert_mode": "taosc", - "insert_rows": 100000, - "multi_thread_write_one_tbl": "no", - "number_of_tbl_in_one_sql": 0, - "rows_per_tbl": 100, - "max_sql_len": 1024000, - "disorder_ratio": 0, - "disorder_range": 1000, - "timestamp_step": 1, - "start_timestamp": "2020-10-01 00:00:00.000", - "sample_format": "csv", - "sample_file": "./sample.csv", - "tags_file": "", - "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":10}, {"type": "BINARY", "len": 16, "count":3}, {"type": "BINARY", "len": 32, "count":6}], - "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":5}] - }] - }] + "childtable_count": 10000, + "childtable_prefix": "stb_", + "auto_create_table": "no", + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 100000, + "multi_thread_write_one_tbl": "no", + "number_of_tbl_in_one_sql": 0, + "rows_per_tbl": 100, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":10}, {"type": "BINARY", "len": 16, "count":3}, {"type": "BINARY", "len": 32, "count":6}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":5}] + }] + }] } diff --git a/src/kit/taosdemo/taosdemo.c b/src/kit/taosdemo/taosdemo.c index 855976789542e3f4aa59fb87b0b40c1bb419ba3b..ce88ba5befd2bf6641c102fa32a245a58b796fed 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -22,7 +22,6 @@ #define CURL_STATICLIB #ifdef LINUX - #include "os.h" #include #include #include @@ -33,7 +32,6 @@ #include #include #include - #include #include #include #include @@ -44,18 +42,12 @@ #include #include #include - #include "os.h" - -#ifdef WINDOWS - #include - typedef unsigned __int32 uint32_t; - - #pragma comment ( lib, "ws2_32.lib" ) -#endif #endif +#include #include "cJSON.h" +#include "os.h" #include "taos.h" #include "taoserror.h" #include "tutil.h" @@ -98,6 +90,8 @@ extern char configDir[]; #define MAX_DATABASE_COUNT 256 #define INPUT_BUF_LEN 256 +#define DEFAULT_TIMESTAMP_STEP 10 + typedef enum CREATE_SUB_TALBE_MOD_EN { PRE_CREATE_SUBTBL, AUTO_CREATE_SUBTBL, @@ -196,6 +190,7 @@ typedef struct SArguments_S { int num_of_threads; int insert_interval; int num_of_RPR; + int max_sql_len; int num_of_tables; int num_of_DPT; int abort; @@ -222,6 +217,8 @@ typedef struct SSuperTable_S { char childTblPrefix[MAX_TB_NAME_SIZE]; char dataSource[MAX_TB_NAME_SIZE+1]; // rand_gen or sample char insertMode[MAX_TB_NAME_SIZE]; // taosc, restful + int childTblLimit; + int childTblOffset; int multiThreadWriteOneTbl; // 0: no, 1: yes int numberOfTblInOneSql; // 0/1: one table, > 1: number of tbl @@ -259,7 +256,7 @@ typedef struct SSuperTable_S { int tagUsePos; // statistics - int64_t totalRowsInserted; + int64_t totalInsertRows; int64_t totalAffectedRows; } SSuperTable; @@ -329,7 +326,7 @@ typedef struct SDbs_S { SDataBase db[MAX_DB_COUNT]; // statistics - int64_t totalRowsInserted; + int64_t totalInsertRows; int64_t totalAffectedRows; } SDbs; @@ -400,7 +397,7 @@ typedef struct SThreadInfo_S { int64_t lastTs; // statistics - int64_t totalRowsInserted; + int64_t totalInsertRows; int64_t totalAffectedRows; // insert delay statistics @@ -413,11 +410,18 @@ typedef struct SThreadInfo_S { } threadInfo; #ifdef WINDOWS +#define _CRT_RAND_S + #include +#include + +typedef unsigned __int32 uint32_t; + +#pragma comment ( lib, "ws2_32.lib" ) // Some old MinGW/CYGWIN distributions don't define this: #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING -#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 -#endif + #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 +#endif // ENABLE_VIRTUAL_TERMINAL_PROCESSING static HANDLE g_stdoutHandle; static DWORD g_consoleMode; @@ -453,6 +457,14 @@ void resetAfterAnsiEscape(void) { exit(GetLastError()); } } + +int taosRandom() +{ + int number; + rand_s(&number); + + return number; +} #else void setupForAnsiEscape(void) {} @@ -460,6 +472,12 @@ void resetAfterAnsiEscape(void) { // Reset colors printf("\x1b[0m"); } + +int taosRandom() +{ + return random(); +} + #endif static int createDatabases(); @@ -514,6 +532,7 @@ SArguments g_args = { 10, // num_of_connections/thread 0, // insert_interval 100, // num_of_RPR + TSDB_PAYLOAD_SIZE, // max_sql_len 10000, // num_of_tables 10000, // num_of_DPT 0, // abort @@ -761,6 +780,8 @@ void parse_args(int argc, char *argv[], SArguments *arguments) { } printf("# Insertion interval: %d\n", arguments->insert_interval); printf("# Number of records per req: %d\n", arguments->num_of_RPR); + printf("# Max SQL length: %d\n", arguments->max_sql_len); + printf("# Length of Binary: %d\n", arguments->len_of_binary); printf("# Number of Threads: %d\n", arguments->num_of_threads); printf("# Number of Tables: %d\n", arguments->num_of_tables); printf("# Number of Data per Table: %d\n", arguments->num_of_DPT); @@ -774,6 +795,7 @@ void parse_args(int argc, char *argv[], SArguments *arguments) { printf("# Delete method: %d\n", arguments->method_of_delete); printf("# Answer yes when prompt: %d\n", arguments->answer_yes); printf("# Print debug info: %d\n", arguments->debug_print); + printf("# Print verbose info: %d\n", arguments->verbose_print); printf("###################################################################\n"); if (!arguments->answer_yes) { printf("Press enter key to continue\n\n"); @@ -968,10 +990,10 @@ static double rand_double() { static void init_rand_data() { for (int i = 0; i < MAX_PREPARED_RAND; i++){ - randint[i] = (int)(rand() % 65535); - randbigint[i] = (int64_t)(rand() % 2147483648); - randfloat[i] = (float)(rand() / 1000.0); - randdouble[i] = (double)(rand() / 1000000.0); + randint[i] = (int)(taosRandom() % 65535); + randbigint[i] = (int64_t)(taosRandom() % 2147483648); + randfloat[i] = (float)(taosRandom() / 1000.0); + randdouble[i] = (double)(taosRandom() / 1000000.0); } } @@ -1006,6 +1028,7 @@ static int printfInsertMeta() { printf("thread num of create table: \033[33m%d\033[0m\n", g_Dbs.threadCountByCreateTbl); printf("insert interval: \033[33m%d\033[0m\n", g_args.insert_interval); printf("number of records per req: \033[33m%d\033[0m\n", g_args.num_of_RPR); + printf("max sql length: \033[33m%d\033[0m\n", g_args.max_sql_len); printf("database count: \033[33m%d\033[0m\n", g_Dbs.dbCount); for (int i = 0; i < g_Dbs.dbCount; i++) { @@ -1085,37 +1108,55 @@ static int printfInsertMeta() { } else { printf(" childTblExists: \033[33m%s\033[0m\n", "error"); } - - printf(" childTblCount: \033[33m%d\033[0m\n", g_Dbs.db[i].superTbls[j].childTblCount); - printf(" childTblPrefix: \033[33m%s\033[0m\n", g_Dbs.db[i].superTbls[j].childTblPrefix); - printf(" dataSource: \033[33m%s\033[0m\n", g_Dbs.db[i].superTbls[j].dataSource); - printf(" insertMode: \033[33m%s\033[0m\n", g_Dbs.db[i].superTbls[j].insertMode); - printf(" insertRows: \033[33m%"PRId64"\033[0m\n", g_Dbs.db[i].superTbls[j].insertRows); + + printf(" childTblCount: \033[33m%d\033[0m\n", g_Dbs.db[i].superTbls[j].childTblCount); + printf(" childTblPrefix: \033[33m%s\033[0m\n", g_Dbs.db[i].superTbls[j].childTblPrefix); + printf(" dataSource: \033[33m%s\033[0m\n", g_Dbs.db[i].superTbls[j].dataSource); + printf(" insertMode: \033[33m%s\033[0m\n", g_Dbs.db[i].superTbls[j].insertMode); + if (g_Dbs.db[i].superTbls[j].childTblLimit > 0) { + printf(" childTblLimit: \033[33m%d\033[0m\n", g_Dbs.db[i].superTbls[j].childTblLimit); + } + if (g_Dbs.db[i].superTbls[j].childTblOffset >= 0) { + printf(" childTblOffset: \033[33m%d\033[0m\n", g_Dbs.db[i].superTbls[j].childTblOffset); + } + printf(" insertRows: \033[33m%"PRId64"\033[0m\n", g_Dbs.db[i].superTbls[j].insertRows); if (0 == g_Dbs.db[i].superTbls[j].multiThreadWriteOneTbl) { - printf(" multiThreadWriteOneTbl: \033[33mno\033[0m\n"); + printf(" multiThreadWriteOneTbl: \033[33mno\033[0m\n"); }else { - printf(" multiThreadWriteOneTbl: \033[33myes\033[0m\n"); + printf(" multiThreadWriteOneTbl: \033[33myes\033[0m\n"); } - printf(" numberOfTblInOneSql: \033[33m%d\033[0m\n", g_Dbs.db[i].superTbls[j].numberOfTblInOneSql); - printf(" rowsPerTbl: \033[33m%d\033[0m\n", g_Dbs.db[i].superTbls[j].rowsPerTbl); - printf(" disorderRange: \033[33m%d\033[0m\n", g_Dbs.db[i].superTbls[j].disorderRange); - printf(" disorderRatio: \033[33m%d\033[0m\n", g_Dbs.db[i].superTbls[j].disorderRatio); - printf(" maxSqlLen: \033[33m%d\033[0m\n", g_Dbs.db[i].superTbls[j].maxSqlLen); - - printf(" timeStampStep: \033[33m%d\033[0m\n", g_Dbs.db[i].superTbls[j].timeStampStep); - printf(" startTimestamp: \033[33m%s\033[0m\n", g_Dbs.db[i].superTbls[j].startTimestamp); - printf(" sampleFormat: \033[33m%s\033[0m\n", g_Dbs.db[i].superTbls[j].sampleFormat); - printf(" sampleFile: \033[33m%s\033[0m\n", g_Dbs.db[i].superTbls[j].sampleFile); - printf(" tagsFile: \033[33m%s\033[0m\n", g_Dbs.db[i].superTbls[j].tagsFile); - - printf(" columnCount: \033[33m%d\033[0m\n ", g_Dbs.db[i].superTbls[j].columnCount); + printf(" numberOfTblInOneSql: \033[33m%d\033[0m\n", + g_Dbs.db[i].superTbls[j].numberOfTblInOneSql); + printf(" rowsPerTbl: \033[33m%d\033[0m\n", + g_Dbs.db[i].superTbls[j].rowsPerTbl); + printf(" disorderRange: \033[33m%d\033[0m\n", + g_Dbs.db[i].superTbls[j].disorderRange); + printf(" disorderRatio: \033[33m%d\033[0m\n", + g_Dbs.db[i].superTbls[j].disorderRatio); + printf(" maxSqlLen: \033[33m%d\033[0m\n", + g_Dbs.db[i].superTbls[j].maxSqlLen); + printf(" timeStampStep: \033[33m%d\033[0m\n", + g_Dbs.db[i].superTbls[j].timeStampStep); + printf(" startTimestamp: \033[33m%s\033[0m\n", + g_Dbs.db[i].superTbls[j].startTimestamp); + printf(" sampleFormat: \033[33m%s\033[0m\n", + g_Dbs.db[i].superTbls[j].sampleFormat); + printf(" sampleFile: \033[33m%s\033[0m\n", + g_Dbs.db[i].superTbls[j].sampleFile); + printf(" tagsFile: \033[33m%s\033[0m\n", + g_Dbs.db[i].superTbls[j].tagsFile); + printf(" columnCount: \033[33m%d\033[0m\n ", + g_Dbs.db[i].superTbls[j].columnCount); for (int k = 0; k < g_Dbs.db[i].superTbls[j].columnCount; k++) { //printf("dataType:%s, dataLen:%d\t", g_Dbs.db[i].superTbls[j].columns[k].dataType, g_Dbs.db[i].superTbls[j].columns[k].dataLen); - if ((0 == strncasecmp(g_Dbs.db[i].superTbls[j].columns[k].dataType, "binary", 6)) - || (0 == strncasecmp(g_Dbs.db[i].superTbls[j].columns[k].dataType, "nchar", 5))) { + if ((0 == strncasecmp(g_Dbs.db[i].superTbls[j].columns[k].dataType, + "binary", 6)) + || (0 == strncasecmp(g_Dbs.db[i].superTbls[j].columns[k].dataType, + "nchar", 5))) { printf("column[\033[33m%d\033[0m]:\033[33m%s(%d)\033[0m ", k, - g_Dbs.db[i].superTbls[j].columns[k].dataType, g_Dbs.db[i].superTbls[j].columns[k].dataLen); + g_Dbs.db[i].superTbls[j].columns[k].dataType, + g_Dbs.db[i].superTbls[j].columns[k].dataLen); } else { printf("column[%d]:\033[33m%s\033[0m ", k, g_Dbs.db[i].superTbls[j].columns[k].dataType); @@ -1130,11 +1171,12 @@ static int printfInsertMeta() { if ((0 == strncasecmp(g_Dbs.db[i].superTbls[j].tags[k].dataType, "binary", 6)) || (0 == strncasecmp(g_Dbs.db[i].superTbls[j].tags[k].dataType, "nchar", 5))) { printf("tag[%d]:\033[33m%s(%d)\033[0m ", k, - g_Dbs.db[i].superTbls[j].tags[k].dataType, g_Dbs.db[i].superTbls[j].tags[k].dataLen); + g_Dbs.db[i].superTbls[j].tags[k].dataType, + g_Dbs.db[i].superTbls[j].tags[k].dataLen); } else { printf("tag[%d]:\033[33m%s\033[0m ", k, g_Dbs.db[i].superTbls[j].tags[k].dataType); - } + } } printf("\n"); } @@ -1768,21 +1810,21 @@ int postProceSql(char* host, uint16_t port, char* sqlstr) return 0; } - -char* getTagValueFromTagSample( SSuperTable* stbInfo, int tagUsePos) { +static char* getTagValueFromTagSample(SSuperTable* stbInfo, int tagUsePos) { char* dataBuf = (char*)calloc(TSDB_MAX_SQL_LEN+1, 1); if (NULL == dataBuf) { printf("calloc failed! size:%d\n", TSDB_MAX_SQL_LEN+1); return NULL; } - + int dataLen = 0; - dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, "(%s)", stbInfo->tagDataBuf + stbInfo->lenOfTagOfOneRow * tagUsePos); - + dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, + "(%s)", stbInfo->tagDataBuf + stbInfo->lenOfTagOfOneRow * tagUsePos); + return dataBuf; } -char* generateTagVaulesForStb(SSuperTable* stbInfo) { +static char* generateTagVaulesForStb(SSuperTable* stbInfo) { char* dataBuf = (char*)calloc(TSDB_MAX_SQL_LEN+1, 1); if (NULL == dataBuf) { printf("calloc failed! size:%d\n", TSDB_MAX_SQL_LEN+1); @@ -1792,13 +1834,15 @@ char* generateTagVaulesForStb(SSuperTable* stbInfo) { int dataLen = 0; dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, "("); for (int i = 0; i < stbInfo->tagCount; i++) { - if ((0 == strncasecmp(stbInfo->tags[i].dataType, "binary", 6)) || (0 == strncasecmp(stbInfo->tags[i].dataType, "nchar", 5))) { + if ((0 == strncasecmp(stbInfo->tags[i].dataType, "binary", strlen("binary"))) + || (0 == strncasecmp(stbInfo->tags[i].dataType, "nchar", strlen("nchar")))) { if (stbInfo->tags[i].dataLen > TSDB_MAX_BINARY_LEN) { - printf("binary or nchar length overflow, max size:%u\n", (uint32_t)TSDB_MAX_BINARY_LEN); + printf("binary or nchar length overflow, max size:%u\n", + (uint32_t)TSDB_MAX_BINARY_LEN); tmfree(dataBuf); return NULL; } - + char* buf = (char*)calloc(stbInfo->tags[i].dataLen+1, 1); if (NULL == buf) { printf("calloc failed! size:%d\n", stbInfo->tags[i].dataLen); @@ -1806,30 +1850,48 @@ char* generateTagVaulesForStb(SSuperTable* stbInfo) { return NULL; } rand_string(buf, stbInfo->tags[i].dataLen); - dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, "\'%s\', ", buf); + dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, + "\'%s\', ", buf); tmfree(buf); - } else if (0 == strncasecmp(stbInfo->tags[i].dataType, "int", 3)) { - dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, "%d, ", rand_int()); - } else if (0 == strncasecmp(stbInfo->tags[i].dataType, "bigint", 6)) { - dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, "%"PRId64", ", rand_bigint()); - } else if (0 == strncasecmp(stbInfo->tags[i].dataType, "float", 5)) { - dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, "%f, ", rand_float()); - } else if (0 == strncasecmp(stbInfo->tags[i].dataType, "double", 6)) { - dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, "%f, ", rand_double()); - } else if (0 == strncasecmp(stbInfo->tags[i].dataType, "smallint", 8)) { - dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, "%d, ", rand_smallint()); - } else if (0 == strncasecmp(stbInfo->tags[i].dataType, "tinyint", 7)) { - dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, "%d, ", rand_tinyint()); - } else if (0 == strncasecmp(stbInfo->tags[i].dataType, "bool", 4)) { - dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, "%d, ", rand_bool()); - } else if (0 == strncasecmp(stbInfo->tags[i].dataType, "timestamp", 4)) { - dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, "%"PRId64", ", rand_bigint()); + } else if (0 == strncasecmp(stbInfo->tags[i].dataType, + "int", strlen("int"))) { + dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, + "%d, ", rand_int()); + } else if (0 == strncasecmp(stbInfo->tags[i].dataType, + "bigint", strlen("bigint"))) { + dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, + "%"PRId64", ", rand_bigint()); + } else if (0 == strncasecmp(stbInfo->tags[i].dataType, + "float", strlen("float"))) { + dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, + "%f, ", rand_float()); + } else if (0 == strncasecmp(stbInfo->tags[i].dataType, + "double", strlen("double"))) { + dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, + "%f, ", rand_double()); + } else if (0 == strncasecmp(stbInfo->tags[i].dataType, + "smallint", strlen("smallint"))) { + dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, + "%d, ", rand_smallint()); + } else if (0 == strncasecmp(stbInfo->tags[i].dataType, + "tinyint", strlen("tinyint"))) { + dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, + "%d, ", rand_tinyint()); + } else if (0 == strncasecmp(stbInfo->tags[i].dataType, + "bool", strlen("bool"))) { + dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, + "%d, ", rand_bool()); + } else if (0 == strncasecmp(stbInfo->tags[i].dataType, + "timestamp", strlen("timestamp"))) { + dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, + "%"PRId64", ", rand_bigint()); } else { printf("No support data type: %s\n", stbInfo->tags[i].dataType); tmfree(dataBuf); return NULL; } } + dataLen -= 2; dataLen += snprintf(dataBuf + dataLen, TSDB_MAX_SQL_LEN - dataLen, ")"); return dataBuf; @@ -1874,7 +1936,7 @@ static int calcRowLen(SSuperTable* superTbls) { int lenOfTagOfOneRow = 0; for (tagIndex = 0; tagIndex < superTbls->tagCount; tagIndex++) { char* dataType = superTbls->tags[tagIndex].dataType; - + if (strcasecmp(dataType, "BINARY") == 0) { lenOfTagOfOneRow += superTbls->tags[tagIndex].dataLen + 3; } else if (strcasecmp(dataType, "NCHAR") == 0) { @@ -1905,15 +1967,25 @@ static int calcRowLen(SSuperTable* superTbls) { } -static int getAllChildNameOfSuperTable(TAOS * taos, char* dbName, char* sTblName, char** childTblNameOfSuperTbl, int* childTblCountOfSuperTbl) { +static int getChildNameOfSuperTableWithLimitAndOffset(TAOS * taos, + char* dbName, char* sTblName, char** childTblNameOfSuperTbl, + int* childTblCountOfSuperTbl, int limit, int offset) { + char command[BUFFER_SIZE] = "\0"; + char limitBuf[100] = "\0"; + TAOS_RES * res; TAOS_ROW row = NULL; char* childTblName = *childTblNameOfSuperTbl; - + + if (offset >= 0) { + snprintf(limitBuf, 100, " limit %d offset %d", limit, offset); + } + //get all child table name use cmd: select tbname from superTblName; - snprintf(command, BUFFER_SIZE, "select tbname from %s.%s", dbName, sTblName); + snprintf(command, BUFFER_SIZE, "select tbname from %s.%s %s", dbName, sTblName, limitBuf); + res = taos_query(taos, command); int32_t code = taos_errno(res); if (code != 0) { @@ -1923,9 +1995,9 @@ static int getAllChildNameOfSuperTable(TAOS * taos, char* dbName, char* sTblName exit(-1); } - int childTblCount = 10000; + int childTblCount = (limit < 0)?10000:limit; int count = 0; - childTblName = (char*)calloc(1, childTblCount * TSDB_TABLE_NAME_LEN); +// childTblName = (char*)calloc(1, childTblCount * TSDB_TABLE_NAME_LEN); char* pTblName = childTblName; while ((row = taos_fetch_row(res)) != NULL) { int32_t* len = taos_fetch_lengths(res); @@ -1937,7 +2009,8 @@ static int getAllChildNameOfSuperTable(TAOS * taos, char* dbName, char* sTblName if (tmp != NULL) { childTblName = tmp; childTblCount = (int)(childTblCount*1.5); - memset(childTblName + count*TSDB_TABLE_NAME_LEN, 0, (size_t)((childTblCount-count)*TSDB_TABLE_NAME_LEN)); + memset(childTblName + count*TSDB_TABLE_NAME_LEN, 0, + (size_t)((childTblCount-count)*TSDB_TABLE_NAME_LEN)); } else { // exit, if allocate more memory failed printf("realloc fail for save child table name of %s.%s\n", dbName, sTblName); @@ -1949,7 +2022,7 @@ static int getAllChildNameOfSuperTable(TAOS * taos, char* dbName, char* sTblName } pTblName = childTblName + count * TSDB_TABLE_NAME_LEN; } - + *childTblCountOfSuperTbl = count; *childTblNameOfSuperTbl = childTblName; @@ -1957,19 +2030,29 @@ static int getAllChildNameOfSuperTable(TAOS * taos, char* dbName, char* sTblName return 0; } -static int getSuperTableFromServer(TAOS * taos, char* dbName, SSuperTable* superTbls) { +static int getAllChildNameOfSuperTable(TAOS * taos, char* dbName, + char* sTblName, char** childTblNameOfSuperTbl, + int* childTblCountOfSuperTbl) { + + return getChildNameOfSuperTableWithLimitAndOffset(taos, dbName, sTblName, + childTblNameOfSuperTbl, childTblCountOfSuperTbl, + -1, -1); +} + +static int getSuperTableFromServer(TAOS * taos, char* dbName, + SSuperTable* superTbls) { char command[BUFFER_SIZE] = "\0"; TAOS_RES * res; TAOS_ROW row = NULL; int count = 0; - - //get schema use cmd: describe superTblName; + + //get schema use cmd: describe superTblName; snprintf(command, BUFFER_SIZE, "describe %s.%s", dbName, superTbls->sTblName); - res = taos_query(taos, command); + res = taos_query(taos, command); int32_t code = taos_errno(res); if (code != 0) { printf("failed to run command %s\n", command); - taos_free_result(res); + taos_free_result(res); return -1; } @@ -1980,19 +2063,33 @@ static int getSuperTableFromServer(TAOS * taos, char* dbName, SSuperTable* supe if (0 == count) { count++; continue; - } + } if (strcmp((char *)row[TSDB_DESCRIBE_METRIC_NOTE_INDEX], "TAG") == 0) { - tstrncpy(superTbls->tags[tagIndex].field, (char *)row[TSDB_DESCRIBE_METRIC_FIELD_INDEX], fields[TSDB_DESCRIBE_METRIC_FIELD_INDEX].bytes); - tstrncpy(superTbls->tags[tagIndex].dataType, (char *)row[TSDB_DESCRIBE_METRIC_TYPE_INDEX], fields[TSDB_DESCRIBE_METRIC_TYPE_INDEX].bytes); - superTbls->tags[tagIndex].dataLen = *((int *)row[TSDB_DESCRIBE_METRIC_LENGTH_INDEX]); - tstrncpy(superTbls->tags[tagIndex].note, (char *)row[TSDB_DESCRIBE_METRIC_NOTE_INDEX], fields[TSDB_DESCRIBE_METRIC_NOTE_INDEX].bytes); + tstrncpy(superTbls->tags[tagIndex].field, + (char *)row[TSDB_DESCRIBE_METRIC_FIELD_INDEX], + fields[TSDB_DESCRIBE_METRIC_FIELD_INDEX].bytes); + tstrncpy(superTbls->tags[tagIndex].dataType, + (char *)row[TSDB_DESCRIBE_METRIC_TYPE_INDEX], + fields[TSDB_DESCRIBE_METRIC_TYPE_INDEX].bytes); + superTbls->tags[tagIndex].dataLen = + *((int *)row[TSDB_DESCRIBE_METRIC_LENGTH_INDEX]); + tstrncpy(superTbls->tags[tagIndex].note, + (char *)row[TSDB_DESCRIBE_METRIC_NOTE_INDEX], + fields[TSDB_DESCRIBE_METRIC_NOTE_INDEX].bytes); tagIndex++; - } else { - tstrncpy(superTbls->columns[columnIndex].field, (char *)row[TSDB_DESCRIBE_METRIC_FIELD_INDEX], fields[TSDB_DESCRIBE_METRIC_FIELD_INDEX].bytes); - tstrncpy(superTbls->columns[columnIndex].dataType, (char *)row[TSDB_DESCRIBE_METRIC_TYPE_INDEX], fields[TSDB_DESCRIBE_METRIC_TYPE_INDEX].bytes); - superTbls->columns[columnIndex].dataLen = *((int *)row[TSDB_DESCRIBE_METRIC_LENGTH_INDEX]); - tstrncpy(superTbls->columns[columnIndex].note, (char *)row[TSDB_DESCRIBE_METRIC_NOTE_INDEX], fields[TSDB_DESCRIBE_METRIC_NOTE_INDEX].bytes); + } else { + tstrncpy(superTbls->columns[columnIndex].field, + (char *)row[TSDB_DESCRIBE_METRIC_FIELD_INDEX], + fields[TSDB_DESCRIBE_METRIC_FIELD_INDEX].bytes); + tstrncpy(superTbls->columns[columnIndex].dataType, + (char *)row[TSDB_DESCRIBE_METRIC_TYPE_INDEX], + fields[TSDB_DESCRIBE_METRIC_TYPE_INDEX].bytes); + superTbls->columns[columnIndex].dataLen = + *((int *)row[TSDB_DESCRIBE_METRIC_LENGTH_INDEX]); + tstrncpy(superTbls->columns[columnIndex].note, + (char *)row[TSDB_DESCRIBE_METRIC_NOTE_INDEX], + fields[TSDB_DESCRIBE_METRIC_NOTE_INDEX].bytes); columnIndex++; } count++; @@ -2006,14 +2103,23 @@ static int getSuperTableFromServer(TAOS * taos, char* dbName, SSuperTable* supe if (TBL_ALREADY_EXISTS == superTbls->childTblExists) { //get all child table name use cmd: select tbname from superTblName; - getAllChildNameOfSuperTable(taos, dbName, superTbls->sTblName, &superTbls->childTblName, &superTbls->childTblCount); + int childTblCount = 10000; + superTbls->childTblName = (char*)calloc(1, childTblCount * TSDB_TABLE_NAME_LEN); + if (superTbls->childTblName == NULL) { + fprintf(stderr, "alloc memory failed!"); + return -1; + } + getAllChildNameOfSuperTable(taos, dbName, + superTbls->sTblName, + &superTbls->childTblName, + &superTbls->childTblCount); } return 0; } -static int createSuperTable(TAOS * taos, char* dbName, SSuperTable* superTbls, bool use_metric) { +static int createSuperTable(TAOS * taos, char* dbName, SSuperTable* superTbls, bool use_metric) { char command[BUFFER_SIZE] = "\0"; - + char cols[STRING_LEN] = "\0"; int colIndex; int len = 0; @@ -2021,7 +2127,7 @@ static int createSuperTable(TAOS * taos, char* dbName, SSuperTable* superTbls, int lenOfOneRow = 0; for (colIndex = 0; colIndex < superTbls->columnCount; colIndex++) { char* dataType = superTbls->columns[colIndex].dataType; - + if (strcasecmp(dataType, "BINARY") == 0) { len += snprintf(cols + len, STRING_LEN - len, ", col%d %s(%d)", colIndex, "BINARY", @@ -2050,10 +2156,10 @@ static int createSuperTable(TAOS * taos, char* dbName, SSuperTable* superTbls, } else if (strcasecmp(dataType, "FLOAT") == 0) { len += snprintf(cols + len, STRING_LEN - len, ", col%d %s", colIndex, "FLOAT"); lenOfOneRow += 22; - } else if (strcasecmp(dataType, "DOUBLE") == 0) { + } else if (strcasecmp(dataType, "DOUBLE") == 0) { len += snprintf(cols + len, STRING_LEN - len, ", col%d %s", colIndex, "DOUBLE"); lenOfOneRow += 42; - } else if (strcasecmp(dataType, "TIMESTAMP") == 0) { + } else if (strcasecmp(dataType, "TIMESTAMP") == 0) { len += snprintf(cols + len, STRING_LEN - len, ", col%d %s", colIndex, "TIMESTAMP"); lenOfOneRow += 21; } else { @@ -2085,33 +2191,42 @@ static int createSuperTable(TAOS * taos, char* dbName, SSuperTable* superTbls, len += snprintf(tags + len, STRING_LEN - len, "("); for (tagIndex = 0; tagIndex < superTbls->tagCount; tagIndex++) { char* dataType = superTbls->tags[tagIndex].dataType; - + if (strcasecmp(dataType, "BINARY") == 0) { - len += snprintf(tags + len, STRING_LEN - len, "t%d %s(%d), ", tagIndex, "BINARY", superTbls->tags[tagIndex].dataLen); + len += snprintf(tags + len, STRING_LEN - len, "t%d %s(%d), ", tagIndex, + "BINARY", superTbls->tags[tagIndex].dataLen); lenOfTagOfOneRow += superTbls->tags[tagIndex].dataLen + 3; } else if (strcasecmp(dataType, "NCHAR") == 0) { - len += snprintf(tags + len, STRING_LEN - len, "t%d %s(%d), ", tagIndex, "NCHAR", superTbls->tags[tagIndex].dataLen); + len += snprintf(tags + len, STRING_LEN - len, "t%d %s(%d), ", tagIndex, + "NCHAR", superTbls->tags[tagIndex].dataLen); lenOfTagOfOneRow += superTbls->tags[tagIndex].dataLen + 3; } else if (strcasecmp(dataType, "INT") == 0) { - len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, "INT"); + len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, + "INT"); lenOfTagOfOneRow += superTbls->tags[tagIndex].dataLen + 11; } else if (strcasecmp(dataType, "BIGINT") == 0) { - len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, "BIGINT"); + len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, + "BIGINT"); lenOfTagOfOneRow += superTbls->tags[tagIndex].dataLen + 21; } else if (strcasecmp(dataType, "SMALLINT") == 0) { - len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, "SMALLINT"); + len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, + "SMALLINT"); lenOfTagOfOneRow += superTbls->tags[tagIndex].dataLen + 6; } else if (strcasecmp(dataType, "TINYINT") == 0) { - len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, "TINYINT"); + len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, + "TINYINT"); lenOfTagOfOneRow += superTbls->tags[tagIndex].dataLen + 4; } else if (strcasecmp(dataType, "BOOL") == 0) { - len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, "BOOL"); + len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, + "BOOL"); lenOfTagOfOneRow += superTbls->tags[tagIndex].dataLen + 6; } else if (strcasecmp(dataType, "FLOAT") == 0) { - len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, "FLOAT"); + len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, + "FLOAT"); lenOfTagOfOneRow += superTbls->tags[tagIndex].dataLen + 22; } else if (strcasecmp(dataType, "DOUBLE") == 0) { - len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, "DOUBLE"); + len += snprintf(tags + len, STRING_LEN - len, "t%d %s, ", tagIndex, + "DOUBLE"); lenOfTagOfOneRow += superTbls->tags[tagIndex].dataLen + 42; } else { taos_close(taos); @@ -2130,7 +2245,8 @@ static int createSuperTable(TAOS * taos, char* dbName, SSuperTable* superTbls, verbosePrint("%s() LN%d: %s\n", __func__, __LINE__, command); if (0 != queryDbExec(taos, command, NO_INSERT_TYPE)) { - fprintf(stderr, "create supertable %s failed!\n\n", superTbls->sTblName); + fprintf(stderr, "create supertable %s failed!\n\n", + superTbls->sTblName); return -1; } debugPrint("create supertable %s success!\n\n", superTbls->sTblName); @@ -2138,7 +2254,6 @@ static int createSuperTable(TAOS * taos, char* dbName, SSuperTable* superTbls, return 0; } - static int createDatabases() { TAOS * taos = NULL; int ret = 0; @@ -2161,31 +2276,35 @@ static int createDatabases() { int dataLen = 0; dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "create database if not exists %s ", g_Dbs.db[i].dbName); + BUFFER_SIZE - dataLen, "create database if not exists %s", g_Dbs.db[i].dbName); if (g_Dbs.db[i].dbCfg.blocks > 0) { dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "blocks %d ", g_Dbs.db[i].dbCfg.blocks); + BUFFER_SIZE - dataLen, " blocks %d", g_Dbs.db[i].dbCfg.blocks); } if (g_Dbs.db[i].dbCfg.cache > 0) { dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "cache %d ", g_Dbs.db[i].dbCfg.cache); + BUFFER_SIZE - dataLen, " cache %d", g_Dbs.db[i].dbCfg.cache); } if (g_Dbs.db[i].dbCfg.days > 0) { dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "days %d ", g_Dbs.db[i].dbCfg.days); + BUFFER_SIZE - dataLen, " days %d", g_Dbs.db[i].dbCfg.days); } if (g_Dbs.db[i].dbCfg.keep > 0) { dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "keep %d ", g_Dbs.db[i].dbCfg.keep); + BUFFER_SIZE - dataLen, " keep %d", g_Dbs.db[i].dbCfg.keep); + } + if (g_Dbs.db[i].dbCfg.quorum > 1) { + dataLen += snprintf(command + dataLen, + BUFFER_SIZE - dataLen, " quorum %d", g_Dbs.db[i].dbCfg.quorum); } if (g_Dbs.db[i].dbCfg.replica > 0) { dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "replica %d ", g_Dbs.db[i].dbCfg.replica); + BUFFER_SIZE - dataLen, " replica %d", g_Dbs.db[i].dbCfg.replica); } if (g_Dbs.db[i].dbCfg.update > 0) { dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "update %d ", g_Dbs.db[i].dbCfg.update); + BUFFER_SIZE - dataLen, " update %d", g_Dbs.db[i].dbCfg.update); } //if (g_Dbs.db[i].dbCfg.maxtablesPerVnode > 0) { // dataLen += snprintf(command + dataLen, @@ -2193,31 +2312,32 @@ static int createDatabases() { //} if (g_Dbs.db[i].dbCfg.minRows > 0) { dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "minrows %d ", g_Dbs.db[i].dbCfg.minRows); + BUFFER_SIZE - dataLen, " minrows %d", g_Dbs.db[i].dbCfg.minRows); } if (g_Dbs.db[i].dbCfg.maxRows > 0) { dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "maxrows %d ", g_Dbs.db[i].dbCfg.maxRows); + BUFFER_SIZE - dataLen, " maxrows %d", g_Dbs.db[i].dbCfg.maxRows); } if (g_Dbs.db[i].dbCfg.comp > 0) { dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "comp %d ", g_Dbs.db[i].dbCfg.comp); + BUFFER_SIZE - dataLen, " comp %d", g_Dbs.db[i].dbCfg.comp); } if (g_Dbs.db[i].dbCfg.walLevel > 0) { dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "wal %d ", g_Dbs.db[i].dbCfg.walLevel); + BUFFER_SIZE - dataLen, " wal %d", g_Dbs.db[i].dbCfg.walLevel); } if (g_Dbs.db[i].dbCfg.cacheLast > 0) { dataLen += snprintf(command + dataLen, - BUFFER_SIZE - dataLen, "cachelast %d ", g_Dbs.db[i].dbCfg.cacheLast); + BUFFER_SIZE - dataLen, " cachelast %d", g_Dbs.db[i].dbCfg.cacheLast); } if (g_Dbs.db[i].dbCfg.fsync > 0) { - dataLen += snprintf(command + dataLen, BUFFER_SIZE - dataLen, "fsync %d ", g_Dbs.db[i].dbCfg.fsync); + dataLen += snprintf(command + dataLen, BUFFER_SIZE - dataLen, + " fsync %d", g_Dbs.db[i].dbCfg.fsync); } - if ((0 == strncasecmp(g_Dbs.db[i].dbCfg.precision, "ms", 2)) - || (0 == strncasecmp(g_Dbs.db[i].dbCfg.precision, "us", 2))) { + if ((0 == strncasecmp(g_Dbs.db[i].dbCfg.precision, "ms", strlen("ms"))) + || (0 == strncasecmp(g_Dbs.db[i].dbCfg.precision, "us", strlen("us")))) { dataLen += snprintf(command + dataLen, BUFFER_SIZE - dataLen, - "precision \'%s\';", g_Dbs.db[i].dbCfg.precision); + " precision \'%s\';", g_Dbs.db[i].dbCfg.precision); } debugPrint("%s() %d command: %s\n", __func__, __LINE__, command); @@ -2347,7 +2467,7 @@ static void* createTable(void *sarg) } int startMultiThreadCreateChildTable( - char* cols, int threads, int ntables, + char* cols, int threads, int startFrom, int ntables, char* db_name, SSuperTable* superTblInfo) { pthread_t *pids = malloc(threads * sizeof(pthread_t)); threadInfo *infos = malloc(threads * sizeof(threadInfo)); @@ -2370,7 +2490,7 @@ int startMultiThreadCreateChildTable( int b = 0; b = ntables % threads; - int last = 0; + int last = startFrom; for (int i = 0; i < threads; i++) { threadInfo *t_info = infos + i; t_info->threadID = i; @@ -2418,7 +2538,7 @@ static void createChildTables() { char tblColsBuf[MAX_SQL_SIZE]; int len; - for (int i = 0; i < g_Dbs.dbCount; i++) { + for (int i = 0; i < g_Dbs.dbCount; i++) { if (g_Dbs.db[i].superTblCount > 0) { // with super table for (int j = 0; j < g_Dbs.db[i].superTblCount; j++) { @@ -2429,12 +2549,17 @@ static void createChildTables() { verbosePrint("%s() LN%d: %s\n", __func__, __LINE__, g_Dbs.db[i].superTbls[j].colsOfCreateChildTable); + int startFrom = 0; + g_totalChildTables += g_Dbs.db[i].superTbls[j].childTblCount; + + verbosePrint("%s() LN%d: create %d child tables from %d\n", __func__, __LINE__, + g_totalChildTables, startFrom); startMultiThreadCreateChildTable( g_Dbs.db[i].superTbls[j].colsOfCreateChildTable, g_Dbs.threadCountByCreateTbl, - g_Dbs.db[i].superTbls[j].childTblCount, + startFrom, + g_totalChildTables, g_Dbs.db[i].dbName, &(g_Dbs.db[i].superTbls[j])); - g_totalChildTables += g_Dbs.db[i].superTbls[j].childTblCount; } } else { // normal table @@ -2442,10 +2567,13 @@ static void createChildTables() { int j = 0; while (g_args.datatype[j]) { if ((strncasecmp(g_args.datatype[j], "BINARY", strlen("BINARY")) == 0) - || (strncasecmp(g_args.datatype[j], "NCHAR", strlen("NCHAR")) == 0)) { - len = snprintf(tblColsBuf + len, MAX_SQL_SIZE, ", COL%d %s(60)", j, g_args.datatype[j]); + || (strncasecmp(g_args.datatype[j], + "NCHAR", strlen("NCHAR")) == 0)) { + len = snprintf(tblColsBuf + len, MAX_SQL_SIZE, + ", COL%d %s(60)", j, g_args.datatype[j]); } else { - len = snprintf(tblColsBuf + len, MAX_SQL_SIZE, ", COL%d %s", j, g_args.datatype[j]); + len = snprintf(tblColsBuf + len, MAX_SQL_SIZE, + ", COL%d %s", j, g_args.datatype[j]); } len = strlen(tblColsBuf); j++; @@ -2453,11 +2581,13 @@ static void createChildTables() { len = snprintf(tblColsBuf + len, MAX_SQL_SIZE - len, ")"); - verbosePrint("%s() LN%d: dbName: %s num of tb: %d schema: %s\n", __func__, __LINE__, + verbosePrint("%s() LN%d: dbName: %s num of tb: %d schema: %s\n", + __func__, __LINE__, g_Dbs.db[i].dbName, g_args.num_of_tables, tblColsBuf); startMultiThreadCreateChildTable( tblColsBuf, g_Dbs.threadCountByCreateTbl, + 0, g_args.num_of_tables, g_Dbs.db[i].dbName, NULL); @@ -2465,30 +2595,6 @@ static void createChildTables() { } } -/* -static int taosGetLineNum(const char *fileName) -{ - int lineNum = 0; - char cmd[1024] = { 0 }; - char buf[1024] = { 0 }; - sprintf(cmd, "wc -l %s", fileName); - - FILE *fp = popen(cmd, "r"); - if (fp == NULL) { - fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno)); - return lineNum; - } - - if (fgets(buf, sizeof(buf), fp)) { - int index = strchr((const char*)buf, ' ') - buf; - buf[index] = '\0'; - lineNum = atoi(buf); - } - pclose(fp); - return lineNum; -} -*/ - /* Read 10000 lines at most. If more than 10000 lines, continue to read after using */ @@ -2566,19 +2672,30 @@ int readSampleFromJsonFileToMem(SSuperTable * superTblInfo) { /* Read 10000 lines at most. If more than 10000 lines, continue to read after using */ -int readSampleFromCsvFileToMem(FILE *fp, SSuperTable* superTblInfo, char* sampleBuf) { +static int readSampleFromCsvFileToMem( + SSuperTable* superTblInfo) { size_t n = 0; ssize_t readLen = 0; char * line = NULL; int getRows = 0; + + FILE* fp = fopen(superTblInfo->sampleFile, "r"); + if (fp == NULL) { + fprintf(stderr, "Failed to open sample file: %s, reason:%s\n", + superTblInfo->sampleFile, strerror(errno)); + return -1; + } - memset(sampleBuf, 0, MAX_SAMPLES_ONCE_FROM_FILE* superTblInfo->lenOfOneRow); + assert(superTblInfo->sampleDataBuf); + memset(superTblInfo->sampleDataBuf, 0, + MAX_SAMPLES_ONCE_FROM_FILE * superTblInfo->lenOfOneRow); while (1) { readLen = tgetline(&line, &n, fp); if (-1 == readLen) { if(0 != fseek(fp, 0, SEEK_SET)) { - printf("Failed to fseek file: %s, reason:%s\n", + fprintf(stderr, "Failed to fseek file: %s, reason:%s\n", superTblInfo->sampleFile, strerror(errno)); + fclose(fp); return -1; } continue; @@ -2598,7 +2715,8 @@ int readSampleFromCsvFileToMem(FILE *fp, SSuperTable* superTblInfo, char* sample continue; } - memcpy(sampleBuf + getRows * superTblInfo->lenOfOneRow, line, readLen); + memcpy(superTblInfo->sampleDataBuf + getRows * superTblInfo->lenOfOneRow, + line, readLen); getRows++; if (getRows == MAX_SAMPLES_ONCE_FROM_FILE) { @@ -2606,6 +2724,7 @@ int readSampleFromCsvFileToMem(FILE *fp, SSuperTable* superTblInfo, char* sample } } + fclose(fp); tmfree(line); return 0; } @@ -2630,7 +2749,7 @@ static bool getColumnAndTagTypeFromInsertJsonFile(cJSON* stbInfo, SSuperTable* s // columns cJSON *columns = cJSON_GetObjectItem(stbInfo, "columns"); if (columns && columns->type != cJSON_Array) { - printf("failed to read json, columns not found\n"); + printf("ERROR: failed to read json, columns not found\n"); goto PARSE_OVER; } else if (NULL == columns) { superTbls->columnCount = 0; @@ -2640,7 +2759,7 @@ static bool getColumnAndTagTypeFromInsertJsonFile(cJSON* stbInfo, SSuperTable* s int columnSize = cJSON_GetArraySize(columns); if (columnSize > MAX_COLUMN_COUNT) { - printf("failed to read json, column size overflow, max column size is %d\n", + printf("ERROR: failed to read json, column size overflow, max column size is %d\n", MAX_COLUMN_COUNT); goto PARSE_OVER; } @@ -2659,7 +2778,7 @@ static bool getColumnAndTagTypeFromInsertJsonFile(cJSON* stbInfo, SSuperTable* s if (countObj && countObj->type == cJSON_Number) { count = countObj->valueint; } else if (countObj && countObj->type != cJSON_Number) { - printf("failed to read json, column count not found"); + printf("ERROR: failed to read json, column count not found\n"); goto PARSE_OVER; } else { count = 1; @@ -2669,7 +2788,7 @@ static bool getColumnAndTagTypeFromInsertJsonFile(cJSON* stbInfo, SSuperTable* s memset(&columnCase, 0, sizeof(StrColumn)); cJSON *dataType = cJSON_GetObjectItem(column, "type"); if (!dataType || dataType->type != cJSON_String || dataType->valuestring == NULL) { - printf("failed to read json, column type not found"); + printf("ERROR: failed to read json, column type not found\n"); goto PARSE_OVER; } //tstrncpy(superTbls->columns[k].dataType, dataType->valuestring, MAX_TB_NAME_SIZE); @@ -2679,7 +2798,7 @@ static bool getColumnAndTagTypeFromInsertJsonFile(cJSON* stbInfo, SSuperTable* s if (dataLen && dataLen->type == cJSON_Number) { columnCase.dataLen = dataLen->valueint; } else if (dataLen && dataLen->type != cJSON_Number) { - printf("failed to read json, column len not found"); + printf("ERROR: failed to read json, column len not found\n"); goto PARSE_OVER; } else { columnCase.dataLen = 8; @@ -2698,13 +2817,13 @@ static bool getColumnAndTagTypeFromInsertJsonFile(cJSON* stbInfo, SSuperTable* s // tags cJSON *tags = cJSON_GetObjectItem(stbInfo, "tags"); if (!tags || tags->type != cJSON_Array) { - printf("failed to read json, tags not found"); + printf("ERROR: failed to read json, tags not found\n"); goto PARSE_OVER; } int tagSize = cJSON_GetArraySize(tags); if (tagSize > MAX_TAG_COUNT) { - printf("failed to read json, tags size overflow, max tag size is %d\n", MAX_TAG_COUNT); + printf("ERROR: failed to read json, tags size overflow, max tag size is %d\n", MAX_TAG_COUNT); goto PARSE_OVER; } @@ -2718,7 +2837,7 @@ static bool getColumnAndTagTypeFromInsertJsonFile(cJSON* stbInfo, SSuperTable* s if (countObj && countObj->type == cJSON_Number) { count = countObj->valueint; } else if (countObj && countObj->type != cJSON_Number) { - printf("failed to read json, column count not found"); + printf("ERROR: failed to read json, column count not found\n"); goto PARSE_OVER; } else { count = 1; @@ -2728,7 +2847,7 @@ static bool getColumnAndTagTypeFromInsertJsonFile(cJSON* stbInfo, SSuperTable* s memset(&columnCase, 0, sizeof(StrColumn)); cJSON *dataType = cJSON_GetObjectItem(tag, "type"); if (!dataType || dataType->type != cJSON_String || dataType->valuestring == NULL) { - printf("failed to read json, tag type not found"); + printf("ERROR: failed to read json, tag type not found\n"); goto PARSE_OVER; } tstrncpy(columnCase.dataType, dataType->valuestring, MAX_TB_NAME_SIZE); @@ -2737,7 +2856,7 @@ static bool getColumnAndTagTypeFromInsertJsonFile(cJSON* stbInfo, SSuperTable* s if (dataLen && dataLen->type == cJSON_Number) { columnCase.dataLen = dataLen->valueint; } else if (dataLen && dataLen->type != cJSON_Number) { - printf("failed to read json, column len not found"); + printf("ERROR: failed to read json, column len not found\n"); goto PARSE_OVER; } else { columnCase.dataLen = 0; @@ -2774,7 +2893,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!host) { tstrncpy(g_Dbs.host, "127.0.0.1", MAX_DB_NAME_SIZE); } else { - printf("failed to read json, host not found\n"); + printf("ERROR: failed to read json, host not found\n"); goto PARSE_OVER; } @@ -2812,17 +2931,17 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!threads) { g_Dbs.threadCount = 1; } else { - printf("failed to read json, threads not found"); + printf("ERROR: failed to read json, threads not found\n"); goto PARSE_OVER; } - + cJSON* threads2 = cJSON_GetObjectItem(root, "thread_count_create_tbl"); if (threads2 && threads2->type == cJSON_Number) { g_Dbs.threadCountByCreateTbl = threads2->valueint; } else if (!threads2) { g_Dbs.threadCountByCreateTbl = 1; } else { - printf("failed to read json, threads2 not found"); + printf("ERROR: failed to read json, threads2 not found\n"); goto PARSE_OVER; } @@ -2832,17 +2951,28 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!gInsertInterval) { g_args.insert_interval = 0; } else { - printf("failed to read json, insert_interval input mistake"); + fprintf(stderr, "ERROR: failed to read json, insert_interval input mistake\n"); goto PARSE_OVER; } + cJSON* maxSqlLen = cJSON_GetObjectItem(root, "max_sql_len"); + if (maxSqlLen && maxSqlLen->type == cJSON_Number) { + g_args.max_sql_len = maxSqlLen->valueint; + } else if (!maxSqlLen) { + g_args.max_sql_len = TSDB_PAYLOAD_SIZE; + } else { + fprintf(stderr, "ERROR: failed to read json, max_sql_len input mistake\n"); + goto PARSE_OVER; + } + + cJSON* numRecPerReq = cJSON_GetObjectItem(root, "num_of_records_per_req"); if (numRecPerReq && numRecPerReq->type == cJSON_Number) { g_args.num_of_RPR = numRecPerReq->valueint; } else if (!numRecPerReq) { g_args.num_of_RPR = 100; } else { - printf("failed to read json, num_of_records_per_req not found"); + printf("ERROR: failed to read json, num_of_records_per_req not found\n"); goto PARSE_OVER; } @@ -2860,19 +2990,19 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!answerPrompt) { g_args.answer_yes = false; } else { - printf("failed to read json, confirm_parameter_prompt not found"); + printf("ERROR: failed to read json, confirm_parameter_prompt not found\n"); goto PARSE_OVER; } cJSON* dbs = cJSON_GetObjectItem(root, "databases"); if (!dbs || dbs->type != cJSON_Array) { - printf("failed to read json, databases not found\n"); + printf("ERROR: failed to read json, databases not found\n"); goto PARSE_OVER; } int dbSize = cJSON_GetArraySize(dbs); if (dbSize > MAX_DB_COUNT) { - printf("failed to read json, databases size overflow, max database is %d\n", MAX_DB_COUNT); + printf("ERROR: failed to read json, databases size overflow, max database is %d\n", MAX_DB_COUNT); goto PARSE_OVER; } @@ -2884,13 +3014,13 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { // dbinfo cJSON *dbinfo = cJSON_GetObjectItem(dbinfos, "dbinfo"); if (!dbinfo || dbinfo->type != cJSON_Object) { - printf("failed to read json, dbinfo not found"); + printf("ERROR: failed to read json, dbinfo not found\n"); goto PARSE_OVER; } cJSON *dbName = cJSON_GetObjectItem(dbinfo, "name"); if (!dbName || dbName->type != cJSON_String || dbName->valuestring == NULL) { - printf("failed to read json, db name not found"); + printf("ERROR: failed to read json, db name not found\n"); goto PARSE_OVER; } tstrncpy(g_Dbs.db[i].dbName, dbName->valuestring, MAX_DB_NAME_SIZE); @@ -2905,7 +3035,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!drop) { g_Dbs.db[i].drop = 0; } else { - printf("failed to read json, drop not found"); + printf("ERROR: failed to read json, drop not found\n"); goto PARSE_OVER; } @@ -2916,7 +3046,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { //tstrncpy(g_Dbs.db[i].dbCfg.precision, "ms", MAX_DB_NAME_SIZE); memset(g_Dbs.db[i].dbCfg.precision, 0, MAX_DB_NAME_SIZE); } else { - printf("failed to read json, precision not found"); + printf("ERROR: failed to read json, precision not found\n"); goto PARSE_OVER; } @@ -2926,7 +3056,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!update) { g_Dbs.db[i].dbCfg.update = -1; } else { - printf("failed to read json, update not found"); + printf("ERROR: failed to read json, update not found\n"); goto PARSE_OVER; } @@ -2936,7 +3066,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!replica) { g_Dbs.db[i].dbCfg.replica = -1; } else { - printf("failed to read json, replica not found"); + printf("ERROR: failed to read json, replica not found\n"); goto PARSE_OVER; } @@ -2946,7 +3076,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!keep) { g_Dbs.db[i].dbCfg.keep = -1; } else { - printf("failed to read json, keep not found"); + printf("ERROR: failed to read json, keep not found\n"); goto PARSE_OVER; } @@ -2956,7 +3086,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!days) { g_Dbs.db[i].dbCfg.days = -1; } else { - printf("failed to read json, days not found"); + printf("ERROR: failed to read json, days not found\n"); goto PARSE_OVER; } @@ -2966,7 +3096,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!cache) { g_Dbs.db[i].dbCfg.cache = -1; } else { - printf("failed to read json, cache not found"); + printf("ERROR: failed to read json, cache not found\n"); goto PARSE_OVER; } @@ -2976,7 +3106,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!blocks) { g_Dbs.db[i].dbCfg.blocks = -1; } else { - printf("failed to read json, block not found"); + printf("ERROR: failed to read json, block not found\n"); goto PARSE_OVER; } @@ -2996,7 +3126,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!minRows) { g_Dbs.db[i].dbCfg.minRows = -1; } else { - printf("failed to read json, minRows not found"); + printf("ERROR: failed to read json, minRows not found\n"); goto PARSE_OVER; } @@ -3006,7 +3136,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!maxRows) { g_Dbs.db[i].dbCfg.maxRows = -1; } else { - printf("failed to read json, maxRows not found"); + printf("ERROR: failed to read json, maxRows not found\n"); goto PARSE_OVER; } @@ -3016,7 +3146,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!comp) { g_Dbs.db[i].dbCfg.comp = -1; } else { - printf("failed to read json, comp not found"); + printf("ERROR: failed to read json, comp not found\n"); goto PARSE_OVER; } @@ -3026,7 +3156,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!walLevel) { g_Dbs.db[i].dbCfg.walLevel = -1; } else { - printf("failed to read json, walLevel not found"); + printf("ERROR: failed to read json, walLevel not found\n"); goto PARSE_OVER; } @@ -3036,7 +3166,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!cacheLast) { g_Dbs.db[i].dbCfg.cacheLast = -1; } else { - printf("failed to read json, cacheLast not found"); + printf("ERROR: failed to read json, cacheLast not found\n"); goto PARSE_OVER; } @@ -3044,9 +3174,9 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { if (quorum && quorum->type == cJSON_Number) { g_Dbs.db[i].dbCfg.quorum = quorum->valueint; } else if (!quorum) { - g_Dbs.db[i].dbCfg.quorum = -1; + g_Dbs.db[i].dbCfg.quorum = 1; } else { - printf("failed to read json, walLevel not found"); + printf("failed to read json, quorum input mistake"); goto PARSE_OVER; } @@ -3056,20 +3186,20 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!fsync) { g_Dbs.db[i].dbCfg.fsync = -1; } else { - printf("failed to read json, fsync not found"); + printf("ERROR: failed to read json, fsync not found\n"); goto PARSE_OVER; } // super_talbes cJSON *stables = cJSON_GetObjectItem(dbinfos, "super_tables"); if (!stables || stables->type != cJSON_Array) { - printf("failed to read json, super_tables not found"); + printf("ERROR: failed to read json, super_tables not found\n"); goto PARSE_OVER; } int stbSize = cJSON_GetArraySize(stables); if (stbSize > MAX_SUPER_TABLE_COUNT) { - printf("failed to read json, databases size overflow, max database is %d\n", MAX_SUPER_TABLE_COUNT); + printf("ERROR: failed to read json, databases size overflow, max database is %d\n", MAX_SUPER_TABLE_COUNT); goto PARSE_OVER; } @@ -3081,14 +3211,14 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { // dbinfo cJSON *stbName = cJSON_GetObjectItem(stbInfo, "name"); if (!stbName || stbName->type != cJSON_String || stbName->valuestring == NULL) { - printf("failed to read json, stb name not found"); + printf("ERROR: failed to read json, stb name not found\n"); goto PARSE_OVER; } tstrncpy(g_Dbs.db[i].superTbls[j].sTblName, stbName->valuestring, MAX_TB_NAME_SIZE); cJSON *prefix = cJSON_GetObjectItem(stbInfo, "childtable_prefix"); if (!prefix || prefix->type != cJSON_String || prefix->valuestring == NULL) { - printf("failed to read json, childtable_prefix not found"); + printf("ERROR: failed to read json, childtable_prefix not found\n"); goto PARSE_OVER; } tstrncpy(g_Dbs.db[i].superTbls[j].childTblPrefix, prefix->valuestring, MAX_DB_NAME_SIZE); @@ -3107,7 +3237,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!autoCreateTbl) { g_Dbs.db[i].superTbls[j].autoCreateTable = PRE_CREATE_SUBTBL; } else { - printf("failed to read json, auto_create_table not found"); + printf("ERROR: failed to read json, auto_create_table not found\n"); goto PARSE_OVER; } @@ -3117,7 +3247,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!batchCreateTbl) { g_Dbs.db[i].superTbls[j].batchCreateTableNum = 1000; } else { - printf("failed to read json, batch_create_tbl_num not found"); + printf("ERROR: failed to read json, batch_create_tbl_num not found\n"); goto PARSE_OVER; } @@ -3135,13 +3265,13 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!childTblExists) { g_Dbs.db[i].superTbls[j].childTblExists = TBL_NO_EXISTS; } else { - printf("failed to read json, child_table_exists not found"); + printf("ERROR: failed to read json, child_table_exists not found\n"); goto PARSE_OVER; } cJSON* count = cJSON_GetObjectItem(stbInfo, "childtable_count"); if (!count || count->type != cJSON_Number || 0 >= count->valueint) { - printf("failed to read json, childtable_count not found"); + printf("ERROR: failed to read json, childtable_count not found\n"); goto PARSE_OVER; } g_Dbs.db[i].superTbls[j].childTblCount = count->valueint; @@ -3154,7 +3284,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!dataSource) { tstrncpy(g_Dbs.db[i].superTbls[j].dataSource, "rand", MAX_DB_NAME_SIZE); } else { - printf("failed to read json, data_source not found"); + printf("ERROR: failed to read json, data_source not found\n"); goto PARSE_OVER; } @@ -3166,27 +3296,49 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!insertMode) { tstrncpy(g_Dbs.db[i].superTbls[j].insertMode, "taosc", MAX_DB_NAME_SIZE); } else { - printf("failed to read json, insert_mode not found"); + printf("ERROR: failed to read json, insert_mode not found\n"); goto PARSE_OVER; } + cJSON* childTbl_limit = cJSON_GetObjectItem(stbInfo, "childtable_limit"); + if (childTbl_limit) { + if (childTbl_limit->type != cJSON_Number) { + printf("ERROR: failed to read json, childtable_limit\n"); + goto PARSE_OVER; + } + g_Dbs.db[i].superTbls[j].childTblLimit = childTbl_limit->valueint; + } else { + g_Dbs.db[i].superTbls[j].childTblLimit = -1; // select ... limit -1 means all query result + } + + cJSON* childTbl_offset = cJSON_GetObjectItem(stbInfo, "childtable_offset"); + if (childTbl_offset) { + if (childTbl_offset->type != cJSON_Number || 0 > childTbl_offset->valueint) { + printf("ERROR: failed to read json, childtable_offset\n"); + goto PARSE_OVER; + } + g_Dbs.db[i].superTbls[j].childTblOffset = childTbl_offset->valueint; + } else { + g_Dbs.db[i].superTbls[j].childTblOffset = 0; + } + cJSON *ts = cJSON_GetObjectItem(stbInfo, "start_timestamp"); if (ts && ts->type == cJSON_String && ts->valuestring != NULL) { tstrncpy(g_Dbs.db[i].superTbls[j].startTimestamp, ts->valuestring, MAX_DB_NAME_SIZE); } else if (!ts) { tstrncpy(g_Dbs.db[i].superTbls[j].startTimestamp, "now", MAX_DB_NAME_SIZE); } else { - printf("failed to read json, start_timestamp not found"); + printf("ERROR: failed to read json, start_timestamp not found\n"); goto PARSE_OVER; } - + cJSON* timestampStep = cJSON_GetObjectItem(stbInfo, "timestamp_step"); if (timestampStep && timestampStep->type == cJSON_Number) { g_Dbs.db[i].superTbls[j].timeStampStep = timestampStep->valueint; } else if (!timestampStep) { - g_Dbs.db[i].superTbls[j].timeStampStep = 1000; + g_Dbs.db[i].superTbls[j].timeStampStep = DEFAULT_TIMESTAMP_STEP; } else { - printf("failed to read json, timestamp_step not found"); + printf("ERROR: failed to read json, timestamp_step not found\n"); goto PARSE_OVER; } @@ -3199,10 +3351,10 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!sampleDataBufSize) { g_Dbs.db[i].superTbls[j].sampleDataBufSize = 1024*1024 + 1024; } else { - printf("failed to read json, sample_buf_size not found"); + printf("ERROR: failed to read json, sample_buf_size not found\n"); goto PARSE_OVER; } - + cJSON *sampleFormat = cJSON_GetObjectItem(stbInfo, "sample_format"); if (sampleFormat && sampleFormat->type == cJSON_String && sampleFormat->valuestring != NULL) { tstrncpy(g_Dbs.db[i].superTbls[j].sampleFormat, @@ -3210,10 +3362,10 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!sampleFormat) { tstrncpy(g_Dbs.db[i].superTbls[j].sampleFormat, "csv", MAX_DB_NAME_SIZE); } else { - printf("failed to read json, sample_format not found"); + printf("ERROR: failed to read json, sample_format not found\n"); goto PARSE_OVER; } - + cJSON *sampleFile = cJSON_GetObjectItem(stbInfo, "sample_file"); if (sampleFile && sampleFile->type == cJSON_String && sampleFile->valuestring != NULL) { tstrncpy(g_Dbs.db[i].superTbls[j].sampleFile, @@ -3221,10 +3373,10 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!sampleFile) { memset(g_Dbs.db[i].superTbls[j].sampleFile, 0, MAX_FILE_NAME_LEN); } else { - printf("failed to read json, sample_file not found"); + printf("ERROR: failed to read json, sample_file not found\n"); goto PARSE_OVER; } - + cJSON *tagsFile = cJSON_GetObjectItem(stbInfo, "tags_file"); if (tagsFile && tagsFile->type == cJSON_String && tagsFile->valuestring != NULL) { tstrncpy(g_Dbs.db[i].superTbls[j].tagsFile, @@ -3238,10 +3390,10 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { memset(g_Dbs.db[i].superTbls[j].tagsFile, 0, MAX_FILE_NAME_LEN); g_Dbs.db[i].superTbls[j].tagSource = 0; } else { - printf("failed to read json, tags_file not found"); + printf("ERROR: failed to read json, tags_file not found\n"); goto PARSE_OVER; } - + cJSON* maxSqlLen = cJSON_GetObjectItem(stbInfo, "max_sql_len"); if (maxSqlLen && maxSqlLen->type == cJSON_Number) { int32_t len = maxSqlLen->valueint; @@ -3254,7 +3406,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!maxSqlLen) { g_Dbs.db[i].superTbls[j].maxSqlLen = TSDB_MAX_SQL_LEN; } else { - printf("failed to read json, maxSqlLen not found"); + printf("ERROR: failed to read json, maxSqlLen not found\n"); goto PARSE_OVER; } @@ -3271,7 +3423,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!multiThreadWriteOneTbl) { g_Dbs.db[i].superTbls[j].multiThreadWriteOneTbl = 0; } else { - printf("failed to read json, multiThreadWriteOneTbl not found"); + printf("ERROR: failed to read json, multiThreadWriteOneTbl not found\n"); goto PARSE_OVER; } @@ -3281,7 +3433,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!numberOfTblInOneSql) { g_Dbs.db[i].superTbls[j].numberOfTblInOneSql = 0; } else { - printf("failed to read json, numberOfTblInOneSql not found"); + printf("ERROR: failed to read json, numberOfTblInOneSql not found\n"); goto PARSE_OVER; } @@ -3291,7 +3443,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!rowsPerTbl) { g_Dbs.db[i].superTbls[j].rowsPerTbl = 1; } else { - printf("failed to read json, rowsPerTbl not found"); + printf("ERROR: failed to read json, rowsPerTbl not found\n"); goto PARSE_OVER; } @@ -3301,7 +3453,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!disorderRatio) { g_Dbs.db[i].superTbls[j].disorderRatio = 0; } else { - printf("failed to read json, disorderRatio not found"); + printf("ERROR: failed to read json, disorderRatio not found\n"); goto PARSE_OVER; } @@ -3311,7 +3463,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!disorderRange) { g_Dbs.db[i].superTbls[j].disorderRange = 1000; } else { - printf("failed to read json, disorderRange not found"); + printf("ERROR: failed to read json, disorderRange not found\n"); goto PARSE_OVER; } @@ -3321,7 +3473,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { } else if (!insertRows) { g_Dbs.db[i].superTbls[j].insertRows = 0x7FFFFFFFFFFFFFFF; } else { - printf("failed to read json, insert_rows input mistake"); + fprintf(stderr, "failed to read json, insert_rows input mistake"); goto PARSE_OVER; } @@ -3333,7 +3485,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { __func__, __LINE__, g_args.insert_interval); g_Dbs.db[i].superTbls[j].insertInterval = g_args.insert_interval; } else { - printf("failed to read json, insert_interval input mistake"); + fprintf(stderr, "failed to read json, insert_interval input mistake"); goto PARSE_OVER; } @@ -3372,7 +3524,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (!host) { tstrncpy(g_queryInfo.host, "127.0.0.1", MAX_DB_NAME_SIZE); } else { - printf("failed to read json, host not found\n"); + printf("ERROR: failed to read json, host not found\n"); goto PARSE_OVER; } @@ -3410,7 +3562,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (!answerPrompt) { g_args.answer_yes = false; } else { - printf("failed to read json, confirm_parameter_prompt not found"); + printf("ERROR: failed to read json, confirm_parameter_prompt not found\n"); goto PARSE_OVER; } @@ -3418,7 +3570,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { if (dbs && dbs->type == cJSON_String && dbs->valuestring != NULL) { tstrncpy(g_queryInfo.dbName, dbs->valuestring, MAX_DB_NAME_SIZE); } else if (!dbs) { - printf("failed to read json, databases not found\n"); + printf("ERROR: failed to read json, databases not found\n"); goto PARSE_OVER; } @@ -3428,7 +3580,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (!queryMode) { tstrncpy(g_queryInfo.queryMode, "taosc", MAX_TB_NAME_SIZE); } else { - printf("failed to read json, query_mode not found\n"); + printf("ERROR: failed to read json, query_mode not found\n"); goto PARSE_OVER; } @@ -3438,7 +3590,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { g_queryInfo.superQueryInfo.concurrent = 0; g_queryInfo.superQueryInfo.sqlCount = 0; } else if (superQuery->type != cJSON_Object) { - printf("failed to read json, super_table_query not found"); + printf("ERROR: failed to read json, super_table_query not found\n"); goto PARSE_OVER; } else { cJSON* rate = cJSON_GetObjectItem(superQuery, "query_interval"); @@ -3462,7 +3614,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (0 == strcmp("async", mode->valuestring)) { g_queryInfo.superQueryInfo.subscribeMode = 1; } else { - printf("failed to read json, subscribe mod error\n"); + printf("ERROR: failed to read json, subscribe mod error\n"); goto PARSE_OVER; } } else { @@ -3485,7 +3637,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (0 == strcmp("no", restart->valuestring)) { g_queryInfo.superQueryInfo.subscribeRestart = 0; } else { - printf("failed to read json, subscribe restart error\n"); + printf("ERROR: failed to read json, subscribe restart error\n"); goto PARSE_OVER; } } else { @@ -3501,7 +3653,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (0 == strcmp("no", keepProgress->valuestring)) { g_queryInfo.superQueryInfo.subscribeKeepProgress = 0; } else { - printf("failed to read json, subscribe keepProgress error\n"); + printf("ERROR: failed to read json, subscribe keepProgress error\n"); goto PARSE_OVER; } } else { @@ -3513,15 +3665,15 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { if (!superSqls) { g_queryInfo.superQueryInfo.sqlCount = 0; } else if (superSqls->type != cJSON_Array) { - printf("failed to read json, super sqls not found\n"); + printf("ERROR: failed to read json, super sqls not found\n"); goto PARSE_OVER; } else { int superSqlSize = cJSON_GetArraySize(superSqls); if (superSqlSize > MAX_QUERY_SQL_COUNT) { - printf("failed to read json, query sql size overflow, max is %d\n", MAX_QUERY_SQL_COUNT); + printf("ERROR: failed to read json, query sql size overflow, max is %d\n", MAX_QUERY_SQL_COUNT); goto PARSE_OVER; } - + g_queryInfo.superQueryInfo.sqlCount = superSqlSize; for (int j = 0; j < superSqlSize; ++j) { cJSON* sql = cJSON_GetArrayItem(superSqls, j); @@ -3529,7 +3681,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { cJSON *sqlStr = cJSON_GetObjectItem(sql, "sql"); if (!sqlStr || sqlStr->type != cJSON_String || sqlStr->valuestring == NULL) { - printf("failed to read json, sql not found\n"); + printf("ERROR: failed to read json, sql not found\n"); goto PARSE_OVER; } tstrncpy(g_queryInfo.superQueryInfo.sql[j], sqlStr->valuestring, MAX_QUERY_SQL_LENGTH); @@ -3540,20 +3692,20 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (NULL == result) { memset(g_queryInfo.superQueryInfo.result[j], 0, MAX_FILE_NAME_LEN); } else { - printf("failed to read json, super query result file not found\n"); + printf("ERROR: failed to read json, super query result file not found\n"); goto PARSE_OVER; } } } } - + // sub_table_query cJSON *subQuery = cJSON_GetObjectItem(root, "super_table_query"); if (!subQuery) { g_queryInfo.subQueryInfo.threadCnt = 0; g_queryInfo.subQueryInfo.sqlCount = 0; } else if (subQuery->type != cJSON_Object) { - printf("failed to read json, sub_table_query not found"); + printf("ERROR: failed to read json, sub_table_query not found\n"); ret = true; goto PARSE_OVER; } else { @@ -3563,29 +3715,29 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (!subrate) { g_queryInfo.subQueryInfo.rate = 0; } - + cJSON* threads = cJSON_GetObjectItem(subQuery, "threads"); if (threads && threads->type == cJSON_Number) { g_queryInfo.subQueryInfo.threadCnt = threads->valueint; } else if (!threads) { g_queryInfo.subQueryInfo.threadCnt = 1; } - + //cJSON* subTblCnt = cJSON_GetObjectItem(subQuery, "childtable_count"); //if (subTblCnt && subTblCnt->type == cJSON_Number) { // g_queryInfo.subQueryInfo.childTblCount = subTblCnt->valueint; //} else if (!subTblCnt) { // g_queryInfo.subQueryInfo.childTblCount = 0; //} - + cJSON* stblname = cJSON_GetObjectItem(subQuery, "stblname"); if (stblname && stblname->type == cJSON_String && stblname->valuestring != NULL) { tstrncpy(g_queryInfo.subQueryInfo.sTblName, stblname->valuestring, MAX_TB_NAME_SIZE); } else { - printf("failed to read json, super table name not found\n"); + printf("ERROR: failed to read json, super table name not found\n"); goto PARSE_OVER; } - + cJSON* submode = cJSON_GetObjectItem(subQuery, "mode"); if (submode && submode->type == cJSON_String && submode->valuestring != NULL) { if (0 == strcmp("sync", submode->valuestring)) { @@ -3593,13 +3745,13 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (0 == strcmp("async", submode->valuestring)) { g_queryInfo.subQueryInfo.subscribeMode = 1; } else { - printf("failed to read json, subscribe mod error\n"); + printf("ERROR: failed to read json, subscribe mod error\n"); goto PARSE_OVER; } } else { g_queryInfo.subQueryInfo.subscribeMode = 0; } - + cJSON* subinterval = cJSON_GetObjectItem(subQuery, "interval"); if (subinterval && subinterval->type == cJSON_Number) { g_queryInfo.subQueryInfo.subscribeInterval = subinterval->valueint; @@ -3616,7 +3768,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (0 == strcmp("no", subrestart->valuestring)) { g_queryInfo.subQueryInfo.subscribeRestart = 0; } else { - printf("failed to read json, subscribe restart error\n"); + printf("ERROR: failed to read json, subscribe restart error\n"); goto PARSE_OVER; } } else { @@ -3630,7 +3782,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (0 == strcmp("no", subkeepProgress->valuestring)) { g_queryInfo.subQueryInfo.subscribeKeepProgress = 0; } else { - printf("failed to read json, subscribe keepProgress error\n"); + printf("ERROR: failed to read json, subscribe keepProgress error\n"); goto PARSE_OVER; } } else { @@ -3642,12 +3794,12 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { if (!subsqls) { g_queryInfo.subQueryInfo.sqlCount = 0; } else if (subsqls->type != cJSON_Array) { - printf("failed to read json, super sqls not found\n"); + printf("ERROR: failed to read json, super sqls not found\n"); goto PARSE_OVER; } else { int superSqlSize = cJSON_GetArraySize(subsqls); if (superSqlSize > MAX_QUERY_SQL_COUNT) { - printf("failed to read json, query sql size overflow, max is %d\n", MAX_QUERY_SQL_COUNT); + printf("ERROR: failed to read json, query sql size overflow, max is %d\n", MAX_QUERY_SQL_COUNT); goto PARSE_OVER; } @@ -3658,7 +3810,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { cJSON *sqlStr = cJSON_GetObjectItem(sql, "sql"); if (!sqlStr || sqlStr->type != cJSON_String || sqlStr->valuestring == NULL) { - printf("failed to read json, sql not found\n"); + printf("ERROR: failed to read json, sql not found\n"); goto PARSE_OVER; } tstrncpy(g_queryInfo.subQueryInfo.sql[j], sqlStr->valuestring, MAX_QUERY_SQL_LENGTH); @@ -3669,7 +3821,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { } else if (NULL == result) { memset(g_queryInfo.subQueryInfo.result[j], 0, MAX_FILE_NAME_LEN); } else { - printf("failed to read json, sub query result file not found\n"); + printf("ERROR: failed to read json, sub query result file not found\n"); goto PARSE_OVER; } } @@ -3708,7 +3860,7 @@ static bool getInfoFromJsonFile(char* file) { content[len] = 0; cJSON* root = cJSON_Parse(content); if (root == NULL) { - printf("failed to cjson parse %s, invalid json format", file); + printf("ERROR: failed to cjson parse %s, invalid json format\n", file); goto PARSE_OVER; } @@ -3721,13 +3873,13 @@ static bool getInfoFromJsonFile(char* file) { } else if (0 == strcasecmp("subscribe", filetype->valuestring)) { g_args.test_mode = SUBSCRIBE_MODE; } else { - printf("failed to read json, filetype not support\n"); + printf("ERROR: failed to read json, filetype not support\n"); goto PARSE_OVER; } } else if (!filetype) { g_args.test_mode = INSERT_MODE; } else { - printf("failed to read json, filetype not found\n"); + printf("ERROR: failed to read json, filetype not found\n"); goto PARSE_OVER; } @@ -3738,7 +3890,7 @@ static bool getInfoFromJsonFile(char* file) { } else if (SUBSCRIBE_MODE == g_args.test_mode) { ret = getMetaFromQueryJsonFile(root); } else { - printf("input json file type error! please input correct file type: insert or query or subscribe\n"); + printf("ERROR: input json file type error! please input correct file type: insert or query or subscribe\n"); goto PARSE_OVER; } @@ -3749,7 +3901,7 @@ PARSE_OVER: return ret; } -void prePareSampleData() { +void prepareSampleData() { for (int i = 0; i < g_Dbs.dbCount; i++) { for (int j = 0; j < g_Dbs.db[i].superTblCount; j++) { //if (0 == strncasecmp(g_Dbs.db[i].superTbls[j].dataSource, "sample", 6)) { @@ -3787,26 +3939,31 @@ void postFreeResource() { } } -int getRowDataFromSample(char* dataBuf, int maxLen, int64_t timestamp, SSuperTable* superTblInfo, int* sampleUsePos, FILE *fp, char* sampleBuf) { +static int getRowDataFromSample(char* dataBuf, int maxLen, int64_t timestamp, + SSuperTable* superTblInfo, int* sampleUsePos) { if ((*sampleUsePos) == MAX_SAMPLES_ONCE_FROM_FILE) { - int ret = readSampleFromCsvFileToMem(fp, superTblInfo, sampleBuf); + int ret = readSampleFromCsvFileToMem(superTblInfo); if (0 != ret) { + tmfree(superTblInfo->sampleDataBuf); + superTblInfo->sampleDataBuf = NULL; return -1; } *sampleUsePos = 0; } int dataLen = 0; - dataLen += snprintf(dataBuf + dataLen, maxLen - dataLen, "(%" PRId64 ", ", timestamp); - dataLen += snprintf(dataBuf + dataLen, maxLen - dataLen, "%s", sampleBuf + superTblInfo->lenOfOneRow * (*sampleUsePos)); + dataLen += snprintf(dataBuf + dataLen, maxLen - dataLen, + "(%" PRId64 ", ", timestamp); + dataLen += snprintf(dataBuf + dataLen, maxLen - dataLen, + "%s", superTblInfo->sampleDataBuf + superTblInfo->lenOfOneRow * (*sampleUsePos)); dataLen += snprintf(dataBuf + dataLen, maxLen - dataLen, ")"); (*sampleUsePos)++; - + return dataLen; } -int generateRowData(char* dataBuf, int maxLen, int64_t timestamp, SSuperTable* stbInfo) { +static int generateRowData(char* dataBuf, int maxLen, int64_t timestamp, SSuperTable* stbInfo) { int dataLen = 0; dataLen += snprintf(dataBuf + dataLen, maxLen - dataLen, "(%" PRId64 ", ", timestamp); for (int i = 0; i < stbInfo->columnCount; i++) { @@ -3853,14 +4010,12 @@ int generateRowData(char* dataBuf, int maxLen, int64_t timestamp, SSuperTable* } static void syncWriteForNumberOfTblInOneSql( - threadInfo *winfo, FILE *fp, char* sampleDataBuf) { + threadInfo *winfo, char* sampleDataBuf) { SSuperTable* superTblInfo = winfo->superTblInfo; int samplePos = 0; //printf("========threadID[%d], table rang: %d - %d \n", winfo->threadID, winfo->start_table_id, winfo->end_table_id); - int64_t totalRowsInserted = 0; - int64_t totalAffectedRows = 0; int64_t lastPrintTime = taosGetTimestampMs(); char* buffer = calloc(superTblInfo->maxSqlLen+1, 1); @@ -3878,12 +4033,15 @@ static void syncWriteForNumberOfTblInOneSql( uint64_t time_counter = winfo->start_time; int sampleUsePos; + int insert_interval = superTblInfo?superTblInfo->insertInterval:g_args.insert_interval; int64_t st = 0; - int64_t et = 0; - for (int i = 0; i < superTblInfo->insertRows;) { + int64_t et = 0xffffffff; + + int64_t insertRows = (superTblInfo)?superTblInfo->insertRows:g_args.num_of_DPT; + for (int i = 0; i < insertRows;) { int32_t tbl_id = 0; - for (int tID = winfo->start_table_id; tID <= winfo->end_table_id; ) { - int64_t tmp_time = 0; + for (int tableSeq = winfo->start_table_id; tableSeq <= winfo->end_table_id; ) { + int64_t start_time = 0; int inserted = i; for (int k = 0; k < g_args.num_of_RPR;) { @@ -3891,12 +4049,12 @@ static void syncWriteForNumberOfTblInOneSql( memset(buffer, 0, superTblInfo->maxSqlLen); char *pstr = buffer; - int32_t end_tbl_id = tID + numberOfTblInOneSql; + int32_t end_tbl_id = tableSeq + numberOfTblInOneSql; if (end_tbl_id > winfo->end_table_id) { end_tbl_id = winfo->end_table_id+1; } - for (tbl_id = tID; tbl_id < end_tbl_id; tbl_id++) { + for (tbl_id = tableSeq ; tbl_id < end_tbl_id; tbl_id++) { sampleUsePos = samplePos; if (AUTO_CREATE_SUBTBL == superTblInfo->autoCreateTable) { char* tagsValBuf = NULL; @@ -3964,18 +4122,16 @@ static void syncWriteForNumberOfTblInOneSql( } } - tmp_time = time_counter; - for (k = 0; k < superTblInfo->rowsPerTbl;) { + start_time = time_counter; + for (int j = 0; j < superTblInfo->rowsPerTbl;) { int retLen = 0; if (0 == strncasecmp(superTblInfo->dataSource, "sample", strlen("sample"))) { - retLen = getRowDataFromSample(pstr + len, - superTblInfo->maxSqlLen - len, - tmp_time += superTblInfo->timeStampStep, - superTblInfo, - &sampleUsePos, - fp, - sampleDataBuf); + retLen = getRowDataFromSample(pstr + len, + superTblInfo->maxSqlLen - len, + start_time += superTblInfo->timeStampStep, + superTblInfo, + &sampleUsePos); if (retLen < 0) { goto free_and_statistics; } @@ -3984,15 +4140,15 @@ static void syncWriteForNumberOfTblInOneSql( int rand_num = rand_tinyint() % 100; if (0 != superTblInfo->disorderRatio && rand_num < superTblInfo->disorderRatio) { - int64_t d = tmp_time - rand() % superTblInfo->disorderRange; - retLen = generateRowData(pstr + len, - superTblInfo->maxSqlLen - len, - d, + int64_t d = start_time - taosRandom() % superTblInfo->disorderRange; + retLen = generateRowData(pstr + len, + superTblInfo->maxSqlLen - len, + d, superTblInfo); } else { - retLen = generateRowData(pstr + len, - superTblInfo->maxSqlLen - len, - tmp_time += superTblInfo->timeStampStep, + retLen = generateRowData(pstr + len, + superTblInfo->maxSqlLen - len, + start_time += superTblInfo->timeStampStep, superTblInfo); } if (retLen < 0) { @@ -4001,12 +4157,12 @@ static void syncWriteForNumberOfTblInOneSql( } len += retLen; //inserted++; - k++; - totalRowsInserted++; + j++; + winfo->totalInsertRows++; if (inserted >= superTblInfo->insertRows || (superTblInfo->maxSqlLen - len) < (superTblInfo->lenOfOneRow + 128)) { - tID = tbl_id + 1; + tableSeq = tbl_id + 1; printf("config rowsPerTbl and numberOfTblInOneSql not match with max_sql_lenth, please reconfig![lenOfOneRow:%d]\n", superTblInfo->lenOfOneRow); goto send_to_server; @@ -4014,18 +4170,18 @@ static void syncWriteForNumberOfTblInOneSql( } } - tID = tbl_id; + tableSeq = tbl_id; inserted += superTblInfo->rowsPerTbl; send_to_server: - if (g_args.insert_interval && (g_args.insert_interval > (et - st))) { - int sleep_time = g_args.insert_interval - (et -st); + if (insert_interval) { + st = taosGetTimestampUs(); + + if (insert_interval > ((et - st)/1000)) { + int sleep_time = insert_interval - (et -st); printf("sleep: %d ms insert interval\n", sleep_time); taosMsleep(sleep_time); // ms - } - - if (g_args.insert_interval) { - st = taosGetTimestampMs(); + } } if (0 == strncasecmp(superTblInfo->insertMode, @@ -4059,7 +4215,7 @@ send_to_server: if (currentPrintTime - lastPrintTime > 30*1000) { printf("thread[%d] has currently inserted rows: %"PRId64 ", affected rows: %"PRId64 "\n", winfo->threadID, - winfo->totalRowsInserted, + winfo->totalInsertRows, winfo->totalAffectedRows); lastPrintTime = currentPrintTime; } @@ -4076,19 +4232,19 @@ send_to_server: goto free_and_statistics; } } - if (g_args.insert_interval) { - et = taosGetTimestampMs(); + if (insert_interval) { + et = taosGetTimestampUs(); } break; } - if (tID > winfo->end_table_id) { + if (tableSeq > winfo->end_table_id) { if (0 == strncasecmp(superTblInfo->dataSource, "sample", strlen("sample"))) { samplePos = sampleUsePos; } i = inserted; - time_counter = tmp_time; + time_counter = start_time; } } @@ -4097,14 +4253,13 @@ send_to_server: free_and_statistics: tmfree(buffer); - winfo->totalRowsInserted = totalRowsInserted; - winfo->totalAffectedRows = totalAffectedRows; - printf("====thread[%d] completed total inserted rows: %"PRId64 ", affected rows: %"PRId64 "====\n", winfo->threadID, totalRowsInserted, totalAffectedRows); + printf("====thread[%d] completed total inserted rows: %"PRId64 ", affected rows: %"PRId64 "====\n", + winfo->threadID, winfo->totalInsertRows, winfo->totalAffectedRows); return; } int32_t generateData(char *res, char **data_type, - int num_of_cols, int64_t timestamp, int len_of_binary) { + int num_of_cols, int64_t timestamp, int lenOfBinary) { memset(res, 0, MAX_DATA_SIZE); char *pstr = res; pstr += sprintf(pstr, "(%" PRId64, timestamp); @@ -4136,16 +4291,16 @@ int32_t generateData(char *res, char **data_type, double t = rand_double(); pstr += sprintf(pstr, ", %20.8f", t); } else if (strcasecmp(data_type[i % c], "bool") == 0) { - bool b = rand() & 1; + bool b = taosRandom() & 1; pstr += sprintf(pstr, ", %s", b ? "true" : "false"); } else if (strcasecmp(data_type[i % c], "binary") == 0) { - char *s = malloc(len_of_binary); - rand_string(s, len_of_binary); + char *s = malloc(lenOfBinary); + rand_string(s, lenOfBinary); pstr += sprintf(pstr, ", \"%s\"", s); free(s); }else if (strcasecmp(data_type[i % c], "nchar") == 0) { - char *s = malloc(len_of_binary); - rand_string(s, len_of_binary); + char *s = malloc(lenOfBinary); + rand_string(s, lenOfBinary); pstr += sprintf(pstr, ", \"%s\"", s); free(s); } @@ -4161,337 +4316,318 @@ int32_t generateData(char *res, char **data_type, return (int32_t)(pstr - res); } -// sync insertion -/* - 1 thread: 100 tables * 2000 rows/s - 1 thread: 10 tables * 20000 rows/s - 6 thread: 300 tables * 2000 rows/s +static int prepareSampleDataForSTable(SSuperTable *superTblInfo) { + char* sampleDataBuf = NULL; - 2 taosinsertdata , 1 thread: 10 tables * 20000 rows/s -*/ -static void* syncWrite(void *sarg) { + // each thread read sample data from csv file + if (0 == strncasecmp(superTblInfo->dataSource, + "sample", + strlen("sample"))) { + sampleDataBuf = calloc( + superTblInfo->lenOfOneRow * MAX_SAMPLES_ONCE_FROM_FILE, 1); + if (sampleDataBuf == NULL) { + fprintf(stderr, "Failed to calloc %d Bytes, reason:%s\n", + superTblInfo->lenOfOneRow * MAX_SAMPLES_ONCE_FROM_FILE, + strerror(errno)); + return -1; + } - threadInfo *winfo = (threadInfo *)sarg; + superTblInfo->sampleDataBuf = sampleDataBuf; + int ret = readSampleFromCsvFileToMem(superTblInfo); + if (0 != ret) { + tmfree(sampleDataBuf); + superTblInfo->sampleDataBuf = NULL; + return -1; + } + } - char buffer[BUFFER_SIZE] = "\0"; - char data[MAX_DATA_SIZE]; - char **data_type = g_args.datatype; - int len_of_binary = g_args.len_of_binary; + return 0; +} + +static int execInsert(threadInfo *winfo, char *buffer, int k) +{ + int affectedRows; + SSuperTable* superTblInfo = winfo->superTblInfo; + + if (superTblInfo) { + if (0 == strncasecmp(superTblInfo->insertMode, "taosc", strlen("taosc"))) { + verbosePrint("%s() LN%d %s\n", __func__, __LINE__, buffer); + affectedRows = queryDbExec(winfo->taos, buffer, INSERT_TYPE); + } else { + verbosePrint("%s() LN%d %s\n", __func__, __LINE__, buffer); + int retCode = postProceSql(g_Dbs.host, g_Dbs.port, buffer); + + if (0 != retCode) { + affectedRows = -1; + printf("========restful return fail, threadID[%d]\n", winfo->threadID); + } else { + affectedRows = k; + } + } + } else { + verbosePrint("%s() LN%d %s\n", __func__, __LINE__, buffer); + affectedRows = queryDbExec(winfo->taos, buffer, 1); + } + + return affectedRows; +} + +static int generateDataBuffer(int32_t tableSeq, + threadInfo *pThreadInfo, char *buffer, + int64_t insertRows, + int64_t startFrom, int64_t startTime, int *pSampleUsePos) +{ + SSuperTable* superTblInfo = pThreadInfo->superTblInfo; int ncols_per_record = 1; // count first col ts - int i = 0; - while(g_args.datatype[i]) { - i ++; - ncols_per_record ++; + + if (superTblInfo == NULL) { + int datatypeSeq = 0; + while(g_args.datatype[datatypeSeq]) { + datatypeSeq ++; + ncols_per_record ++; + } } - srand((uint32_t)time(NULL)); - int64_t time_counter = winfo->start_time; + assert(buffer != NULL); - uint64_t st = 0; - uint64_t et = 0; + char *pChildTblName; + int childTblCount; - winfo->totalRowsInserted = 0; - winfo->totalAffectedRows = 0; + pChildTblName = calloc(TSDB_TABLE_NAME_LEN, 1); + if (NULL == pChildTblName) { + fprintf(stderr, "failed to alloc memory %d\n", TSDB_TABLE_NAME_LEN); + return -1; + } - for (int tID = winfo->start_table_id; tID <= winfo->end_table_id; tID++) { - int64_t tmp_time = time_counter; + if (superTblInfo && (superTblInfo->childTblOffset > 0)) { + // select tbname from stb limit 1 offset tableSeq + getChildNameOfSuperTableWithLimitAndOffset(pThreadInfo->taos, + pThreadInfo->db_name, superTblInfo->sTblName, + &pChildTblName, &childTblCount, + 1, tableSeq); + } else { + snprintf(pChildTblName, TSDB_TABLE_NAME_LEN, "%s%d", + superTblInfo?superTblInfo->childTblPrefix:g_args.tb_prefix, tableSeq); + } + + memset(buffer, 0, superTblInfo?superTblInfo->maxSqlLen:g_args.max_sql_len); + + char *pstr = buffer; + + if (superTblInfo) { + if (AUTO_CREATE_SUBTBL == superTblInfo->autoCreateTable) { + char* tagsValBuf = NULL; + if (0 == superTblInfo->tagSource) { + tagsValBuf = generateTagVaulesForStb(superTblInfo); + } else { + tagsValBuf = getTagValueFromTagSample( + superTblInfo, + tableSeq % superTblInfo->tagSampleCount); + } + if (NULL == tagsValBuf) { + free(pChildTblName); + fprintf(stderr, "tag buf failed to allocate memory\n"); + return -1; + } - for (int i = 0; i < g_args.num_of_DPT;) { + pstr += snprintf(pstr, + superTblInfo->maxSqlLen, + "insert into %s.%s using %s.%s tags %s values", + pThreadInfo->db_name, + pChildTblName, + pThreadInfo->db_name, + superTblInfo->sTblName, + tagsValBuf); + tmfree(tagsValBuf); + } else if (TBL_ALREADY_EXISTS == superTblInfo->childTblExists) { + pstr += snprintf(pstr, + superTblInfo->maxSqlLen, + "insert into %s.%s values", + pThreadInfo->db_name, + superTblInfo->childTblName + tableSeq * TSDB_TABLE_NAME_LEN); + } else { + pstr += snprintf(pstr, + (superTblInfo?superTblInfo->maxSqlLen:g_args.max_sql_len), + "insert into %s.%s values", + pThreadInfo->db_name, + pChildTblName); + } + } else { + pstr += snprintf(pstr, + g_args.max_sql_len, + "insert into %s.%s values", + pThreadInfo->db_name, + pChildTblName); + } - int tblInserted = i; + int k; + int len = 0; - char *pstr = buffer; - pstr += sprintf(pstr, - "insert into %s.%s%d values ", - winfo->db_name, g_args.tb_prefix, tID); - int k; - for (k = 0; k < g_args.num_of_RPR;) { - int rand_num = rand() % 100; - int len = -1; + verbosePrint("%s() LN%d num_of_RPR=%d\n", __func__, __LINE__, g_args.num_of_RPR); + for (k = 0; k < g_args.num_of_RPR;) { + if (superTblInfo) { + int retLen = 0; - if ((g_args.disorderRatio != 0) - && (rand_num < g_args.disorderRange)) { - - int64_t d = tmp_time - rand() % 1000000 + rand_num; - len = generateData(data, data_type, - ncols_per_record, d, len_of_binary); + if (0 == strncasecmp(superTblInfo->dataSource, + "sample", strlen("sample"))) { + retLen = getRowDataFromSample( + pstr + len, + superTblInfo->maxSqlLen - len, + startTime + superTblInfo->timeStampStep * startFrom, + superTblInfo, + pSampleUsePos); + } else if (0 == strncasecmp(superTblInfo->dataSource, + "rand", strlen("rand"))) { + int rand_num = rand_tinyint() % 100; + if (0 != superTblInfo->disorderRatio + && rand_num < superTblInfo->disorderRatio) { + int64_t d = startTime - taosRandom() % superTblInfo->disorderRange; + retLen = generateRowData( + pstr + len, + superTblInfo->maxSqlLen - len, + d, + superTblInfo); + //printf("disorder rows, rand_num:%d, last ts:%"PRId64" current ts:%"PRId64"\n", rand_num, start_time, d); } else { - len = generateData(data, data_type, - ncols_per_record, tmp_time += 1000, len_of_binary); + retLen = generateRowData( + pstr + len, + superTblInfo->maxSqlLen - len, + startTime + superTblInfo->timeStampStep * startFrom, + superTblInfo); } - //assert(len + pstr - buffer < BUFFER_SIZE); - if (len + pstr - buffer >= BUFFER_SIZE) { // too long - break; + if (retLen < 0) { + free(pChildTblName); + return -1; } - pstr += sprintf(pstr, " %s", data); - tblInserted++; - k++; - i++; + len += retLen; - if (tblInserted >= g_args.num_of_DPT) + if (len >= (superTblInfo->maxSqlLen - 256)) { // reserve for overwrite + k++; break; + } } + } else { + int rand_num = taosRandom() % 100; + char data[MAX_DATA_SIZE]; + char **data_type = g_args.datatype; + int lenOfBinary = g_args.len_of_binary; - winfo->totalRowsInserted += k; - /* puts(buffer); */ - int64_t startTs; - int64_t endTs; - startTs = taosGetTimestampUs(); - //queryDB(winfo->taos, buffer); - if (i > 0 && g_args.insert_interval - && (g_args.insert_interval > (et - st) )) { - int sleep_time = g_args.insert_interval - (et -st); - printf("sleep: %d ms specified by insert_interval\n", sleep_time); - taosMsleep(sleep_time); // ms - } - - if (g_args.insert_interval) { - st = taosGetTimestampMs(); - } - verbosePrint("%s() LN%d %s\n", __func__, __LINE__, buffer); - int affectedRows = queryDbExec(winfo->taos, buffer, 1); - - if (0 < affectedRows){ - endTs = taosGetTimestampUs(); - int64_t delay = endTs - startTs; - if (delay > winfo->maxDelay) - winfo->maxDelay = delay; - if (delay < winfo->minDelay) - winfo->minDelay = delay; - winfo->cntDelay++; - winfo->totalDelay += delay; - winfo->totalAffectedRows += affectedRows; - winfo->avgDelay = (double)winfo->totalDelay / winfo->cntDelay; + if ((g_args.disorderRatio != 0) + && (rand_num < g_args.disorderRange)) { + + int64_t d = startTime - taosRandom() % 1000000 + rand_num; + len = generateData(data, data_type, + ncols_per_record, d, lenOfBinary); } else { - fprintf(stderr, "queryDbExec() buffer:\n%s\naffected rows is %d", buffer, affectedRows); + len = generateData(data, data_type, + ncols_per_record, + startTime + DEFAULT_TIMESTAMP_STEP * startFrom, + lenOfBinary); } - verbosePrint("%s() LN%d: totalaffectedRows:%"PRId64" tblInserted=%d\n", __func__, __LINE__, winfo->totalAffectedRows, tblInserted); - if (g_args.insert_interval) { - et = taosGetTimestampMs(); + pstr += sprintf(pstr, " %s", data); + //assert(len + pstr - buffer < BUFFER_SIZE); + if (len + pstr - buffer >= (g_args.max_sql_len - 256)) { // too long + k++; + break; } + } - if (tblInserted >= g_args.num_of_DPT) { - break; - } - } // num_of_DPT - } // tId + verbosePrint("%s() LN%d len=%d k=%d \nbuffer=%p\n", __func__, __LINE__, len, k, buffer); - printf("====thread[%d] completed total inserted rows: %"PRId64 ", total affected rows: %"PRId64 "====\n", - winfo->threadID, - winfo->totalRowsInserted, - winfo->totalAffectedRows); + k++; + startFrom ++; - return NULL; + if (startFrom >= insertRows) + break; + } + + if (pChildTblName) + free(pChildTblName); + + return k; } +// sync insertion +/* + 1 thread: 100 tables * 2000 rows/s + 1 thread: 10 tables * 20000 rows/s + 6 thread: 300 tables * 2000 rows/s -static void* syncWriteWithStb(void *sarg) { - uint64_t lastPrintTime = taosGetTimestampMs(); + 2 taosinsertdata , 1 thread: 10 tables * 20000 rows/s +*/ +static void* syncWrite(void *sarg) { threadInfo *winfo = (threadInfo *)sarg; SSuperTable* superTblInfo = winfo->superTblInfo; - FILE *fp = NULL; - char* sampleDataBuf = NULL; - int samplePos = 0; - - // each thread read sample data from csv file - if (0 == strncasecmp(superTblInfo->dataSource, - "sample", - strlen("sample"))) { - sampleDataBuf = calloc( - superTblInfo->lenOfOneRow * MAX_SAMPLES_ONCE_FROM_FILE, 1); - if (sampleDataBuf == NULL) { - printf("Failed to calloc %d Bytes, reason:%s\n", - superTblInfo->lenOfOneRow * MAX_SAMPLES_ONCE_FROM_FILE, + char* buffer = calloc(superTblInfo?superTblInfo->maxSqlLen:g_args.max_sql_len, 1); + if (NULL == buffer) { + fprintf(stderr, "Failed to alloc %d Bytes, reason:%s\n", + superTblInfo?superTblInfo->maxSqlLen:g_args.max_sql_len, strerror(errno)); - return NULL; - } + return NULL; + } - fp = fopen(superTblInfo->sampleFile, "r"); - if (fp == NULL) { - printf("Failed to open sample file: %s, reason:%s\n", - superTblInfo->sampleFile, strerror(errno)); - tmfree(sampleDataBuf); - return NULL; - } - int ret = readSampleFromCsvFileToMem(fp, - superTblInfo, sampleDataBuf); - if (0 != ret) { - tmfree(sampleDataBuf); - tmfclose(fp); + if (superTblInfo) { + if (0 != prepareSampleDataForSTable(superTblInfo)) + return NULL; + + if (superTblInfo->numberOfTblInOneSql > 0) { + syncWriteForNumberOfTblInOneSql(winfo, superTblInfo->sampleDataBuf); + tmfree(superTblInfo->sampleDataBuf); + superTblInfo->sampleDataBuf = NULL; return NULL; } } + + int samplePos = 0; - if (superTblInfo->numberOfTblInOneSql > 0) { - syncWriteForNumberOfTblInOneSql(winfo, fp, sampleDataBuf); - tmfree(sampleDataBuf); - tmfclose(fp); - return NULL; - } - - char* buffer = calloc(superTblInfo->maxSqlLen, 1); - if (NULL == buffer) { - printf("Failed to calloc %d Bytes, reason:%s\n", - superTblInfo->maxSqlLen, - strerror(errno)); - tmfree(sampleDataBuf); - tmfclose(fp); - return NULL; - } + int64_t lastPrintTime = taosGetTimestampMs(); + int64_t startTs = taosGetTimestampUs(); + int64_t endTs; + int insert_interval = superTblInfo?superTblInfo->insertInterval: + g_args.insert_interval; uint64_t st = 0; - uint64_t et = 0; + uint64_t et = 0xffffffff; - winfo->totalRowsInserted = 0; + winfo->totalInsertRows = 0; winfo->totalAffectedRows = 0; int sampleUsePos; - verbosePrint("%s() LN%d insertRows=%"PRId64"\n", __func__, __LINE__, superTblInfo->insertRows); - - for (uint32_t tID = winfo->start_table_id; tID <= winfo->end_table_id; - tID++) { + for (uint32_t tableSeq = winfo->start_table_id; tableSeq <= winfo->end_table_id; + tableSeq ++) { int64_t start_time = winfo->start_time; - for (int i = 0; i < superTblInfo->insertRows;) { - - int64_t tblInserted = i; + int64_t insertRows = (superTblInfo)?superTblInfo->insertRows:g_args.num_of_DPT; + verbosePrint("%s() LN%d insertRows=%"PRId64"\n", __func__, __LINE__, insertRows); - if (i > 0 && superTblInfo->insertInterval - && (superTblInfo->insertInterval > (et - st) )) { - int sleep_time = superTblInfo->insertInterval - (et -st); - printf("sleep: %d ms insert interval\n", sleep_time); - taosMsleep(sleep_time); // ms - } - - if (superTblInfo->insertInterval) { - st = taosGetTimestampMs(); + for (int64_t i = 0; i < insertRows;) { + if (insert_interval) { + st = taosGetTimestampUs(); } sampleUsePos = samplePos; - verbosePrint("%s() LN%d num_of_RPR=%d\n", __func__, __LINE__, g_args.num_of_RPR); - memset(buffer, 0, superTblInfo->maxSqlLen); - int len = 0; + int generated = generateDataBuffer(tableSeq, winfo, buffer, insertRows, + i, start_time, &sampleUsePos); + if (generated > 0) + i += generated; + else + goto free_and_statistics_2; - char *pstr = buffer; + int affectedRows = execInsert(winfo, buffer, generated); + if (affectedRows < 0) + goto free_and_statistics_2; - if (AUTO_CREATE_SUBTBL == superTblInfo->autoCreateTable) { - char* tagsValBuf = NULL; - if (0 == superTblInfo->tagSource) { - tagsValBuf = generateTagVaulesForStb(superTblInfo); - } else { - tagsValBuf = getTagValueFromTagSample( - superTblInfo, - tID % superTblInfo->tagSampleCount); - } - if (NULL == tagsValBuf) { - goto free_and_statistics_2; - } - - len += snprintf(pstr + len, - superTblInfo->maxSqlLen - len, - "insert into %s.%s%d using %s.%s tags %s values", - winfo->db_name, - superTblInfo->childTblPrefix, - tID, - winfo->db_name, - superTblInfo->sTblName, - tagsValBuf); - tmfree(tagsValBuf); - } else if (TBL_ALREADY_EXISTS == superTblInfo->childTblExists) { - len += snprintf(pstr + len, - superTblInfo->maxSqlLen - len, - "insert into %s.%s values", - winfo->db_name, - superTblInfo->childTblName + tID * TSDB_TABLE_NAME_LEN); - } else { - len += snprintf(pstr + len, - superTblInfo->maxSqlLen - len, - "insert into %s.%s%d values", - winfo->db_name, - superTblInfo->childTblPrefix, - tID); - } - int k; - for (k = 0; k < g_args.num_of_RPR;) { - int retLen = 0; - if (0 == strncasecmp(superTblInfo->dataSource, "sample", strlen("sample"))) { - retLen = getRowDataFromSample( - pstr + len, - superTblInfo->maxSqlLen - len, - start_time + superTblInfo->timeStampStep * i, - superTblInfo, - &sampleUsePos, - fp, - sampleDataBuf); - if (retLen < 0) { - goto free_and_statistics_2; - } - } else if (0 == strncasecmp(superTblInfo->dataSource, "rand", strlen("rand"))) { - int rand_num = rand_tinyint() % 100; - if (0 != superTblInfo->disorderRatio - && rand_num < superTblInfo->disorderRatio) { - int64_t d = start_time - rand() % superTblInfo->disorderRange; - retLen = generateRowData( - pstr + len, - superTblInfo->maxSqlLen - len, - d, - superTblInfo); - //printf("disorder rows, rand_num:%d, last ts:%"PRId64" current ts:%"PRId64"\n", rand_num, tmp_time, d); - } else { - retLen = generateRowData( - pstr + len, - superTblInfo->maxSqlLen - len, - start_time + superTblInfo->timeStampStep * i, - superTblInfo); - } - if (retLen < 0) { - goto free_and_statistics_2; - } - } - - len += retLen; - verbosePrint("%s() LN%d retLen=%d len=%d k=%d \nbuffer=%s\n", __func__, __LINE__, retLen, len, k, buffer); - - tblInserted++; - k++; - i++; - - if (tblInserted >= superTblInfo->insertRows) - break; - } - - winfo->totalRowsInserted += k; - - int64_t startTs = taosGetTimestampUs(); - int64_t endTs; - int affectedRows; - if (0 == strncasecmp(superTblInfo->insertMode, "taosc", strlen("taosc"))) { - verbosePrint("%s() LN%d %s\n", __func__, __LINE__, buffer); - affectedRows = queryDbExec(winfo->taos, buffer, INSERT_TYPE); - - if (0 > affectedRows){ - goto free_and_statistics_2; - } - } else { - verbosePrint("%s() LN%d %s\n", __func__, __LINE__, buffer); - int retCode = postProceSql(g_Dbs.host, g_Dbs.port, buffer); - - if (0 != retCode) { - printf("========restful return fail, threadID[%d]\n", winfo->threadID); - goto free_and_statistics_2; - } - - affectedRows = k; - } + winfo->totalInsertRows += generated; + winfo->totalAffectedRows += affectedRows; endTs = taosGetTimestampUs(); int64_t delay = endTs - startTs; @@ -4500,53 +4636,59 @@ static void* syncWriteWithStb(void *sarg) { winfo->cntDelay++; winfo->totalDelay += delay; - winfo->totalAffectedRows += affectedRows; - int64_t currentPrintTime = taosGetTimestampMs(); if (currentPrintTime - lastPrintTime > 30*1000) { printf("thread[%d] has currently inserted rows: %"PRId64 ", affected rows: %"PRId64 "\n", winfo->threadID, - winfo->totalRowsInserted, + winfo->totalInsertRows, winfo->totalAffectedRows); lastPrintTime = currentPrintTime; } - if (superTblInfo->insertInterval) { - et = taosGetTimestampMs(); - } - - if (tblInserted >= superTblInfo->insertRows) + if (i >= insertRows) break; + + if (insert_interval) { + et = taosGetTimestampUs(); + + if (insert_interval > ((et - st)/1000) ) { + int sleep_time = insert_interval - (et -st)/1000; + verbosePrint("%s() LN%d sleep: %d ms for insert interval\n", __func__, __LINE__, sleep_time); + taosMsleep(sleep_time); // ms + } + } } // num_of_DPT - if (tID == winfo->end_table_id) { - if (0 == strncasecmp( - superTblInfo->dataSource, "sample", strlen("sample"))) { + if ((tableSeq == winfo->end_table_id) && superTblInfo && + (0 == strncasecmp( + superTblInfo->dataSource, "sample", strlen("sample")))) { samplePos = sampleUsePos; - } - } - } // tID + } // tableSeq free_and_statistics_2: tmfree(buffer); - tmfree(sampleDataBuf); - tmfclose(fp); + if (superTblInfo) { + tmfree(superTblInfo->sampleDataBuf); + superTblInfo->sampleDataBuf = NULL; + } printf("====thread[%d] completed total inserted rows: %"PRId64 ", total affected rows: %"PRId64 "====\n", winfo->threadID, - winfo->totalRowsInserted, + winfo->totalInsertRows, winfo->totalAffectedRows); return NULL; } void callBack(void *param, TAOS_RES *res, int code) { threadInfo* winfo = (threadInfo*)param; + SSuperTable* superTblInfo = winfo->superTblInfo; - if (g_args.insert_interval) { - winfo->et = taosGetTimestampMs(); - if (winfo->et - winfo->st < 1000) { - taosMsleep(1000 - (winfo->et - winfo->st)); // ms + int insert_interval = superTblInfo?superTblInfo->insertInterval:g_args.insert_interval; + if (insert_interval) { + winfo->et = taosGetTimestampUs(); + if (((winfo->et - winfo->st)/1000) < insert_interval) { + taosMsleep(insert_interval - (winfo->et - winfo->st)/1000); // ms } } @@ -4568,14 +4710,14 @@ void callBack(void *param, TAOS_RES *res, int code) { } for (int i = 0; i < g_args.num_of_RPR; i++) { - int rand_num = rand() % 100; + int rand_num = taosRandom() % 100; if (0 != winfo->superTblInfo->disorderRatio && rand_num < winfo->superTblInfo->disorderRatio) { - int64_t d = winfo->lastTs - rand() % 1000000 + rand_num; + int64_t d = winfo->lastTs - taosRandom() % 1000000 + rand_num; //generateData(data, datatype, ncols_per_record, d, len_of_binary); (void)generateRowData(data, MAX_DATA_SIZE, d, winfo->superTblInfo); } else { - //generateData(data, datatype, ncols_per_record, tmp_time += 1000, len_of_binary); + //generateData(data, datatype, ncols_per_record, start_time += 1000, len_of_binary); (void)generateRowData(data, MAX_DATA_SIZE, winfo->lastTs += 1000, winfo->superTblInfo); } pstr += sprintf(pstr, "%s", data); @@ -4585,9 +4727,9 @@ void callBack(void *param, TAOS_RES *res, int code) { break; } } - - if (g_args.insert_interval) { - winfo->st = taosGetTimestampMs(); + + if (insert_interval) { + winfo->st = taosGetTimestampUs(); } taos_query_a(winfo->taos, buffer, callBack, winfo); free(buffer); @@ -4598,13 +4740,15 @@ void callBack(void *param, TAOS_RES *res, int code) { void *asyncWrite(void *sarg) { threadInfo *winfo = (threadInfo *)sarg; + SSuperTable* superTblInfo = winfo->superTblInfo; winfo->st = 0; winfo->et = 0; winfo->lastTs = winfo->start_time; - if (g_args.insert_interval) { - winfo->st = taosGetTimestampMs(); + int insert_interval = superTblInfo?superTblInfo->insertInterval:g_args.insert_interval; + if (insert_interval) { + winfo->st = taosGetTimestampUs(); } taos_query_a(winfo->taos, "show databases", callBack, winfo); @@ -4613,19 +4757,31 @@ void *asyncWrite(void *sarg) { return NULL; } -void startMultiThreadInsertData(int threads, char* db_name, char* precision, - SSuperTable* superTblInfo) { +static void startMultiThreadInsertData(int threads, char* db_name, + char* precision,SSuperTable* superTblInfo) { pthread_t *pids = malloc(threads * sizeof(pthread_t)); + assert(pids != NULL); + threadInfo *infos = malloc(threads * sizeof(threadInfo)); + assert(infos != NULL); + memset(pids, 0, threads * sizeof(pthread_t)); memset(infos, 0, threads * sizeof(threadInfo)); int ntables = 0; - if (superTblInfo) - ntables = superTblInfo->childTblCount; - else + if (superTblInfo) { + + if ((superTblInfo->childTblOffset >= 0) + && (superTblInfo->childTblLimit > 0)) { + + ntables = superTblInfo->childTblLimit; + } else { + ntables = superTblInfo->childTblCount; + } + } else { ntables = g_args.num_of_tables; + } int a = ntables / threads; if (a < 1) { @@ -4654,7 +4810,7 @@ void startMultiThreadInsertData(int threads, char* db_name, char* precision, } else if (0 == strncasecmp(precision, "us", 2)) { timePrec = TSDB_TIME_PRECISION_MICRO; } else { - printf("No support precision: %s\n", precision); + fprintf(stderr, "No support precision: %s\n", precision); exit(-1); } } @@ -4663,11 +4819,11 @@ void startMultiThreadInsertData(int threads, char* db_name, char* precision, if (superTblInfo) { if (0 == strncasecmp(superTblInfo->startTimestamp, "now", 3)) { start_time = taosGetTimestamp(timePrec); - } else { + } else { if (TSDB_CODE_SUCCESS != taosParseTime( - superTblInfo->startTimestamp, - &start_time, - strlen(superTblInfo->startTimestamp), + superTblInfo->startTimestamp, + &start_time, + strlen(superTblInfo->startTimestamp), timePrec, 0)) { printf("ERROR to parse time!\n"); exit(-1); @@ -4678,8 +4834,14 @@ void startMultiThreadInsertData(int threads, char* db_name, char* precision, } double start = getCurrentTime(); - - int last = 0; + + int last; + + if ((superTblInfo) && (superTblInfo->childTblOffset >= 0)) + last = superTblInfo->childTblOffset; + else + last = 0; + for (int i = 0; i < threads; i++) { threadInfo *t_info = infos + i; t_info->threadID = i; @@ -4693,7 +4855,7 @@ void startMultiThreadInsertData(int threads, char* db_name, char* precision, (0 == strncasecmp(superTblInfo->insertMode, "taosc", 5))) { //t_info->taos = taos; t_info->taos = taos_connect( - g_Dbs.host, g_Dbs.user, + g_Dbs.host, g_Dbs.user, g_Dbs.password, db_name, g_Dbs.port); if (NULL == t_info->taos) { printf("connect to server fail from insert sub thread, reason: %s\n", @@ -4717,16 +4879,12 @@ void startMultiThreadInsertData(int threads, char* db_name, char* precision, tsem_init(&(t_info->lock_sem), 0, 0); if (SYNC == g_Dbs.queryMode) { - if (superTblInfo) { - pthread_create(pids + i, NULL, syncWriteWithStb, t_info); - } else { - pthread_create(pids + i, NULL, syncWrite, t_info); - } - } else { + pthread_create(pids + i, NULL, syncWrite, t_info); + } else { pthread_create(pids + i, NULL, asyncWrite, t_info); } } - + for (int i = 0; i < threads; i++) { pthread_join(pids[i], NULL); } @@ -4745,13 +4903,13 @@ void startMultiThreadInsertData(int threads, char* db_name, char* precision, if (superTblInfo) { superTblInfo->totalAffectedRows += t_info->totalAffectedRows; - superTblInfo->totalRowsInserted += t_info->totalRowsInserted; + superTblInfo->totalInsertRows += t_info->totalInsertRows; } totalDelay += t_info->totalDelay; cntDelay += t_info->cntDelay; if (t_info->maxDelay > maxDelay) maxDelay = t_info->maxDelay; - if (t_info->minDelay < minDelay) minDelay = t_info->minDelay; + if (t_info->minDelay < minDelay) minDelay = t_info->minDelay; } cntDelay -= 1; @@ -4762,16 +4920,17 @@ void startMultiThreadInsertData(int threads, char* db_name, char* precision, double t = end - start; if (superTblInfo) { - printf("Spent %.4f seconds to insert rows: %"PRId64", affected rows: %"PRId64" with %d thread(s) into %s.%s. %2.f records/second\n\n", - t, superTblInfo->totalRowsInserted, + printf("Spent %.4f seconds to insert rows: %"PRId64", affected rows: %"PRId64" with %d thread(s) into %s.%s. %2.f records/second\n\n", + t, superTblInfo->totalInsertRows, superTblInfo->totalAffectedRows, threads, db_name, superTblInfo->sTblName, - superTblInfo->totalRowsInserted / t); - fprintf(g_fpOfInsertResult, "Spent %.4f seconds to insert rows: %"PRId64", affected rows: %"PRId64" with %d thread(s) into %s.%s. %2.f records/second\n\n", - t, superTblInfo->totalRowsInserted, + superTblInfo->totalInsertRows / t); + fprintf(g_fpOfInsertResult, + "Spent %.4f seconds to insert rows: %"PRId64", affected rows: %"PRId64" with %d thread(s) into %s.%s. %2.f records/second\n\n", + t, superTblInfo->totalInsertRows, superTblInfo->totalAffectedRows, threads, db_name, superTblInfo->sTblName, - superTblInfo->totalRowsInserted / t); + superTblInfo->totalInsertRows/ t); } printf("insert delay, avg: %10.6fms, max: %10.6fms, min: %10.6fms\n\n", @@ -4782,10 +4941,9 @@ void startMultiThreadInsertData(int threads, char* db_name, char* precision, //taos_close(taos); free(pids); - free(infos); + free(infos); } - void *readTable(void *sarg) { #if 1 threadInfo *rinfo = (threadInfo *)sarg; @@ -4930,7 +5088,7 @@ void *readMetric(void *sarg) { } -int insertTestProcess() { +static int insertTestProcess() { setupForAnsiEscape(); int ret = printfInsertMeta(); @@ -4944,15 +5102,15 @@ int insertTestProcess() { if (NULL == g_fpOfInsertResult) { fprintf(stderr, "Failed to open %s for save result\n", g_Dbs.resultFile); return -1; - } { - printfInsertMetaToFile(g_fpOfInsertResult); } + printfInsertMetaToFile(g_fpOfInsertResult); + if (!g_args.answer_yes) { printf("Press enter key to continue\n\n"); (void)getchar(); } - + init_rand_data(); // create database and super tables @@ -4962,7 +5120,7 @@ int insertTestProcess() { } // pretreatement - prePareSampleData(); + prepareSampleData(); double start; double end; @@ -4973,47 +5131,47 @@ int insertTestProcess() { end = getCurrentTime(); if (g_totalChildTables > 0) { - printf("Spent %.4f seconds to create %d tables with %d thread(s)\n\n", + printf("Spent %.4f seconds to create %d tables with %d thread(s)\n\n", end - start, g_totalChildTables, g_Dbs.threadCount); - fprintf(g_fpOfInsertResult, - "Spent %.4f seconds to create %d tables with %d thread(s)\n\n", + fprintf(g_fpOfInsertResult, + "Spent %.4f seconds to create %d tables with %d thread(s)\n\n", end - start, g_totalChildTables, g_Dbs.threadCount); } taosMsleep(1000); // create sub threads for inserting data //start = getCurrentTime(); - for (int i = 0; i < g_Dbs.dbCount; i++) { - if (g_Dbs.db[i].superTblCount > 0) { - for (int j = 0; j < g_Dbs.db[i].superTblCount; j++) { - SSuperTable* superTblInfo = &g_Dbs.db[i].superTbls[j]; - if (0 == g_Dbs.db[i].superTbls[j].insertRows) { - continue; - } - startMultiThreadInsertData( - g_Dbs.threadCount, - g_Dbs.db[i].dbName, - g_Dbs.db[i].dbCfg.precision, - superTblInfo); - } - } else { - startMultiThreadInsertData( - g_Dbs.threadCount, - g_Dbs.db[i].dbName, - g_Dbs.db[i].dbCfg.precision, - NULL); + for (int i = 0; i < g_Dbs.dbCount; i++) { + if (g_Dbs.db[i].superTblCount > 0) { + for (int j = 0; j < g_Dbs.db[i].superTblCount; j++) { + SSuperTable* superTblInfo = &g_Dbs.db[i].superTbls[j]; + if (0 == g_Dbs.db[i].superTbls[j].insertRows) { + continue; } + startMultiThreadInsertData( + g_Dbs.threadCount, + g_Dbs.db[i].dbName, + g_Dbs.db[i].dbCfg.precision, + superTblInfo); + } + } else { + startMultiThreadInsertData( + g_Dbs.threadCount, + g_Dbs.db[i].dbName, + g_Dbs.db[i].dbCfg.precision, + NULL); } + } //end = getCurrentTime(); - //int64_t totalRowsInserted = 0; + //int64_t totalInsertRows = 0; //int64_t totalAffectedRows = 0; //for (int i = 0; i < g_Dbs.dbCount; i++) { // for (int j = 0; j < g_Dbs.db[i].superTblCount; j++) { - // totalRowsInserted += g_Dbs.db[i].superTbls[j].totalRowsInserted; + // totalInsertRows+= g_Dbs.db[i].superTbls[j].totalInsertRows; // totalAffectedRows += g_Dbs.db[i].superTbls[j].totalAffectedRows; //} - //printf("Spent %.4f seconds to insert rows: %"PRId64", affected rows: %"PRId64" with %d thread(s)\n\n", end - start, totalRowsInserted, totalAffectedRows, g_Dbs.threadCount); + //printf("Spent %.4f seconds to insert rows: %"PRId64", affected rows: %"PRId64" with %d thread(s)\n\n", end - start, totalInsertRows, totalAffectedRows, g_Dbs.threadCount); postFreeResource(); return 0; @@ -5034,7 +5192,7 @@ void *superQueryProcess(void *sarg) { //printf("========sleep duration:%"PRId64 "========inserted rows:%d, table range:%d - %d\n", (1000 - (et - st)), i, winfo->start_table_id, winfo->end_table_id); } - st = taosGetTimestampMs(); + st = taosGetTimestampUs(); for (int i = 0; i < g_queryInfo.superQueryInfo.sqlCount; i++) { if (0 == strncasecmp(g_queryInfo.queryMode, "taosc", 5)) { int64_t t1 = taosGetTimestampUs(); @@ -5060,14 +5218,14 @@ void *superQueryProcess(void *sarg) { } } } - et = taosGetTimestampMs(); + et = taosGetTimestampUs(); printf("==thread[%"PRId64"] complete all sqls to specify tables once queries duration:%.6fs\n\n", taosGetSelfPthreadId(), (double)(et - st)/1000.0); } return NULL; } -void replaceSubTblName(char* inSql, char* outSql, int tblIndex) { +static void replaceSubTblName(char* inSql, char* outSql, int tblIndex) { char sourceString[32] = "xxxx"; char subTblName[MAX_TB_NAME_SIZE*3]; sprintf(subTblName, "%s.%s", @@ -5089,7 +5247,7 @@ void replaceSubTblName(char* inSql, char* outSql, int tblIndex) { //printf("3: %s\n", outSql); } -void *subQueryProcess(void *sarg) { +static void *subQueryProcess(void *sarg) { char sqlstr[1024]; threadInfo *winfo = (threadInfo *)sarg; int64_t st = 0; @@ -5100,7 +5258,7 @@ void *subQueryProcess(void *sarg) { //printf("========sleep duration:%"PRId64 "========inserted rows:%d, table range:%d - %d\n", (1000 - (et - st)), i, winfo->start_table_id, winfo->end_table_id); } - st = taosGetTimestampMs(); + st = taosGetTimestampUs(); for (int i = winfo->start_table_id; i <= winfo->end_table_id; i++) { for (int j = 0; j < g_queryInfo.subQueryInfo.sqlCount; j++) { memset(sqlstr,0,sizeof(sqlstr)); @@ -5114,12 +5272,12 @@ void *subQueryProcess(void *sarg) { selectAndGetResult(winfo->taos, sqlstr, tmpFile); } } - et = taosGetTimestampMs(); + et = taosGetTimestampUs(); printf("####thread[%"PRId64"] complete all sqls to allocate all sub-tables[%d - %d] once queries duration:%.4fs\n\n", taosGetSelfPthreadId(), winfo->start_table_id, winfo->end_table_id, - (double)(et - st)/1000.0); + (double)(et - st)/1000000.0); } return NULL; } @@ -5137,10 +5295,10 @@ static int queryTestProcess() { } if (0 != g_queryInfo.subQueryInfo.sqlCount) { - (void)getAllChildNameOfSuperTable(taos, - g_queryInfo.dbName, - g_queryInfo.subQueryInfo.sTblName, - &g_queryInfo.subQueryInfo.childTblName, + getAllChildNameOfSuperTable(taos, + g_queryInfo.dbName, + g_queryInfo.subQueryInfo.sTblName, + &g_queryInfo.subQueryInfo.childTblName, &g_queryInfo.subQueryInfo.childTblCount); } @@ -5157,7 +5315,7 @@ static int queryTestProcess() { threadInfo *infos = NULL; //==== create sub threads for query from specify table if (g_queryInfo.superQueryInfo.sqlCount > 0 && g_queryInfo.superQueryInfo.concurrent > 0) { - + pids = malloc(g_queryInfo.superQueryInfo.concurrent * sizeof(pthread_t)); infos = malloc(g_queryInfo.superQueryInfo.concurrent * sizeof(threadInfo)); if ((NULL == pids) || (NULL == infos)) { @@ -5165,14 +5323,14 @@ static int queryTestProcess() { taos_close(taos); exit(-1); } - - for (int i = 0; i < g_queryInfo.superQueryInfo.concurrent; i++) { + + for (int i = 0; i < g_queryInfo.superQueryInfo.concurrent; i++) { threadInfo *t_info = infos + i; - t_info->threadID = i; + t_info->threadID = i; if (0 == strncasecmp(g_queryInfo.queryMode, "taosc", 5)) { t_info->taos = taos; - + char sqlStr[MAX_TB_NAME_SIZE*2]; sprintf(sqlStr, "use %s", g_queryInfo.dbName); verbosePrint("%s() %d sqlStr: %s\n", __func__, __LINE__, sqlStr); @@ -5181,16 +5339,17 @@ static int queryTestProcess() { t_info->taos = NULL; } - pthread_create(pids + i, NULL, superQueryProcess, t_info); - } + pthread_create(pids + i, NULL, superQueryProcess, t_info); + } }else { g_queryInfo.superQueryInfo.concurrent = 0; } - + pthread_t *pidsOfSub = NULL; threadInfo *infosOfSub = NULL; //==== create sub threads for query from all sub table of the super table - if ((g_queryInfo.subQueryInfo.sqlCount > 0) && (g_queryInfo.subQueryInfo.threadCnt > 0)) { + if ((g_queryInfo.subQueryInfo.sqlCount > 0) + && (g_queryInfo.subQueryInfo.threadCnt > 0)) { pidsOfSub = malloc(g_queryInfo.subQueryInfo.threadCnt * sizeof(pthread_t)); infosOfSub = malloc(g_queryInfo.subQueryInfo.threadCnt * sizeof(threadInfo)); if ((NULL == pidsOfSub) || (NULL == infosOfSub)) { @@ -5198,7 +5357,7 @@ static int queryTestProcess() { taos_close(taos); exit(-1); } - + int ntables = g_queryInfo.subQueryInfo.childTblCount; int threads = g_queryInfo.subQueryInfo.threadCnt; @@ -5207,12 +5366,12 @@ static int queryTestProcess() { threads = ntables; a = 1; } - + int b = 0; if (threads != 0) { b = ntables % threads; } - + int last = 0; for (int i = 0; i < threads; i++) { threadInfo *t_info = infosOfSub + i; @@ -5406,7 +5565,7 @@ void *superSubscribeProcess(void *sarg) { } } taos_free_result(res); - + for (int i = 0; i < g_queryInfo.superQueryInfo.sqlCount; i++) { taos_unsubscribe(g_queryInfo.superQueryInfo.tsub[i], g_queryInfo.superQueryInfo.subscribeKeepProgress); @@ -5434,14 +5593,13 @@ static int subscribeTestProcess() { } if (0 != g_queryInfo.subQueryInfo.sqlCount) { - (void)getAllChildNameOfSuperTable(taos, + getAllChildNameOfSuperTable(taos, g_queryInfo.dbName, g_queryInfo.subQueryInfo.sTblName, &g_queryInfo.subQueryInfo.childTblName, &g_queryInfo.subQueryInfo.childTblCount); } - pthread_t *pids = NULL; threadInfo *infos = NULL; //==== create sub threads for query from super table @@ -5450,53 +5608,53 @@ static int subscribeTestProcess() { pids = malloc(g_queryInfo.superQueryInfo.concurrent * sizeof(pthread_t)); infos = malloc(g_queryInfo.superQueryInfo.concurrent * sizeof(threadInfo)); if ((NULL == pids) || (NULL == infos)) { - printf("malloc failed for create threads\n"); + printf("malloc failed for create threads\n"); taos_close(taos); exit(-1); } - - for (int i = 0; i < g_queryInfo.superQueryInfo.concurrent; i++) { + + for (int i = 0; i < g_queryInfo.superQueryInfo.concurrent; i++) { threadInfo *t_info = infos + i; t_info->threadID = i; t_info->taos = taos; pthread_create(pids + i, NULL, superSubscribeProcess, t_info); } } - + //==== create sub threads for query from sub table pthread_t *pidsOfSub = NULL; threadInfo *infosOfSub = NULL; - if ((g_queryInfo.subQueryInfo.sqlCount > 0) + if ((g_queryInfo.subQueryInfo.sqlCount > 0) && (g_queryInfo.subQueryInfo.threadCnt > 0)) { - pidsOfSub = malloc(g_queryInfo.subQueryInfo.threadCnt * + pidsOfSub = malloc(g_queryInfo.subQueryInfo.threadCnt * sizeof(pthread_t)); infosOfSub = malloc(g_queryInfo.subQueryInfo.threadCnt * sizeof(threadInfo)); if ((NULL == pidsOfSub) || (NULL == infosOfSub)) { - printf("malloc failed for create threads\n"); + printf("malloc failed for create threads\n"); taos_close(taos); exit(-1); } - + int ntables = g_queryInfo.subQueryInfo.childTblCount; int threads = g_queryInfo.subQueryInfo.threadCnt; - + int a = ntables / threads; if (a < 1) { threads = ntables; a = 1; } - + int b = 0; if (threads != 0) { b = ntables % threads; } - + int last = 0; - for (int i = 0; i < threads; i++) { + for (int i = 0; i < threads; i++) { threadInfo *t_info = infosOfSub + i; t_info->threadID = i; - + t_info->start_table_id = last; t_info->end_table_id = i < b ? last + a : last + a - 1; t_info->taos = taos; @@ -5504,20 +5662,20 @@ static int subscribeTestProcess() { } g_queryInfo.subQueryInfo.threadCnt = threads; } - + for (int i = 0; i < g_queryInfo.superQueryInfo.concurrent; i++) { pthread_join(pids[i], NULL); } tmfree((char*)pids); - tmfree((char*)infos); + tmfree((char*)infos); for (int i = 0; i < g_queryInfo.subQueryInfo.threadCnt; i++) { pthread_join(pidsOfSub[i], NULL); } tmfree((char*)pidsOfSub); - tmfree((char*)infosOfSub); + tmfree((char*)infosOfSub); taos_close(taos); return 0; } @@ -5610,9 +5768,9 @@ void setParaFromArg(){ tstrncpy(g_Dbs.db[0].superTbls[0].insertMode, "taosc", MAX_TB_NAME_SIZE); tstrncpy(g_Dbs.db[0].superTbls[0].startTimestamp, "2017-07-14 10:40:00.000", MAX_TB_NAME_SIZE); - g_Dbs.db[0].superTbls[0].timeStampStep = 10; + g_Dbs.db[0].superTbls[0].timeStampStep = DEFAULT_TIMESTAMP_STEP; - g_Dbs.db[0].superTbls[0].insertRows = g_args.num_of_DPT; + g_Dbs.db[0].superTbls[0].insertRows = g_args.num_of_DPT; g_Dbs.db[0].superTbls[0].maxSqlLen = TSDB_PAYLOAD_SIZE; g_Dbs.db[0].superTbls[0].columnCount = 0; @@ -5756,14 +5914,7 @@ static void testMetaFile() { } } -static void testCmdLine() { - - g_args.test_mode = INSERT_MODE; - insertTestProcess(); - - if (g_Dbs.insert_only) - return; - +static void queryResult() { // select if (false == g_Dbs.insert_only) { // query data @@ -5809,6 +5960,17 @@ static void testCmdLine() { } } +static void testCmdLine() { + + g_args.test_mode = INSERT_MODE; + insertTestProcess(); + + if (g_Dbs.insert_only) + return; + else + queryResult(); +} + int main(int argc, char *argv[]) { parse_args(argc, argv, &g_args); diff --git a/src/os/inc/osTime.h b/src/os/inc/osTime.h index b20ccadadb22a04733d97bd19b919660ee677d0d..2c50e7eeabd934a9d88f32d4b3bdc29d41d699b1 100644 --- a/src/os/inc/osTime.h +++ b/src/os/inc/osTime.h @@ -72,6 +72,11 @@ typedef struct SInterval { int64_t offset; } SInterval; +typedef struct SSessionWindow { + int64_t gap; // gap between two session window(in microseconds) + int32_t primaryColId; // primary timestamp column +} SSessionWindow; + int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision); int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precision); int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t interval, char unit, int32_t precision); diff --git a/src/plugins/http/inc/httpJson.h b/src/plugins/http/inc/httpJson.h index fcb74253b99c0cd8921d981e3ba7a4a027bf2b31..4d182d0132528367a8e25b8f72329331be2cb5c5 100644 --- a/src/plugins/http/inc/httpJson.h +++ b/src/plugins/http/inc/httpJson.h @@ -63,9 +63,11 @@ void httpJsonString(JsonBuf* buf, char* sVal, int32_t len); void httpJsonOriginString(JsonBuf* buf, char* sVal, int32_t len); void httpJsonStringForTransMean(JsonBuf* buf, char* SVal, int32_t maxLen); void httpJsonInt64(JsonBuf* buf, int64_t num); +void httpJsonUInt64(JsonBuf* buf, uint64_t num); void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us); void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, bool us); void httpJsonInt(JsonBuf* buf, int32_t num); +void httpJsonUInt(JsonBuf* buf, uint32_t num); void httpJsonFloat(JsonBuf* buf, float num); void httpJsonDouble(JsonBuf* buf, double num); void httpJsonNull(JsonBuf* buf); diff --git a/src/plugins/http/src/httpJson.c b/src/plugins/http/src/httpJson.c index b120496898c12e4f189337387a5f9fad1fadccbe..19166e720f0e517ccd1a793742836ae4bc245bd1 100644 --- a/src/plugins/http/src/httpJson.c +++ b/src/plugins/http/src/httpJson.c @@ -256,6 +256,12 @@ void httpJsonInt64(JsonBuf* buf, int64_t num) { buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%" PRId64, num); } +void httpJsonUInt64(JsonBuf* buf, uint64_t num) { + httpJsonItemToken(buf); + httpJsonTestBuf(buf, MAX_NUM_STR_SZ); + buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%" PRIu64, num); +} + void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us) { char ts[35] = {0}; struct tm* ptm; @@ -303,6 +309,12 @@ void httpJsonInt(JsonBuf* buf, int32_t num) { buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%d", num); } +void httpJsonUInt(JsonBuf* buf, uint32_t num) { + httpJsonItemToken(buf); + httpJsonTestBuf(buf, MAX_NUM_STR_SZ); + buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%u", num); +} + void httpJsonFloat(JsonBuf* buf, float num) { httpJsonItemToken(buf); httpJsonTestBuf(buf, MAX_NUM_STR_SZ); diff --git a/src/plugins/http/src/httpParser.c b/src/plugins/http/src/httpParser.c index 669b352ab65b7efcdacd98ae16ca59204185efd3..18cea56cfe6e0d1e8a4f833a920e5473536e1c7e 100644 --- a/src/plugins/http/src/httpParser.c +++ b/src/plugins/http/src/httpParser.c @@ -110,7 +110,7 @@ static void httpCleanupString(HttpString *str) { static int32_t httpAppendString(HttpString *str, const char *s, int32_t len) { if (str->size == 0) { str->pos = 0; - str->size = len; + str->size = len + 1; str->str = malloc(str->size); } else if (str->pos + len + 1 >= str->size) { str->size += len; diff --git a/src/plugins/http/src/httpRestJson.c b/src/plugins/http/src/httpRestJson.c index 61a5a361c4865d6b3dbac73773cc0b8cfc562e56..60c23e603e5107e858db2138d1aed87d5f8a5e1b 100644 --- a/src/plugins/http/src/httpRestJson.c +++ b/src/plugins/http/src/httpRestJson.c @@ -162,6 +162,18 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, case TSDB_DATA_TYPE_BIGINT: httpJsonInt64(jsonBuf, *((int64_t *)row[i])); break; + case TSDB_DATA_TYPE_UTINYINT: + httpJsonUInt(jsonBuf, *((uint8_t *)row[i])); + break; + case TSDB_DATA_TYPE_USMALLINT: + httpJsonUInt(jsonBuf, *((uint16_t *)row[i])); + break; + case TSDB_DATA_TYPE_UINT: + httpJsonUInt(jsonBuf, *((uint32_t *)row[i])); + break; + case TSDB_DATA_TYPE_UBIGINT: + httpJsonUInt64(jsonBuf, *((uint64_t *)row[i])); + break; case TSDB_DATA_TYPE_FLOAT: httpJsonFloat(jsonBuf, GET_FLOAT_VAL(row[i])); break; diff --git a/src/query/inc/qAggMain.h b/src/query/inc/qAggMain.h index 7122f63593c64a6bffd6f3d53cd8ef21eca800ff..c59067c4b357aa084cd6f31c39f3038075d18a82 100644 --- a/src/query/inc/qAggMain.h +++ b/src/query/inc/qAggMain.h @@ -26,6 +26,7 @@ extern "C" { #include "taosdef.h" #include "trpc.h" #include "tvariant.h" +#include "tsdb.h" #define TSDB_FUNC_INVALID_ID -1 #define TSDB_FUNC_COUNT 0 @@ -70,15 +71,17 @@ extern "C" { #define TSDB_FUNC_AVG_IRATE 34 #define TSDB_FUNC_TID_TAG 35 -#define TSDB_FUNC_HISTOGRAM 36 -#define TSDB_FUNC_HLL 37 -#define TSDB_FUNC_MODE 38 -#define TSDB_FUNC_SAMPLE 39 -#define TSDB_FUNC_CEIL 40 -#define TSDB_FUNC_FLOOR 41 -#define TSDB_FUNC_ROUND 42 -#define TSDB_FUNC_MAVG 43 -#define TSDB_FUNC_CSUM 44 +#define TSDB_FUNC_BLKINFO 36 + +#define TSDB_FUNC_HISTOGRAM 37 +#define TSDB_FUNC_HLL 38 +#define TSDB_FUNC_MODE 39 +#define TSDB_FUNC_SAMPLE 40 +#define TSDB_FUNC_CEIL 41 +#define TSDB_FUNC_FLOOR 42 +#define TSDB_FUNC_ROUND 43 +#define TSDB_FUNC_MAVG 44 +#define TSDB_FUNC_CSUM 45 #define TSDB_FUNCSTATE_SO 0x1u // single output @@ -214,13 +217,14 @@ typedef struct SAggFunctionInfo { void (*xFinalize)(SQLFunctionCtx *pCtx); void (*mergeFunc)(SQLFunctionCtx *pCtx); - int32_t (*dataReqFunc)(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, int32_t colId); + int32_t (*dataReqFunc)(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId); } SAggFunctionInfo; #define GET_RES_INFO(ctx) ((ctx)->resultInfo) int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionId, int32_t param, int16_t *type, int16_t *len, int32_t *interBytes, int16_t extLength, bool isSuperTable); +int32_t isValidFunction(const char* name, int32_t len); #define IS_STREAM_QUERY_VALID(x) (((x)&TSDB_FUNCSTATE_STREAM) != 0) #define IS_MULTIOUTPUT(x) (((x)&TSDB_FUNCSTATE_MO) != 0) @@ -242,12 +246,16 @@ typedef struct STwaInfo { STimeWindow win; } STwaInfo; +struct SBufferWriter; +void blockDistInfoToBinary(STableBlockDist* pDist, struct SBufferWriter* bw); +void blockDistInfoFromBinary(const char* data, int32_t len, STableBlockDist* pDist); + /* global sql function array */ extern struct SAggFunctionInfo aAggs[]; extern int32_t functionCompatList[]; // compatible check array list -bool topbot_datablock_filter(SQLFunctionCtx *pCtx, int32_t functionId, const char *minval, const char *maxval); +bool topbot_datablock_filter(SQLFunctionCtx *pCtx, const char *minval, const char *maxval); /** * the numOfRes should be kept, since it may be used later @@ -258,14 +266,14 @@ bool topbot_datablock_filter(SQLFunctionCtx *pCtx, int32_t functionId, const cha (_r)->initialized = false; \ } while (0) -static FORCE_INLINE void initResultInfo(SResultRowCellInfo *pResInfo, uint32_t bufLen) { +static FORCE_INLINE void initResultInfo(SResultRowCellInfo *pResInfo, int32_t bufLen) { pResInfo->initialized = true; // the this struct has been initialized flag pResInfo->complete = false; pResInfo->hasResult = false; pResInfo->numOfRes = 0; - memset(GET_ROWCELL_INTERBUF(pResInfo), 0, (size_t)bufLen); + memset(GET_ROWCELL_INTERBUF(pResInfo), 0, bufLen); } #ifdef __cplusplus diff --git a/src/query/inc/qExecutor.h b/src/query/inc/qExecutor.h index ec1261da0a45ad24985ebf51b9b16c2acfad7709..3615c32a905a87f7a5ed604bcd2e86baa3a5ee77 100644 --- a/src/query/inc/qExecutor.h +++ b/src/query/inc/qExecutor.h @@ -12,8 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef TDENGINE_QUERYEXECUTOR_H -#define TDENGINE_QUERYEXECUTOR_H +#ifndef TDENGINE_QEXECUTOR_H +#define TDENGINE_QEXECUTOR_H #include "os.h" @@ -37,30 +37,24 @@ typedef int32_t (*__block_search_fn_t)(char* data, int32_t num, int64_t key, int #define Q_STATUS_EQUAL(p, s) (((p) & (s)) != 0u) #define QUERY_IS_ASC_QUERY(q) (GET_FORWARD_DIRECTION_FACTOR((q)->order.order) == QUERY_ASC_FORWARD_STEP) -#define SET_STABLE_QUERY_OVER(_q) ((_q)->tableIndex = (int32_t)((_q)->tableqinfoGroupInfo.numOfTables)) -#define IS_STASBLE_QUERY_OVER(_q) ((_q)->tableIndex >= (int32_t)((_q)->tableqinfoGroupInfo.numOfTables)) - #define GET_TABLEGROUP(q, _index) ((SArray*) taosArrayGetP((q)->tableqinfoGroupInfo.pGroupList, (_index))) +#define GET_NUM_OF_RESULTS(_r) (((_r)->outputBuf) == NULL? 0:((_r)->outputBuf)->info.rows) + enum { // when query starts to execute, this status will set QUERY_NOT_COMPLETED = 0x1u, - /* result output buffer is full, current query is paused. - * this status is only exist in group-by clause and diff/add/division/multiply/ query. - */ - QUERY_RESBUF_FULL = 0x2u, - /* query is over * 1. this status is used in one row result query process, e.g., count/sum/first/last/ avg...etc. * 2. when all data within queried time window, it is also denoted as query_completed */ - QUERY_COMPLETED = 0x4u, + QUERY_COMPLETED = 0x2u, /* when the result is not completed return to client, this status will be * usually used in case of interval query with interpolation option */ - QUERY_OVER = 0x8u, + QUERY_OVER = 0x4u, }; typedef struct SResultRowPool { @@ -86,13 +80,13 @@ typedef struct SSqlGroupbyExpr { typedef struct SResultRow { int32_t pageId; // pageId & rowId is the position of current result in disk-based output buffer - int32_t rowId:29; // row index in buffer page + int32_t offset:29; // row index in buffer page bool startInterp; // the time window start timestamp has done the interpolation already. bool endInterp; // the time window end timestamp has done the interpolation already. bool closed; // this result status: closed or opened uint32_t numOfRows; // number of rows of current time window SResultRowCellInfo* pCellInfo; // For each result column, there is a resultInfo - union {STimeWindow win; char* key;}; // start key of current time window + union {STimeWindow win; char* key;}; // start key of current result row } SResultRow; typedef struct SGroupResInfo { @@ -106,12 +100,11 @@ typedef struct SGroupResInfo { * If the number of generated results is greater than this value, * query query will be halt and return results to client immediate. */ -typedef struct SResultRec { +typedef struct SRspResultInfo { int64_t total; // total generated result size in rows - int64_t rows; // current result set size in rows - int64_t capacity; // capacity of current result output buffer + int32_t capacity; // capacity of current result output buffer int32_t threshold; // result size threshold in rows. -} SResultRec; +} SRspResultInfo; typedef struct SResultRowInfo { SResultRow** pResult; // result list @@ -138,7 +131,6 @@ typedef struct SSingleColumnFilterInfo { typedef struct STableQueryInfo { TSKEY lastKey; int32_t groupIndex; // group id in table list - int16_t queryRangeSet; // denote if the query range is set, only available for interval query tVariant tag; STimeWindow win; STSCursor cur; @@ -179,82 +171,136 @@ typedef struct { SArray* pResult; // SArray } SInterResult; +typedef struct SSDataBlock { + SDataStatis *pBlockStatis; + SArray *pDataBlock; + SDataBlockInfo info; +} SSDataBlock; + typedef struct SQuery { + SLimitVal limit; + + bool stableQuery; // super table query or not + bool topBotQuery; // TODO used bitwise flag + bool groupbyColumn; // denote if this is a groupby normal column query + bool hasTagResults; // if there are tag values in final result or not + bool timeWindowInterpo;// if the time window start/end required interpolation + bool queryBlockDist; // if query data block distribution + bool stabledev; // super table stddev query + int32_t interBufSize; // intermediate buffer sizse + + SOrderVal order; int16_t numOfCols; int16_t numOfTags; - SOrderVal order; + STimeWindow window; SInterval interval; + SSessionWindow sw; int16_t precision; int16_t numOfOutput; int16_t fillType; int16_t checkResultBuf; // check if the buffer is full during scan each block - SLimitVal limit; int32_t srcRowSize; // todo extract struct int32_t resultRowSize; + int32_t intermediateResultRowSize; // intermediate result row size, in case of top-k query. int32_t maxSrcColumnSize; int32_t tagLen; // tag value length of current query - SSqlGroupbyExpr* pGroupbyExpr; SExprInfo* pExpr1; SExprInfo* pExpr2; int32_t numOfExpr2; - SColumnInfo* colList; SColumnInfo* tagColList; int32_t numOfFilterCols; int64_t* fillVal; - uint32_t status; // query status - SResultRec rec; - int32_t pos; - tFilePage** sdata; - STableQueryInfo* current; - int32_t numOfCheckedBlocks; // number of check data blocks - SOrderedPrjQueryInfo prjInfo; // limit value for each vgroup, only available in global order projection query. SSingleColumnFilterInfo* pFilterInfo; + + STableQueryInfo* current; + void* tsdb; + SMemRef memRef; + STableGroupInfo tableGroupInfo; // table list SArray + int32_t vgId; } SQuery; +typedef SSDataBlock* (*__operator_fn_t)(void* param); +typedef void (*__optr_cleanup_fn_t)(void* param, int32_t num); + +struct SOperatorInfo; + typedef struct SQueryRuntimeEnv { - jmp_buf env; - SQuery* pQuery; - SQLFunctionCtx* pCtx; - int32_t numOfRowsPerPage; - uint16_t* offset; - uint16_t scanFlag; // denotes reversed scan of data or not - SFillInfo* pFillInfo; - SResultRowInfo resultRowInfo; - - SQueryCostInfo summary; - void* pQueryHandle; - void* pSecQueryHandle; // another thread for - bool stableQuery; // super table query or not - bool topBotQuery; // TODO used bitwise flag - bool groupbyColumn; // denote if this is a groupby normal column query - bool hasTagResults; // if there are tag values in final result or not - bool timeWindowInterpo;// if the time window start/end required interpolation - bool queryWindowIdentical; // all query time windows are identical for all tables in one group - bool queryBlockDist; // if query data block distribution - bool stabledev; // super table stddev query - int32_t interBufSize; // intermediate buffer sizse - int32_t prevGroupId; // previous executed group id - SDiskbasedResultBuf* pResultBuf; // query result buffer based on blocked-wised disk file - SHashObj* pResultRowHashTable; // quick locate the window object for each result - char* keyBuf; // window key buffer - SResultRowPool* pool; // window result object pool - - int32_t* rowCellInfoOffset;// offset value for each row result cell info - char** prevRow; - - SArray* prevResult; // intermediate result, SArray - STSBuf* pTsBuf; // timestamp filter list - STSCursor cur; - - char* tagVal; // tag value of current data block - SArithmeticSupport *sasArray; + jmp_buf env; + SQuery* pQuery; + uint32_t status; // query status + void* qinfo; + uint8_t scanFlag; // denotes reversed scan of data or not + void* pQueryHandle; + + int32_t prevGroupId; // previous executed group id + SDiskbasedResultBuf* pResultBuf; // query result buffer based on blocked-wised disk file + SHashObj* pResultRowHashTable; // quick locate the window object for each result + char* keyBuf; // window key buffer + SResultRowPool* pool; // window result object pool + char** prevRow; + + SArray* prevResult; // intermediate result, SArray + STSBuf* pTsBuf; // timestamp filter list + STSCursor cur; + + char* tagVal; // tag value of current data block + SArithmeticSupport *sasArray; + + SSDataBlock *outputBuf; + STableGroupInfo tableqinfoGroupInfo; // this is a group array list, including SArray structure + struct SOperatorInfo *proot; + struct SOperatorInfo *pTableScanner; // table scan operator + SGroupResInfo groupResInfo; + int64_t currentOffset; // dynamic offset value + + SRspResultInfo resultInfo; + SHashObj *pTableRetrieveTsMap; } SQueryRuntimeEnv; +enum { + OP_IN_EXECUTING = 1, + OP_RES_TO_RETURN = 2, + OP_EXEC_DONE = 3, +}; + +enum OPERATOR_TYPE_E { + OP_TableScan = 1, + OP_DataBlocksOptScan = 2, + OP_TableSeqScan = 3, + OP_TagScan = 4, + OP_TableBlockInfoScan= 5, + OP_Aggregate = 6, + OP_Arithmetic = 7, + OP_Groupby = 8, + OP_Limit = 9, + OP_Offset = 10, + OP_TimeWindow = 11, + OP_SessionWindow = 12, + OP_Fill = 13, + OP_MultiTableAggregate = 14, + OP_MultiTableTimeInterval = 15, +}; + +typedef struct SOperatorInfo { + uint8_t operatorType; + bool blockingOptr; // block operator or not + uint8_t status; // denote if current operator is completed + int32_t numOfOutput; // number of columns of the current operator results + char *name; // name, used to show the query execution plan + void *info; // extension attribution + SExprInfo *pExpr; + SQueryRuntimeEnv *pRuntimeEnv; + + struct SOperatorInfo *upstream; + __operator_fn_t exec; + __optr_cleanup_fn_t cleanup; +} SOperatorInfo; + enum { QUERY_RESULT_NOT_READY = 1, QUERY_RESULT_READY = 2, @@ -262,23 +308,11 @@ enum { typedef struct SQInfo { void* signature; - int32_t code; // error code to returned to client - int64_t owner; // if it is in execution - void* tsdb; - SMemRef memRef; - int32_t vgId; - STableGroupInfo tableGroupInfo; // table list SArray - STableGroupInfo tableqinfoGroupInfo; // this is a group array list, including SArray structure - SQueryRuntimeEnv runtimeEnv; - SHashObj* arrTableIdInfo; - int32_t groupIndex; + int32_t code; // error code to returned to client + int64_t owner; // if it is in execution - /* - * the query is executed position on which meter of the whole list. - * when the index reaches the last one of the list, it means the query is completed. - */ - int32_t tableIndex; - SGroupResInfo groupResInfo; + SQueryRuntimeEnv runtimeEnv; + SQuery query; void* pBuf; // allocated buffer for STableQueryInfo, sizeof(STableQueryInfo)*numOfTables; pthread_mutex_t lock; // used to synchronize the rsp/query threads @@ -287,6 +321,7 @@ typedef struct SQInfo { void* rspContext; // response context int64_t startExecTs; // start to exec timestamp char* sql; // query sql string + SQueryCostInfo summary; } SQInfo; typedef struct SQueryParam { @@ -305,10 +340,93 @@ typedef struct SQueryParam { SSqlGroupbyExpr *pGroupbyExpr; } SQueryParam; +typedef struct STableScanInfo { + void *pQueryHandle; + int32_t numOfBlocks; + int32_t numOfSkipped; + int32_t numOfBlockStatis; + int64_t numOfRows; + + int32_t order; // scan order + int32_t times; // repeat counts + int32_t current; + int32_t reverseTimes; // 0 by default + + SQLFunctionCtx *pCtx; // next operator query context + SResultRowInfo *pResultRowInfo; + int32_t *rowCellInfoOffset; + SExprInfo *pExpr; + SSDataBlock block; + bool loadExternalRows; // load external rows (prev & next rows) + int32_t numOfOutput; + int64_t elapsedTime; + + int32_t tableIndex; +} STableScanInfo; + +typedef struct STagScanInfo { + SColumnInfo* pCols; + SSDataBlock* pRes; + int32_t totalTables; + int32_t currentIndex; +} STagScanInfo; + +typedef struct SOptrBasicInfo { + SResultRowInfo resultRowInfo; + int32_t *rowCellInfoOffset; // offset value for each row result cell info + SQLFunctionCtx *pCtx; + SSDataBlock *pRes; +} SOptrBasicInfo; + +typedef struct SOptrBasicInfo STableIntervalOperatorInfo; + +typedef struct SAggOperatorInfo { + SOptrBasicInfo binfo; + uint32_t seed; +} SAggOperatorInfo; + +typedef struct SArithOperatorInfo { + SOptrBasicInfo binfo; + int32_t bufCapacity; + uint32_t seed; +} SArithOperatorInfo; + +typedef struct SLimitOperatorInfo { + int64_t limit; + int64_t total; +} SLimitOperatorInfo; + +typedef struct SOffsetOperatorInfo { + int64_t offset; +} SOffsetOperatorInfo; + +typedef struct SFillOperatorInfo { + SFillInfo *pFillInfo; + SSDataBlock *pRes; + int64_t totalInputRows; +} SFillOperatorInfo; + +typedef struct SGroupbyOperatorInfo { + SOptrBasicInfo binfo; + int32_t colIndex; + char *prevData; // previous group by value +} SGroupbyOperatorInfo; + +typedef struct SSWindowOperatorInfo { + SOptrBasicInfo binfo; + STimeWindow curWindow; // current time window + TSKEY prevTs; // previous timestamp + int32_t numOfRows; // number of rows + int32_t start; // start row index +} SSWindowOperatorInfo; + void freeParam(SQueryParam *param); int32_t convertQueryMsg(SQueryTableMsg *pQueryMsg, SQueryParam* param); int32_t createQueryFuncExprFromMsg(SQueryTableMsg *pQueryMsg, int32_t numOfOutput, SExprInfo **pExprInfo, SSqlFuncMsg **pExprMsg, SColumnInfo* pTagCols); +int32_t createIndirectQueryFuncExprFromMsg(SQueryTableMsg *pQueryMsg, int32_t numOfOutput, SExprInfo **pExprInfo, + SSqlFuncMsg **pExprMsg, SExprInfo *prevExpr); + SSqlGroupbyExpr *createGroupbyExprFromMsg(SQueryTableMsg *pQueryMsg, SColIndex *pColIndex, int32_t *code); SQInfo *createQInfoImpl(SQueryTableMsg *pQueryMsg, SSqlGroupbyExpr *pGroupbyExpr, SExprInfo *pExprs, SExprInfo *pSecExprs, STableGroupInfo *pTableGroupInfo, SColumnInfo* pTagCols, bool stableQuery, char* sql); @@ -318,13 +436,9 @@ void freeColumnFilterInfo(SColumnFilterInfo* pFilter, int32_t numOfFilters); bool isQueryKilled(SQInfo *pQInfo); int32_t checkForQueryBuf(size_t numOfTables); bool doBuildResCheck(SQInfo* pQInfo); -void setQueryStatus(SQuery *pQuery, int8_t status); +void setQueryStatus(SQueryRuntimeEnv *pRuntimeEnv, int8_t status); bool onlyQueryTags(SQuery* pQuery); -void buildTagQueryResult(SQInfo *pQInfo); -void stableQueryImpl(SQInfo *pQInfo); -void buildTableBlockDistResult(SQInfo *pQInfo); -void tableQueryImpl(SQInfo *pQInfo); bool isValidQInfo(void *param); int32_t doDumpQueryResult(SQInfo *pQInfo, char *data); @@ -336,4 +450,4 @@ void freeQInfo(SQInfo *pQInfo); int32_t getMaximumIdleDurationSec(); -#endif // TDENGINE_QUERYEXECUTOR_H +#endif // TDENGINE_QEXECUTOR_H diff --git a/src/query/inc/qFill.h b/src/query/inc/qFill.h index aa6df9279acb9e3cecf20d71f726974ee89a4030..00ac86caf4a079e01ad239496370d97ac28e84f2 100644 --- a/src/query/inc/qFill.h +++ b/src/query/inc/qFill.h @@ -24,6 +24,8 @@ extern "C" { #include "qExtbuffer.h" #include "taosdef.h" +struct SSDataBlock; + typedef struct { STColumn col; // column info int16_t functionId; // sql function id @@ -78,7 +80,7 @@ void* taosDestroyFillInfo(SFillInfo *pFillInfo); void taosFillSetStartInfo(SFillInfo* pFillInfo, int32_t numOfRows, TSKEY endKey); -void taosFillSetDataBlockFromFilePage(SFillInfo* pFillInfo, const tFilePage** pInput); +void taosFillSetInputDataBlock(SFillInfo* pFillInfo, const struct SSDataBlock* pInput); void taosFillCopyInputDataFromOneFilePage(SFillInfo* pFillInfo, const tFilePage* pInput); @@ -88,7 +90,7 @@ int64_t getNumOfResultsAfterFillGap(SFillInfo* pFillInfo, int64_t ekey, int32_t int32_t taosGetLinearInterpolationVal(SPoint* point, int32_t outputType, SPoint* point1, SPoint* point2, int32_t inputType); -int64_t taosFillResultDataBlock(SFillInfo* pFillInfo, tFilePage** output, int32_t capacity); +int64_t taosFillResultDataBlock(SFillInfo* pFillInfo, void** output, int32_t capacity); #ifdef __cplusplus } diff --git a/src/query/inc/qResultbuf.h b/src/query/inc/qResultbuf.h index 704df9f3f29cc0653c353ed194e02e72cb4c5fb2..e46af8c29891cf52fbbac97c6afb42e0d0571215 100644 --- a/src/query/inc/qResultbuf.h +++ b/src/query/inc/qResultbuf.h @@ -55,7 +55,6 @@ typedef struct SResultBufStatis { } SResultBufStatis; typedef struct SDiskbasedResultBuf { - int32_t numOfRowsPerPage; int32_t numOfPages; int64_t totalBufSize; int64_t fileSize; // disk file size @@ -77,7 +76,7 @@ typedef struct SDiskbasedResultBuf { SResultBufStatis statis; } SDiskbasedResultBuf; -#define DEFAULT_INTERN_BUF_PAGE_SIZE (256L) // in bytes +#define DEFAULT_INTERN_BUF_PAGE_SIZE (1024L) // in bytes #define PAGE_INFO_INITIALIZER (SPageDiskInfo){-1, -1} /** @@ -89,8 +88,7 @@ typedef struct SDiskbasedResultBuf { * @param handle * @return */ -int32_t createDiskbasedResultBuffer(SDiskbasedResultBuf** pResultBuf, int32_t rowSize, int32_t pagesize, - int32_t inMemBufSize, const void* handle); +int32_t createDiskbasedResultBuffer(SDiskbasedResultBuf** pResultBuf, int32_t pagesize, int32_t inMemBufSize, const void* handle); /** * @@ -101,13 +99,6 @@ int32_t createDiskbasedResultBuffer(SDiskbasedResultBuf** pResultBuf, int32_t ro */ tFilePage* getNewDataBuf(SDiskbasedResultBuf* pResultBuf, int32_t groupId, int32_t* pageId); -/** - * - * @param pResultBuf - * @return - */ -size_t getNumOfRowsPerPage(const SDiskbasedResultBuf* pResultBuf); - /** * * @param pResultBuf diff --git a/src/query/inc/qSqlparser.h b/src/query/inc/qSqlparser.h index 33348c85652a9d207e58c6f83346c4231df56962..3ce81787f05b7336d25df39fbd5cad75685e1527 100644 --- a/src/query/inc/qSqlparser.h +++ b/src/query/inc/qSqlparser.h @@ -27,6 +27,23 @@ extern "C" { #include "tvariant.h" #define ParseTOKENTYPE SStrToken + +#define NON_ARITHMEIC_EXPR 0 +#define NORMAL_ARITHMETIC 1 +#define AGG_ARIGHTMEIC 2 + +enum SQL_NODE_TYPE { + SQL_NODE_TABLE_COLUMN= 1, + SQL_NODE_SQLFUNCTION = 2, + SQL_NODE_VALUE = 3, + SQL_NODE_EXPR = 4, +}; + +enum SQL_NODE_FROM_TYPE { + SQL_NODE_FROM_SUBQUERY = 1, + SQL_NODE_FROM_NAMELIST = 2, +}; + extern char tTokenTypeSwitcher[13]; #define toTSDBType(x) \ @@ -38,227 +55,235 @@ extern char tTokenTypeSwitcher[13]; } \ } while (0) +#define TPARSER_HAS_TOKEN(_t) ((_t).n > 0) +#define TPARSER_SET_NONE_TOKEN(_t) ((_t).n = 0) + typedef struct SLimitVal { - int64_t limit; - int64_t offset; + int64_t limit; + int64_t offset; } SLimitVal; typedef struct SOrderVal { - uint32_t order; - int32_t orderColId; + uint32_t order; + int32_t orderColId; } SOrderVal; typedef struct tVariantListItem { - tVariant pVar; - uint8_t sortOrder; + tVariant pVar; + uint8_t sortOrder; } tVariantListItem; typedef struct SIntervalVal { - SStrToken interval; - SStrToken offset; + SStrToken interval; + SStrToken offset; } SIntervalVal; -typedef struct SQuerySQL { - struct tSQLExprList *pSelection; // select clause - SArray * from; // from clause SArray - struct tSQLExpr * pWhere; // where clause [optional] - SArray * pGroupby; // groupby clause, only for tags[optional], SArray - SArray * pSortOrder; // orderby [optional], SArray - SStrToken interval; // interval [optional] - SStrToken offset; // offset window [optional] - SStrToken sliding; // sliding window [optional] - SLimitVal limit; // limit offset [optional] - SLimitVal slimit; // group limit offset [optional] - SArray * fillType; // fill type[optional], SArray - SStrToken selectToken; // sql string -} SQuerySQL; +typedef struct SSessionWindowVal { + SStrToken col; + SStrToken gap; +} SSessionWindowVal; + +struct SFromInfo; + +typedef struct SQuerySqlNode { + struct SArray *pSelectList; // select clause + struct SFromInfo *from; // from clause SArray + struct tSqlExpr *pWhere; // where clause [optional] + SArray *pGroupby; // groupby clause, only for tags[optional], SArray + SArray *pSortOrder; // orderby [optional], SArray + SArray *fillType; // fill type[optional], SArray + SIntervalVal interval; // (interval, interval_offset) [optional] + SSessionWindowVal sessionVal; // session window [optional] + SStrToken sliding; // sliding window [optional] + SLimitVal limit; // limit offset [optional] + SLimitVal slimit; // group limit offset [optional] + SStrToken sqlstr; // sql string in select clause +} SQuerySqlNode; + +typedef struct STableNamePair { + SStrToken name; + SStrToken aliasName; +} STableNamePair; + +typedef struct SSubclauseInfo { // "UNION" multiple select sub-clause + SQuerySqlNode **pClause; + int32_t numOfClause; +} SSubclauseInfo; + +typedef struct SFromInfo { + int32_t type; // nested query|table name list + union { + SSubclauseInfo *pNode; + SArray *tableList; // SArray + }; +} SFromInfo; typedef struct SCreatedTableInfo { - SStrToken name; // table name token - SStrToken stableName; // super table name token , for using clause - SArray *pTagNames; // create by using super table, tag name - SArray *pTagVals; // create by using super table, tag value - char *fullname; // table full name - STagData tagdata; // true tag data, super table full name is in STagData - int8_t igExist; // ignore if exists + SStrToken name; // table name token + SStrToken stableName; // super table name token , for using clause + SArray *pTagNames; // create by using super table, tag name + SArray *pTagVals; // create by using super table, tag value + char *fullname; // table full name + STagData tagdata; // true tag data, super table full name is in STagData + int8_t igExist; // ignore if exists } SCreatedTableInfo; -typedef struct SCreateTableSQL { - SStrToken name; // table name, create table [name] xxx - int8_t type; // create normal table/from super table/ stream - bool existCheck; +typedef struct SCreateTableSql { + SStrToken name; // table name, create table [name] xxx + int8_t type; // create normal table/from super table/ stream + bool existCheck; struct { - SArray *pTagColumns; // SArray - SArray *pColumns; // SArray + SArray *pTagColumns; // SArray + SArray *pColumns; // SArray } colInfo; - SArray *childTableInfo; // SArray - SQuerySQL *pSelect; -} SCreateTableSQL; + SArray *childTableInfo; // SArray + SQuerySqlNode *pSelect; +} SCreateTableSql; typedef struct SAlterTableInfo { - SStrToken name; - int16_t tableType; - int16_t type; - STagData tagData; - SArray *pAddColumns; // SArray - SArray *varList; // set t=val or: change src dst, SArray + SStrToken name; + int16_t tableType; + int16_t type; + STagData tagData; + SArray *pAddColumns; // SArray + SArray *varList; // set t=val or: change src dst, SArray } SAlterTableInfo; typedef struct SCreateDbInfo { - SStrToken dbname; - int32_t replica; - int32_t cacheBlockSize; - int32_t maxTablesPerVnode; - int32_t numOfBlocks; - int32_t daysPerFile; - int32_t minRowsPerBlock; - int32_t maxRowsPerBlock; - int32_t fsyncPeriod; - int64_t commitTime; - int32_t walLevel; - int32_t quorum; - int32_t compressionLevel; - SStrToken precision; - bool ignoreExists; - int8_t update; - int8_t cachelast; - SArray *keep; - int8_t dbType; - int16_t partitions; + SStrToken dbname; + int32_t replica; + int32_t cacheBlockSize; + int32_t maxTablesPerVnode; + int32_t numOfBlocks; + int32_t daysPerFile; + int32_t minRowsPerBlock; + int32_t maxRowsPerBlock; + int32_t fsyncPeriod; + int64_t commitTime; + int32_t walLevel; + int32_t quorum; + int32_t compressionLevel; + SStrToken precision; + bool ignoreExists; + int8_t update; + int8_t cachelast; + SArray *keep; + int8_t dbType; + int16_t partitions; } SCreateDbInfo; typedef struct SCreateAcctInfo { - int32_t maxUsers; - int32_t maxDbs; - int32_t maxTimeSeries; - int32_t maxStreams; - int32_t maxPointsPerSecond; - int64_t maxStorage; - int64_t maxQueryTime; - int32_t maxConnections; - SStrToken stat; + int32_t maxUsers; + int32_t maxDbs; + int32_t maxTimeSeries; + int32_t maxStreams; + int32_t maxPointsPerSecond; + int64_t maxStorage; + int64_t maxQueryTime; + int32_t maxConnections; + SStrToken stat; } SCreateAcctInfo; typedef struct SShowInfo { - uint8_t showType; - SStrToken prefix; - SStrToken pattern; + uint8_t showType; + SStrToken prefix; + SStrToken pattern; } SShowInfo; typedef struct SUserInfo { - SStrToken user; - SStrToken passwd; - SStrToken privilege; - int16_t type; + SStrToken user; + SStrToken passwd; + SStrToken privilege; + int16_t type; } SUserInfo; typedef struct SMiscInfo { - SArray *a; // SArray - bool existsCheck; - int16_t dbType; - int16_t tableType; - SUserInfo user; + SArray *a; // SArray + bool existsCheck; + int16_t dbType; + int16_t tableType; + SUserInfo user; union { - SCreateDbInfo dbOpt; - SCreateAcctInfo acctOpt; - SShowInfo showOpt; - SStrToken id; + SCreateDbInfo dbOpt; + SCreateAcctInfo acctOpt; + SShowInfo showOpt; + SStrToken id; }; } SMiscInfo; -typedef struct SSubclauseInfo { // "UNION" multiple select sub-clause - SQuerySQL **pClause; - int32_t numOfClause; -} SSubclauseInfo; - typedef struct SSqlInfo { int32_t type; bool valid; SSubclauseInfo subclauseInfo; char msg[256]; union { - SCreateTableSQL *pCreateTableInfo; - SAlterTableInfo *pAlterInfo; - SMiscInfo *pMiscInfo; + SCreateTableSql *pCreateTableInfo; + SAlterTableInfo *pAlterInfo; + SMiscInfo *pMiscInfo; }; } SSqlInfo; -typedef struct tSQLExpr { - uint32_t nSQLOptr; // TK_FUNCTION: sql function, TK_LE: less than(binary expr) - - // the full sql string of function(col, param), which is actually the raw - // field name, since the function name is kept in nSQLOptr already - SStrToken operand; - SStrToken colInfo; // field id - tVariant val; // value only for string, float, int - SStrToken token; // original sql expr string - - struct tSQLExpr *pLeft; // left child - struct tSQLExpr *pRight; // right child - struct tSQLExprList *pParam; // function parameters -} tSQLExpr; - -// used in select clause. select from xxx -typedef struct tSqlExprItem { - tSQLExpr *pNode; // The list of expressions - char * aliasName; // alias name, null-terminated string - bool distinct; -} tSqlExprItem; +typedef struct tSqlExpr { + uint16_t type; // sql node type + uint32_t tokenId; // TK_LE: less than(binary expr) -// todo refactor by using SArray -typedef struct tSQLExprList { - int32_t nExpr; /* Number of expressions on the list */ - int32_t nAlloc; /* Number of entries allocated below */ - tSqlExprItem *a; /* One entry for each expression */ -} tSQLExprList; + // the whole string of the function(col, param), while the function name is kept in token + SStrToken operand; + uint32_t functionId; // function id -/** - * - * @param yyp The parser - * @param yymajor The major token code number - * @param yyminor The value for the token - */ -void Parse(void *yyp, int yymajor, ParseTOKENTYPE yyminor, SSqlInfo *); + SStrToken colInfo; // table column info + tVariant value; // the use input value + SStrToken token; // original sql expr string -/** - * - * @param p The parser to be deleted - * @param freeProc Function used to reclaim memory - */ -void ParseFree(void *p, void (*freeProc)(void *)); + struct tSqlExpr *pLeft; // left child + struct tSqlExpr *pRight; // right child + struct SArray *pParam; // function parameters list +} tSqlExpr; + +// used in select clause. select from xxx +typedef struct tSqlExprItem { + tSqlExpr *pNode; // The list of expressions + char *aliasName; // alias name, null-terminated string + bool distinct; +} tSqlExprItem; SArray *tVariantListAppend(SArray *pList, tVariant *pVar, uint8_t sortOrder); SArray *tVariantListInsert(SArray *pList, tVariant *pVar, uint8_t sortOrder, int32_t index); SArray *tVariantListAppendToken(SArray *pList, SStrToken *pAliasToken, uint8_t sortOrder); -tSQLExpr *tSqlExprCreate(tSQLExpr *pLeft, tSQLExpr *pRight, int32_t optrType); - -tSQLExpr *tSqlExprClone(tSQLExpr *pSrc); +SFromInfo *setTableNameList(SFromInfo* pFromInfo, SStrToken *pName, SStrToken* pAlias); +SFromInfo *setSubquery(SFromInfo* pFromInfo, SQuerySqlNode *pSqlNode); +void *destroyFromInfo(SFromInfo* pFromInfo); -void tSqlExprDestroy(tSQLExpr *pExpr); +// sql expr leaf node +tSqlExpr *tSqlExprCreateIdValue(SStrToken *pToken, int32_t optrType); +tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType); -tSQLExprList *tSqlExprListAppend(tSQLExprList *pList, tSQLExpr *pNode, SStrToken *pDistinct, SStrToken *pToken); +tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType); +tSqlExpr *tSqlExprClone(tSqlExpr *pSrc); +void tSqlExprCompact(tSqlExpr** pExpr); +bool tSqlExprIsLeaf(tSqlExpr* pExpr); +bool tSqlExprIsParentOfLeaf(tSqlExpr* pExpr); +void tSqlExprDestroy(tSqlExpr *pExpr); +SArray *tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SStrToken *pDistinct, SStrToken *pToken); +void tSqlExprListDestroy(SArray *pList); -void tSqlExprListDestroy(tSQLExprList *pList); +SQuerySqlNode *tSetQuerySqlNode(SStrToken *pSelectToken, SArray *pSelectList, SFromInfo *pFrom, tSqlExpr *pWhere, + SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, SSessionWindowVal *ps, + SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *pgLimit); -SQuerySQL *tSetQuerySqlElems(SStrToken *pSelectToken, tSQLExprList *pSelection, SArray *pFrom, tSQLExpr *pWhere, - SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, - SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *pGLimit); +SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SQuerySqlNode *pSelect, int32_t type); -SCreateTableSQL *tSetCreateSqlElems(SArray *pCols, SArray *pTags, SQuerySQL *pSelect, int32_t type); - -void tSqlExprNodeDestroy(tSQLExpr *pExpr); - -SAlterTableInfo * tAlterTableSqlElems(SStrToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableTable); +SAlterTableInfo *tSetAlterTableInfo(SStrToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableTable); SCreatedTableInfo createNewChildTableInfo(SStrToken *pTableName, SArray *pTagNames, SArray *pTagVals, SStrToken *pToken, SStrToken* igExists); void destroyAllSelectClause(SSubclauseInfo *pSql); -void doDestroyQuerySql(SQuerySQL *pSql); +void destroyQuerySqlNode(SQuerySqlNode *pSql); void freeCreateTableInfo(void* p); -SSqlInfo * setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type); +SSqlInfo *setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type); SSubclauseInfo *setSubclause(SSubclauseInfo *pClause, void *pSqlExprInfo); SSubclauseInfo *appendSelectClause(SSubclauseInfo *pInfo, void *pSubclause); @@ -267,7 +292,7 @@ void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pTableNameToken, SStrToken void SqlInfoDestroy(SSqlInfo *pInfo); -void setDCLSQLElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...); +void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...); void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SStrToken* pToken, SStrToken* existsCheck,int16_t dbType,int16_t tableType); void setShowOptions(SSqlInfo *pInfo, int32_t type, SStrToken* prefix, SStrToken* pPatterns); @@ -282,29 +307,39 @@ void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo); void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo); // prefix show db.tables; -void setDbName(SStrToken *pCpxName, SStrToken *pDb); +void tSetDbName(SStrToken *pCpxName, SStrToken *pDb); -tSQLExpr *tSqlExprIdValueCreate(SStrToken *pToken, int32_t optrType); +void tSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType); +void tSetColumnType(TAOS_FIELD *pField, SStrToken *type); -tSQLExpr *tSqlExprCreateFunction(tSQLExprList *pList, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType); - -void tSqlSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType); +/** + * + * @param yyp The parser + * @param yymajor The major token code number + * @param yyminor The value for the token + */ +void Parse(void *yyp, int yymajor, ParseTOKENTYPE yyminor, SSqlInfo *); -void tSqlSetColumnType(TAOS_FIELD *pField, SStrToken *type); +/** + * + * @param p The parser to be deleted + * @param freeProc Function used to reclaim memory + */ +void ParseFree(void *p, void (*freeProc)(void *)); +/** + * + * @param mallocProc The parser allocator + * @return + */ void *ParseAlloc(void *(*mallocProc)(size_t)); -enum { - TSQL_NODE_TYPE_EXPR = 0x1, - TSQL_NODE_TYPE_ID = 0x2, - TSQL_NODE_TYPE_VALUE = 0x4, -}; - -#define NON_ARITHMEIC_EXPR 0 -#define NORMAL_ARITHMETIC 1 -#define AGG_ARIGHTMEIC 2 - -SSqlInfo qSQLParse(const char *str); +/** + * + * @param str sql string + * @return sql ast + */ +SSqlInfo qSqlParse(const char *str); #ifdef __cplusplus } diff --git a/src/query/inc/qTsbuf.h b/src/query/inc/qTsbuf.h index 5d055782c9b82a1444c97a62d429cc2ba9a53986..00cc4e897f130348b81a7d96419c1b292cacca8c 100644 --- a/src/query/inc/qTsbuf.h +++ b/src/query/inc/qTsbuf.h @@ -112,13 +112,11 @@ STSBuf* tsBufClone(STSBuf* pTSBuf); STSGroupBlockInfo* tsBufGetGroupBlockInfo(STSBuf* pTSBuf, int32_t id); -void tsBufFlush(STSBuf* pTSBuf); - +void tsBufFlush(STSBuf* pTSBuf); void tsBufResetPos(STSBuf* pTSBuf); -STSElem tsBufGetElem(STSBuf* pTSBuf); - bool tsBufNextPos(STSBuf* pTSBuf); +STSElem tsBufGetElem(STSBuf* pTSBuf); STSElem tsBufGetElemStartPos(STSBuf* pTSBuf, int32_t id, tVariant* tag); STSCursor tsBufGetCursor(STSBuf* pTSBuf); diff --git a/src/query/inc/qUtil.h b/src/query/inc/qUtil.h index d4a0c25886ad63ff5cc3e79cb0a9e84c156197cd..31dfc350a39b642631c1e9c60f713e953d6898c3 100644 --- a/src/query/inc/qUtil.h +++ b/src/query/inc/qUtil.h @@ -27,7 +27,7 @@ #define GET_RES_WINDOW_KEY_LEN(_l) ((_l) + sizeof(uint64_t)) #define curTimeWindowIndex(_winres) ((_winres)->curIndex) -#define GET_ROW_PARAM_FOR_MULTIOUTPUT(_q, tbq, sq) (((tbq) && (!sq))? (_q)->pExpr1[1].base.arg->argValue.i64:1) +#define GET_ROW_PARAM_FOR_MULTIOUTPUT(_q, tbq, sq) (((tbq) && (!(sq)))? (_q)->pExpr1[1].base.arg->argValue.i64:1) int32_t getOutputInterResultBufSize(SQuery* pQuery); @@ -44,22 +44,18 @@ void closeResultRow(SResultRowInfo* pResultRowInfo, int32_t slot); bool isResultRowClosed(SResultRowInfo *pResultRowInfo, int32_t slot); void clearResultRow(SQueryRuntimeEnv* pRuntimeEnv, SResultRow* pResultRow, int16_t type); -SResultRowCellInfo* getResultCell(SQueryRuntimeEnv* pRuntimeEnv, const SResultRow* pRow, int32_t index); +SResultRowCellInfo* getResultCell(const SResultRow* pRow, int32_t index, int32_t* offset); static FORCE_INLINE SResultRow *getResultRow(SResultRowInfo *pResultRowInfo, int32_t slot) { assert(pResultRowInfo != NULL && slot >= 0 && slot < pResultRowInfo->size); return pResultRowInfo->pResult[slot]; } -static FORCE_INLINE char *getPosInResultPage(SQueryRuntimeEnv *pRuntimeEnv, int32_t columnIndex, SResultRow *pResult, - tFilePage* page) { - assert(pResult != NULL && pRuntimeEnv != NULL); +static FORCE_INLINE char *getPosInResultPage(SQuery *pQuery, tFilePage* page, int32_t rowOffset, int16_t offset) { + assert(rowOffset >= 0 && pQuery != NULL); - SQuery *pQuery = pRuntimeEnv->pQuery; - - int32_t realRowId = (int32_t)(pResult->rowId * GET_ROW_PARAM_FOR_MULTIOUTPUT(pQuery, pRuntimeEnv->topBotQuery, pRuntimeEnv->stableQuery)); - return ((char *)page->data) + pRuntimeEnv->offset[columnIndex] * pRuntimeEnv->numOfRowsPerPage + - pQuery->pExpr1[columnIndex].bytes * realRowId; + int32_t numOfRows = (int32_t)GET_ROW_PARAM_FOR_MULTIOUTPUT(pQuery, pQuery->topBotQuery, pQuery->stableQuery); + return ((char *)page->data) + rowOffset + offset * numOfRows; } bool isNullOperator(SColumnFilterElem *pFilter, const char* minval, const char* maxval, int16_t type); @@ -74,8 +70,6 @@ void* destroyResultRowPool(SResultRowPool* p); int32_t getNumOfAllocatedResultRows(SResultRowPool* p); int32_t getNumOfUsedResultRows(SResultRowPool* p); -bool isPointInterpoQuery(SQuery *pQuery); - typedef struct { SArray* pResult; // SArray int32_t colId; @@ -85,12 +79,14 @@ void interResToBinary(SBufferWriter* bw, SArray* pRes, int32_t tagLen); SArray* interResFromBinary(const char* data, int32_t len); void freeInterResult(void* param); -void initGroupResInfo(SGroupResInfo* pGroupResInfo, SResultRowInfo* pResultInfo, int32_t offset); +void initGroupResInfo(SGroupResInfo* pGroupResInfo, SResultRowInfo* pResultInfo); void cleanupGroupResInfo(SGroupResInfo* pGroupResInfo); +bool hasRemainDataInCurrentGroup(SGroupResInfo* pGroupResInfo); bool hasRemainData(SGroupResInfo* pGroupResInfo); + bool incNextGroup(SGroupResInfo* pGroupResInfo); int32_t getNumOfTotalRes(SGroupResInfo* pGroupResInfo); -int32_t mergeIntoGroupResult(SGroupResInfo* pGroupResInfo, SQInfo *pQInfo); +int32_t mergeIntoGroupResult(SGroupResInfo* pGroupResInfo, SQueryRuntimeEnv *pRuntimeEnv, int32_t* offset); #endif // TDENGINE_QUERYUTIL_H diff --git a/src/query/inc/sql.y b/src/query/inc/sql.y index 1bafe241e3e6cb32a102631be9e9620a4ae48429..7d704e03b4c73c270b32d1a1f1398583bca07f74 100644 --- a/src/query/inc/sql.y +++ b/src/query/inc/sql.y @@ -92,11 +92,11 @@ cpxName(A) ::= DOT ids(Y). {A = Y; A.n += 1; } cmd ::= SHOW CREATE TABLE ids(X) cpxName(Y). { X.n += Y.n; - setDCLSQLElems(pInfo, TSDB_SQL_SHOW_CREATE_TABLE, 1, &X); + setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_TABLE, 1, &X); } cmd ::= SHOW CREATE DATABASE ids(X). { - setDCLSQLElems(pInfo, TSDB_SQL_SHOW_CREATE_DATABASE, 1, &X); + setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_DATABASE, 1, &X); } cmd ::= SHOW dbPrefix(X) TABLES. { @@ -113,19 +113,19 @@ cmd ::= SHOW dbPrefix(X) STABLES. { cmd ::= SHOW dbPrefix(X) STABLES LIKE ids(Y). { SStrToken token; - setDbName(&token, &X); + tSetDbName(&token, &X); setShowOptions(pInfo, TSDB_MGMT_TABLE_METRIC, &token, &Y); } cmd ::= SHOW dbPrefix(X) VGROUPS. { SStrToken token; - setDbName(&token, &X); + tSetDbName(&token, &X); setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, 0); } cmd ::= SHOW dbPrefix(X) VGROUPS ids(Y). { SStrToken token; - setDbName(&token, &X); + tSetDbName(&token, &X); setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, &Y); } @@ -144,26 +144,26 @@ cmd ::= DROP STABLE ifexists(Y) ids(X) cpxName(Z). { cmd ::= DROP DATABASE ifexists(Y) ids(X). { setDropDbTableInfo(pInfo, TSDB_SQL_DROP_DB, &X, &Y, TSDB_DB_TYPE_DEFAULT, -1); } cmd ::= DROP TOPIC ifexists(Y) ids(X). { setDropDbTableInfo(pInfo, TSDB_SQL_DROP_DB, &X, &Y, TSDB_DB_TYPE_TOPIC, -1); } -cmd ::= DROP DNODE ids(X). { setDCLSQLElems(pInfo, TSDB_SQL_DROP_DNODE, 1, &X); } -cmd ::= DROP USER ids(X). { setDCLSQLElems(pInfo, TSDB_SQL_DROP_USER, 1, &X); } -cmd ::= DROP ACCOUNT ids(X). { setDCLSQLElems(pInfo, TSDB_SQL_DROP_ACCT, 1, &X); } +cmd ::= DROP DNODE ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_DROP_DNODE, 1, &X); } +cmd ::= DROP USER ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_DROP_USER, 1, &X); } +cmd ::= DROP ACCOUNT ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_DROP_ACCT, 1, &X); } /////////////////////////////////THE USE STATEMENT////////////////////////////////////////// -cmd ::= USE ids(X). { setDCLSQLElems(pInfo, TSDB_SQL_USE_DB, 1, &X);} +cmd ::= USE ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_USE_DB, 1, &X);} /////////////////////////////////THE DESCRIBE STATEMENT///////////////////////////////////// cmd ::= DESCRIBE ids(X) cpxName(Y). { X.n += Y.n; - setDCLSQLElems(pInfo, TSDB_SQL_DESCRIBE_TABLE, 1, &X); + setDCLSqlElems(pInfo, TSDB_SQL_DESCRIBE_TABLE, 1, &X); } /////////////////////////////////THE ALTER STATEMENT//////////////////////////////////////// cmd ::= ALTER USER ids(X) PASS ids(Y). { setAlterUserSql(pInfo, TSDB_ALTER_USER_PASSWD, &X, &Y, NULL); } cmd ::= ALTER USER ids(X) PRIVILEGE ids(Y). { setAlterUserSql(pInfo, TSDB_ALTER_USER_PRIVILEGES, &X, NULL, &Y);} -cmd ::= ALTER DNODE ids(X) ids(Y). { setDCLSQLElems(pInfo, TSDB_SQL_CFG_DNODE, 2, &X, &Y); } -cmd ::= ALTER DNODE ids(X) ids(Y) ids(Z). { setDCLSQLElems(pInfo, TSDB_SQL_CFG_DNODE, 3, &X, &Y, &Z); } -cmd ::= ALTER LOCAL ids(X). { setDCLSQLElems(pInfo, TSDB_SQL_CFG_LOCAL, 1, &X); } -cmd ::= ALTER LOCAL ids(X) ids(Y). { setDCLSQLElems(pInfo, TSDB_SQL_CFG_LOCAL, 2, &X, &Y); } +cmd ::= ALTER DNODE ids(X) ids(Y). { setDCLSqlElems(pInfo, TSDB_SQL_CFG_DNODE, 2, &X, &Y); } +cmd ::= ALTER DNODE ids(X) ids(Y) ids(Z). { setDCLSqlElems(pInfo, TSDB_SQL_CFG_DNODE, 3, &X, &Y, &Z); } +cmd ::= ALTER LOCAL ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_CFG_LOCAL, 1, &X); } +cmd ::= ALTER LOCAL ids(X) ids(Y). { setDCLSqlElems(pInfo, TSDB_SQL_CFG_LOCAL, 2, &X, &Y); } cmd ::= ALTER DATABASE ids(X) alter_db_optr(Y). { SStrToken t = {0}; setCreateDbInfo(pInfo, TSDB_SQL_ALTER_DB, &X, &Y, &t);} cmd ::= ALTER TOPIC ids(X) alter_topic_optr(Y). { SStrToken t = {0}; setCreateDbInfo(pInfo, TSDB_SQL_ALTER_DB, &X, &Y, &t);} @@ -187,7 +187,7 @@ ifnotexists(X) ::= . { X.n = 0;} /////////////////////////////////THE CREATE STATEMENT/////////////////////////////////////// //create option for dnode/db/user/account -cmd ::= CREATE DNODE ids(X). { setDCLSQLElems(pInfo, TSDB_SQL_CREATE_DNODE, 1, &X);} +cmd ::= CREATE DNODE ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_CREATE_DNODE, 1, &X);} cmd ::= CREATE ACCOUNT ids(X) PASS ids(Y) acct_optr(Z). { setCreateAcctSql(pInfo, TSDB_SQL_CREATE_ACCT, &X, &Y, &Z);} cmd ::= CREATE DATABASE ifnotexists(Z) ids(X) db_optr(Y). { setCreateDbInfo(pInfo, TSDB_SQL_CREATE_DB, &X, &Y, &Z);} @@ -299,17 +299,17 @@ alter_topic_optr(Y) ::= alter_topic_optr(Z) partitions(X). { Y = Z; Y.parti %type typename {TAOS_FIELD} typename(A) ::= ids(X). { X.type = 0; - tSqlSetColumnType (&A, &X); + tSetColumnType (&A, &X); } //define binary type, e.g., binary(10), nchar(10) typename(A) ::= ids(X) LP signed(Y) RP. { if (Y <= 0) { X.type = 0; - tSqlSetColumnType(&A, &X); + tSetColumnType(&A, &X); } else { X.type = -Y; // negative value of name length - tSqlSetColumnType(&A, &X); + tSetColumnType(&A, &X); } } @@ -317,7 +317,7 @@ typename(A) ::= ids(X) LP signed(Y) RP. { typename(A) ::= ids(X) UNSIGNED(Z). { X.type = 0; X.n = ((Z.z + Z.n) - X.z); - tSqlSetColumnType (&A, &X); + tSetColumnType (&A, &X); } %type signed {int64_t} @@ -326,15 +326,15 @@ signed(A) ::= PLUS INTEGER(X). { A = strtol(X.z, NULL, 10); } signed(A) ::= MINUS INTEGER(X). { A = -strtol(X.z, NULL, 10);} ////////////////////////////////// The CREATE TABLE statement /////////////////////////////// -cmd ::= CREATE TABLE create_table_args. {} -cmd ::= CREATE TABLE create_stable_args. {} +cmd ::= CREATE TABLE create_table_args. {} +cmd ::= CREATE TABLE create_stable_args. {} cmd ::= CREATE STABLE create_stable_args. {} cmd ::= CREATE TABLE create_table_list(Z). { pInfo->type = TSDB_SQL_CREATE_TABLE; pInfo->pCreateTableInfo = Z;} -%type create_table_list{SCreateTableSQL*} +%type create_table_list{SCreateTableSql*} %destructor create_table_list{destroyCreateTableSql($$);} create_table_list(A) ::= create_from_stable(Z). { - SCreateTableSQL* pCreateTable = calloc(1, sizeof(SCreateTableSQL)); + SCreateTableSql* pCreateTable = calloc(1, sizeof(SCreateTableSql)); pCreateTable->childTableInfo = taosArrayInit(4, sizeof(SCreatedTableInfo)); taosArrayPush(pCreateTable->childTableInfo, &Z); @@ -347,9 +347,9 @@ create_table_list(A) ::= create_table_list(X) create_from_stable(Z). { A = X; } -%type create_table_args{SCreateTableSQL*} +%type create_table_args{SCreateTableSql*} create_table_args(A) ::= ifnotexists(U) ids(V) cpxName(Z) LP columnlist(X) RP. { - A = tSetCreateSqlElems(X, NULL, NULL, TSQL_CREATE_TABLE); + A = tSetCreateTableInfo(X, NULL, NULL, TSQL_CREATE_TABLE); setSqlInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE); V.n += Z.n; @@ -357,9 +357,9 @@ create_table_args(A) ::= ifnotexists(U) ids(V) cpxName(Z) LP columnlist(X) RP. { } // create super table -%type create_stable_args{SCreateTableSQL*} +%type create_stable_args{SCreateTableSql*} create_stable_args(A) ::= ifnotexists(U) ids(V) cpxName(Z) LP columnlist(X) RP TAGS LP columnlist(Y) RP. { - A = tSetCreateSqlElems(X, Y, NULL, TSQL_CREATE_STABLE); + A = tSetCreateTableInfo(X, Y, NULL, TSQL_CREATE_STABLE); setSqlInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE); V.n += Z.n; @@ -389,7 +389,7 @@ tagNamelist(A) ::= ids(X). {A = taosArrayInit(4, sizeof(SSt // create stream // create table table_name as select count(*) from super_table_name interval(time) create_table_args(A) ::= ifnotexists(U) ids(V) cpxName(Z) AS select(S). { - A = tSetCreateSqlElems(NULL, NULL, S, TSQL_CREATE_STREAM); + A = tSetCreateTableInfo(NULL, NULL, S, TSQL_CREATE_STREAM); setSqlInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE); V.n += Z.n; @@ -405,7 +405,7 @@ columnlist(A) ::= column(X). {A = taosArrayInit(4, sizeof(T // The information used for a column is the name and type of column: // tinyint smallint int bigint float double bool timestamp binary(x) nchar(x) column(A) ::= ids(X) typename(Y). { - tSqlSetColumnInfo(&A, &X, &Y); + tSetColumnInfo(&A, &X, &Y); } %type tagitemlist {SArray*} @@ -450,37 +450,37 @@ tagitem(A) ::= PLUS(X) FLOAT(Y). { } //////////////////////// The SELECT statement ///////////////////////////////// -%type select {SQuerySQL*} -%destructor select {doDestroyQuerySql($$);} -select(A) ::= SELECT(T) selcollist(W) from(X) where_opt(Y) interval_opt(K) fill_opt(F) sliding_opt(S) groupby_opt(P) orderby_opt(Z) having_opt(N) slimit_opt(G) limit_opt(L). { - A = tSetQuerySqlElems(&T, W, X, Y, P, Z, &K, &S, F, &L, &G); +%type select {SQuerySqlNode*} +%destructor select {destroyQuerySqlNode($$);} +select(A) ::= SELECT(T) selcollist(W) from(X) where_opt(Y) interval_opt(K) session_option(H) fill_opt(F) sliding_opt(S) groupby_opt(P) orderby_opt(Z) having_opt(N) slimit_opt(G) limit_opt(L). { + A = tSetQuerySqlNode(&T, W, X, Y, P, Z, &K, &H, &S, F, &L, &G); } +select(A) ::= LP select(B) RP. {A = B;} + %type union {SSubclauseInfo*} %destructor union {destroyAllSelectClause($$);} - union(Y) ::= select(X). { Y = setSubclause(NULL, X); } -union(Y) ::= LP union(X) RP. { Y = X; } union(Y) ::= union(Z) UNION ALL select(X). { Y = appendSelectClause(Z, X); } -union(Y) ::= union(Z) UNION ALL LP select(X) RP. { Y = appendSelectClause(Z, X); } cmd ::= union(X). { setSqlInfo(pInfo, X, NULL, TSDB_SQL_SELECT); } // Support for the SQL exprssion without from & where subclauses, e.g., -// select current_database(), -// select server_version(), select client_version(), -// select server_state(); +// select current_database() +// select server_version() +// select client_version() +// select server_state() select(A) ::= SELECT(T) selcollist(W). { - A = tSetQuerySqlElems(&T, W, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + A = tSetQuerySqlNode(&T, W, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); } // selcollist is a list of expressions that are to become the return // values of the SELECT statement. The "*" in statements like // "SELECT * FROM ..." is encoded as a special expression with an opcode of TK_ALL. -%type selcollist {tSQLExprList*} +%type selcollist {SArray*} %destructor selcollist {tSqlExprListDestroy($$);} -%type sclp {tSQLExprList*} +%type sclp {SArray*} %destructor sclp {tSqlExprListDestroy($$);} sclp(A) ::= selcollist(X) COMMA. {A = X;} sclp(A) ::= . {A = 0;} @@ -489,13 +489,12 @@ selcollist(A) ::= sclp(P) distinct(Z) expr(X) as(Y). { } selcollist(A) ::= sclp(P) STAR. { - tSQLExpr *pNode = tSqlExprIdValueCreate(NULL, TK_ALL); + tSqlExpr *pNode = tSqlExprCreateIdValue(NULL, TK_ALL); A = tSqlExprListAppend(P, pNode, 0, 0); } // An option "AS " phrase that can follow one of the expressions that // define the result set, or one of the tables in the FROM clause. -// %type as {SStrToken} as(X) ::= AS ids(Y). { X = Y; } as(X) ::= ids(Y). { X = Y; } @@ -506,39 +505,36 @@ distinct(X) ::= DISTINCT(Y). { X = Y; } distinct(X) ::= . { X.n = 0;} // A complete FROM clause. -%type from {SArray*} -// current not support query from no-table +%type from {SFromInfo*} from(A) ::= FROM tablelist(X). {A = X;} +from(A) ::= FROM LP union(Y) RP. {A = Y;} %type tablelist {SArray*} tablelist(A) ::= ids(X) cpxName(Y). { toTSDBType(X.type); X.n += Y.n; - A = tVariantListAppendToken(NULL, &X, -1); - A = tVariantListAppendToken(A, &X, -1); // table alias name + A = setTableNameList(NULL, &X, NULL); } tablelist(A) ::= ids(X) cpxName(Y) ids(Z). { toTSDBType(X.type); toTSDBType(Z.type); X.n += Y.n; - A = tVariantListAppendToken(NULL, &X, -1); - A = tVariantListAppendToken(A, &Z, -1); + A = setTableNameList(NULL, &X, &Z); } tablelist(A) ::= tablelist(Y) COMMA ids(X) cpxName(Z). { toTSDBType(X.type); X.n += Z.n; - A = tVariantListAppendToken(Y, &X, -1); - A = tVariantListAppendToken(A, &X, -1); + A = setTableNameList(Y, &X, NULL); } tablelist(A) ::= tablelist(Y) COMMA ids(X) cpxName(Z) ids(F). { toTSDBType(X.type); toTSDBType(F.type); X.n += Z.n; - A = tVariantListAppendToken(Y, &X, -1); - A = tVariantListAppendToken(A, &F, -1); + + A = setTableNameList(Y, &X, &F); } // The value of interval should be the form of "number+[a,s,m,h,d,n,y]" or "now" @@ -546,13 +542,21 @@ tablelist(A) ::= tablelist(Y) COMMA ids(X) cpxName(Z) ids(F). { tmvar(A) ::= VARIABLE(X). {A = X;} %type interval_opt {SIntervalVal} -interval_opt(N) ::= INTERVAL LP tmvar(E) RP. {N.interval = E; N.offset.n = 0; N.offset.z = NULL; N.offset.type = 0;} -interval_opt(N) ::= INTERVAL LP tmvar(E) COMMA tmvar(O) RP. {N.interval = E; N.offset = O;} +interval_opt(N) ::= INTERVAL LP tmvar(E) RP. {N.interval = E; N.offset.n = 0;} +interval_opt(N) ::= INTERVAL LP tmvar(E) COMMA tmvar(X) RP. {N.interval = E; N.offset = X;} interval_opt(N) ::= . {memset(&N, 0, sizeof(N));} +%type session_option {SSessionWindowVal} +session_option(X) ::= . {X.col.n = 0; X.gap.n = 0;} +session_option(X) ::= SESSION LP ids(V) cpxName(Z) COMMA tmvar(Y) RP. { + V.n += Z.n; + X.col = V; + X.gap = Y; +} + %type fill_opt {SArray*} %destructor fill_opt {taosArrayDestroy($$);} -fill_opt(N) ::= . {N = 0; } +fill_opt(N) ::= . { N = 0; } fill_opt(N) ::= FILL LP ID(Y) COMMA tagitemlist(X) RP. { tVariant A = {0}; toTSDBType(Y.type); @@ -622,7 +626,7 @@ grouplist(A) ::= item(X). { } //having clause, ignore the input condition in having -%type having_opt {tSQLExpr*} +%type having_opt {tSqlExpr*} %destructor having_opt {tSqlExprDestroy($$);} having_opt(A) ::=. {A = 0;} having_opt(A) ::= HAVING expr(X). {A = X;} @@ -644,7 +648,7 @@ slimit_opt(A) ::= SLIMIT signed(X) SOFFSET signed(Y). slimit_opt(A) ::= SLIMIT signed(X) COMMA signed(Y). {A.limit = Y; A.offset = X;} -%type where_opt {tSQLExpr*} +%type where_opt {tSqlExpr*} %destructor where_opt {tSqlExprDestroy($$);} where_opt(A) ::= . {A = 0;} @@ -652,25 +656,25 @@ where_opt(A) ::= WHERE expr(X). {A = X;} /////////////////////////// Expression Processing ///////////////////////////// // -%type expr {tSQLExpr*} +%type expr {tSqlExpr*} %destructor expr {tSqlExprDestroy($$);} expr(A) ::= LP(X) expr(Y) RP(Z). {A = Y; A->token.z = X.z; A->token.n = (Z.z - X.z + 1);} -expr(A) ::= ID(X). { A = tSqlExprIdValueCreate(&X, TK_ID);} -expr(A) ::= ID(X) DOT ID(Y). { X.n += (1+Y.n); A = tSqlExprIdValueCreate(&X, TK_ID);} -expr(A) ::= ID(X) DOT STAR(Y). { X.n += (1+Y.n); A = tSqlExprIdValueCreate(&X, TK_ALL);} - -expr(A) ::= INTEGER(X). { A = tSqlExprIdValueCreate(&X, TK_INTEGER);} -expr(A) ::= MINUS(X) INTEGER(Y). { X.n += Y.n; X.type = TK_INTEGER; A = tSqlExprIdValueCreate(&X, TK_INTEGER);} -expr(A) ::= PLUS(X) INTEGER(Y). { X.n += Y.n; X.type = TK_INTEGER; A = tSqlExprIdValueCreate(&X, TK_INTEGER);} -expr(A) ::= FLOAT(X). { A = tSqlExprIdValueCreate(&X, TK_FLOAT);} -expr(A) ::= MINUS(X) FLOAT(Y). { X.n += Y.n; X.type = TK_FLOAT; A = tSqlExprIdValueCreate(&X, TK_FLOAT);} -expr(A) ::= PLUS(X) FLOAT(Y). { X.n += Y.n; X.type = TK_FLOAT; A = tSqlExprIdValueCreate(&X, TK_FLOAT);} -expr(A) ::= STRING(X). { A = tSqlExprIdValueCreate(&X, TK_STRING);} -expr(A) ::= NOW(X). { A = tSqlExprIdValueCreate(&X, TK_NOW); } -expr(A) ::= VARIABLE(X). { A = tSqlExprIdValueCreate(&X, TK_VARIABLE);} -expr(A) ::= BOOL(X). { A = tSqlExprIdValueCreate(&X, TK_BOOL);} +expr(A) ::= ID(X). { A = tSqlExprCreateIdValue(&X, TK_ID);} +expr(A) ::= ID(X) DOT ID(Y). { X.n += (1+Y.n); A = tSqlExprCreateIdValue(&X, TK_ID);} +expr(A) ::= ID(X) DOT STAR(Y). { X.n += (1+Y.n); A = tSqlExprCreateIdValue(&X, TK_ALL);} + +expr(A) ::= INTEGER(X). { A = tSqlExprCreateIdValue(&X, TK_INTEGER);} +expr(A) ::= MINUS(X) INTEGER(Y). { X.n += Y.n; X.type = TK_INTEGER; A = tSqlExprCreateIdValue(&X, TK_INTEGER);} +expr(A) ::= PLUS(X) INTEGER(Y). { X.n += Y.n; X.type = TK_INTEGER; A = tSqlExprCreateIdValue(&X, TK_INTEGER);} +expr(A) ::= FLOAT(X). { A = tSqlExprCreateIdValue(&X, TK_FLOAT);} +expr(A) ::= MINUS(X) FLOAT(Y). { X.n += Y.n; X.type = TK_FLOAT; A = tSqlExprCreateIdValue(&X, TK_FLOAT);} +expr(A) ::= PLUS(X) FLOAT(Y). { X.n += Y.n; X.type = TK_FLOAT; A = tSqlExprCreateIdValue(&X, TK_FLOAT);} +expr(A) ::= STRING(X). { A = tSqlExprCreateIdValue(&X, TK_STRING);} +expr(A) ::= NOW(X). { A = tSqlExprCreateIdValue(&X, TK_NOW); } +expr(A) ::= VARIABLE(X). { A = tSqlExprCreateIdValue(&X, TK_VARIABLE);} +expr(A) ::= BOOL(X). { A = tSqlExprCreateIdValue(&X, TK_BOOL);} // ordinary functions: min(x), max(x), top(k, 20) expr(A) ::= ID(X) LP exprlist(Y) RP(E). { A = tSqlExprCreateFunction(Y, &X, &E, X.type); } @@ -690,7 +694,7 @@ expr(A) ::= expr(X) GE expr(Y). {A = tSqlExprCreate(X, Y, TK_GE);} expr(A) ::= expr(X) NE expr(Y). {A = tSqlExprCreate(X, Y, TK_NE);} expr(A) ::= expr(X) EQ expr(Y). {A = tSqlExprCreate(X, Y, TK_EQ);} -expr(A) ::= expr(X) BETWEEN expr(Y) AND expr(Z). { tSQLExpr* X2 = tSqlExprClone(X); A = tSqlExprCreate(tSqlExprCreate(X, Y, TK_GE), tSqlExprCreate(X2, Z, TK_LE), TK_AND);} +expr(A) ::= expr(X) BETWEEN expr(Y) AND expr(Z). { tSqlExpr* X2 = tSqlExprClone(X); A = tSqlExprCreate(tSqlExprCreate(X, Y, TK_GE), tSqlExprCreate(X2, Z, TK_LE), TK_AND);} expr(A) ::= expr(X) AND expr(Y). {A = tSqlExprCreate(X, Y, TK_AND);} expr(A) ::= expr(X) OR expr(Y). {A = tSqlExprCreate(X, Y, TK_OR); } @@ -706,12 +710,12 @@ expr(A) ::= expr(X) REM expr(Y). {A = tSqlExprCreate(X, Y, TK_REM); } expr(A) ::= expr(X) LIKE expr(Y). {A = tSqlExprCreate(X, Y, TK_LIKE); } //in expression -expr(A) ::= expr(X) IN LP exprlist(Y) RP. {A = tSqlExprCreate(X, (tSQLExpr*)Y, TK_IN); } +expr(A) ::= expr(X) IN LP exprlist(Y) RP. {A = tSqlExprCreate(X, (tSqlExpr*)Y, TK_IN); } -%type exprlist {tSQLExprList*} +%type exprlist {SArray*} %destructor exprlist {tSqlExprListDestroy($$);} -%type expritem {tSQLExpr*} +%type expritem {tSqlExpr*} %destructor expritem {tSqlExprDestroy($$);} exprlist(A) ::= exprlist(X) COMMA expritem(Y). {A = tSqlExprListAppend(X,Y,0, 0);} @@ -720,12 +724,12 @@ expritem(A) ::= expr(X). {A = X;} expritem(A) ::= . {A = 0;} ///////////////////////////////////reset query cache////////////////////////////////////// -cmd ::= RESET QUERY CACHE. { setDCLSQLElems(pInfo, TSDB_SQL_RESET_CACHE, 0);} +cmd ::= RESET QUERY CACHE. { setDCLSqlElems(pInfo, TSDB_SQL_RESET_CACHE, 0);} ///////////////////////////////////ALTER TABLE statement////////////////////////////////// cmd ::= ALTER TABLE ids(X) cpxName(F) ADD COLUMN columnlist(A). { X.n += F.n; - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&X, A, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } @@ -735,14 +739,14 @@ cmd ::= ALTER TABLE ids(X) cpxName(F) DROP COLUMN ids(A). { toTSDBType(A.type); SArray* K = tVariantListAppendToken(NULL, &A, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&X, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } //////////////////////////////////ALTER TAGS statement///////////////////////////////////// cmd ::= ALTER TABLE ids(X) cpxName(Y) ADD TAG columnlist(A). { X.n += Y.n; - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&X, A, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } cmd ::= ALTER TABLE ids(X) cpxName(Z) DROP TAG ids(Y). { @@ -751,7 +755,7 @@ cmd ::= ALTER TABLE ids(X) cpxName(Z) DROP TAG ids(Y). { toTSDBType(Y.type); SArray* A = tVariantListAppendToken(NULL, &Y, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&X, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } @@ -764,7 +768,7 @@ cmd ::= ALTER TABLE ids(X) cpxName(F) CHANGE TAG ids(Y) ids(Z). { toTSDBType(Z.type); A = tVariantListAppendToken(A, &Z, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&X, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } @@ -775,7 +779,7 @@ cmd ::= ALTER TABLE ids(X) cpxName(F) SET TAG ids(Y) EQ tagitem(Z). { SArray* A = tVariantListAppendToken(NULL, &Y, -1); A = tVariantListAppend(A, &Z, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&X, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } @@ -783,7 +787,7 @@ cmd ::= ALTER TABLE ids(X) cpxName(F) SET TAG ids(Y) EQ tagitem(Z). { ///////////////////////////////////ALTER STABLE statement////////////////////////////////// cmd ::= ALTER STABLE ids(X) cpxName(F) ADD COLUMN columnlist(A). { X.n += F.n; - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&X, A, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, TSDB_SUPER_TABLE); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, TSDB_SUPER_TABLE); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } @@ -793,14 +797,14 @@ cmd ::= ALTER STABLE ids(X) cpxName(F) DROP COLUMN ids(A). { toTSDBType(A.type); SArray* K = tVariantListAppendToken(NULL, &A, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&X, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, TSDB_SUPER_TABLE); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, TSDB_SUPER_TABLE); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } //////////////////////////////////ALTER TAGS statement///////////////////////////////////// cmd ::= ALTER STABLE ids(X) cpxName(Y) ADD TAG columnlist(A). { X.n += Y.n; - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&X, A, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, TSDB_SUPER_TABLE); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, TSDB_SUPER_TABLE); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } cmd ::= ALTER STABLE ids(X) cpxName(Z) DROP TAG ids(Y). { @@ -809,7 +813,7 @@ cmd ::= ALTER STABLE ids(X) cpxName(Z) DROP TAG ids(Y). { toTSDBType(Y.type); SArray* A = tVariantListAppendToken(NULL, &Y, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&X, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, TSDB_SUPER_TABLE); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, TSDB_SUPER_TABLE); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } @@ -822,7 +826,7 @@ cmd ::= ALTER STABLE ids(X) cpxName(F) CHANGE TAG ids(Y) ids(Z). { toTSDBType(Z.type); A = tVariantListAppendToken(A, &Z, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&X, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, TSDB_SUPER_TABLE); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, TSDB_SUPER_TABLE); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } @@ -834,7 +838,6 @@ cmd ::= KILL QUERY INTEGER(X) COLON(Z) INTEGER(Y). {X.n += (Z.n + Y.n); s %fallback ID ABORT AFTER ASC ATTACH BEFORE BEGIN CASCADE CLUSTER CONFLICT COPY DATABASE DEFERRED DELIMITERS DESC DETACH EACH END EXPLAIN FAIL FOR GLOB IGNORE IMMEDIATE INITIALLY INSTEAD LIKE MATCH KEY OF OFFSET RAISE REPLACE RESTRICT ROW STATEMENT TRIGGER VIEW ALL - COUNT SUM AVG MIN MAX FIRST LAST TOP BOTTOM STDDEV PERCENTILE APERCENTILE LEASTSQUARES HISTOGRAM DIFF - SPREAD TWA INTERP LAST_ROW RATE IRATE SUM_RATE SUM_IRATE AVG_RATE AVG_IRATE TBID NOW IPTOKEN SEMI NONE PREV LINEAR IMPORT + NOW IPTOKEN SEMI NONE PREV LINEAR IMPORT METRIC TBNAME JOIN METRICS STABLE NULL INSERT INTO VALUES. \ No newline at end of file diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index f99fe3f0720784db0a5e0b526dddb436a91f87d3..f18d093b89d48419e9707d606a289b3168fea8bf 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -18,6 +18,7 @@ #include "taosmsg.h" #include "texpr.h" #include "ttype.h" +#include "tsdb.h" #include "qAggMain.h" #include "qFill.h" @@ -26,11 +27,9 @@ #include "qTsbuf.h" #include "queryLog.h" -//#define GET_INPUT_DATA_LIST(x) (((char *)((x)->pInput)) + ((x)->startOffset) * ((x)->inputBytes)) #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[(x)->startOffset])) #define GET_TS_LIST(x) ((TSKEY*)((x)->ptsList)) #define GET_TS_DATA(x, y) (GET_TS_LIST(x)[(y)]) @@ -191,6 +190,11 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI *bytes = (int16_t)(dataBytes + sizeof(int16_t) + sizeof(int64_t) + sizeof(int32_t) + sizeof(int32_t) + VARSTR_HEADER_SIZE); *interBytes = 0; return TSDB_CODE_SUCCESS; + } else if (functionId == TSDB_FUNC_BLKINFO) { + *type = TSDB_DATA_TYPE_BINARY; + *bytes = 16384; + *interBytes = 0; + return TSDB_CODE_SUCCESS; } if (functionId == TSDB_FUNC_COUNT) { @@ -209,7 +213,7 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI if (functionId == TSDB_FUNC_TS_COMP) { *type = TSDB_DATA_TYPE_BINARY; - *bytes = sizeof(int32_t); // this results is compressed ts data + *bytes = 1; // this results is compressed ts data, only one byte *interBytes = POINTER_BYTES; return TSDB_CODE_SUCCESS; } @@ -355,6 +359,22 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI return TSDB_CODE_SUCCESS; } +// TODO use hash table +int32_t isValidFunction(const char* name, int32_t len) { + for(int32_t i = 0; i <= TSDB_FUNC_BLKINFO; ++i) { + int32_t nameLen = (int32_t) strlen(aAggs[i].name); + if (len != nameLen) { + continue; + } + + if (strncasecmp(aAggs[i].name, name, len) == 0) { + return i; + } + } + + return -1; +} + // set the query flag to denote that query is completed static void no_next_step(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); @@ -458,7 +478,7 @@ static void count_func_merge(SQLFunctionCtx *pCtx) { * @param filterCols * @return */ -int32_t count_load_data_info(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, 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 { @@ -466,7 +486,7 @@ int32_t count_load_data_info(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, int32 } } -int32_t no_data_info(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, int32_t colId) { +int32_t noDataRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { return BLK_DATA_NO_NEEDED; } @@ -674,16 +694,16 @@ static void sum_func_merge(SQLFunctionCtx *pCtx) { } } -static int32_t statisRequired(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, int32_t colId) { +static int32_t statisRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { return BLK_DATA_STATIS_NEEDED; } -static int32_t dataBlockRequired(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, int32_t colId) { +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, TSKEY start, TSKEY end, int32_t colId) { +// todo: if column in current data block are null, opt for this case +static int32_t firstFuncRequired(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId) { if (pCtx->order == TSDB_ORDER_DESC) { return BLK_DATA_NO_NEEDED; } @@ -696,7 +716,7 @@ static int32_t firstFuncRequired(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, i } } -static int32_t lastFuncRequired(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, 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; } @@ -708,7 +728,7 @@ static int32_t lastFuncRequired(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, in } } -static int32_t firstDistFuncRequired(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, 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; } @@ -724,11 +744,11 @@ static int32_t firstDistFuncRequired(SQLFunctionCtx *pCtx, TSKEY start, TSKEY en if (pInfo->hasResult != DATA_SET_FLAG) { return BLK_DATA_ALL_NEEDED; } else { // data in current block is not earlier than current result - return (pInfo->ts <= start) ? BLK_DATA_NO_NEEDED : BLK_DATA_ALL_NEEDED; + return (pInfo->ts <= w->skey) ? BLK_DATA_NO_NEEDED : BLK_DATA_ALL_NEEDED; } } -static int32_t lastDistFuncRequired(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, 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; } @@ -744,7 +764,7 @@ static int32_t lastDistFuncRequired(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end if (pInfo->hasResult != DATA_SET_FLAG) { return BLK_DATA_ALL_NEEDED; } else { - return (pInfo->ts > end) ? BLK_DATA_NO_NEEDED : BLK_DATA_ALL_NEEDED; + return (pInfo->ts > w->ekey) ? BLK_DATA_NO_NEEDED : BLK_DATA_ALL_NEEDED; } } @@ -1359,7 +1379,21 @@ static void min_function_f(SQLFunctionCtx *pCtx, int32_t index) { } static void stddev_function(SQLFunctionCtx *pCtx) { - SStddevInfo *pStd = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + 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 + + // save average value into tmpBuf, for second stage scan + SAvgInfo *pAvg = GET_ROWCELL_INTERBUF(pResInfo); + + 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 @@ -1432,7 +1466,20 @@ static void stddev_function_f(SQLFunctionCtx *pCtx, int32_t index) { // the second stage to calculate standard deviation SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); 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 + + // save average value into tmpBuf, for second stage scan + SAvgInfo *pAvg = GET_ROWCELL_INTERBUF(pResInfo); + + pStd->avg = GET_DOUBLE_VAL(pCtx->pOutput); + assert((isnan(pAvg->sum) && pAvg->num == 0) || (pStd->num == pAvg->num && pStd->avg == pAvg->sum)); + } + /* the first stage is to calculate average value */ if (pStd->stage == 0) { avg_function_f(pCtx, index); @@ -1574,7 +1621,7 @@ static void stddev_dst_function(SQLFunctionCtx *pCtx) { if (p == NULL) { return; } - + avg = p->avg; } @@ -1776,7 +1823,7 @@ static bool first_last_function_setup(SQLFunctionCtx *pCtx) { // todo opt for null block static void first_function(SQLFunctionCtx *pCtx) { - if (pCtx->order == TSDB_ORDER_DESC || pCtx->preAggVals.dataBlockLoaded == false) { + if (pCtx->order == TSDB_ORDER_DESC /*|| pCtx->preAggVals.dataBlockLoaded == false*/) { return; } @@ -1850,7 +1897,7 @@ 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; } @@ -1921,7 +1968,7 @@ 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) { - if (pCtx->order != pCtx->param[0].i64 || pCtx->preAggVals.dataBlockLoaded == false) { + if (pCtx->order != pCtx->param[0].i64/* || pCtx->preAggVals.dataBlockLoaded == false*/) { return; } @@ -1934,6 +1981,7 @@ static void last_function(SQLFunctionCtx *pCtx) { continue; } } + memcpy(pCtx->pOutput, data, pCtx->inputBytes); TSKEY ts = GET_TS_DATA(pCtx, i); @@ -2013,13 +2061,7 @@ static void last_dist_function(SQLFunctionCtx *pCtx) { return; } - // data block is discard, not loaded, do not need to check it - if (!pCtx->preAggVals.dataBlockLoaded) { - return; - } - int32_t notNullElems = 0; - for (int32_t i = pCtx->size - 1; i >= 0; --i) { char *data = GET_INPUT_DATA(pCtx, i); if (pCtx->hasNull && isNull(data, pCtx->inputType)) { @@ -2125,12 +2167,7 @@ static void last_row_finalizer(SQLFunctionCtx *pCtx) { // do nothing at the first stage SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); if (pResInfo->hasResult != DATA_SET_FLAG) { - if (pCtx->outputType == TSDB_DATA_TYPE_BINARY || pCtx->outputType == TSDB_DATA_TYPE_NCHAR) { - setVardataNull(pCtx->pOutput, pCtx->outputType); - } else { - setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); - } - + setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); return; } @@ -2445,7 +2482,7 @@ static STopBotInfo *getTopBotOutputInfo(SQLFunctionCtx *pCtx) { } } -bool topbot_datablock_filter(SQLFunctionCtx *pCtx, int32_t functionId, const char *minval, const char *maxval) { +bool topbot_datablock_filter(SQLFunctionCtx *pCtx, const char *minval, const char *maxval) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); if (pResInfo == NULL) { return true; @@ -2460,7 +2497,7 @@ bool topbot_datablock_filter(SQLFunctionCtx *pCtx, int32_t functionId, const cha tValuePair **pRes = (tValuePair**) pTopBotInfo->res; - if (functionId == TSDB_FUNC_TOP) { + if (pCtx->functionId == TSDB_FUNC_TOP) { switch (pCtx->inputType) { case TSDB_DATA_TYPE_TINYINT: return GET_INT8_VAL(maxval) > pRes[0]->v.i64; @@ -2531,9 +2568,13 @@ static bool top_bottom_function_setup(SQLFunctionCtx *pCtx) { static void top_function(SQLFunctionCtx *pCtx) { int32_t notNullElems = 0; - + STopBotInfo *pRes = getTopBotOutputInfo(pCtx); assert(pRes->num >= 0); + + 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); @@ -2609,13 +2650,13 @@ static void bottom_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); TSKEY ts = GET_TS_DATA(pCtx, i); if (pCtx->hasNull && isNull(data, pCtx->inputType)) { - continue; + continue; } notNullElems++; @@ -2648,7 +2689,7 @@ static void bottom_function_f(SQLFunctionCtx *pCtx, int32_t index) { if ((void *)pRes->res[0] != (void *)((char *)pRes + sizeof(STopBotInfo) + POINTER_BYTES * pCtx->param[0].i64)) { buildTopBotStruct(pRes, pCtx); } - + SET_VAL(pCtx, 1, 1); do_bottom_function_add(pRes, (int32_t)pCtx->param[0].i64, pData, ts, pCtx->inputType, &pCtx->tagInfo, NULL, 0); @@ -2729,6 +2770,17 @@ static void percentile_function(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); SPercentileInfo *pInfo = GET_ROWCELL_INTERBUF(pResInfo); + if (pCtx->currentStage == REPEAT_SCAN && pInfo->stage == 0) { + // all data are null, set it completed + if (pInfo->numOfElems == 0) { + pResInfo->complete = true; + } else { + pInfo->pMemBucket = tMemBucketCreate(pCtx->inputBytes, pCtx->inputType, pInfo->minval, pInfo->maxval); + } + + pInfo->stage += 1; + } + // the first stage, only acquire the min/max value if (pInfo->stage == 0) { if (pCtx->preAggVals.isSet) { @@ -2802,10 +2854,20 @@ static void percentile_function_f(SQLFunctionCtx *pCtx, int32_t index) { } SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); - SPercentileInfo *pInfo = (SPercentileInfo *)GET_ROWCELL_INTERBUF(pResInfo); - if (pInfo->stage == 0) { + if (pCtx->currentStage == REPEAT_SCAN && pInfo->stage == 0) { + // all data are null, set it completed + if (pInfo->numOfElems == 0) { + pResInfo->complete = true; + } else { + pInfo->pMemBucket = tMemBucketCreate(pCtx->inputBytes, pCtx->inputType, pInfo->minval, pInfo->maxval); + } + + pInfo->stage += 1; + } + + if (pInfo->stage == 0) { double v = 0; GET_TYPED_DATA(v, double, pCtx->inputType, pData); @@ -3240,8 +3302,6 @@ static void col_project_function(SQLFunctionCtx *pCtx) { pCtx->inputBytes); } } - - pCtx->pOutput += pCtx->size * pCtx->outputBytes; } static void col_project_function_f(SQLFunctionCtx *pCtx, int32_t index) { @@ -3547,9 +3607,6 @@ static void diff_function(SQLFunctionCtx *pCtx) { int32_t forwardStep = (isFirstBlock) ? notNullElems - 1 : notNullElems; GET_RES_INFO(pCtx)->numOfRes += forwardStep; - - pCtx->pOutput += forwardStep * pCtx->outputBytes; - pCtx->ptsOutputBuf = (char*)pCtx->ptsOutputBuf + forwardStep * TSDB_KEYSIZE; } } @@ -3631,7 +3688,7 @@ char *getArithColumnData(void *param, const char* name, int32_t colId) { } } - assert(index >= 0 && colId >= 0); + assert(index >= 0 /*&& colId >= 0*/); return pSupport->data[index] + pSupport->offset * pSupport->colList[index].bytes; } @@ -3640,7 +3697,6 @@ static void arithmetic_function(SQLFunctionCtx *pCtx) { SArithmeticSupport *sas = (SArithmeticSupport *)pCtx->param[1].pz; arithmeticTreeTraverse(sas->pArithExpr->pExpr, pCtx->size, pCtx->pOutput, sas, pCtx->order, getArithColumnData); - pCtx->pOutput += pCtx->outputBytes * pCtx->size; } static void arithmetic_function_f(SQLFunctionCtx *pCtx, int32_t index) { @@ -4171,50 +4227,88 @@ static void interp_function_impl(SQLFunctionCtx *pCtx) { } if (pCtx->inputType == TSDB_DATA_TYPE_TIMESTAMP) { - *(TSKEY *) pCtx->pOutput = pCtx->startTs; + *(TSKEY *)pCtx->pOutput = pCtx->startTs; + } else if (type == TSDB_FILL_NULL) { + setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); + } else if (type == TSDB_FILL_SET_VALUE) { + tVariantDump(&pCtx->param[1], pCtx->pOutput, pCtx->inputType, true); } else { - if (pCtx->start.key == INT64_MIN) { - assert(pCtx->end.key == INT64_MIN); - return; - } - - if (type == TSDB_FILL_NULL) { - setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes); - } else if (type == TSDB_FILL_SET_VALUE) { - tVariantDump(&pCtx->param[1], pCtx->pOutput, pCtx->inputType, true); - } else if (type == TSDB_FILL_PREV) { - if (IS_NUMERIC_TYPE(pCtx->inputType) || pCtx->inputType == TSDB_DATA_TYPE_BOOL) { - SET_TYPED_DATA(pCtx->pOutput, pCtx->inputType, pCtx->start.val); - } else { - assignVal(pCtx->pOutput, pCtx->start.ptr, pCtx->outputBytes, pCtx->inputType); + if (pCtx->start.key != INT64_MIN && pCtx->start.key < pCtx->startTs && pCtx->end.key > pCtx->startTs) { + if (type == TSDB_FILL_PREV) { + if (IS_NUMERIC_TYPE(pCtx->inputType) || pCtx->inputType == TSDB_DATA_TYPE_BOOL) { + SET_TYPED_DATA(pCtx->pOutput, pCtx->inputType, pCtx->start.val); + } else { + assignVal(pCtx->pOutput, pCtx->start.ptr, pCtx->outputBytes, pCtx->inputType); + } + } else if (type == TSDB_FILL_NEXT) { + if (IS_NUMERIC_TYPE(pCtx->inputType) || pCtx->inputType == TSDB_DATA_TYPE_BOOL) { + SET_TYPED_DATA(pCtx->pOutput, pCtx->inputType, pCtx->end.val); + } else { + assignVal(pCtx->pOutput, pCtx->end.ptr, pCtx->outputBytes, pCtx->inputType); + } + } else if (type == TSDB_FILL_LINEAR) { + SPoint point1 = {.key = pCtx->start.key, .val = &pCtx->start.val}; + SPoint point2 = {.key = pCtx->end.key, .val = &pCtx->end.val}; + SPoint point = {.key = pCtx->startTs, .val = pCtx->pOutput}; + + int32_t srcType = pCtx->inputType; + if (IS_NUMERIC_TYPE(srcType)) { // TODO should find the not null data? + if (isNull((char *)&pCtx->start.val, srcType) || isNull((char *)&pCtx->end.val, srcType)) { + setNull(pCtx->pOutput, srcType, pCtx->inputBytes); + } else { + taosGetLinearInterpolationVal(&point, pCtx->outputType, &point1, &point2, TSDB_DATA_TYPE_DOUBLE); + } + } else { + setNull(pCtx->pOutput, srcType, pCtx->inputBytes); + } } - } else if (type == TSDB_FILL_NEXT) { - if (IS_NUMERIC_TYPE(pCtx->inputType) || pCtx->inputType == TSDB_DATA_TYPE_BOOL) { - SET_TYPED_DATA(pCtx->pOutput, pCtx->inputType, pCtx->end.val); - } else { - assignVal(pCtx->pOutput, pCtx->end.ptr, pCtx->outputBytes, pCtx->inputType); + } else { + // no data generated yet + if (pCtx->size == 1) { + return; } - } else if (type == TSDB_FILL_LINEAR) { - SPoint point1 = {.key = pCtx->start.key, .val = &pCtx->start.val}; - SPoint point2 = {.key = pCtx->end.key, .val = &pCtx->end.val}; - SPoint point = {.key = pCtx->startTs, .val = pCtx->pOutput}; - - int32_t srcType = pCtx->inputType; - if (IS_NUMERIC_TYPE(srcType)) { // TODO should find the not null data? - if (isNull((char *)&pCtx->start.val, srcType) || isNull((char *)&pCtx->end.val, srcType)) { - setNull(pCtx->pOutput, srcType, pCtx->inputBytes); + + // check the timestamp in input buffer + TSKEY skey = GET_TS_DATA(pCtx, 0); + TSKEY ekey = GET_TS_DATA(pCtx, 1); + + // no data generated yet + if (!(skey < pCtx->startTs && ekey > pCtx->startTs)) { + return; + } + + assert(pCtx->start.key == INT64_MIN && skey < pCtx->startTs && ekey > pCtx->startTs); + + if (type == TSDB_FILL_PREV) { + assignVal(pCtx->pOutput, pCtx->pInput, pCtx->outputBytes, pCtx->inputType); + } else if (type == TSDB_FILL_NEXT) { + char* val = ((char*)pCtx->pInput) + pCtx->inputBytes; + assignVal(pCtx->pOutput, val, pCtx->outputBytes, pCtx->inputType); + } else if (type == TSDB_FILL_LINEAR) { + char *start = GET_INPUT_DATA(pCtx, 0); + char *end = GET_INPUT_DATA(pCtx, 1); + + SPoint point1 = {.key = skey, .val = start}; + SPoint point2 = {.key = ekey, .val = end}; + SPoint point = {.key = pCtx->startTs, .val = pCtx->pOutput}; + + int32_t srcType = pCtx->inputType; + if (IS_NUMERIC_TYPE(srcType)) { // TODO should find the not null data? + if (isNull(start, srcType) || isNull(end, srcType)) { + setNull(pCtx->pOutput, srcType, pCtx->inputBytes); + } else { + taosGetLinearInterpolationVal(&point, pCtx->outputType, &point1, &point2, srcType); + } } else { - taosGetLinearInterpolationVal(&point, pCtx->outputType, &point1, &point2, TSDB_DATA_TYPE_DOUBLE); + setNull(pCtx->pOutput, srcType, pCtx->inputBytes); } - } else { - setNull(pCtx->pOutput, srcType, pCtx->inputBytes); } } } SET_VAL(pCtx, 1, 1); - } + static void interp_function(SQLFunctionCtx *pCtx) { // at this point, the value is existed, return directly if (pCtx->size > 0) { @@ -4289,11 +4383,22 @@ static void ts_comp_finalize(SQLFunctionCtx *pCtx) { STSBuf * pTSbuf = pInfo->pTSBuf; tsBufFlush(pTSbuf); - + qDebug("total timestamp :%"PRId64, pTSbuf->numOfTotal); + + // TODO refactor transfer ownership of current file *(FILE **)pCtx->pOutput = pTSbuf->f; + pResInfo->complete = true; + + // get the file size + struct stat fStat; + if ((fstat(fileno(pTSbuf->f), &fStat) == 0)) { + pResInfo->numOfRes = fStat.st_size; + } + pTSbuf->remainOpen = true; tsBufDestroy(pTSbuf); + doFinalizer(pCtx); } @@ -4637,10 +4742,126 @@ static void sumrate_finalizer(SQLFunctionCtx *pCtx) { doFinalizer(pCtx); } +void blockInfo_func(SQLFunctionCtx* pCtx) { + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + STableBlockDist* pDist = (STableBlockDist*) GET_ROWCELL_INTERBUF(pResInfo); -///////////////////////////////////////////////////////////////////////////////////////////// + int32_t len = *(int32_t*) pCtx->pInput; + blockDistInfoFromBinary((char*)pCtx->pInput + sizeof(int32_t), len, pDist); + pDist->rowSize = (int16_t) pCtx->param[0].i64; + + memcpy(pCtx->pOutput, pCtx->pInput, sizeof(int32_t) + len); + pResInfo->numOfRes = 1; + pResInfo->hasResult = DATA_SET_FLAG; +} +static void mergeTableBlockDist(STableBlockDist* pDist, const STableBlockDist* pSrc) { + assert(pDist != NULL && pSrc != NULL); + pDist->numOfTables += pSrc->numOfTables; + pDist->numOfRowsInMemTable += pSrc->numOfRowsInMemTable; + pDist->numOfFiles += pSrc->numOfFiles; + pDist->totalSize += pSrc->totalSize; + + if (pDist->dataBlockInfos == NULL) { + pDist->dataBlockInfos = taosArrayInit(4, sizeof(SFileBlockInfo)); + } + + taosArrayPushBatch(pDist->dataBlockInfos, pSrc->dataBlockInfos->pData, (int32_t) taosArrayGetSize(pSrc->dataBlockInfos)); +} + +void block_func_merge(SQLFunctionCtx* pCtx) { + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + + STableBlockDist* pDist = (STableBlockDist*) GET_ROWCELL_INTERBUF(pResInfo); + STableBlockDist info = {0}; + + int32_t len = *(int32_t*) pCtx->pInput; + blockDistInfoFromBinary(((char*)pCtx->pInput) + sizeof(int32_t), len, &info); + + mergeTableBlockDist(pDist, &info); +} + +static int32_t doGetPercentile(const SArray* pArray, double rate) { + int32_t len = (int32_t)taosArrayGetSize(pArray); + if (len <= 0) { + return 0; + } + + assert(rate >= 0 && rate <= 1.0); + int idx = (int32_t)((len - 1) * rate); + + return ((SFileBlockInfo *)(taosArrayGet(pArray, idx)))->numOfRows; +} + +static int compareBlockInfo(const void *pLeft, const void *pRight) { + int32_t left = ((SFileBlockInfo *)pLeft)->numOfRows; + int32_t right = ((SFileBlockInfo *)pRight)->numOfRows; + + if (left > right) return 1; + if (left < right) return -1; + return 0; +} + +void generateBlockDistResult(STableBlockDist *pTableBlockDist, char* result) { + if (pTableBlockDist == NULL) { + return; + } + + int64_t min = INT64_MAX, max = INT64_MIN, avg = 0; + SArray* blockInfos= pTableBlockDist->dataBlockInfos; + int64_t totalRows = 0, totalBlocks = taosArrayGetSize(blockInfos); + + for (size_t i = 0; i < taosArrayGetSize(blockInfos); i++) { + SFileBlockInfo *blockInfo = taosArrayGet(blockInfos, i); + int64_t rows = blockInfo->numOfRows; + + min = MIN(min, rows); + max = MAX(max, rows); + totalRows += rows; + } + + avg = totalBlocks > 0 ? (int64_t)(totalRows/totalBlocks) : 0; + taosArraySort(blockInfos, compareBlockInfo); + + uint64_t totalLen = pTableBlockDist->totalSize; + int32_t rowSize = pTableBlockDist->rowSize; + + 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=[%"PRId64"], Blocks=[%"PRId64"], Size=[%.3f(Kb)] Comp=[%.2f%%]\n\t " + "RowsInMem=[%d] \n\t SeekHeaderTime=[%d(us)]", + doGetPercentile(blockInfos, 0.05), doGetPercentile(blockInfos, 0.10), + doGetPercentile(blockInfos, 0.20), doGetPercentile(blockInfos, 0.30), + doGetPercentile(blockInfos, 0.40), doGetPercentile(blockInfos, 0.50), + doGetPercentile(blockInfos, 0.60), doGetPercentile(blockInfos, 0.70), + doGetPercentile(blockInfos, 0.80), doGetPercentile(blockInfos, 0.90), + doGetPercentile(blockInfos, 0.95), doGetPercentile(blockInfos, 0.99), + min, max, avg, 0.0, + totalRows, totalBlocks, totalLen/1024.0, (double)(totalLen*100.0)/(rowSize*totalRows), + pTableBlockDist->numOfRowsInMemTable, pTableBlockDist->firstSeekTimeUs); + varDataSetLen(result, sz); + UNUSED(sz); +} + +void blockinfo_func_finalizer(SQLFunctionCtx* pCtx) { + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + STableBlockDist* pDist = (STableBlockDist*) GET_ROWCELL_INTERBUF(pResInfo); + + pDist->rowSize = (int16_t)pCtx->param[0].i64; + generateBlockDistResult(pDist, pCtx->pOutput); + + // cannot set the numOfIteratedElems again since it is set during previous iteration + pResInfo->numOfRes = 1; + pResInfo->hasResult = DATA_SET_FLAG; + + doFinalizer(pCtx); +} + +///////////////////////////////////////////////////////////////////////////////////////////// /* * function compatible list. * tag and ts are not involved in the compatibility check @@ -4659,8 +4880,8 @@ int32_t functionCompatList[] = { 4, -1, -1, 1, 1, 1, 1, 1, 1, -1, // tag, colprj, tagprj, arithmetic, diff, first_dist, last_dist, interp rate irate 1, 1, 1, 1, -1, 1, 1, 5, 1, 1, - // sum_rate, sum_irate, avg_rate, avg_irate - 1, 1, 1, 1, + // sum_rate, sum_irate, avg_rate, avg_irate, tid_tag, blk_info + 1, 1, 1, 1, 6, 7 }; SAggFunctionInfo aAggs[] = {{ @@ -4675,7 +4896,7 @@ SAggFunctionInfo aAggs[] = {{ no_next_step, doFinalizer, count_func_merge, - count_load_data_info, + countRequired, }, { // 1 @@ -4860,7 +5081,7 @@ SAggFunctionInfo aAggs[] = {{ no_next_step, spread_function_finalizer, spread_func_merge, - count_load_data_info, + countRequired, }, { // 14 @@ -4902,7 +5123,7 @@ SAggFunctionInfo aAggs[] = {{ no_next_step, doFinalizer, copy_function, - no_data_info, + noDataRequired, }, { // 17 @@ -4930,7 +5151,7 @@ SAggFunctionInfo aAggs[] = {{ no_next_step, doFinalizer, copy_function, - no_data_info, + noDataRequired, }, { // 19 @@ -4958,7 +5179,7 @@ SAggFunctionInfo aAggs[] = {{ no_next_step, doFinalizer, copy_function, - no_data_info, + noDataRequired, }, { // 21, column project sql function @@ -4986,7 +5207,7 @@ SAggFunctionInfo aAggs[] = {{ no_next_step, doFinalizer, copy_function, - no_data_info, + noDataRequired, }, { // 23 @@ -5159,7 +5380,7 @@ SAggFunctionInfo aAggs[] = {{ }, { // 35 - "tid_tag", // return table id and the corresponding tags for join match and subscribe + "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, @@ -5170,4 +5391,18 @@ SAggFunctionInfo aAggs[] = {{ noop1, noop1, dataBlockRequired, - } }; + }, + { + // 35 + "_block_dist", // return table id and the corresponding tags for join match and subscribe + TSDB_FUNC_BLKINFO, + TSDB_FUNC_BLKINFO, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE, + function_setup, + blockInfo_func, + noop2, + no_next_step, + blockinfo_func_finalizer, + block_func_merge, + dataBlockRequired, + }}; diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 57efcf4b3456dbbda3c4db16a2f31670381346ca..b2d97e2d6935096ed5171d5284cfcb1987e36535 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -26,24 +26,22 @@ #include "queryLog.h" #include "tlosertree.h" #include "ttype.h" - -#define MAX_ROWS_PER_RESBUF_PAGE ((1u<<12) - 1) - -/** - * check if the primary column is load by default, otherwise, the program will - * forced to load primary column explicitly. - */ +#include "tscompression.h" #define IS_MASTER_SCAN(runtime) ((runtime)->scanFlag == MASTER_SCAN) #define IS_REVERSE_SCAN(runtime) ((runtime)->scanFlag == REVERSE_SCAN) #define SET_MASTER_SCAN_FLAG(runtime) ((runtime)->scanFlag = MASTER_SCAN) #define SET_REVERSE_SCAN_FLAG(runtime) ((runtime)->scanFlag = REVERSE_SCAN) -#define GET_QINFO_ADDR(x) ((SQInfo *)((char *)(x)-offsetof(SQInfo, runtimeEnv))) - -#define GET_COL_DATA_POS(query, index, step) ((query)->pos + (index) * (step)) #define SWITCH_ORDER(n) (((n) = ((n) == TSDB_ORDER_ASC) ? TSDB_ORDER_DESC : TSDB_ORDER_ASC)) +#define CHECK_IF_QUERY_KILLED(_q) \ + do { \ + if (isQueryKilled((_q)->qinfo)) { \ + longjmp((_q)->env, TSDB_CODE_TSC_QUERY_CANCELLED); \ + } \ + } while (0) + #define SDATA_BLOCK_INITIALIZER (SDataBlockInfo) {{0}, 0} #define TIME_WINDOW_COPY(_dst, _src) do {\ @@ -57,20 +55,10 @@ enum { TS_JOIN_TAG_NOT_EQUALS = 2, }; -typedef struct { - int32_t status; // query status - TSKEY lastKey; // the lastKey value before query executed - STimeWindow w; // whole query time window - int32_t windowIndex; // index of active time window result for interval query - STSCursor cur; -} SQueryStatusInfo; - -typedef struct { - SArray *dataBlockInfos; - int64_t firstSeekTimeUs; - int64_t numOfRowsInMemTable; - char *result; -} STableBlockDist; +typedef enum SResultTsInterpType { + RESULT_ROW_START_INTERP = 1, + RESULT_ROW_END_INTERP = 2, +} SResultTsInterpType; #if 0 static UNUSED_FUNC void *u_malloc (size_t __size) { @@ -110,8 +98,6 @@ static UNUSED_FUNC void* u_realloc(void* p, size_t __size) { #define GET_NUM_OF_TABLEGROUP(q) taosArrayGetSize((q)->tableqinfoGroupInfo.pGroupList) #define QUERY_IS_INTERVAL_QUERY(_q) ((_q)->interval.interval > 0) -static void finalizeQueryResult(SQueryRuntimeEnv *pRuntimeEnv); - int32_t getMaximumIdleDurationSec() { return tsShellActivityTimer * 2; } @@ -153,84 +139,123 @@ static void getNextTimeWindow(SQuery* pQuery, STimeWindow* tw) { tw->ekey -= 1; } -static void setResultOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, SResultRow *pResult); -static void setResultRowOutputBufInitCtx(SQueryRuntimeEnv *pRuntimeEnv, SResultRow *pResult); +static void doSetTagValueToResultBuf(char* output, const char* val, int16_t type, int16_t bytes); +static void setResultOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, SResultRow *pResult, SQLFunctionCtx* pCtx, + int32_t numOfCols, int32_t* rowCellInfoOffset); + +void setResultRowOutputBufInitCtx(SQueryRuntimeEnv *pRuntimeEnv, SResultRow *pResult, SQLFunctionCtx* pCtx, int32_t numOfOutput, int32_t* rowCellInfoOffset); static bool functionNeedToExecute(SQueryRuntimeEnv *pRuntimeEnv, SQLFunctionCtx *pCtx, int32_t functionId); -static void setExecParams(SQuery *pQuery, SQLFunctionCtx *pCtx, void* inputData, TSKEY *tsCol, SDataBlockInfo* pBlockInfo, - SDataStatis *pStatis, SExprInfo* pExprInfo); +static void setBlockStatisInfo(SQLFunctionCtx *pCtx, SSDataBlock* pSDataBlock, SColIndex* pColIndex); -static void initCtxOutputBuf(SQueryRuntimeEnv *pRuntimeEnv); static void destroyTableQueryInfoImpl(STableQueryInfo *pTableQueryInfo); -static void resetDefaultResInfoOutputBuf(SQueryRuntimeEnv *pRuntimeEnv); static bool hasMainOutput(SQuery *pQuery); -static int32_t setTimestampListJoinInfo(SQInfo *pQInfo, STableQueryInfo *pTableQueryInfo); +static int32_t setTimestampListJoinInfo(SQueryRuntimeEnv* pRuntimeEnv, tVariant* pTag, STableQueryInfo *pTableQueryInfo); static void releaseQueryBuf(size_t numOfTables); static int32_t binarySearchForKey(char *pValue, int num, TSKEY key, int order); -static void doRowwiseTimeWindowInterpolation(SQueryRuntimeEnv* pRuntimeEnv, SArray* pDataBlock, TSKEY prevTs, int32_t prevRowIndex, TSKEY curTs, int32_t curRowIndex, TSKEY windowKey, int32_t type); static STsdbQueryCond createTsdbQueryCond(SQuery* pQuery, STimeWindow* win); -static STableIdInfo createTableIdInfo(SQuery* pQuery); - -bool doFilterData(SQuery *pQuery, int32_t elemPos) { - for (int32_t k = 0; k < pQuery->numOfFilterCols; ++k) { - SSingleColumnFilterInfo *pFilterInfo = &pQuery->pFilterInfo[k]; - - char *pElem = (char*)pFilterInfo->pData + pFilterInfo->info.bytes * elemPos; +static STableIdInfo createTableIdInfo(STableQueryInfo* pTableQueryInfo); + +static void setTableScanFilterOperatorInfo(STableScanInfo* pTableScanInfo, SOperatorInfo* pDownstream); + +static int32_t getNumOfScanTimes(SQuery* pQuery); +static bool isFixedOutputQuery(SQuery* pQuery); + +static SOperatorInfo* createDataBlocksOptScanInfo(void* pTsdbQueryHandle, SQueryRuntimeEnv* pRuntimeEnv, int32_t repeatTime, int32_t reverseTime); +static SOperatorInfo* createTableScanOperator(void* pTsdbQueryHandle, SQueryRuntimeEnv* pRuntimeEnv, int32_t repeatTime); +static SOperatorInfo* createTableSeqScanOperator(void* pTsdbQueryHandle, SQueryRuntimeEnv* pRuntimeEnv); + +static SOperatorInfo* createAggregateOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput); +static SOperatorInfo* createArithOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput); +static SOperatorInfo* createLimitOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream); +static SOperatorInfo* createOffsetOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream); +static SOperatorInfo* createTimeIntervalOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput); +static SOperatorInfo* createSWindowOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput); +static SOperatorInfo* createFillOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput); +static SOperatorInfo* createGroupbyOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput); +static SOperatorInfo* createMultiTableAggOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput); +static SOperatorInfo* createMultiTableTimeIntervalOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput); +static SOperatorInfo* createTagScanOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SExprInfo* pExpr, int32_t numOfOutput); +static SOperatorInfo* createTableBlockInfoScanOperator(void* pTsdbQueryHandle, SQueryRuntimeEnv* pRuntimeEnv); + +static void destroyBasicOperatorInfo(void* param, int32_t numOfOutput); +static void destroySFillOperatorInfo(void* param, int32_t numOfOutput); +static void destroyGroupbyOperatorInfo(void* param, int32_t numOfOutput); +static void destroyArithOperatorInfo(void* param, int32_t numOfOutput); +static void destroyTagScanOperatorInfo(void* param, int32_t numOfOutput); +static void destroyOperatorInfo(SOperatorInfo* pOperator); + +static int32_t doCopyToSDataBlock(SQueryRuntimeEnv* pRuntimeEnv, SGroupResInfo* pGroupResInfo, int32_t orderType, SSDataBlock* pBlock); + +static int32_t getGroupbyColumnIndex(SSqlGroupbyExpr *pGroupbyExpr, SSDataBlock* pDataBlock); +static int32_t setGroupResultOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, SGroupbyOperatorInfo *pInfo, int32_t numOfCols, char *pData, int16_t type, int16_t bytes, int32_t groupIndex); + +static void initCtxOutputBuffer(SQLFunctionCtx* pCtx, int32_t size); +static void getAlignQueryTimeWindow(SQuery *pQuery, int64_t key, int64_t keyFirst, int64_t keyLast, STimeWindow *win); +static bool isPointInterpoQuery(SQuery *pQuery); +static void setResultBufSize(SQuery* pQuery, SRspResultInfo* pResultInfo); +static void setCtxTagForJoin(SQueryRuntimeEnv* pRuntimeEnv, SQLFunctionCtx* pCtx, SExprInfo* pExprInfo, void* pTable); +static void setParamForStableStddev(SQueryRuntimeEnv* pRuntimeEnv, SQLFunctionCtx* pCtx, int32_t numOfOutput, SExprInfo* pExpr); +static void doSetTableGroupOutputBuf(SQueryRuntimeEnv* pRuntimeEnv, SResultRowInfo* pResultRowInfo, + SQLFunctionCtx* pCtx, int32_t* rowCellInfoOffset, int32_t numOfOutput, + int32_t groupIndex); + +// setup the output buffer for each operator +static SSDataBlock* createOutputBuf(SExprInfo* pExpr, int32_t numOfOutput, int32_t numOfRows) { + const static int32_t minSize = 8; + + SSDataBlock *res = calloc(1, sizeof(SSDataBlock)); + res->info.numOfCols = numOfOutput; + + res->pDataBlock = taosArrayInit(numOfOutput, sizeof(SColumnInfoData)); + for (int32_t i = 0; i < numOfOutput; ++i) { + SColumnInfoData idata = {{0}}; + idata.info.type = pExpr[i].type; + idata.info.bytes = pExpr[i].bytes; + idata.info.colId = pExpr[i].base.resColId; - bool qualified = false; - for (int32_t j = 0; j < pFilterInfo->numOfFilters; ++j) { - SColumnFilterElem *pFilterElem = &pFilterInfo->pFilters[j]; + idata.pData = calloc(1, MAX(idata.info.bytes * numOfRows, minSize)); // at least to hold a pointer on x64 platform + taosArrayPush(res->pDataBlock, &idata); + } - bool isnull = isNull(pElem, pFilterInfo->info.type); - if (isnull) { - if (pFilterElem->fp == isNullOperator) { - qualified = true; - break; - } else { - continue; - } - } else { - if (pFilterElem->fp == notNullOperator) { - qualified = true; - break; - } else if (pFilterElem->fp == isNullOperator) { - continue; - } - } + return res; +} - if (pFilterElem->fp(pFilterElem, pElem, pElem, pFilterInfo->info.type)) { - qualified = true; - break; - } - } +static void* destroyOutputBuf(SSDataBlock* pBlock) { + if (pBlock == NULL) { + return NULL; + } - if (!qualified) { - return false; - } + int32_t numOfOutput = pBlock->info.numOfCols; + for(int32_t i = 0; i < numOfOutput; ++i) { + SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i); + tfree(pColInfoData->pData); } - return true; + taosArrayDestroy(pBlock->pDataBlock); + tfree(pBlock->pBlockStatis); + tfree(pBlock); + return NULL; } -int64_t getNumOfResult(SQueryRuntimeEnv *pRuntimeEnv) { +int32_t getNumOfResult(SQueryRuntimeEnv *pRuntimeEnv, SQLFunctionCtx* pCtx, int32_t numOfOutput) { SQuery *pQuery = pRuntimeEnv->pQuery; bool hasMainFunction = hasMainOutput(pQuery); - int64_t maxOutput = 0; - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - int32_t functionId = pQuery->pExpr1[j].base.functionId; + int32_t maxOutput = 0; + for (int32_t j = 0; j < numOfOutput; ++j) { + int32_t id = pCtx[j].functionId; /* * ts, tag, tagprj function can not decide the output number of current query * the number of output result is decided by main output */ - if (hasMainFunction && - (functionId == TSDB_FUNC_TS || functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_TAGPRJ)) { + if (hasMainFunction && (id == TSDB_FUNC_TS || id == TSDB_FUNC_TAG || id == TSDB_FUNC_TAGPRJ)) { continue; } - SResultRowCellInfo *pResInfo = GET_RES_INFO(&pRuntimeEnv->pCtx[j]); + SResultRowCellInfo *pResInfo = GET_RES_INFO(&pCtx[j]); if (pResInfo != NULL && maxOutput < pResInfo->numOfRes) { maxOutput = pResInfo->numOfRes; } @@ -240,27 +265,14 @@ int64_t getNumOfResult(SQueryRuntimeEnv *pRuntimeEnv) { return maxOutput; } -/* - * the value of number of result needs to be update due to offset value upated. - */ -void updateNumOfResult(SQueryRuntimeEnv *pRuntimeEnv, int32_t numOfRes) { - SQuery *pQuery = pRuntimeEnv->pQuery; - - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - SResultRowCellInfo *pResInfo = GET_RES_INFO(&pRuntimeEnv->pCtx[j]); - - int16_t functionId = pRuntimeEnv->pCtx[j].functionId; - if (functionId == TSDB_FUNC_TS || functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_TAGPRJ || - functionId == TSDB_FUNC_TS_DUMMY) { - continue; - } - - assert(pResInfo->numOfRes > numOfRes); - pResInfo->numOfRes = numOfRes; +static void clearNumOfRes(SQLFunctionCtx* pCtx, int32_t numOfOutput) { + for (int32_t j = 0; j < numOfOutput; ++j) { + SResultRowCellInfo *pResInfo = GET_RES_INFO(&pCtx[j]); + pResInfo->numOfRes = 0; } } -bool isGroupbyColumn(SSqlGroupbyExpr *pGroupbyExpr) { +static bool isGroupbyColumn(SSqlGroupbyExpr *pGroupbyExpr) { if (pGroupbyExpr == NULL || pGroupbyExpr->numOfGroupCols == 0) { return false; } @@ -280,7 +292,7 @@ bool isGroupbyColumn(SSqlGroupbyExpr *pGroupbyExpr) { return false; } -bool isStabledev(SQuery* pQuery) { +static bool isStabledev(SQuery* pQuery) { for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { int32_t functId = pQuery->pExpr1[i].base.functionId; if (functId == TSDB_FUNC_STDDEV_DST) { @@ -291,36 +303,12 @@ bool isStabledev(SQuery* pQuery) { return false; } -int16_t getGroupbyColumnType(SQuery *pQuery, SSqlGroupbyExpr *pGroupbyExpr) { - assert(pGroupbyExpr != NULL); - - int32_t colId = -2; - int16_t type = TSDB_DATA_TYPE_NULL; - - for (int32_t i = 0; i < pGroupbyExpr->numOfGroupCols; ++i) { - SColIndex *pColIndex = taosArrayGet(pGroupbyExpr->columnInfo, i); - if (TSDB_COL_IS_NORMAL_COL(pColIndex->flag)) { - colId = pColIndex->colId; - break; - } - } - - for (int32_t i = 0; i < pQuery->numOfCols; ++i) { - if (colId == pQuery->colList[i].colId) { - type = pQuery->colList[i].type; - break; - } - } - - return type; -} - -static bool isSelectivityWithTagsQuery(SQuery *pQuery) { +static bool isSelectivityWithTagsQuery(SQLFunctionCtx *pCtx, int32_t numOfOutput) { bool hasTags = false; int32_t numOfSelectivity = 0; - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - int32_t functId = pQuery->pExpr1[i].base.functionId; + for (int32_t i = 0; i < numOfOutput; ++i) { + int32_t functId = pCtx[i].functionId; if (functId == TSDB_FUNC_TAG_DUMMY || functId == TSDB_FUNC_TS_DUMMY) { hasTags = true; continue; @@ -331,11 +319,7 @@ static bool isSelectivityWithTagsQuery(SQuery *pQuery) { } } - if (numOfSelectivity > 0 && hasTags) { - return true; - } - - return false; + return (numOfSelectivity > 0 && hasTags); } static bool isProjQuery(SQuery *pQuery) { @@ -351,20 +335,6 @@ static bool isProjQuery(SQuery *pQuery) { static bool isTsCompQuery(SQuery *pQuery) { return pQuery->pExpr1[0].base.functionId == TSDB_FUNC_TS_COMP; } -static bool limitOperator(SQuery* pQuery, void* qinfo) { - if ((pQuery->limit.limit > 0) && (pQuery->rec.total + pQuery->rec.rows > pQuery->limit.limit)) { - pQuery->rec.rows = pQuery->limit.limit - pQuery->rec.total; - - qDebug("QInfo:%p discard remain data due to result limitation, limit:%"PRId64", current return:%" PRId64 ", total:%"PRId64, - qinfo, pQuery->limit.limit, pQuery->rec.rows, pQuery->rec.total + pQuery->rec.rows); - assert(pQuery->rec.rows >= 0); - setQueryStatus(pQuery, QUERY_COMPLETED); - return true; - } - - return false; -} - static bool isTopBottomQuery(SQuery *pQuery) { for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { int32_t functionId = pQuery->pExpr1[i].base.functionId; @@ -409,33 +379,44 @@ static bool hasTagValOutput(SQuery* pQuery) { return false; } -/** - * @param pQuery - * @param col - * @param pDataBlockInfo - * @param pStatis - * @param pColStatis - * @return - */ -static bool hasNullValue(SColIndex* pColIndex, SDataStatis *pStatis, SDataStatis **pColStatis) { - if (pStatis != NULL && TSDB_COL_IS_NORMAL_COL(pColIndex->flag)) { - *pColStatis = &pStatis[pColIndex->colIndex]; - assert((*pColStatis)->colId == pColIndex->colId); - } else { - *pColStatis = NULL; - } - +static bool hasNullRv(SColIndex* pColIndex, SDataStatis *pStatis) { if (TSDB_COL_IS_TAG(pColIndex->flag) || TSDB_COL_IS_UD_COL(pColIndex->flag) || pColIndex->colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) { return false; } - if ((*pColStatis) != NULL && (*pColStatis)->numOfNull == 0) { + if (pStatis != NULL && pStatis->numOfNull == 0) { return false; } return true; } +static void prepareResultListBuffer(SResultRowInfo* pResultRowInfo, SQueryRuntimeEnv* pRuntimeEnv) { + // more than the capacity, reallocate the resources + if (pResultRowInfo->size < pResultRowInfo->capacity) { + return; + } + + int64_t newCapacity = 0; + if (pResultRowInfo->capacity > 10000) { + newCapacity = (int64_t)(pResultRowInfo->capacity * 1.25); + } else { + newCapacity = (int64_t)(pResultRowInfo->capacity * 1.5); + } + + char *t = realloc(pResultRowInfo->pResult, (size_t)(newCapacity * POINTER_BYTES)); + if (t == NULL) { + longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY); + } + + pResultRowInfo->pResult = (SResultRow **)t; + + int32_t inc = (int32_t)newCapacity - pResultRowInfo->capacity; + memset(&pResultRowInfo->pResult[pResultRowInfo->capacity], 0, POINTER_BYTES * inc); + + pResultRowInfo->capacity = (int32_t)newCapacity; +} + static SResultRow *doPrepareResultRowFromKey(SQueryRuntimeEnv *pRuntimeEnv, SResultRowInfo *pResultRowInfo, char *pData, int16_t bytes, bool masterscan, uint64_t uid) { bool existed = false; @@ -466,28 +447,7 @@ static SResultRow *doPrepareResultRowFromKey(SQueryRuntimeEnv *pRuntimeEnv, SRes } if (!existed) { - // TODO refactor - // more than the capacity, reallocate the resources - if (pResultRowInfo->size >= pResultRowInfo->capacity) { - int64_t newCapacity = 0; - if (pResultRowInfo->capacity > 10000) { - newCapacity = (int64_t)(pResultRowInfo->capacity * 1.25); - } else { - newCapacity = (int64_t)(pResultRowInfo->capacity * 1.5); - } - - char *t = realloc(pResultRowInfo->pResult, (size_t)(newCapacity * POINTER_BYTES)); - if (t == NULL) { - longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY); - } - - pResultRowInfo->pResult = (SResultRow **)t; - - int32_t inc = (int32_t)newCapacity - pResultRowInfo->capacity; - memset(&pResultRowInfo->pResult[pResultRowInfo->capacity], 0, POINTER_BYTES * inc); - - pResultRowInfo->capacity = (int32_t)newCapacity; - } + prepareResultListBuffer(pResultRowInfo, pRuntimeEnv); SResultRow *pResult = NULL; @@ -516,20 +476,50 @@ static SResultRow *doPrepareResultRowFromKey(SQueryRuntimeEnv *pRuntimeEnv, SRes return getResultRow(pResultRowInfo, pResultRowInfo->curIndex); } +static void getInitialStartTimeWindow(SQuery* pQuery, TSKEY ts, STimeWindow* w) { + if (QUERY_IS_ASC_QUERY(pQuery)) { + getAlignQueryTimeWindow(pQuery, ts, ts, pQuery->window.ekey, w); + } else { + // the start position of the first time window in the endpoint that spreads beyond the queried last timestamp + getAlignQueryTimeWindow(pQuery, ts, pQuery->window.ekey, ts, w); + + int64_t key = w->skey; + while(key < ts) { // moving towards end + if (pQuery->interval.intervalUnit == 'n' || pQuery->interval.intervalUnit == 'y') { + key = taosTimeAdd(key, pQuery->interval.sliding, pQuery->interval.slidingUnit, pQuery->precision); + } else { + key += pQuery->interval.sliding; + } + + if (key >= ts) { + break; + } + + w->skey = key; + } + } +} + // get the correct time window according to the handled timestamp -static STimeWindow getActiveTimeWindow(SResultRowInfo *pWindowResInfo, int64_t ts, SQuery *pQuery) { +static STimeWindow getActiveTimeWindow(SResultRowInfo * pResultRowInfo, int64_t ts, SQuery *pQuery) { STimeWindow w = {0}; - if (pWindowResInfo->curIndex == -1) { // the first window, from the previous stored value - w.skey = pWindowResInfo->prevSKey; + if (pResultRowInfo->curIndex == -1) { // the first window, from the previous stored value + if (pResultRowInfo->prevSKey == TSKEY_INITIAL_VAL) { + getInitialStartTimeWindow(pQuery, ts, &w); + pResultRowInfo->prevSKey = w.skey; + } else { + w.skey = pResultRowInfo->prevSKey; + } + if (pQuery->interval.intervalUnit == 'n' || pQuery->interval.intervalUnit == 'y') { w.ekey = taosTimeAdd(w.skey, pQuery->interval.interval, pQuery->interval.intervalUnit, pQuery->precision) - 1; } else { w.ekey = w.skey + pQuery->interval.interval - 1; } } else { - int32_t slot = curTimeWindowIndex(pWindowResInfo); - SResultRow* pWindowRes = getResultRow(pWindowResInfo, slot); + int32_t slot = curTimeWindowIndex(pResultRowInfo); + SResultRow* pWindowRes = getResultRow(pResultRowInfo, slot); w = pWindowRes->win; } @@ -565,8 +555,8 @@ static STimeWindow getActiveTimeWindow(SResultRowInfo *pWindowResInfo, int64_t t return w; } -static int32_t addNewWindowResultBuf(SResultRow *pWindowRes, SDiskbasedResultBuf *pResultBuf, int32_t tid, - int32_t numOfRowsPerPage) { +// a new buffer page for each table. Needs to opt this design +static int32_t addNewWindowResultBuf(SResultRow *pWindowRes, SDiskbasedResultBuf *pResultBuf, int32_t tid, uint32_t size) { if (pWindowRes->pageId != -1) { return 0; } @@ -584,7 +574,7 @@ static int32_t addNewWindowResultBuf(SResultRow *pWindowRes, SDiskbasedResultBuf pData = getResBufPage(pResultBuf, pi->pageId); pageId = pi->pageId; - if (pData->num >= numOfRowsPerPage) { + if (pData->num + size > pResultBuf->pageSize) { // release current page first, and prepare the next one releaseResBufPageInfo(pResultBuf, pi); pData = getNewDataBuf(pResultBuf, tid, &pageId); @@ -601,8 +591,9 @@ static int32_t addNewWindowResultBuf(SResultRow *pWindowRes, SDiskbasedResultBuf // set the number of rows in current disk page if (pWindowRes->pageId == -1) { // not allocated yet, allocate new buffer pWindowRes->pageId = pageId; - pWindowRes->rowId = (int32_t)(pData->num++); + pWindowRes->offset = (int32_t)pData->num; + pData->num += size; assert(pWindowRes->pageId >= 0); } @@ -610,7 +601,8 @@ static int32_t addNewWindowResultBuf(SResultRow *pWindowRes, SDiskbasedResultBuf } static int32_t setWindowOutputBufByKey(SQueryRuntimeEnv *pRuntimeEnv, SResultRowInfo *pResultRowInfo, STimeWindow *win, - bool masterscan, SResultRow **pResult, int64_t groupId) { + bool masterscan, SResultRow **pResult, int64_t groupId, SQLFunctionCtx* pCtx, + int32_t numOfOutput, int32_t* rowCellInfoOffset) { assert(win->skey <= win->ekey); SDiskbasedResultBuf *pResultBuf = pRuntimeEnv->pResultBuf; @@ -622,7 +614,7 @@ static int32_t setWindowOutputBufByKey(SQueryRuntimeEnv *pRuntimeEnv, SResultRow // not assign result buffer yet, add new result buffer if (pResultRow->pageId == -1) { - int32_t ret = addNewWindowResultBuf(pResultRow, pResultBuf, (int32_t) groupId, pRuntimeEnv->numOfRowsPerPage); + int32_t ret = addNewWindowResultBuf(pResultRow, pResultBuf, (int32_t) groupId, pRuntimeEnv->pQuery->intermediateResultRowSize); if (ret != TSDB_CODE_SUCCESS) { return -1; } @@ -631,21 +623,11 @@ static int32_t setWindowOutputBufByKey(SQueryRuntimeEnv *pRuntimeEnv, SResultRow // set time window for current result pResultRow->win = (*win); *pResult = pResultRow; - setResultRowOutputBufInitCtx(pRuntimeEnv, pResultRow); + setResultRowOutputBufInitCtx(pRuntimeEnv, pResultRow, pCtx, numOfOutput, rowCellInfoOffset); return TSDB_CODE_SUCCESS; } -static bool getResultRowStatus(SResultRowInfo *pWindowResInfo, int32_t slot) { - assert(slot >= 0 && slot < pWindowResInfo->size); - return pWindowResInfo->pResult[slot]->closed; -} - -typedef enum SResultTsInterpType { - RESULT_ROW_START_INTERP = 1, - RESULT_ROW_END_INTERP = 2, -} SResultTsInterpType; - static void setResultRowInterpo(SResultRow* pResult, SResultTsInterpType type) { assert(pResult != NULL && (type == RESULT_ROW_START_INTERP || type == RESULT_ROW_END_INTERP)); if (type == RESULT_ROW_START_INTERP) { @@ -743,13 +725,14 @@ static void doUpdateResultRowIndex(SResultRowInfo*pResultRowInfo, TSKEY lastKey, } } -static void updateResultRowIndex(SResultRowInfo* pResultRowInfo, STableQueryInfo* pTableQueryInfo, bool ascQuery, bool timeWindowInterpo) { - if ((pTableQueryInfo->lastKey > pTableQueryInfo->win.ekey && ascQuery) || (pTableQueryInfo->lastKey < pTableQueryInfo->win.ekey && (!ascQuery))) { +static void updateResultRowInfoActiveIndex(SResultRowInfo* pResultRowInfo, SQuery* pQuery, TSKEY lastKey) { + bool ascQuery = QUERY_IS_ASC_QUERY(pQuery); + if ((lastKey > pQuery->window.ekey && ascQuery) || (lastKey < pQuery->window.ekey && (!ascQuery))) { closeAllResultRows(pResultRowInfo); pResultRowInfo->curIndex = pResultRowInfo->size - 1; } else { - int32_t step = ascQuery? 1:-1; - doUpdateResultRowIndex(pResultRowInfo, pTableQueryInfo->lastKey - step, ascQuery, timeWindowInterpo); + int32_t step = ascQuery ? 1 : -1; + doUpdateResultRowIndex(pResultRowInfo, lastKey - step, ascQuery, pQuery->timeWindowInterpo); } } @@ -793,31 +776,27 @@ static int32_t getNumOfRowsInTimeWindow(SQuery *pQuery, SDataBlockInfo *pDataBlo return num; } -static char *getDataBlock(SQueryRuntimeEnv *pRuntimeEnv, SArithmeticSupport *sas, int32_t col, int32_t size, SArray *pDataBlock); - -static void doBlockwiseApplyFunctions(SQueryRuntimeEnv *pRuntimeEnv, STimeWindow *pWin, int32_t offset, - int32_t forwardStep, TSKEY *tsCol, int32_t numOfTotal, SArray *pDataBlock) { - SQuery * pQuery = pRuntimeEnv->pQuery; - SQLFunctionCtx *pCtx = pRuntimeEnv->pCtx; - +static void doApplyFunctions(SQueryRuntimeEnv* pRuntimeEnv, SQLFunctionCtx* pCtx, STimeWindow* pWin, int32_t offset, + int32_t forwardStep, TSKEY* tsCol, int32_t numOfTotal, int32_t numOfOutput) { + SQuery *pQuery = pRuntimeEnv->pQuery; bool hasPrev = pCtx[0].preAggVals.isSet; - for (int32_t k = 0; k < pQuery->numOfOutput; ++k) { + for (int32_t k = 0; k < numOfOutput; ++k) { pCtx[k].size = forwardStep; pCtx[k].startTs = pWin->skey; - char *dataBlock = getDataBlock(pRuntimeEnv, &pRuntimeEnv->sasArray[k], k, numOfTotal, pDataBlock); + char* start = pCtx[k].pInput; int32_t pos = (QUERY_IS_ASC_QUERY(pQuery)) ? offset : offset - (forwardStep - 1); - if (dataBlock != NULL) { - pCtx[k].pInput = (char *)dataBlock + pos * pCtx[k].inputBytes; + if (pCtx[k].pInput != NULL) { + pCtx[k].pInput = (char *)pCtx[k].pInput + pos * pCtx[k].inputBytes; } if (tsCol != NULL) { pCtx[k].ptsList = &tsCol[pos]; } - int32_t functionId = pQuery->pExpr1[k].base.functionId; + int32_t functionId = pCtx[k].functionId; // not a whole block involved in query processing, statistics data can not be used // NOTE: the original value of isSet have been changed here @@ -831,27 +810,13 @@ static void doBlockwiseApplyFunctions(SQueryRuntimeEnv *pRuntimeEnv, STimeWindow // restore it pCtx[k].preAggVals.isSet = hasPrev; + pCtx[k].pInput = start; } } -static void doRowwiseApplyFunctions(SQueryRuntimeEnv *pRuntimeEnv, STimeWindow *pWin, int32_t offset) { - SQuery *pQuery = pRuntimeEnv->pQuery; - SQLFunctionCtx *pCtx = pRuntimeEnv->pCtx; - - for (int32_t k = 0; k < pQuery->numOfOutput; ++k) { - pCtx[k].startTs = pWin->skey; - - int32_t functionId = pQuery->pExpr1[k].base.functionId; - if (functionNeedToExecute(pRuntimeEnv, &pCtx[k], functionId)) { - aAggs[functionId].xFunctionF(&pCtx[k], offset); - } - } -} -static int32_t getNextQualifiedWindow(SQueryRuntimeEnv *pRuntimeEnv, STimeWindow *pNext, SDataBlockInfo *pDataBlockInfo, +static int32_t getNextQualifiedWindow(SQuery* pQuery, STimeWindow *pNext, SDataBlockInfo *pDataBlockInfo, TSKEY *primaryKeys, __block_search_fn_t searchFn, int32_t prevPosition) { - SQuery *pQuery = pRuntimeEnv->pQuery; - getNextTimeWindow(pQuery, pNext); // next time window is not in current block @@ -941,126 +906,251 @@ static FORCE_INLINE TSKEY reviseWindowEkey(SQuery *pQuery, STimeWindow *pWindow) return ekey; } -//todo binary search -static void* getDataBlockImpl(SArray* pDataBlock, int32_t colId) { - int32_t numOfCols = (int32_t)taosArrayGetSize(pDataBlock); - - for (int32_t i = 0; i < numOfCols; ++i) { - SColumnInfoData *p = taosArrayGet(pDataBlock, i); - if (colId == p->info.colId) { - return p->pData; +static void setNotInterpoWindowKey(SQLFunctionCtx* pCtx, int32_t numOfOutput, int32_t type) { + if (type == RESULT_ROW_START_INTERP) { + for (int32_t k = 0; k < numOfOutput; ++k) { + pCtx[k].start.key = INT64_MIN; + } + } else { + for (int32_t k = 0; k < numOfOutput; ++k) { + pCtx[k].end.key = INT64_MIN; } } - - return NULL; } -// todo refactor -static char *getDataBlock(SQueryRuntimeEnv *pRuntimeEnv, SArithmeticSupport *sas, int32_t col, int32_t size, SArray *pDataBlock) { +// window start key interpolation + + +static void saveDataBlockLastRow(SQueryRuntimeEnv* pRuntimeEnv, SDataBlockInfo* pDataBlockInfo, SArray* pDataBlock, + int32_t rowIndex) { if (pDataBlock == NULL) { - return NULL; + return; } - char *dataBlock = NULL; - SQuery *pQuery = pRuntimeEnv->pQuery; + SQuery* pQuery = pRuntimeEnv->pQuery; + for (int32_t k = 0; k < pQuery->numOfCols; ++k) { + SColumnInfoData *pColInfo = taosArrayGet(pDataBlock, k); + memcpy(pRuntimeEnv->prevRow[k], ((char*)pColInfo->pData) + (pColInfo->info.bytes * rowIndex), pColInfo->info.bytes); + } +} - int32_t functionId = pQuery->pExpr1[col].base.functionId; - if (functionId == TSDB_FUNC_ARITHM) { - sas->pArithExpr = &pQuery->pExpr1[col]; - sas->offset = (QUERY_IS_ASC_QUERY(pQuery))? pQuery->pos : pQuery->pos - (size - 1); - sas->colList = pQuery->colList; - sas->numOfCols = pQuery->numOfCols; - - // here the pQuery->colList and sas->colList are identical - int32_t numOfCols = (int32_t)taosArrayGetSize(pDataBlock); - for (int32_t i = 0; i < pQuery->numOfCols; ++i) { - SColumnInfo *pColMsg = &pQuery->colList[i]; - - dataBlock = NULL; - for (int32_t k = 0; k < numOfCols; ++k) { //todo refactor - SColumnInfoData *p = taosArrayGet(pDataBlock, k); - if (pColMsg->colId == p->info.colId) { - dataBlock = p->pData; - break; - } - } +static TSKEY getStartTsKey(SQuery* pQuery, STimeWindow* win, const TSKEY* tsCols, int32_t rows) { + TSKEY ts = TSKEY_INITIAL_VAL; + + bool ascQuery = QUERY_IS_ASC_QUERY(pQuery); + if (tsCols == NULL) { + ts = ascQuery? win->skey : win->ekey; + } else { + int32_t offset = ascQuery? 0:rows-1; + ts = tsCols[offset]; + } + + return ts; +} + +static void setArithParams(SArithmeticSupport* sas, SExprInfo *pExprInfo, SSDataBlock* pSDataBlock) { + sas->numOfCols = (int32_t) pSDataBlock->info.numOfCols; + sas->pArithExpr = pExprInfo; + + sas->colList = calloc(1, pSDataBlock->info.numOfCols*sizeof(SColumnInfo)); + for(int32_t i = 0; i < sas->numOfCols; ++i) { + SColumnInfoData* pColData = taosArrayGet(pSDataBlock->pDataBlock, i); + sas->colList[i] = pColData->info; + } + + sas->data = calloc(sas->numOfCols, POINTER_BYTES); + + // set the input column data + for (int32_t f = 0; f < pSDataBlock->info.numOfCols; ++f) { + SColumnInfoData *pColumnInfoData = taosArrayGet(pSDataBlock->pDataBlock, f); + sas->data[f] = pColumnInfoData->pData; + } +} + +static void doSetInputDataBlock(SOperatorInfo* pOperator, SQLFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t order); +static void doSetInputDataBlockInfo(SOperatorInfo* pOperator, SQLFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t order) { + for (int32_t i = 0; i < pOperator->numOfOutput; ++i) { + pCtx[i].order = order; + pCtx[i].size = pBlock->info.rows; + pCtx[i].currentStage = (uint8_t)pOperator->pRuntimeEnv->scanFlag; + + setBlockStatisInfo(&pCtx[i], pBlock, &pOperator->pExpr[i].base.colInfo); + } +} - assert(dataBlock != NULL); - sas->data[i] = dataBlock; // start from the offset +static void setInputDataBlock(SOperatorInfo* pOperator, SQLFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t order) { + if (pCtx[0].functionId == TSDB_FUNC_ARITHM) { + SArithmeticSupport* pSupport = (SArithmeticSupport*) pCtx[0].param[1].pz; + if (pSupport->colList == NULL) { + doSetInputDataBlock(pOperator, pCtx, pBlock, order); + } else { + doSetInputDataBlockInfo(pOperator, pCtx, pBlock, order); + } + } else { + if (pCtx[0].pInput == NULL && pBlock->pDataBlock != NULL) { + doSetInputDataBlock(pOperator, pCtx, pBlock, order); + } else { + doSetInputDataBlockInfo(pOperator, pCtx, pBlock, order); } + } +} + +static void doSetInputDataBlock(SOperatorInfo* pOperator, SQLFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t order) { + for (int32_t i = 0; i < pOperator->numOfOutput; ++i) { + pCtx[i].order = order; + pCtx[i].size = pBlock->info.rows; + pCtx[i].currentStage = (uint8_t)pOperator->pRuntimeEnv->scanFlag; - } else { // other type of query function - SColIndex *pCol = &pQuery->pExpr1[col].base.colInfo; - if (TSDB_COL_IS_NORMAL_COL(pCol->flag)) { - SColIndex* pColIndex = &pQuery->pExpr1[col].base.colInfo; - SColumnInfoData *p = taosArrayGet(pDataBlock, pColIndex->colIndex); - assert(p->info.colId == pColIndex->colId); + setBlockStatisInfo(&pCtx[i], pBlock, &pOperator->pExpr[i].base.colInfo); - dataBlock = p->pData; + if (pCtx[i].functionId == TSDB_FUNC_ARITHM) { + setArithParams((SArithmeticSupport*)pCtx[i].param[1].pz, &pOperator->pExpr[i], pBlock); } else { - dataBlock = NULL; + SColIndex* pCol = &pOperator->pExpr[i].base.colInfo; + if (TSDB_COL_IS_NORMAL_COL(pCol->flag) || pCol->colId == TSDB_BLOCK_DIST_COLUMN_INDEX) { + SColIndex* pColIndex = &pOperator->pExpr[i].base.colInfo; + SColumnInfoData* p = taosArrayGet(pBlock->pDataBlock, pColIndex->colIndex); + + // in case of the block distribution query, the inputBytes is not a constant value. + pCtx[i].pInput = p->pData; + assert(p->info.colId == pColIndex->colId && pCtx[i].inputType == p->info.type);// && pCtx[i].inputBytes == p->info.bytes); + + uint32_t status = aAggs[pCtx[i].functionId].status; + if ((status & (TSDB_FUNCSTATE_SELECTIVITY | TSDB_FUNCSTATE_NEED_TS)) != 0) { + SColumnInfoData* tsInfo = taosArrayGet(pBlock->pDataBlock, 0); + pCtx[i].ptsList = (int64_t*) tsInfo->pData; + } + } + } + } +} + +static void doAggregateImpl(SOperatorInfo* pOperator, TSKEY startTs, SQLFunctionCtx* pCtx, SSDataBlock* pSDataBlock) { + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; + + for (int32_t k = 0; k < pOperator->numOfOutput; ++k) { + int32_t functionId = pCtx[k].functionId; + if (functionNeedToExecute(pRuntimeEnv, &pCtx[k], functionId)) { + pCtx[k].startTs = startTs;// this can be set during create the struct + aAggs[functionId].xFunction(&pCtx[k]); } } +} + +static void arithmeticApplyFunctions(SQueryRuntimeEnv *pRuntimeEnv, SQLFunctionCtx *pCtx, int32_t numOfOutput) { + SQuery *pQuery = pRuntimeEnv->pQuery; - return dataBlock; + for (int32_t k = 0; k < numOfOutput; ++k) { + pCtx[k].startTs = pQuery->window.skey; + aAggs[pCtx[k].functionId].xFunction(&pCtx[k]); + } } -static void setNotInterpoWindowKey(SQLFunctionCtx* pCtx, int32_t numOfOutput, int32_t type) { - if (type == RESULT_ROW_START_INTERP) { - for (int32_t k = 0; k < numOfOutput; ++k) { +void doTimeWindowInterpolation(SOperatorInfo* pOperator, SOptrBasicInfo* pInfo, SArray* pDataBlock, TSKEY prevTs, + int32_t prevRowIndex, TSKEY curTs, int32_t curRowIndex, TSKEY windowKey, int32_t type) { + SQueryRuntimeEnv *pRuntimeEnv = pOperator->pRuntimeEnv; + SExprInfo* pExpr = pOperator->pExpr; + + SQLFunctionCtx* pCtx = pInfo->pCtx; + + for (int32_t k = 0; k < pOperator->numOfOutput; ++k) { + int32_t functionId = pCtx[k].functionId; + if (functionId != TSDB_FUNC_TWA && functionId != TSDB_FUNC_INTERP) { pCtx[k].start.key = INT64_MIN; + continue; } - } else { - for (int32_t k = 0; k < numOfOutput; ++k) { - pCtx[k].end.key = INT64_MIN; + + SColIndex * pColIndex = &pExpr[k].base.colInfo; + int16_t index = pColIndex->colIndex; + SColumnInfoData *pColInfo = taosArrayGet(pDataBlock, index); + + assert(pColInfo->info.colId == pColIndex->colId && curTs != windowKey); + double v1 = 0, v2 = 0, v = 0; + + if (prevRowIndex == -1) { + GET_TYPED_DATA(v1, double, pColInfo->info.type, (char *)pRuntimeEnv->prevRow[index]); + } else { + GET_TYPED_DATA(v1, double, pColInfo->info.type, (char *)pColInfo->pData + prevRowIndex * pColInfo->info.bytes); + } + + GET_TYPED_DATA(v2, double, pColInfo->info.type, (char *)pColInfo->pData + curRowIndex * pColInfo->info.bytes); + + if (functionId == TSDB_FUNC_INTERP) { + if (type == RESULT_ROW_START_INTERP) { + pCtx[k].start.key = prevTs; + pCtx[k].start.val = v1; + + pCtx[k].end.key = curTs; + pCtx[k].end.val = v2; + } + } else if (functionId == TSDB_FUNC_TWA) { + SPoint point1 = (SPoint){.key = prevTs, .val = &v1}; + SPoint point2 = (SPoint){.key = curTs, .val = &v2}; + SPoint point = (SPoint){.key = windowKey, .val = &v }; + + taosGetLinearInterpolationVal(&point, TSDB_DATA_TYPE_DOUBLE, &point1, &point2, TSDB_DATA_TYPE_DOUBLE); + + if (type == RESULT_ROW_START_INTERP) { + pCtx[k].start.key = point.key; + pCtx[k].start.val = v; + } else { + pCtx[k].end.key = point.key; + pCtx[k].end.val = v; + } } } } -// window start key interpolation -static bool setTimeWindowInterpolationStartTs(SQueryRuntimeEnv* pRuntimeEnv, int32_t pos, int32_t numOfRows, - SArray* pDataBlock, TSKEY* tsCols, STimeWindow* win, int16_t type) { +static bool setTimeWindowInterpolationStartTs(SOperatorInfo* pOperatorInfo, SQLFunctionCtx* pCtx, int32_t pos, + int32_t numOfRows, SArray* pDataBlock, const TSKEY* tsCols, STimeWindow* win) { + SQueryRuntimeEnv* pRuntimeEnv = pOperatorInfo->pRuntimeEnv; SQuery* pQuery = pRuntimeEnv->pQuery; + bool ascQuery = QUERY_IS_ASC_QUERY(pQuery); + TSKEY curTs = tsCols[pos]; TSKEY lastTs = *(TSKEY *) pRuntimeEnv->prevRow[0]; // lastTs == INT64_MIN and pos == 0 means this is the first time window, interpolation is not needed. // start exactly from this point, no need to do interpolation - TSKEY key = QUERY_IS_ASC_QUERY(pQuery)? win->skey:win->ekey; + TSKEY key = ascQuery? win->skey:win->ekey; if (key == curTs) { - setNotInterpoWindowKey(pRuntimeEnv->pCtx, pQuery->numOfOutput, RESULT_ROW_START_INTERP); + setNotInterpoWindowKey(pCtx, pOperatorInfo->numOfOutput, RESULT_ROW_START_INTERP); return true; } - if (lastTs == INT64_MIN && ((pos == 0 && QUERY_IS_ASC_QUERY(pQuery)) || (pos == (numOfRows - 1) && !QUERY_IS_ASC_QUERY(pQuery)))) { - setNotInterpoWindowKey(pRuntimeEnv->pCtx, pQuery->numOfOutput, RESULT_ROW_START_INTERP); + if (lastTs == INT64_MIN && ((pos == 0 && ascQuery) || (pos == (numOfRows - 1) && !ascQuery))) { + setNotInterpoWindowKey(pCtx, pOperatorInfo->numOfOutput, RESULT_ROW_START_INTERP); return true; } int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); - TSKEY prevTs = ((pos == 0 && QUERY_IS_ASC_QUERY(pQuery)) || (pos == (numOfRows - 1) && !QUERY_IS_ASC_QUERY(pQuery)))? - lastTs:tsCols[pos - step]; + TSKEY prevTs = ((pos == 0 && ascQuery) || (pos == (numOfRows - 1) && !ascQuery))? lastTs:tsCols[pos - step]; - doRowwiseTimeWindowInterpolation(pRuntimeEnv, pDataBlock, prevTs, pos - step, curTs, pos, key, RESULT_ROW_START_INTERP); + doTimeWindowInterpolation(pOperatorInfo, pOperatorInfo->info, pDataBlock, prevTs, pos - step, curTs, pos, + key, RESULT_ROW_START_INTERP); return true; } -static bool setTimeWindowInterpolationEndTs(SQueryRuntimeEnv* pRuntimeEnv, int32_t endRowIndex, SArray* pDataBlock, TSKEY* tsCols, TSKEY blockEkey, STimeWindow* win) { +static bool setTimeWindowInterpolationEndTs(SOperatorInfo* pOperatorInfo, SQLFunctionCtx* pCtx, + int32_t endRowIndex, SArray* pDataBlock, const TSKEY* tsCols, TSKEY blockEkey, STimeWindow* win) { + SQueryRuntimeEnv *pRuntimeEnv = pOperatorInfo->pRuntimeEnv; SQuery* pQuery = pRuntimeEnv->pQuery; + int32_t numOfOutput = pOperatorInfo->numOfOutput; + TSKEY actualEndKey = tsCols[endRowIndex]; TSKEY key = QUERY_IS_ASC_QUERY(pQuery)? win->ekey:win->skey; // not ended in current data block, do not invoke interpolation if ((key > blockEkey && QUERY_IS_ASC_QUERY(pQuery)) || (key < blockEkey && !QUERY_IS_ASC_QUERY(pQuery))) { - setNotInterpoWindowKey(pRuntimeEnv->pCtx, pQuery->numOfOutput, RESULT_ROW_END_INTERP); + setNotInterpoWindowKey(pCtx, numOfOutput, RESULT_ROW_END_INTERP); return false; } // there is actual end point of current time window, no interpolation need if (key == actualEndKey) { - setNotInterpoWindowKey(pRuntimeEnv->pCtx, pQuery->numOfOutput, RESULT_ROW_END_INTERP); + setNotInterpoWindowKey(pCtx, numOfOutput, RESULT_ROW_END_INTERP); return true; } @@ -1069,61 +1159,35 @@ static bool setTimeWindowInterpolationEndTs(SQueryRuntimeEnv* pRuntimeEnv, int32 assert(nextRowIndex >= 0); TSKEY nextKey = tsCols[nextRowIndex]; - doRowwiseTimeWindowInterpolation(pRuntimeEnv, pDataBlock, actualEndKey, endRowIndex, nextKey, nextRowIndex, key, RESULT_ROW_END_INTERP); + doTimeWindowInterpolation(pOperatorInfo, pOperatorInfo->info, pDataBlock, actualEndKey, endRowIndex, nextKey, + nextRowIndex, key, RESULT_ROW_END_INTERP); return true; } -static void saveDataBlockLastRow(SQueryRuntimeEnv* pRuntimeEnv, SDataBlockInfo* pDataBlockInfo, SArray* pDataBlock, - int32_t rowIndex) { - if (pDataBlock == NULL) { - return; - } - - SQuery* pQuery = pRuntimeEnv->pQuery; - for (int32_t k = 0; k < pQuery->numOfCols; ++k) { - SColumnInfoData *pColInfo = taosArrayGet(pDataBlock, k); - memcpy(pRuntimeEnv->prevRow[k], ((char*)pColInfo->pData) + (pColInfo->info.bytes * rowIndex), pColInfo->info.bytes); - } -} - -static TSKEY getStartTsKey(SQuery* pQuery, SDataBlockInfo* pDataBlockInfo, TSKEY* tsCols, int32_t step) { - TSKEY ts = TSKEY_INITIAL_VAL; - - if (tsCols == NULL) { - ts = QUERY_IS_ASC_QUERY(pQuery) ? pDataBlockInfo->window.skey : pDataBlockInfo->window.ekey; - } else { - int32_t offset = GET_COL_DATA_POS(pQuery, 0, step); - ts = tsCols[offset]; - } - - return ts; -} - -static void doWindowBorderInterpolation(SQueryRuntimeEnv* pRuntimeEnv, SDataBlockInfo* pDataBlockInfo, SArray *pDataBlock, +static void doWindowBorderInterpolation(SOperatorInfo* pOperatorInfo, SSDataBlock* pBlock, SQLFunctionCtx* pCtx, SResultRow* pResult, STimeWindow* win, int32_t startPos, int32_t forwardStep) { - if (!pRuntimeEnv->timeWindowInterpo) { + SQueryRuntimeEnv* pRuntimeEnv = pOperatorInfo->pRuntimeEnv; + SQuery* pQuery = pRuntimeEnv->pQuery; + if (!pQuery->timeWindowInterpo) { return; } - assert(pDataBlock != NULL); - - SQuery* pQuery = pRuntimeEnv->pQuery; - int32_t fillType = pQuery->fillType; - + assert(pBlock != NULL); int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); - SColumnInfoData *pColInfo = taosArrayGet(pDataBlock, 0); + SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, 0); TSKEY *tsCols = (TSKEY *)(pColInfo->pData); bool done = resultRowInterpolated(pResult, RESULT_ROW_START_INTERP); - if (!done) { + if (!done) { // it is not interpolated, now start to generated the interpolated value int32_t startRowIndex = startPos; - bool interp = setTimeWindowInterpolationStartTs(pRuntimeEnv, startRowIndex, pDataBlockInfo->rows, pDataBlock, tsCols, win, fillType); + bool interp = setTimeWindowInterpolationStartTs(pOperatorInfo, pCtx, startRowIndex, pBlock->info.rows, pBlock->pDataBlock, + tsCols, win); if (interp) { setResultRowInterpo(pResult, RESULT_ROW_START_INTERP); } } else { - setNotInterpoWindowKey(pRuntimeEnv->pCtx, pQuery->numOfOutput, RESULT_ROW_START_INTERP); + setNotInterpoWindowKey(pCtx, pQuery->numOfOutput, RESULT_ROW_START_INTERP); } // point interpolation does not require the end key time window interpolation. @@ -1136,157 +1200,231 @@ static void doWindowBorderInterpolation(SQueryRuntimeEnv* pRuntimeEnv, SDataBloc if (!done) { int32_t endRowIndex = startPos + (forwardStep - 1) * step; - TSKEY endKey = QUERY_IS_ASC_QUERY(pQuery)? pDataBlockInfo->window.ekey:pDataBlockInfo->window.skey; - bool interp = setTimeWindowInterpolationEndTs(pRuntimeEnv, endRowIndex, pDataBlock, tsCols, endKey, win); + TSKEY endKey = QUERY_IS_ASC_QUERY(pQuery)? pBlock->info.window.ekey:pBlock->info.window.skey; + bool interp = setTimeWindowInterpolationEndTs(pOperatorInfo, pCtx, endRowIndex, pBlock->pDataBlock, tsCols, endKey, win); if (interp) { setResultRowInterpo(pResult, RESULT_ROW_END_INTERP); } } else { - setNotInterpoWindowKey(pRuntimeEnv->pCtx, pQuery->numOfOutput, RESULT_ROW_END_INTERP); + setNotInterpoWindowKey(pCtx, pQuery->numOfOutput, RESULT_ROW_END_INTERP); } } -/** - * todo set the last value for pQueryTableInfo as in rowwiseapplyfunctions - * @param pRuntimeEnv - * @param forwardStep - * @param tsCols - * @param pFields - * @param isDiskFileBlock - * @return the incremental number of output value, so it maybe 0 for fixed number of query, - * such as count/min/max etc. - */ -static void blockwiseApplyFunctions(SQueryRuntimeEnv *pRuntimeEnv, SDataStatis *pStatis, SDataBlockInfo *pDataBlockInfo, - SResultRowInfo *pWindowResInfo, __block_search_fn_t searchFn, SArray *pDataBlock) { - SQLFunctionCtx *pCtx = pRuntimeEnv->pCtx; - bool masterScan = IS_MASTER_SCAN(pRuntimeEnv); +static void hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pResultRowInfo, SSDataBlock* pSDataBlock, int32_t groupId) { + STableIntervalOperatorInfo* pInfo = (STableIntervalOperatorInfo*) pOperatorInfo->info; - SQuery *pQuery = pRuntimeEnv->pQuery; - int64_t groupId = pQuery->current->groupIndex; + SQueryRuntimeEnv* pRuntimeEnv = pOperatorInfo->pRuntimeEnv; + int32_t numOfOutput = pOperatorInfo->numOfOutput; + SQuery* pQuery = pRuntimeEnv->pQuery; + + int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); + bool ascQuery = QUERY_IS_ASC_QUERY(pQuery); - TSKEY *tsCols = NULL; - if (pDataBlock != NULL) { - SColumnInfoData *pColInfo = taosArrayGet(pDataBlock, 0); - tsCols = (TSKEY *)(pColInfo->pData); + int32_t prevIndex = curTimeWindowIndex(pResultRowInfo); + + TSKEY* tsCols = NULL; + if (pSDataBlock->pDataBlock != NULL) { + SColumnInfoData* pColDataInfo = taosArrayGet(pSDataBlock->pDataBlock, 0); + tsCols = (int64_t*) pColDataInfo->pData; + assert(tsCols[0] == pSDataBlock->info.window.skey && + tsCols[pSDataBlock->info.rows - 1] == pSDataBlock->info.window.ekey); } - for (int32_t k = 0; k < pQuery->numOfOutput; ++k) { - char *dataBlock = getDataBlock(pRuntimeEnv, &pRuntimeEnv->sasArray[k], k, pDataBlockInfo->rows, pDataBlock); - setExecParams(pQuery, &pCtx[k], dataBlock, tsCols, pDataBlockInfo, pStatis, &pQuery->pExpr1[k]); + int32_t startPos = ascQuery? 0 : (pSDataBlock->info.rows - 1); + TSKEY ts = getStartTsKey(pQuery, &pSDataBlock->info.window, tsCols, pSDataBlock->info.rows); + + STimeWindow win = getActiveTimeWindow(pResultRowInfo, ts, pQuery); + bool masterScan = IS_MASTER_SCAN(pRuntimeEnv); + + SResultRow* pResult = NULL; + int32_t ret = setWindowOutputBufByKey(pRuntimeEnv, pResultRowInfo, &win, masterScan, &pResult, groupId, pInfo->pCtx, + numOfOutput, pInfo->rowCellInfoOffset); + if (ret != TSDB_CODE_SUCCESS || pResult == NULL) { + longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY); } - int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); - if (QUERY_IS_INTERVAL_QUERY(pQuery)) { - int32_t prevIndex = curTimeWindowIndex(pWindowResInfo); + int32_t forwardStep = 0; + TSKEY ekey = reviseWindowEkey(pQuery, &win); + forwardStep = + getNumOfRowsInTimeWindow(pQuery, &pSDataBlock->info, tsCols, startPos, ekey, binarySearchForKey, true); + + // prev time window not interpolation yet. + int32_t curIndex = curTimeWindowIndex(pResultRowInfo); + if (prevIndex != -1 && prevIndex < curIndex && pQuery->timeWindowInterpo) { + for (int32_t j = prevIndex; j < curIndex; ++j) { // previous time window may be all closed already. + SResultRow* pRes = pResultRowInfo->pResult[j]; + if (pRes->closed) { + assert(resultRowInterpolated(pRes, RESULT_ROW_START_INTERP) && + resultRowInterpolated(pRes, RESULT_ROW_END_INTERP)); + continue; + } - TSKEY ts = getStartTsKey(pQuery, pDataBlockInfo, tsCols, step); - STimeWindow win = getActiveTimeWindow(pWindowResInfo, ts, pQuery); + STimeWindow w = pRes->win; + ret = setWindowOutputBufByKey(pRuntimeEnv, pResultRowInfo, &w, masterScan, &pResult, groupId, pInfo->pCtx, + numOfOutput, pInfo->rowCellInfoOffset); + if (ret != TSDB_CODE_SUCCESS) { + longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY); + } - SResultRow* pResult = NULL; - int32_t ret = setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, &win, masterScan, &pResult, groupId); - if (ret != TSDB_CODE_SUCCESS || pResult == NULL) { - goto _end; - } + assert(!resultRowInterpolated(pResult, RESULT_ROW_END_INTERP)); - int32_t forwardStep = 0; - int32_t startPos = pQuery->pos; + doTimeWindowInterpolation(pOperatorInfo, pInfo, pSDataBlock->pDataBlock, *(TSKEY *)pRuntimeEnv->prevRow[0], + -1, tsCols[startPos], startPos, w.ekey, RESULT_ROW_END_INTERP); - TSKEY ekey = reviseWindowEkey(pQuery, &win); - forwardStep = getNumOfRowsInTimeWindow(pQuery, pDataBlockInfo, tsCols, pQuery->pos, ekey, searchFn, true); + setResultRowInterpo(pResult, RESULT_ROW_END_INTERP); + setNotInterpoWindowKey(pInfo->pCtx, pQuery->numOfOutput, RESULT_ROW_START_INTERP); - // prev time window not interpolation yet. - int32_t curIndex = curTimeWindowIndex(pWindowResInfo); - if (prevIndex != -1 && prevIndex < curIndex && pRuntimeEnv->timeWindowInterpo) { - for(int32_t j = prevIndex; j < curIndex; ++j) { // previous time window may be all closed already. - SResultRow *pRes = pWindowResInfo->pResult[j]; - if (pRes->closed) { - assert(resultRowInterpolated(pRes, RESULT_ROW_START_INTERP) && resultRowInterpolated(pRes, RESULT_ROW_END_INTERP)); - continue; - } + doApplyFunctions(pRuntimeEnv, pInfo->pCtx, &w, startPos, 0, tsCols, pSDataBlock->info.rows, numOfOutput); + } - STimeWindow w = pRes->win; - ret = setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, &w, masterScan, &pResult, groupId); - assert(ret == TSDB_CODE_SUCCESS && !resultRowInterpolated(pResult, RESULT_ROW_END_INTERP)); + // restore current time window + ret = setWindowOutputBufByKey(pRuntimeEnv, pResultRowInfo, &win, masterScan, &pResult, groupId, pInfo->pCtx, + numOfOutput, pInfo->rowCellInfoOffset); + if (ret != TSDB_CODE_SUCCESS) { + longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY); + } + } - int32_t p = QUERY_IS_ASC_QUERY(pQuery)? 0:pDataBlockInfo->rows-1; - doRowwiseTimeWindowInterpolation(pRuntimeEnv, pDataBlock, *(TSKEY*) pRuntimeEnv->prevRow[0], -1, tsCols[0], p, w.ekey, RESULT_ROW_END_INTERP); - setResultRowInterpo(pResult, RESULT_ROW_END_INTERP); - setNotInterpoWindowKey(pRuntimeEnv->pCtx, pQuery->numOfOutput, RESULT_ROW_START_INTERP); + // window start key interpolation + doWindowBorderInterpolation(pOperatorInfo, pSDataBlock, pInfo->pCtx, pResult, &win, startPos, forwardStep); + doApplyFunctions(pRuntimeEnv, pInfo->pCtx, &win, startPos, forwardStep, tsCols, pSDataBlock->info.rows, numOfOutput); - doBlockwiseApplyFunctions(pRuntimeEnv, &w, startPos, 0, tsCols, pDataBlockInfo->rows, pDataBlock); - } + STimeWindow nextWin = win; + while (1) { + int32_t prevEndPos = (forwardStep - 1) * step + startPos; + startPos = getNextQualifiedWindow(pQuery, &nextWin, &pSDataBlock->info, tsCols, binarySearchForKey, prevEndPos); + if (startPos < 0) { + break; + } - // restore current time window - ret = setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, &win, masterScan, &pResult, groupId); - assert (ret == TSDB_CODE_SUCCESS); + // null data, failed to allocate more memory buffer + int32_t code = setWindowOutputBufByKey(pRuntimeEnv, pResultRowInfo, &nextWin, masterScan, &pResult, groupId, + pInfo->pCtx, numOfOutput, pInfo->rowCellInfoOffset); + if (code != TSDB_CODE_SUCCESS || pResult == NULL) { + longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY); } - // window start key interpolation - doWindowBorderInterpolation(pRuntimeEnv, pDataBlockInfo, pDataBlock, pResult, &win, pQuery->pos, forwardStep); - doBlockwiseApplyFunctions(pRuntimeEnv, &win, startPos, forwardStep, tsCols, pDataBlockInfo->rows, pDataBlock); + ekey = reviseWindowEkey(pQuery, &nextWin); + forwardStep = getNumOfRowsInTimeWindow(pQuery, &pSDataBlock->info, tsCols, startPos, ekey, binarySearchForKey, true); - STimeWindow nextWin = win; - while (1) { - int32_t prevEndPos = (forwardStep - 1) * step + startPos; - startPos = getNextQualifiedWindow(pRuntimeEnv, &nextWin, pDataBlockInfo, tsCols, searchFn, prevEndPos); - if (startPos < 0) { - break; - } + // window start(end) key interpolation + doWindowBorderInterpolation(pOperatorInfo, pSDataBlock, pInfo->pCtx, pResult, &nextWin, startPos, forwardStep); + doApplyFunctions(pRuntimeEnv, pInfo->pCtx, &nextWin, startPos, forwardStep, tsCols, pSDataBlock->info.rows, numOfOutput); + } - // null data, failed to allocate more memory buffer - int32_t code = setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, &nextWin, masterScan, &pResult, groupId); - if (code != TSDB_CODE_SUCCESS || pResult == NULL) { - break; + if (pQuery->timeWindowInterpo) { + int32_t rowIndex = ascQuery? (pSDataBlock->info.rows-1):0; + saveDataBlockLastRow(pRuntimeEnv, &pSDataBlock->info, pSDataBlock->pDataBlock, rowIndex); + } + + updateResultRowInfoActiveIndex(pResultRowInfo, pQuery, pQuery->current->lastKey); +} + +static void doHashGroupbyAgg(SOperatorInfo* pOperator, SGroupbyOperatorInfo *pInfo, SSDataBlock *pSDataBlock) { + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; + STableQueryInfo* item = pRuntimeEnv->pQuery->current; + + SColumnInfoData* pColInfoData = taosArrayGet(pSDataBlock->pDataBlock, pInfo->colIndex); + int16_t bytes = pColInfoData->info.bytes; + int16_t type = pColInfoData->info.type; + + if (type == TSDB_DATA_TYPE_FLOAT || type == TSDB_DATA_TYPE_DOUBLE) { + qError("QInfo:%p group by not supported on double/float columns, abort", pRuntimeEnv->qinfo); + return; + } + + for (int32_t j = 0; j < pSDataBlock->info.rows; ++j) { + char* val = ((char*)pColInfoData->pData) + bytes * j; + if (isNull(val, type)) { + continue; + } + + // Compare with the previous row of this column, and do not set the output buffer again if they are identical. + if (pInfo->prevData == NULL || (memcmp(pInfo->prevData, val, bytes) != 0)) { + if (pInfo->prevData == NULL) { + pInfo->prevData = malloc(bytes); } - ekey = reviseWindowEkey(pQuery, &nextWin); - forwardStep = getNumOfRowsInTimeWindow(pQuery, pDataBlockInfo, tsCols, startPos, ekey, searchFn, true); + memcpy(pInfo->prevData, val, bytes); - // window start(end) key interpolation - doWindowBorderInterpolation(pRuntimeEnv, pDataBlockInfo, pDataBlock, pResult, &nextWin, startPos, forwardStep); - doBlockwiseApplyFunctions(pRuntimeEnv, &nextWin, startPos, forwardStep, tsCols, pDataBlockInfo->rows, pDataBlock); + int32_t ret = + setGroupResultOutputBuf(pRuntimeEnv, pInfo, pOperator->numOfOutput, val, type, bytes, item->groupIndex); + if (ret != TSDB_CODE_SUCCESS) { // null data, too many state code + longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_APP_ERROR); + } } - } else { - /* - * the sqlfunctionCtx parameters should be set done before all functions are invoked, - * since the selectivity + tag_prj query needs all parameters been set done. - * tag_prj function are changed to be TSDB_FUNC_TAG_DUMMY - */ - for (int32_t k = 0; k < pQuery->numOfOutput; ++k) { - int32_t functionId = pQuery->pExpr1[k].base.functionId; - if (functionNeedToExecute(pRuntimeEnv, &pCtx[k], functionId)) { - pCtx[k].startTs = pQuery->window.skey; - aAggs[functionId].xFunction(&pCtx[k]); + for (int32_t k = 0; k < pOperator->numOfOutput; ++k) { + pInfo->binfo.pCtx[k].size = 1; + int32_t functionId = pInfo->binfo.pCtx[k].functionId; + if (functionNeedToExecute(pRuntimeEnv, &pInfo->binfo.pCtx[k], functionId)) { + aAggs[functionId].xFunctionF(&pInfo->binfo.pCtx[k], j); } } } +} + +static void doSessionWindowAggImpl(SOperatorInfo* pOperator, SSWindowOperatorInfo *pInfo, SSDataBlock *pSDataBlock) { + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; + STableQueryInfo* item = pRuntimeEnv->pQuery->current; + + // primary timestamp column + SColumnInfoData* pColInfoData = taosArrayGet(pSDataBlock->pDataBlock, 0); + + bool masterScan = IS_MASTER_SCAN(pRuntimeEnv); + SOptrBasicInfo* pBInfo = &pInfo->binfo; + + int64_t gap = pOperator->pRuntimeEnv->pQuery->sw.gap; + pInfo->numOfRows = 0; + + TSKEY* tsList = (TSKEY*)pColInfoData->pData; + for (int32_t j = 0; j < pSDataBlock->info.rows; ++j) { + if (pInfo->prevTs == INT64_MIN) { + pInfo->curWindow.skey = tsList[j]; + pInfo->curWindow.ekey = tsList[j]; + pInfo->prevTs = tsList[j]; + pInfo->numOfRows = 1; + pInfo->start = j; + } else if (tsList[j] - pInfo->prevTs <= gap) { + pInfo->curWindow.ekey = tsList[j]; + pInfo->prevTs = tsList[j]; + pInfo->numOfRows += 1; + pInfo->start = j; + } else { // start a new session window + SResultRow* pResult = NULL; + + int32_t ret = setWindowOutputBufByKey(pRuntimeEnv, &pBInfo->resultRowInfo, &pInfo->curWindow, masterScan, + &pResult, item->groupIndex, pBInfo->pCtx, pOperator->numOfOutput, + pBInfo->rowCellInfoOffset); + if (ret != TSDB_CODE_SUCCESS) { // null data, too many state code + longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_APP_ERROR); + } + + doApplyFunctions(pRuntimeEnv, pBInfo->pCtx, &pInfo->curWindow, pInfo->start, pInfo->numOfRows, tsList, + pSDataBlock->info.rows, pOperator->numOfOutput); - _end: - if (pRuntimeEnv->timeWindowInterpo) { - int32_t rowIndex = QUERY_IS_ASC_QUERY(pQuery)? pDataBlockInfo->rows-1:0; - saveDataBlockLastRow(pRuntimeEnv, pDataBlockInfo, pDataBlock, rowIndex); + pInfo->curWindow.skey = tsList[j]; + pInfo->curWindow.ekey = tsList[j]; + pInfo->prevTs = tsList[j]; + pInfo->numOfRows = 1; + pInfo->start = j; + } } -} -static int32_t setGroupResultOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, char *pData, int16_t type, int16_t bytes, int32_t groupIndex) { - SDiskbasedResultBuf *pResultBuf = pRuntimeEnv->pResultBuf; + SResultRow* pResult = NULL; - // not assign result buffer yet, add new result buffer, TODO remove it - char* d = pData; - int16_t len = bytes; - if (type == TSDB_DATA_TYPE_BINARY||type == TSDB_DATA_TYPE_NCHAR) { - d = varDataVal(pData); - len = varDataLen(pData); - } else if (type == TSDB_DATA_TYPE_FLOAT || type == TSDB_DATA_TYPE_DOUBLE) { - SQInfo* pQInfo = GET_QINFO_ADDR(pRuntimeEnv); - qError("QInfo:%p group by not supported on double/float columns, abort", pQInfo); - return -1; + int32_t ret = setWindowOutputBufByKey(pRuntimeEnv, &pBInfo->resultRowInfo, &pInfo->curWindow, masterScan, + &pResult, item->groupIndex, pBInfo->pCtx, pOperator->numOfOutput, + pBInfo->rowCellInfoOffset); + if (ret != TSDB_CODE_SUCCESS) { // null data, too many state code + longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_APP_ERROR); } - SResultRow *pResultRow = doPrepareResultRowFromKey(pRuntimeEnv, &pRuntimeEnv->resultRowInfo, d, len, true, groupIndex); - assert (pResultRow != NULL); + doApplyFunctions(pRuntimeEnv, pBInfo->pCtx, &pInfo->curWindow, pInfo->start, pInfo->numOfRows, tsList, + pSDataBlock->info.rows, pOperator->numOfOutput); +} +static void setResultRowKey(SResultRow* pResultRow, char* pData, int16_t type) { int64_t v = -1; GET_TYPED_DATA(v, int64_t, type, pData); if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { @@ -1300,94 +1438,58 @@ static int32_t setGroupResultOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, char *pDat pResultRow->win.skey = v; pResultRow->win.ekey = v; } +} + +static int32_t setGroupResultOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, SGroupbyOperatorInfo *pInfo, int32_t numOfCols, char *pData, int16_t type, int16_t bytes, int32_t groupIndex) { + SDiskbasedResultBuf *pResultBuf = pRuntimeEnv->pResultBuf; + + int32_t *rowCellInfoOffset = pInfo->binfo.rowCellInfoOffset; + SResultRowInfo *pResultRowInfo = &pInfo->binfo.resultRowInfo; + SQLFunctionCtx *pCtx = pInfo->binfo.pCtx; + + // not assign result buffer yet, add new result buffer, TODO remove it + char* d = pData; + int16_t len = bytes; + if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { + d = varDataVal(pData); + len = varDataLen(pData); + } + + SResultRow *pResultRow = doPrepareResultRowFromKey(pRuntimeEnv, pResultRowInfo, d, len, true, groupIndex); + assert (pResultRow != NULL); + setResultRowKey(pResultRow, pData, type); if (pResultRow->pageId == -1) { - int32_t ret = addNewWindowResultBuf(pResultRow, pResultBuf, groupIndex, pRuntimeEnv->numOfRowsPerPage); + int32_t ret = addNewWindowResultBuf(pResultRow, pResultBuf, groupIndex, pRuntimeEnv->pQuery->resultRowSize); if (ret != 0) { return -1; } } - setResultOutputBuf(pRuntimeEnv, pResultRow); - initCtxOutputBuf(pRuntimeEnv); + setResultOutputBuf(pRuntimeEnv, pResultRow, pCtx, numOfCols, rowCellInfoOffset); + initCtxOutputBuffer(pCtx, numOfCols); return TSDB_CODE_SUCCESS; } -static char *getGroupbyColumnData(SQuery *pQuery, int16_t *type, int16_t *bytes, SArray* pDataBlock) { - SSqlGroupbyExpr *pGroupbyExpr = pQuery->pGroupbyExpr; - +static int32_t getGroupbyColumnIndex(SSqlGroupbyExpr *pGroupbyExpr, SSDataBlock* pDataBlock) { for (int32_t k = 0; k < pGroupbyExpr->numOfGroupCols; ++k) { SColIndex* pColIndex = taosArrayGet(pGroupbyExpr->columnInfo, k); if (TSDB_COL_IS_TAG(pColIndex->flag)) { continue; } - int16_t colIndex = -1; int32_t colId = pColIndex->colId; - for (int32_t i = 0; i < pQuery->numOfCols; ++i) { - if (pQuery->colList[i].colId == colId) { - colIndex = i; - break; - } - } - - assert(colIndex >= 0 && colIndex < pQuery->numOfCols); - - *type = pQuery->colList[colIndex].type; - *bytes = pQuery->colList[colIndex].bytes; - /* - * the colIndex is acquired from the first tables of all qualified tables in this vnode during query prepare - * stage, the remain tables may not have the required column in cache actually. So, the validation of required - * column in cache with the corresponding schema is reinforced. - */ - int32_t numOfCols = (int32_t)taosArrayGetSize(pDataBlock); - - for (int32_t i = 0; i < numOfCols; ++i) { - SColumnInfoData *p = taosArrayGet(pDataBlock, i); - if (pColIndex->colId == p->info.colId) { - return p->pData; + for (int32_t i = 0; i < pDataBlock->info.numOfCols; ++i) { + SColumnInfoData* pColInfo = taosArrayGet(pDataBlock->pDataBlock, i); + if (pColInfo->info.colId == colId) { + return i; } } } - return NULL; -} - -static int32_t doTSJoinFilter(SQueryRuntimeEnv *pRuntimeEnv, int32_t offset) { - SQuery *pQuery = pRuntimeEnv->pQuery; - - STSElem elem = tsBufGetElem(pRuntimeEnv->pTsBuf); - SQLFunctionCtx *pCtx = pRuntimeEnv->pCtx; - - // compare tag first - if (tVariantCompare(&pCtx[0].tag, elem.tag) != 0) { - return TS_JOIN_TAG_NOT_EQUALS; - } - - TSKEY key = *(TSKEY *)((char*)pCtx[0].pInput + TSDB_KEYSIZE * offset); - -#if defined(_DEBUG_VIEW) - printf("elem in comp ts file:%" PRId64 ", key:%" PRId64 ", tag:%"PRIu64", query order:%d, ts order:%d, traverse:%d, index:%d\n", - elem.ts, key, elem.tag.i64, pQuery->order.order, pRuntimeEnv->pTsBuf->tsOrder, - pRuntimeEnv->pTsBuf->cur.order, pRuntimeEnv->pTsBuf->cur.tsIndex); -#endif - - if (QUERY_IS_ASC_QUERY(pQuery)) { - if (key < elem.ts) { - return TS_JOIN_TS_NOT_EQUALS; - } else if (key > elem.ts) { - longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_INCONSISTAN); - } - } else { - if (key > elem.ts) { - return TS_JOIN_TS_NOT_EQUALS; - } else if (key < elem.ts) { - longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_INCONSISTAN); - } - } - - return TS_JOIN_TS_EQUAL; + assert(0); + return -1; } static bool functionNeedToExecute(SQueryRuntimeEnv *pRuntimeEnv, SQLFunctionCtx *pCtx, int32_t functionId) { @@ -1412,613 +1514,313 @@ static bool functionNeedToExecute(SQueryRuntimeEnv *pRuntimeEnv, SQLFunctionCtx return pCtx->param[0].i64 == pQuery->order.order; } - // in the supplementary scan, only the following functions need to be executed - if (IS_REVERSE_SCAN(pRuntimeEnv)) { + // in the reverse table scan, only the following functions need to be executed + if (IS_REVERSE_SCAN(pRuntimeEnv) || + (pRuntimeEnv->scanFlag == REPEAT_SCAN && functionId != TSDB_FUNC_STDDEV && functionId != TSDB_FUNC_PERCT)) { return false; } return true; } -void doRowwiseTimeWindowInterpolation(SQueryRuntimeEnv *pRuntimeEnv, SArray *pDataBlock, TSKEY prevTs, - int32_t prevRowIndex, TSKEY curTs, int32_t curRowIndex, TSKEY windowKey, - int32_t type) { - SQuery *pQuery = pRuntimeEnv->pQuery; - for (int32_t k = 0; k < pQuery->numOfOutput; ++k) { - int32_t functionId = pQuery->pExpr1[k].base.functionId; - if (functionId != TSDB_FUNC_TWA && functionId != TSDB_FUNC_INTERP) { - pRuntimeEnv->pCtx[k].start.key = INT64_MIN; - continue; - } - - SColIndex * pColIndex = &pQuery->pExpr1[k].base.colInfo; - int16_t index = pColIndex->colIndex; - SColumnInfoData *pColInfo = taosArrayGet(pDataBlock, index); +void setBlockStatisInfo(SQLFunctionCtx *pCtx, SSDataBlock* pSDataBlock, SColIndex* pColIndex) { + SDataStatis *pStatis = NULL; - assert(pColInfo->info.colId == pColIndex->colId && curTs != windowKey); - double v1 = 0, v2 = 0, v = 0; + if (pSDataBlock->pBlockStatis != NULL && TSDB_COL_IS_NORMAL_COL(pColIndex->flag)) { + pStatis = &pSDataBlock->pBlockStatis[pColIndex->colIndex]; - if (prevRowIndex == -1) { - GET_TYPED_DATA(v1, double, pColInfo->info.type, (char *)pRuntimeEnv->prevRow[index]); - } else { - GET_TYPED_DATA(v1, double, pColInfo->info.type, (char *)pColInfo->pData + prevRowIndex * pColInfo->info.bytes); - } + pCtx->preAggVals.statis = *pStatis; + pCtx->preAggVals.isSet = true; + assert(pCtx->preAggVals.statis.numOfNull <= pSDataBlock->info.rows); + } else { + pCtx->preAggVals.isSet = false; + } - GET_TYPED_DATA(v2, double, pColInfo->info.type, (char *)pColInfo->pData + curRowIndex * pColInfo->info.bytes); + pCtx->hasNull = hasNullRv(pColIndex, pStatis); - SPoint point1 = (SPoint){.key = prevTs, .val = &v1}; - SPoint point2 = (SPoint){.key = curTs, .val = &v2}; - SPoint point = (SPoint){.key = windowKey, .val = &v}; + // set the statistics data for primary time stamp column + if (pCtx->functionId == TSDB_FUNC_SPREAD && pColIndex->colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) { + pCtx->preAggVals.isSet = true; + pCtx->preAggVals.statis.min = pSDataBlock->info.window.skey; + pCtx->preAggVals.statis.max = pSDataBlock->info.window.ekey; + } +} - if (functionId == TSDB_FUNC_TWA) { - taosGetLinearInterpolationVal(&point, TSDB_DATA_TYPE_DOUBLE, &point1, &point2, TSDB_DATA_TYPE_DOUBLE); +// set the output buffer for the selectivity + tag query +static int32_t setCtxTagColumnInfo(SQLFunctionCtx *pCtx, int32_t numOfOutput) { + if (!isSelectivityWithTagsQuery(pCtx, numOfOutput)) { + return TSDB_CODE_SUCCESS; + } - if (type == RESULT_ROW_START_INTERP) { - pRuntimeEnv->pCtx[k].start.key = point.key; - pRuntimeEnv->pCtx[k].start.val = v; - } else { - pRuntimeEnv->pCtx[k].end.key = point.key; - pRuntimeEnv->pCtx[k].end.val = v; - } - } else { - if (type == RESULT_ROW_START_INTERP) { - pRuntimeEnv->pCtx[k].start.key = prevTs; - pRuntimeEnv->pCtx[k].start.val = v1; + int32_t num = 0; + int16_t tagLen = 0; - pRuntimeEnv->pCtx[k].end.key = curTs; - pRuntimeEnv->pCtx[k].end.val = v2; - } - } + SQLFunctionCtx* p = NULL; + SQLFunctionCtx** pTagCtx = calloc(numOfOutput, POINTER_BYTES); + if (pTagCtx == NULL) { + return TSDB_CODE_QRY_OUT_OF_MEMORY; } -} - -static void setTimeWindowSKeyInterp(SQueryRuntimeEnv* pRuntimeEnv, SArray* pDataBlock, TSKEY prevTs, int32_t prevRowIndex, TSKEY ts, int32_t offset, SResultRow* pResult, STimeWindow* win) { - SQuery* pQuery = pRuntimeEnv->pQuery; - bool done = resultRowInterpolated(pResult, RESULT_ROW_START_INTERP); - if (!done) { - TSKEY key = QUERY_IS_ASC_QUERY(pQuery)? win->skey:win->ekey; - if (key == ts) { - setResultRowInterpo(pResult, RESULT_ROW_START_INTERP); - } else if (prevTs != INT64_MIN && ((QUERY_IS_ASC_QUERY(pQuery) && prevTs < key) || (!QUERY_IS_ASC_QUERY(pQuery) && prevTs > key))) { - doRowwiseTimeWindowInterpolation(pRuntimeEnv, pDataBlock, prevTs, prevRowIndex, ts, offset, key, RESULT_ROW_START_INTERP); - setResultRowInterpo(pResult, RESULT_ROW_START_INTERP); + for (int32_t i = 0; i < numOfOutput; ++i) { + int32_t functionId = pCtx[i].functionId; + + if (functionId == TSDB_FUNC_TAG_DUMMY || functionId == TSDB_FUNC_TS_DUMMY) { + tagLen += pCtx[i].outputBytes; + pTagCtx[num++] = &pCtx[i]; + } else if ((aAggs[functionId].status & TSDB_FUNCSTATE_SELECTIVITY) != 0) { + p = &pCtx[i]; + } else if (functionId == TSDB_FUNC_TS || functionId == TSDB_FUNC_TAG) { + // tag function may be the group by tag column + // ts may be the required primary timestamp column + continue; } else { - setNotInterpoWindowKey(pRuntimeEnv->pCtx, pQuery->numOfOutput, RESULT_ROW_START_INTERP); - } - - setNotInterpoWindowKey(pRuntimeEnv->pCtx, pQuery->numOfOutput, RESULT_ROW_END_INTERP); - for (int32_t k = 0; k < pQuery->numOfOutput; ++k) { - pRuntimeEnv->pCtx[k].size = 1; + // the column may be the normal column, group by normal_column, the functionId is TSDB_FUNC_PRJ } + } + if (p != NULL) { + p->tagInfo.pTagCtxList = pTagCtx; + p->tagInfo.numOfTagCols = num; + p->tagInfo.tagsLen = tagLen; } else { - setNotInterpoWindowKey(pRuntimeEnv->pCtx, pQuery->numOfOutput, RESULT_ROW_START_INTERP); + tfree(pTagCtx); } + + return TSDB_CODE_SUCCESS; } -static void setTimeWindowEKeyInterp(SQueryRuntimeEnv* pRuntimeEnv, SArray* pDataBlock, TSKEY prevTs, int32_t prevRowIndex, TSKEY ts, int32_t offset, SResultRow* pResult, STimeWindow* win) { +static SQLFunctionCtx* createSQLFunctionCtx(SQueryRuntimeEnv* pRuntimeEnv, SExprInfo* pExpr, int32_t numOfOutput, + int32_t** rowCellInfoOffset) { SQuery* pQuery = pRuntimeEnv->pQuery; - TSKEY key = QUERY_IS_ASC_QUERY(pQuery)? win->ekey:win->skey; - doRowwiseTimeWindowInterpolation(pRuntimeEnv, pDataBlock, prevTs, prevRowIndex, ts, offset, key, RESULT_ROW_END_INTERP); - setResultRowInterpo(pResult, RESULT_ROW_END_INTERP); - - setNotInterpoWindowKey(pRuntimeEnv->pCtx, pQuery->numOfOutput, RESULT_ROW_START_INTERP); - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - pRuntimeEnv->pCtx[i].size = 0; + SQLFunctionCtx * pFuncCtx = (SQLFunctionCtx *)calloc(numOfOutput, sizeof(SQLFunctionCtx)); + if (pFuncCtx == NULL) { + return NULL; } -} -static void rowwiseApplyFunctions(SQueryRuntimeEnv *pRuntimeEnv, SDataStatis *pStatis, SDataBlockInfo *pDataBlockInfo, - SResultRowInfo *pWindowResInfo, SArray *pDataBlock) { - SQLFunctionCtx *pCtx = pRuntimeEnv->pCtx; - bool masterScan = IS_MASTER_SCAN(pRuntimeEnv); + *rowCellInfoOffset = calloc(numOfOutput, sizeof(int32_t)); + if (*rowCellInfoOffset == 0) { + tfree(pFuncCtx); + return NULL; + } - SQuery *pQuery = pRuntimeEnv->pQuery; - STableQueryInfo* item = pQuery->current; + for (int32_t i = 0; i < numOfOutput; ++i) { + SSqlFuncMsg *pSqlFuncMsg = &pExpr[i].base; + SQLFunctionCtx* pCtx = &pFuncCtx[i]; - int64_t groupId = item->groupIndex; + SColIndex *pIndex = &pSqlFuncMsg->colInfo; - SColumnInfoData* pColumnInfoData = (SColumnInfoData *)taosArrayGet(pDataBlock, 0); + if (TSDB_COL_REQ_NULL(pIndex->flag)) { + pCtx->requireNull = true; + pIndex->flag &= ~(TSDB_COL_NULL); + } else { + pCtx->requireNull = false; + } - TSKEY *tsCols = (pColumnInfoData->info.type == TSDB_DATA_TYPE_TIMESTAMP)? (TSKEY*) pColumnInfoData->pData:NULL; - bool groupbyColumnValue = pRuntimeEnv->groupbyColumn; + pCtx->inputBytes = pSqlFuncMsg->colBytes; + pCtx->inputType = pSqlFuncMsg->colType; - int16_t type = 0; - int16_t bytes = 0; + pCtx->ptsOutputBuf = NULL; - char *groupbyColumnData = NULL; - if (groupbyColumnValue) { - groupbyColumnData = getGroupbyColumnData(pQuery, &type, &bytes, pDataBlock); - } + pCtx->outputBytes = pExpr[i].bytes; + pCtx->outputType = pExpr[i].type; - SQInfo* pQInfo = GET_QINFO_ADDR(pRuntimeEnv); - for (int32_t k = 0; k < pQuery->numOfOutput; ++k) { - char *dataBlock = getDataBlock(pRuntimeEnv, &pRuntimeEnv->sasArray[k], k, pDataBlockInfo->rows, pDataBlock); - setExecParams(pQuery, &pCtx[k], dataBlock, tsCols, pDataBlockInfo, pStatis, &pQuery->pExpr1[k]); - pCtx[k].size = 1; - } + pCtx->order = pQuery->order.order; + pCtx->functionId = pSqlFuncMsg->functionId; + pCtx->stableQuery = pQuery->stableQuery; + pCtx->interBufBytes = pExpr[i].interBytes; + pCtx->start.key = INT64_MIN; + pCtx->end.key = INT64_MIN; - // set the input column data - for (int32_t k = 0; k < pQuery->numOfFilterCols; ++k) { - SSingleColumnFilterInfo *pFilterInfo = &pQuery->pFilterInfo[k]; - pFilterInfo->pData = getDataBlockImpl(pDataBlock, pFilterInfo->info.colId); - assert(pFilterInfo->pData != NULL); - } + pCtx->numOfParams = pSqlFuncMsg->numOfParams; + for (int32_t j = 0; j < pCtx->numOfParams; ++j) { + int16_t type = pSqlFuncMsg->arg[j].argType; + int16_t bytes = pSqlFuncMsg->arg[j].argBytes; + if (pSqlFuncMsg->functionId == TSDB_FUNC_STDDEV_DST) { + continue; + } - int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); + if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { + tVariantCreateFromBinary(&pCtx->param[j], pSqlFuncMsg->arg[j].argValue.pz, bytes, type); + } else { + tVariantCreateFromBinary(&pCtx->param[j], (char *)&pSqlFuncMsg->arg[j].argValue.i64, bytes, type); + } + } - // from top to bottom in desc - // from bottom to top in asc order - if (pRuntimeEnv->pTsBuf != NULL) { - qDebug("QInfo:%p process data rows, numOfRows:%d, query order:%d, ts comp order:%d", pQInfo, pDataBlockInfo->rows, - pQuery->order.order, pRuntimeEnv->pTsBuf->cur.order); - } + // set the order information for top/bottom query + int32_t functionId = pCtx->functionId; - int32_t offset = -1; - TSKEY prevTs = *(TSKEY*) pRuntimeEnv->prevRow[0]; - int32_t prevRowIndex = -1; + if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF) { + int32_t f = pExpr[0].base.functionId; + assert(f == TSDB_FUNC_TS || f == TSDB_FUNC_TS_DUMMY); - for (int32_t j = 0; j < pDataBlockInfo->rows; ++j) { - offset = GET_COL_DATA_POS(pQuery, j, step); - - if (pRuntimeEnv->pTsBuf != NULL) { - int32_t ret = doTSJoinFilter(pRuntimeEnv, offset); - if (ret == TS_JOIN_TAG_NOT_EQUALS) { - break; - } else if (ret == TS_JOIN_TS_NOT_EQUALS) { - continue; - } else { - assert(ret == TS_JOIN_TS_EQUAL); - } - } - - if (pQuery->numOfFilterCols > 0 && (!doFilterData(pQuery, offset))) { - continue; - } - - // interval window query, decide the time window according to the primary timestamp - if (QUERY_IS_INTERVAL_QUERY(pQuery)) { - int32_t prevWindowIndex = curTimeWindowIndex(pWindowResInfo); - int64_t ts = tsCols[offset]; - - STimeWindow win = getActiveTimeWindow(pWindowResInfo, ts, pQuery); - - SResultRow* pResult = NULL; - int32_t ret = setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, &win, masterScan, &pResult, groupId); - if (ret != TSDB_CODE_SUCCESS || pResult == NULL) { // null data, too many state code - goto _end; - } - - // window start key interpolation - if (pRuntimeEnv->timeWindowInterpo) { - // check for the time window end time interpolation - int32_t curIndex = curTimeWindowIndex(pWindowResInfo); - if (prevWindowIndex != -1 && prevWindowIndex < curIndex) { - for (int32_t k = prevWindowIndex; k < curIndex; ++k) { - SResultRow *pRes = pWindowResInfo->pResult[k]; - if (pRes->closed) { - assert(resultRowInterpolated(pResult, RESULT_ROW_START_INTERP) && resultRowInterpolated(pResult, RESULT_ROW_END_INTERP)); - continue; - } - - ret = setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, &pRes->win, masterScan, &pResult, groupId); - assert(ret == TSDB_CODE_SUCCESS && !resultRowInterpolated(pResult, RESULT_ROW_END_INTERP)); - - setTimeWindowEKeyInterp(pRuntimeEnv, pDataBlock, prevTs, prevRowIndex, ts, offset, pResult, &pRes->win); - doRowwiseApplyFunctions(pRuntimeEnv, &pRes->win, offset); - } - - // restore current time window - ret = setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, &win, masterScan, &pResult, groupId); - if (ret != TSDB_CODE_SUCCESS) { // null data, too many state code - continue; - } - } - - setTimeWindowSKeyInterp(pRuntimeEnv, pDataBlock, prevTs, prevRowIndex, ts, offset, pResult, &win); - } - - doRowwiseApplyFunctions(pRuntimeEnv, &win, offset); - int32_t index = pWindowResInfo->curIndex; - - STimeWindow nextWin = win; - while (1) { - getNextTimeWindow(pQuery, &nextWin); - if ((nextWin.skey > pQuery->window.ekey && QUERY_IS_ASC_QUERY(pQuery)) || - (nextWin.ekey < pQuery->window.ekey && !QUERY_IS_ASC_QUERY(pQuery))) { - break; - } - - if (ts < nextWin.skey || ts > nextWin.ekey) { - break; - } - - // null data, failed to allocate more memory buffer - int32_t code = setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, &nextWin, masterScan, &pResult, groupId); - if (code != TSDB_CODE_SUCCESS || pResult == NULL) { - break; - } - - setTimeWindowSKeyInterp(pRuntimeEnv, pDataBlock, prevTs, prevRowIndex, ts, offset, pResult, &nextWin); - doRowwiseApplyFunctions(pRuntimeEnv, &nextWin, offset); - } - - // restore the index, add the result row will move the index - pWindowResInfo->curIndex = index; - } else { // other queries - // decide which group this rows belongs to according to current state value - char* val = NULL; - if (groupbyColumnValue) { - val = groupbyColumnData + bytes * offset; - if (isNull(val, type)) { // ignore the null value - continue; - } - - int32_t ret = setGroupResultOutputBuf(pRuntimeEnv, val, type, bytes, item->groupIndex); - if (ret != TSDB_CODE_SUCCESS) { // null data, too many state code - longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_APP_ERROR); - } - } - - if (pRuntimeEnv->stabledev) { - for (int32_t k = 0; k < pQuery->numOfOutput; ++k) { - int32_t functionId = pQuery->pExpr1[k].base.functionId; - if (functionId != TSDB_FUNC_STDDEV_DST) { - continue; - } + pCtx->param[2].i64 = pQuery->order.order; + pCtx->param[2].nType = TSDB_DATA_TYPE_BIGINT; + pCtx->param[3].i64 = functionId; + pCtx->param[3].nType = TSDB_DATA_TYPE_BIGINT; - pRuntimeEnv->pCtx[k].param[0].arr = NULL; - pRuntimeEnv->pCtx[k].param[0].nType = TSDB_DATA_TYPE_INT; // avoid freeing the memory by setting the type to be int - - // todo opt perf - int32_t numOfGroup = (int32_t)taosArrayGetSize(pRuntimeEnv->prevResult); - for (int32_t i = 0; i < numOfGroup; ++i) { - SInterResult *p = taosArrayGet(pRuntimeEnv->prevResult, i); - if (memcmp(p->tags, val, bytes) == 0) { - int32_t numOfCols = (int32_t)taosArrayGetSize(p->pResult); - for (int32_t f = 0; f < numOfCols; ++f) { - SStddevInterResult *pres = taosArrayGet(p->pResult, f); - if (pres->colId == pQuery->pExpr1[k].base.colInfo.colId) { - pRuntimeEnv->pCtx[k].param[0].arr = pres->pResult; - break; - } - } - } + pCtx->param[1].i64 = pQuery->order.orderColId; + } else if (functionId == TSDB_FUNC_INTERP) { + pCtx->param[2].i64 = (int8_t)pQuery->fillType; + if (pQuery->fillVal != NULL) { + if (isNull((const char *)&pQuery->fillVal[i], pCtx->inputType)) { + pCtx->param[1].nType = TSDB_DATA_TYPE_NULL; + } else { // todo refactor, tVariantCreateFromBinary should handle the NULL value + if (pCtx->inputType != TSDB_DATA_TYPE_BINARY && pCtx->inputType != TSDB_DATA_TYPE_NCHAR) { + tVariantCreateFromBinary(&pCtx->param[1], (char *)&pQuery->fillVal[i], pCtx->inputBytes, pCtx->inputType); } } } - - for (int32_t k = 0; k < pQuery->numOfOutput; ++k) { - int32_t functionId = pQuery->pExpr1[k].base.functionId; - if (functionNeedToExecute(pRuntimeEnv, &pCtx[k], functionId)) { - aAggs[functionId].xFunctionF(&pCtx[k], offset); - } - } - } - - prevTs = tsCols[offset]; - prevRowIndex = offset; - - if (pRuntimeEnv->pTsBuf != NULL) { - // if timestamp filter list is empty, quit current query - if (!tsBufNextPos(pRuntimeEnv->pTsBuf)) { - setQueryStatus(pQuery, QUERY_COMPLETED); - break; - } + } else if (functionId == TSDB_FUNC_TS_COMP) { + pCtx->param[0].i64 = pQuery->vgId; //TODO this should be the parameter from client + pCtx->param[0].nType = TSDB_DATA_TYPE_BIGINT; + } else if (functionId == TSDB_FUNC_TWA) { + pCtx->param[1].i64 = pQuery->window.skey; + pCtx->param[1].nType = TSDB_DATA_TYPE_BIGINT; + pCtx->param[2].i64 = pQuery->window.ekey; + pCtx->param[2].nType = TSDB_DATA_TYPE_BIGINT; + } else if (functionId == TSDB_FUNC_ARITHM) { + pCtx->param[1].pz = (char*) &pRuntimeEnv->sasArray[i]; } } - _end: - assert(offset >= 0 && tsCols != NULL); - if (prevTs != INT64_MIN && prevTs != *(int64_t*)pRuntimeEnv->prevRow[0]) { - assert(prevRowIndex >= 0); - item->lastKey = prevTs + step; + for(int32_t i = 1; i < numOfOutput; ++i) { + (*rowCellInfoOffset)[i] = (int32_t)((*rowCellInfoOffset)[i - 1] + sizeof(SResultRowCellInfo) + + pExpr[i - 1].interBytes * GET_ROW_PARAM_FOR_MULTIOUTPUT(pQuery, pQuery->topBotQuery, pQuery->stableQuery)); } - // In case of all rows in current block are not qualified - if (pRuntimeEnv->timeWindowInterpo && prevRowIndex != -1) { - saveDataBlockLastRow(pRuntimeEnv, pDataBlockInfo, pDataBlock, prevRowIndex); - } - - if (pRuntimeEnv->pTsBuf != NULL) { - item->cur = tsBufGetCursor(pRuntimeEnv->pTsBuf); - } -} - -static int32_t tableApplyFunctionsOnBlock(SQueryRuntimeEnv *pRuntimeEnv, SDataBlockInfo *pDataBlockInfo, - SDataStatis *pStatis, __block_search_fn_t searchFn, SArray *pDataBlock) { - SQuery *pQuery = pRuntimeEnv->pQuery; - - STableQueryInfo* pTableQueryInfo = pQuery->current; - SResultRowInfo* pResultRowInfo = &pRuntimeEnv->resultRowInfo; - - if (pQuery->numOfFilterCols > 0 || pRuntimeEnv->pTsBuf != NULL || pRuntimeEnv->groupbyColumn) { - rowwiseApplyFunctions(pRuntimeEnv, pStatis, pDataBlockInfo, pResultRowInfo, pDataBlock); - } else { - blockwiseApplyFunctions(pRuntimeEnv, pStatis, pDataBlockInfo, pResultRowInfo, searchFn, pDataBlock); - } - - // update the lastkey of current table for projection/aggregation query - TSKEY lastKey = QUERY_IS_ASC_QUERY(pQuery) ? pDataBlockInfo->window.ekey : pDataBlockInfo->window.skey; - pTableQueryInfo->lastKey = lastKey + GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); - - // interval query with limit applied - int32_t numOfRes = 0; - if (QUERY_IS_INTERVAL_QUERY(pQuery) || pRuntimeEnv->groupbyColumn) { - numOfRes = pResultRowInfo->size; - updateResultRowIndex(pResultRowInfo, pTableQueryInfo, QUERY_IS_ASC_QUERY(pQuery), pRuntimeEnv->timeWindowInterpo); - } else { // projection query - numOfRes = (int32_t) getNumOfResult(pRuntimeEnv); - - // update the number of output result - if (numOfRes > 0 && pQuery->checkResultBuf == 1) { - assert(numOfRes >= pQuery->rec.rows); - pQuery->rec.rows = numOfRes; - - if (numOfRes >= pQuery->rec.threshold) { - setQueryStatus(pQuery, QUERY_RESBUF_FULL); - } - - if ((pQuery->limit.limit >= 0) && (pQuery->limit.limit + pQuery->limit.offset) <= numOfRes) { - setQueryStatus(pQuery, QUERY_COMPLETED); - } - - if (((pTableQueryInfo->lastKey > pTableQueryInfo->win.ekey) && QUERY_IS_ASC_QUERY(pQuery)) || - ((pTableQueryInfo->lastKey < pTableQueryInfo->win.ekey) && (!QUERY_IS_ASC_QUERY(pQuery)))) { - setQueryStatus(pQuery, QUERY_COMPLETED); - } - } - } + setCtxTagColumnInfo(pFuncCtx, numOfOutput); - return numOfRes; + return pFuncCtx; } -void setExecParams(SQuery *pQuery, SQLFunctionCtx *pCtx, void* inputData, TSKEY *tsCol, SDataBlockInfo* pBlockInfo, - SDataStatis *pStatis, SExprInfo* pExprInfo) { - - int32_t functionId = pExprInfo->base.functionId; - int32_t colId = pExprInfo->base.colInfo.colId; - - SDataStatis *tpField = NULL; - pCtx->hasNull = hasNullValue(&pExprInfo->base.colInfo, pStatis, &tpField); - - if (tpField != NULL) { - pCtx->preAggVals.isSet = true; - pCtx->preAggVals.statis = *tpField; - assert(pCtx->preAggVals.statis.numOfNull <= pBlockInfo->rows); - } else { - pCtx->preAggVals.isSet = false; - } - - pCtx->preAggVals.dataBlockLoaded = (inputData != NULL); - - // limit/offset query will affect this value - pCtx->size = QUERY_IS_ASC_QUERY(pQuery) ? pBlockInfo->rows - pQuery->pos : pQuery->pos + 1; - - // set the start position in current block - int32_t offset = QUERY_IS_ASC_QUERY(pQuery) ? pQuery->pos: (pQuery->pos - pCtx->size + 1); - if (inputData != NULL) { - pCtx->pInput = (char*)inputData + offset * pCtx->inputBytes; - } - - uint32_t status = aAggs[functionId].status; - if (((status & (TSDB_FUNCSTATE_SELECTIVITY | TSDB_FUNCSTATE_NEED_TS)) != 0) && (tsCol != NULL)) { - pCtx->ptsList = tsCol + offset; - } - - if (functionId == TSDB_FUNC_SPREAD) { // set the statistics data for primary time stamp column - if (colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) { - pCtx->preAggVals.isSet = true; - pCtx->preAggVals.statis.min = pBlockInfo->window.skey; - pCtx->preAggVals.statis.max = pBlockInfo->window.ekey; - } +static void* destroySQLFunctionCtx(SQLFunctionCtx* pCtx, int32_t numOfOutput) { + if (pCtx == NULL) { + return NULL; } -} - -// set the output buffer for the selectivity + tag query -static int32_t setCtxTagColumnInfo(SQueryRuntimeEnv *pRuntimeEnv, SQLFunctionCtx *pCtx) { - SQuery* pQuery = pRuntimeEnv->pQuery; - if (isSelectivityWithTagsQuery(pQuery)) { - int32_t num = 0; - int16_t tagLen = 0; - - SQLFunctionCtx *p = NULL; - SQLFunctionCtx **pTagCtx = calloc(pQuery->numOfOutput, POINTER_BYTES); - if (pTagCtx == NULL) { - return TSDB_CODE_QRY_OUT_OF_MEMORY; + for (int32_t i = 0; i < numOfOutput; ++i) { + for (int32_t j = 0; j < pCtx[i].numOfParams; ++j) { + tVariantDestroy(&pCtx[i].param[j]); } - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - SSqlFuncMsg *pSqlFuncMsg = &pQuery->pExpr1[i].base; - - if (pSqlFuncMsg->functionId == TSDB_FUNC_TAG_DUMMY || pSqlFuncMsg->functionId == TSDB_FUNC_TS_DUMMY) { - tagLen += pCtx[i].outputBytes; - pTagCtx[num++] = &pCtx[i]; - } else if ((aAggs[pSqlFuncMsg->functionId].status & TSDB_FUNCSTATE_SELECTIVITY) != 0) { - p = &pCtx[i]; - } else if (pSqlFuncMsg->functionId == TSDB_FUNC_TS || pSqlFuncMsg->functionId == TSDB_FUNC_TAG) { - // tag function may be the group by tag column - // ts may be the required primary timestamp column - continue; - } else { - // the column may be the normal column, group by normal_column, the functionId is TSDB_FUNC_PRJ - } - } - if (p != NULL) { - p->tagInfo.pTagCtxList = pTagCtx; - p->tagInfo.numOfTagCols = num; - p->tagInfo.tagsLen = tagLen; - } else { - tfree(pTagCtx); - } + tVariantDestroy(&pCtx[i].tag); + tfree(pCtx[i].tagInfo.pTagCtxList); } - return TSDB_CODE_SUCCESS; + tfree(pCtx); + return NULL; } -static int32_t setupQueryRuntimeEnv(SQueryRuntimeEnv *pRuntimeEnv, int32_t numOfTables, int16_t order, int32_t vgId) { - qDebug("QInfo:%p setup runtime env", GET_QINFO_ADDR(pRuntimeEnv)); +static int32_t setupQueryRuntimeEnv(SQueryRuntimeEnv *pRuntimeEnv, int32_t numOfTables) { + qDebug("QInfo:%p setup runtime env", pRuntimeEnv->qinfo); SQuery *pQuery = pRuntimeEnv->pQuery; - pRuntimeEnv->interBufSize = getOutputInterResultBufSize(pQuery); - pRuntimeEnv->summary.tableInfoSize += (numOfTables * sizeof(STableQueryInfo)); + pRuntimeEnv->prevGroupId = INT32_MIN; + pRuntimeEnv->pQuery = pQuery; pRuntimeEnv->pResultRowHashTable = taosHashInit(numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); pRuntimeEnv->keyBuf = malloc(pQuery->maxSrcColumnSize + sizeof(int64_t)); pRuntimeEnv->pool = initResultRowPool(getResultRowSize(pRuntimeEnv)); pRuntimeEnv->prevRow = malloc(POINTER_BYTES * pQuery->numOfCols + pQuery->srcRowSize); pRuntimeEnv->tagVal = malloc(pQuery->tagLen); - pRuntimeEnv->pCtx = (SQLFunctionCtx *)calloc(pQuery->numOfOutput, sizeof(SQLFunctionCtx)); - pRuntimeEnv->offset = calloc(pQuery->numOfOutput, sizeof(int16_t)); - pRuntimeEnv->rowCellInfoOffset = calloc(pQuery->numOfOutput, sizeof(int32_t)); + pRuntimeEnv->currentOffset = pQuery->limit.offset; + + // NOTE: pTableCheckInfo need to update the query time range and the lastKey info + pRuntimeEnv->pTableRetrieveTsMap = taosHashInit(numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK); + pRuntimeEnv->sasArray = calloc(pQuery->numOfOutput, sizeof(SArithmeticSupport)); - if (pRuntimeEnv->offset == NULL || pRuntimeEnv->pCtx == NULL || pRuntimeEnv->rowCellInfoOffset == NULL || - pRuntimeEnv->sasArray == NULL || pRuntimeEnv->pResultRowHashTable == NULL || pRuntimeEnv->keyBuf == NULL || - pRuntimeEnv->prevRow == NULL || pRuntimeEnv->tagVal == NULL) { + if (pRuntimeEnv->sasArray == NULL || pRuntimeEnv->pResultRowHashTable == NULL || pRuntimeEnv->keyBuf == NULL || + pRuntimeEnv->prevRow == NULL || pRuntimeEnv->tagVal == NULL) { goto _clean; } - char* start = POINTER_BYTES * pQuery->numOfCols + (char*) pRuntimeEnv->prevRow; - pRuntimeEnv->prevRow[0] = start; - for(int32_t i = 1; i < pQuery->numOfCols; ++i) { - pRuntimeEnv->prevRow[i] = pRuntimeEnv->prevRow[i - 1] + pQuery->colList[i-1].bytes; - } - - pRuntimeEnv->offset[0] = 0; - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - SSqlFuncMsg *pSqlFuncMsg = &pQuery->pExpr1[i].base; - - SQLFunctionCtx *pCtx = &pRuntimeEnv->pCtx[i]; - SColIndex * pIndex = &pSqlFuncMsg->colInfo; - - if (TSDB_COL_REQ_NULL(pIndex->flag)) { - pCtx->requireNull = true; - pIndex->flag &= ~(TSDB_COL_NULL); - } else { - pCtx->requireNull = false; - } - - int32_t index = pSqlFuncMsg->colInfo.colIndex; - if (TSDB_COL_IS_TAG(pIndex->flag)) { - if (pIndex->colId == TSDB_TBNAME_COLUMN_INDEX) { // todo refactor - SSchema *s = tGetTbnameColumnSchema(); - - pCtx->inputBytes = s->bytes; - pCtx->inputType = s->type; - } else if (pIndex->colId == TSDB_BLOCK_DIST_COLUMN_INDEX) { - SSchema s = tGetBlockDistColumnSchema(); - pCtx->inputBytes = s.bytes; - pCtx->inputType = s.type; - } else { - pCtx->inputBytes = pQuery->tagColList[index].bytes; - pCtx->inputType = pQuery->tagColList[index].type; - } - } else if (TSDB_COL_IS_UD_COL(pIndex->flag)) { - pCtx->inputBytes = pSqlFuncMsg->arg[0].argBytes; - pCtx->inputType = pSqlFuncMsg->arg[0].argType; - } else { - pCtx->inputBytes = pQuery->colList[index].bytes; - pCtx->inputType = pQuery->colList[index].type; + if (pQuery->numOfCols) { + char* start = POINTER_BYTES * pQuery->numOfCols + (char*) pRuntimeEnv->prevRow; + pRuntimeEnv->prevRow[0] = start; + for(int32_t i = 1; i < pQuery->numOfCols; ++i) { + pRuntimeEnv->prevRow[i] = pRuntimeEnv->prevRow[i - 1] + pQuery->colList[i-1].bytes; } - assert(isValidDataType(pCtx->inputType)); - pCtx->ptsOutputBuf = NULL; + *(int64_t*) pRuntimeEnv->prevRow[0] = INT64_MIN; + } - pCtx->outputBytes = pQuery->pExpr1[i].bytes; - pCtx->outputType = pQuery->pExpr1[i].type; + qDebug("QInfo:%p init runtime environment completed", pRuntimeEnv->qinfo); - pCtx->order = pQuery->order.order; - pCtx->functionId = pSqlFuncMsg->functionId; - pCtx->stableQuery = pRuntimeEnv->stableQuery; - pCtx->interBufBytes = pQuery->pExpr1[i].interBytes; - pCtx->start.key = INT64_MIN; - pCtx->end.key = INT64_MIN; + // group by normal column, sliding window query, interval query are handled by interval query processor + // interval (down sampling operation) + if (onlyQueryTags(pQuery)) { // do nothing for tags query + + } else if (QUERY_IS_INTERVAL_QUERY(pQuery)) { + if (pQuery->stableQuery) { + pRuntimeEnv->proot = createMultiTableTimeIntervalOperatorInfo(pRuntimeEnv, pRuntimeEnv->pTableScanner, + pQuery->pExpr1, pQuery->numOfOutput); + setTableScanFilterOperatorInfo(pRuntimeEnv->pTableScanner->info, pRuntimeEnv->proot); + } else { + pRuntimeEnv->proot = + createTimeIntervalOperatorInfo(pRuntimeEnv, pRuntimeEnv->pTableScanner, pQuery->pExpr1, pQuery->numOfOutput); + setTableScanFilterOperatorInfo(pRuntimeEnv->pTableScanner->info, pRuntimeEnv->proot); - pCtx->numOfParams = pSqlFuncMsg->numOfParams; - for (int32_t j = 0; j < pCtx->numOfParams; ++j) { - int16_t type = pSqlFuncMsg->arg[j].argType; - int16_t bytes = pSqlFuncMsg->arg[j].argBytes; - if (pSqlFuncMsg->functionId == TSDB_FUNC_STDDEV_DST) { - continue; + if (pQuery->pExpr2 != NULL) { + pRuntimeEnv->proot = + createArithOperatorInfo(pRuntimeEnv, pRuntimeEnv->proot, pQuery->pExpr2, pQuery->numOfExpr2); } - if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { - tVariantCreateFromBinary(&pCtx->param[j], pSqlFuncMsg->arg[j].argValue.pz, bytes, type); - } else { - tVariantCreateFromBinary(&pCtx->param[j], (char *)&pSqlFuncMsg->arg[j].argValue.i64, bytes, type); + if (pQuery->fillType != TSDB_FILL_NONE && !isPointInterpoQuery(pQuery)) { + SOperatorInfo* pInfo = pRuntimeEnv->proot; + pRuntimeEnv->proot = createFillOperatorInfo(pRuntimeEnv, pInfo, pInfo->pExpr, pInfo->numOfOutput); } } - // set the order information for top/bottom query - int32_t functionId = pCtx->functionId; + } else if (pQuery->groupbyColumn) { + pRuntimeEnv->proot = + createGroupbyOperatorInfo(pRuntimeEnv, pRuntimeEnv->pTableScanner, pQuery->pExpr1, pQuery->numOfOutput); + setTableScanFilterOperatorInfo(pRuntimeEnv->pTableScanner->info, pRuntimeEnv->proot); - if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF) { - int32_t f = pQuery->pExpr1[0].base.functionId; - assert(f == TSDB_FUNC_TS || f == TSDB_FUNC_TS_DUMMY); + if (pQuery->pExpr2 != NULL) { + pRuntimeEnv->proot = createArithOperatorInfo(pRuntimeEnv, pRuntimeEnv->proot, pQuery->pExpr2, pQuery->numOfExpr2); + } + } else if (pQuery->sw.gap > 0) { + pRuntimeEnv->proot = createSWindowOperatorInfo(pRuntimeEnv, pRuntimeEnv->pTableScanner, pQuery->pExpr1, pQuery->numOfOutput); + setTableScanFilterOperatorInfo(pRuntimeEnv->pTableScanner->info, pRuntimeEnv->proot); - pCtx->param[2].i64 = order; - pCtx->param[2].nType = TSDB_DATA_TYPE_BIGINT; - pCtx->param[3].i64 = functionId; - pCtx->param[3].nType = TSDB_DATA_TYPE_BIGINT; + if (pQuery->pExpr2 != NULL) { + pRuntimeEnv->proot = createArithOperatorInfo(pRuntimeEnv, pRuntimeEnv->proot, pQuery->pExpr2, pQuery->numOfExpr2); + } + } else if (isFixedOutputQuery(pQuery)) { + if (pQuery->stableQuery && !isTsCompQuery(pQuery)) { + pRuntimeEnv->proot = + createMultiTableAggOperatorInfo(pRuntimeEnv, pRuntimeEnv->pTableScanner, pQuery->pExpr1, pQuery->numOfOutput); + } else { + pRuntimeEnv->proot = + createAggregateOperatorInfo(pRuntimeEnv, pRuntimeEnv->pTableScanner, pQuery->pExpr1, pQuery->numOfOutput); + } - pCtx->param[1].i64 = pQuery->order.orderColId; - } else if (functionId == TSDB_FUNC_INTERP) { - pCtx->param[2].i64 = (int8_t)pQuery->fillType; - if (pQuery->fillVal != NULL) { - if (isNull((const char *)&pQuery->fillVal[i], pCtx->inputType)) { - pCtx->param[1].nType = TSDB_DATA_TYPE_NULL; - } else { // todo refactor, tVariantCreateFromBinary should handle the NULL value - if (pCtx->inputType != TSDB_DATA_TYPE_BINARY && pCtx->inputType != TSDB_DATA_TYPE_NCHAR) { - tVariantCreateFromBinary(&pCtx->param[1], (char *)&pQuery->fillVal[i], pCtx->inputBytes, pCtx->inputType); - } - } - } - } else if (functionId == TSDB_FUNC_TS_COMP) { - pCtx->param[0].i64 = vgId; - pCtx->param[0].nType = TSDB_DATA_TYPE_BIGINT; - } else if (functionId == TSDB_FUNC_TWA) { - pCtx->param[1].i64 = pQuery->window.skey; - pCtx->param[1].nType = TSDB_DATA_TYPE_BIGINT; - pCtx->param[2].i64 = pQuery->window.ekey; - pCtx->param[2].nType = TSDB_DATA_TYPE_BIGINT; - } else if (functionId == TSDB_FUNC_ARITHM) { - pRuntimeEnv->sasArray[i].data = calloc(pQuery->numOfCols, POINTER_BYTES); - if (pRuntimeEnv->sasArray[i].data == NULL) { - goto _clean; - } + setTableScanFilterOperatorInfo(pRuntimeEnv->pTableScanner->info, pRuntimeEnv->proot); - pCtx->param[1].pz = (char*) &pRuntimeEnv->sasArray[i]; + if (pQuery->pExpr2 != NULL) { + pRuntimeEnv->proot = createArithOperatorInfo(pRuntimeEnv, pRuntimeEnv->proot, pQuery->pExpr2, pQuery->numOfExpr2); } - - if (i > 0) { - pRuntimeEnv->offset[i] = pRuntimeEnv->offset[i - 1] + pRuntimeEnv->pCtx[i - 1].outputBytes; - pRuntimeEnv->rowCellInfoOffset[i] = - pRuntimeEnv->rowCellInfoOffset[i - 1] + sizeof(SResultRowCellInfo) + pQuery->pExpr1[i - 1].interBytes; + } else { // diff/add/multiply/subtract/division + assert(pQuery->checkResultBuf == 1); + if (!onlyQueryTags(pQuery)) { + pRuntimeEnv->proot = + createArithOperatorInfo(pRuntimeEnv, pRuntimeEnv->pTableScanner, pQuery->pExpr1, pQuery->numOfOutput); + setTableScanFilterOperatorInfo(pRuntimeEnv->pTableScanner->info, pRuntimeEnv->proot); } } - *(int64_t*) pRuntimeEnv->prevRow[0] = INT64_MIN; - - // if it is group by normal column, do not set output buffer, the output buffer is pResult - // fixed output query/multi-output query for normal table - if (!pRuntimeEnv->groupbyColumn && !pRuntimeEnv->stableQuery && !QUERY_IS_INTERVAL_QUERY(pRuntimeEnv->pQuery)) { - resetDefaultResInfoOutputBuf(pRuntimeEnv); + if (pQuery->limit.offset > 0) { + pRuntimeEnv->proot = createOffsetOperatorInfo(pRuntimeEnv, pRuntimeEnv->proot); } - if (setCtxTagColumnInfo(pRuntimeEnv, pRuntimeEnv->pCtx) != TSDB_CODE_SUCCESS) { - goto _clean; + if (pQuery->limit.limit > 0) { + pRuntimeEnv->proot = createLimitOperatorInfo(pRuntimeEnv, pRuntimeEnv->proot); } - qDebug("QInfo:%p init runtime completed", GET_QINFO_ADDR(pRuntimeEnv)); return TSDB_CODE_SUCCESS; _clean: - tfree(pRuntimeEnv->pCtx); - tfree(pRuntimeEnv->offset); - tfree(pRuntimeEnv->rowCellInfoOffset); tfree(pRuntimeEnv->sasArray); tfree(pRuntimeEnv->pResultRowHashTable); tfree(pRuntimeEnv->keyBuf); @@ -2028,74 +1830,37 @@ _clean: return TSDB_CODE_QRY_OUT_OF_MEMORY; } -static void doFreeQueryHandle(SQInfo* pQInfo) { - SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; +static void doFreeQueryHandle(SQueryRuntimeEnv* pRuntimeEnv) { + SQuery* pQuery = pRuntimeEnv->pQuery; tsdbCleanupQueryHandle(pRuntimeEnv->pQueryHandle); - tsdbCleanupQueryHandle(pRuntimeEnv->pSecQueryHandle); - pRuntimeEnv->pQueryHandle = NULL; - pRuntimeEnv->pSecQueryHandle = NULL; - SMemRef* pMemRef = &pQInfo->memRef; + SMemRef* pMemRef = &pQuery->memRef; assert(pMemRef->ref == 0 && pMemRef->imem == NULL && pMemRef->mem == NULL); } - static void teardownQueryRuntimeEnv(SQueryRuntimeEnv *pRuntimeEnv) { - if (pRuntimeEnv->pQuery == NULL) { - return; - } - SQuery *pQuery = pRuntimeEnv->pQuery; - SQInfo* pQInfo = (SQInfo*) GET_QINFO_ADDR(pRuntimeEnv); + SQInfo* pQInfo = (SQInfo*) pRuntimeEnv->qinfo; qDebug("QInfo:%p teardown runtime env", pQInfo); - cleanupResultRowInfo(&pRuntimeEnv->resultRowInfo); - - if (isTsCompQuery(pQuery)) { - FILE *f = *(FILE **)pQuery->sdata[0]->data; - - if (f) { - fclose(f); - *(FILE **)pQuery->sdata[0]->data = NULL; - } - } - - - if (pRuntimeEnv->pCtx != NULL) { - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - SQLFunctionCtx *pCtx = &pRuntimeEnv->pCtx[i]; - - for (int32_t j = 0; j < pCtx->numOfParams; ++j) { - tVariantDestroy(&pCtx->param[j]); - } - - tVariantDestroy(&pCtx->tag); - tfree(pCtx->tagInfo.pTagCtxList); - } - - tfree(pRuntimeEnv->pCtx); - } if (pRuntimeEnv->sasArray != NULL) { for(int32_t i = 0; i < pQuery->numOfOutput; ++i) { tfree(pRuntimeEnv->sasArray[i].data); + tfree(pRuntimeEnv->sasArray[i].colList); } tfree(pRuntimeEnv->sasArray); } - pRuntimeEnv->pFillInfo = taosDestroyFillInfo(pRuntimeEnv->pFillInfo); - destroyResultBuf(pRuntimeEnv->pResultBuf); - doFreeQueryHandle(pQInfo); + doFreeQueryHandle(pRuntimeEnv); pRuntimeEnv->pTsBuf = tsBufDestroy(pRuntimeEnv->pTsBuf); - tfree(pRuntimeEnv->offset); tfree(pRuntimeEnv->keyBuf); - tfree(pRuntimeEnv->rowCellInfoOffset); tfree(pRuntimeEnv->prevRow); tfree(pRuntimeEnv->tagVal); @@ -2105,6 +1870,11 @@ static void teardownQueryRuntimeEnv(SQueryRuntimeEnv *pRuntimeEnv) { pRuntimeEnv->pool = destroyResultRowPool(pRuntimeEnv->pool); taosArrayDestroyEx(pRuntimeEnv->prevResult, freeInterResult); pRuntimeEnv->prevResult = NULL; + + taosHashCleanup(pRuntimeEnv->pTableRetrieveTsMap); + pRuntimeEnv->pTableRetrieveTsMap = NULL; + + destroyOperatorInfo(pRuntimeEnv->proot); } static bool needBuildResAfterQueryComplete(SQInfo* pQInfo) { @@ -2132,14 +1902,13 @@ bool isQueryKilled(SQInfo *pQInfo) { void setQueryKilled(SQInfo *pQInfo) { pQInfo->code = TSDB_CODE_TSC_QUERY_CANCELLED;} -static bool isFixedOutputQuery(SQueryRuntimeEnv* pRuntimeEnv) { - SQuery* pQuery = pRuntimeEnv->pQuery; +static bool isFixedOutputQuery(SQuery* pQuery) { if (QUERY_IS_INTERVAL_QUERY(pQuery)) { return false; } // Note:top/bottom query is fixed output query - if (pRuntimeEnv->topBotQuery || pRuntimeEnv->groupbyColumn) { + if (pQuery->topBotQuery || pQuery->groupbyColumn || isTsCompQuery(pQuery)) { return true; } @@ -2177,7 +1946,7 @@ bool isPointInterpoQuery(SQuery *pQuery) { } // TODO REFACTOR:MERGE WITH CLIENT-SIDE FUNCTION -static bool isSumAvgRateQuery(SQuery *pQuery) { +static UNUSED_FUNC bool isSumAvgRateQuery(SQuery *pQuery) { for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { int32_t functionId = pQuery->pExpr1[i].base.functionId; if (functionId == TSDB_FUNC_TS) { @@ -2294,11 +2063,11 @@ static void setScanLimitationByResultBuffer(SQuery *pQuery) { /* * todo add more parameters to check soon.. */ -bool colIdCheck(SQuery *pQuery) { +bool colIdCheck(SQuery *pQuery, void* qinfo) { // load data column information is incorrect for (int32_t i = 0; i < pQuery->numOfCols - 1; ++i) { if (pQuery->colList[i].colId == pQuery->colList[i + 1].colId) { - qError("QInfo:%p invalid data load column for query", GET_QINFO_ADDR(pQuery)); + qError("QInfo:%p invalid data load column for query", qinfo); return false; } } @@ -2330,9 +2099,10 @@ static bool onlyFirstQuery(SQuery *pQuery) { return onlyOneQueryType(pQuery, TSD static bool onlyLastQuery(SQuery *pQuery) { return onlyOneQueryType(pQuery, TSDB_FUNC_LAST, TSDB_FUNC_LAST_DST); } static void doExchangeTimeWindow(SQInfo* pQInfo, STimeWindow* win) { - size_t t = taosArrayGetSize(pQInfo->tableGroupInfo.pGroupList); + SQuery* pQuery = &pQInfo->query; + size_t t = taosArrayGetSize(pQuery->tableGroupInfo.pGroupList); for(int32_t i = 0; i < t; ++i) { - SArray* p1 = taosArrayGetP(pQInfo->tableGroupInfo.pGroupList, i); + SArray* p1 = taosArrayGetP(pQuery->tableGroupInfo.pGroupList, i); size_t len = taosArrayGetSize(p1); for(int32_t j = 0; j < len; ++j) { @@ -2378,8 +2148,7 @@ static void changeExecuteScanOrder(SQInfo *pQInfo, SQueryTableMsg* pQueryMsg, bo if (isPointInterpoQuery(pQuery) && pQuery->interval.interval == 0) { if (!QUERY_IS_ASC_QUERY(pQuery)) { - qDebug(msg, GET_QINFO_ADDR(pQuery), "interp", pQuery->order.order, TSDB_ORDER_ASC, pQuery->window.skey, - pQuery->window.ekey, pQuery->window.ekey, pQuery->window.skey); + qDebug(msg, pQInfo, "interp", pQuery->order.order, TSDB_ORDER_ASC, pQuery->window.skey, pQuery->window.ekey, pQuery->window.ekey, pQuery->window.skey); SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); } @@ -2390,7 +2159,7 @@ static void changeExecuteScanOrder(SQInfo *pQInfo, SQueryTableMsg* pQueryMsg, bo if (pQuery->interval.interval == 0) { if (onlyFirstQuery(pQuery)) { if (!QUERY_IS_ASC_QUERY(pQuery)) { - qDebug(msg, GET_QINFO_ADDR(pQuery), "only-first", pQuery->order.order, TSDB_ORDER_ASC, pQuery->window.skey, + qDebug(msg, pQInfo, "only-first", pQuery->order.order, TSDB_ORDER_ASC, pQuery->window.skey, pQuery->window.ekey, pQuery->window.ekey, pQuery->window.skey); SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); @@ -2400,7 +2169,7 @@ static void changeExecuteScanOrder(SQInfo *pQInfo, SQueryTableMsg* pQueryMsg, bo pQuery->order.order = TSDB_ORDER_ASC; } else if (onlyLastQuery(pQuery)) { if (QUERY_IS_ASC_QUERY(pQuery)) { - qDebug(msg, GET_QINFO_ADDR(pQuery), "only-last", pQuery->order.order, TSDB_ORDER_DESC, pQuery->window.skey, + qDebug(msg, pQInfo, "only-last", pQuery->order.order, TSDB_ORDER_DESC, pQuery->window.skey, pQuery->window.ekey, pQuery->window.ekey, pQuery->window.skey); SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); @@ -2414,7 +2183,7 @@ static void changeExecuteScanOrder(SQInfo *pQInfo, SQueryTableMsg* pQueryMsg, bo if (stableQuery) { if (onlyFirstQuery(pQuery)) { if (!QUERY_IS_ASC_QUERY(pQuery)) { - qDebug(msg, GET_QINFO_ADDR(pQuery), "only-first stable", pQuery->order.order, TSDB_ORDER_ASC, + qDebug(msg, pQInfo, "only-first stable", pQuery->order.order, TSDB_ORDER_ASC, pQuery->window.skey, pQuery->window.ekey, pQuery->window.ekey, pQuery->window.skey); SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); @@ -2424,7 +2193,7 @@ static void changeExecuteScanOrder(SQInfo *pQInfo, SQueryTableMsg* pQueryMsg, bo pQuery->order.order = TSDB_ORDER_ASC; } else if (onlyLastQuery(pQuery)) { if (QUERY_IS_ASC_QUERY(pQuery)) { - qDebug(msg, GET_QINFO_ADDR(pQuery), "only-last stable", pQuery->order.order, TSDB_ORDER_DESC, + qDebug(msg, pQInfo, "only-last stable", pQuery->order.order, TSDB_ORDER_DESC, pQuery->window.skey, pQuery->window.ekey, pQuery->window.ekey, pQuery->window.skey); SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); @@ -2437,48 +2206,29 @@ static void changeExecuteScanOrder(SQInfo *pQInfo, SQueryTableMsg* pQueryMsg, bo } } -static int32_t getInitialPageNum(SQInfo *pQInfo) { - SQuery *pQuery = pQInfo->runtimeEnv.pQuery; - int32_t INITIAL_RESULT_ROWS_VALUE = 16; - - int32_t num = 0; - - if (isGroupbyColumn(pQuery->pGroupbyExpr)) { - num = 128; - } else if (QUERY_IS_INTERVAL_QUERY(pQuery)) { // time window query, allocate one page for each table - size_t s = pQInfo->tableqinfoGroupInfo.numOfTables; - num = (int32_t)(MAX(s, INITIAL_RESULT_ROWS_VALUE)); - } else { // for super table query, one page for each subset - num = 1; // pQInfo->pSidSet->numOfSubSet; - } - - assert(num > 0); - return num; -} - static void getIntermediateBufInfo(SQueryRuntimeEnv* pRuntimeEnv, int32_t* ps, int32_t* rowsize) { SQuery* pQuery = pRuntimeEnv->pQuery; int32_t MIN_ROWS_PER_PAGE = 4; - *rowsize = (int32_t)(pQuery->resultRowSize * GET_ROW_PARAM_FOR_MULTIOUTPUT(pQuery, pRuntimeEnv->topBotQuery, pRuntimeEnv->stableQuery)); + *rowsize = (int32_t)(pQuery->resultRowSize * GET_ROW_PARAM_FOR_MULTIOUTPUT(pQuery, pQuery->topBotQuery, pQuery->stableQuery)); int32_t overhead = sizeof(tFilePage); // one page contains at least two rows *ps = DEFAULT_INTERN_BUF_PAGE_SIZE; while(((*rowsize) * MIN_ROWS_PER_PAGE) > (*ps) - overhead) { - *ps = (*ps << 1u); + *ps = ((*ps) << 1u); } - pRuntimeEnv->numOfRowsPerPage = ((*ps) - sizeof(tFilePage)) / (*rowsize); - assert(pRuntimeEnv->numOfRowsPerPage <= MAX_ROWS_PER_RESBUF_PAGE); +// pRuntimeEnv->numOfRowsPerPage = ((*ps) - sizeof(tFilePage)) / (*rowsize); +// assert(pRuntimeEnv->numOfRowsPerPage <= MAX_ROWS_PER_RESBUF_PAGE); } #define IS_PREFILTER_TYPE(_t) ((_t) != TSDB_DATA_TYPE_BINARY && (_t) != TSDB_DATA_TYPE_NCHAR) -static bool needToLoadDataBlock(SQueryRuntimeEnv* pRuntimeEnv, SDataStatis *pDataStatis, SQLFunctionCtx *pCtx, - int32_t numOfRows) { +static bool doFilterByBlockStatistics(SQueryRuntimeEnv* pRuntimeEnv, SDataStatis *pDataStatis, SQLFunctionCtx *pCtx, int32_t numOfRows) { SQuery* pQuery = pRuntimeEnv->pQuery; - if (pDataStatis == NULL || (pQuery->numOfFilterCols == 0 && (!pRuntimeEnv->topBotQuery))) { + + if (pDataStatis == NULL || pQuery->numOfFilterCols == 0) { return true; } @@ -2537,15 +2287,6 @@ static bool needToLoadDataBlock(SQueryRuntimeEnv* pRuntimeEnv, SDataStatis *pDat } } - if (pRuntimeEnv->topBotQuery) { - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - int32_t functionId = pQuery->pExpr1[i].base.functionId; - if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM) { - return topbot_datablock_filter(&pCtx[i], functionId, (char *)&pDataStatis[i].min, (char *)&pDataStatis[i].max); - } - } - } - return false; } @@ -2598,86 +2339,325 @@ static bool overlapWithTimeWindow(SQuery* pQuery, SDataBlockInfo* pBlockInfo) { return false; } -int32_t loadDataBlockOnDemand(SQueryRuntimeEnv *pRuntimeEnv, SResultRowInfo * pWindowResInfo, void* pQueryHandle, SDataBlockInfo* pBlockInfo, SDataStatis **pStatis, SArray** pDataBlock, uint32_t* status) { - *status = BLK_DATA_NO_NEEDED; +static int32_t doTSJoinFilter(SQueryRuntimeEnv *pRuntimeEnv, TSKEY key, bool ascQuery) { + STSElem elem = tsBufGetElem(pRuntimeEnv->pTsBuf); - SQuery *pQuery = pRuntimeEnv->pQuery; - int64_t groupId = pQuery->current->groupIndex; +#if defined(_DEBUG_VIEW) + printf("elem in comp ts file:%" PRId64 ", key:%" PRId64 ", tag:%"PRIu64", query order:%d, ts order:%d, traverse:%d, index:%d\n", + elem.ts, key, elem.tag.i64, pQuery->order.order, pRuntimeEnv->pTsBuf->tsOrder, + pRuntimeEnv->pTsBuf->cur.order, pRuntimeEnv->pTsBuf->cur.tsIndex); +#endif + + if (ascQuery) { + if (key < elem.ts) { + return TS_JOIN_TS_NOT_EQUALS; + } else if (key > elem.ts) { + longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_INCONSISTAN); + } + } else { + if (key > elem.ts) { + return TS_JOIN_TS_NOT_EQUALS; + } else if (key < elem.ts) { + longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_INCONSISTAN); + } + } + + return TS_JOIN_TS_EQUAL; +} + +void filterRowsInDataBlock(SQueryRuntimeEnv* pRuntimeEnv, SSingleColumnFilterInfo* pFilterInfo, int32_t numOfFilterCols, + SSDataBlock* pBlock, bool ascQuery) { + int32_t numOfRows = pBlock->info.rows; + + int8_t *p = calloc(numOfRows, sizeof(int8_t)); + bool all = true; - SQueryCostInfo* pCost = &pRuntimeEnv->summary; + if (pRuntimeEnv->pTsBuf != NULL) { + SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, 0); + + TSKEY* k = (TSKEY*) pColInfoData->pData; + for (int32_t i = 0; i < numOfRows; ++i) { + int32_t offset = ascQuery? i:(numOfRows - i - 1); + int32_t ret = doTSJoinFilter(pRuntimeEnv, k[offset], ascQuery); + if (ret == TS_JOIN_TAG_NOT_EQUALS) { + break; + } else if (ret == TS_JOIN_TS_NOT_EQUALS) { + all = false; + continue; + } else { + assert(ret == TS_JOIN_TS_EQUAL); + p[offset] = true; + } - if (pQuery->numOfFilterCols > 0 || pRuntimeEnv->pTsBuf > 0) { - *status = BLK_DATA_ALL_NEEDED; - } else { // check if this data block is required to load - // Calculate all time windows that are overlapping or contain current data block. - // If current data block is contained by all possible time window, do not load current data block. - if (QUERY_IS_INTERVAL_QUERY(pQuery) && overlapWithTimeWindow(pQuery, pBlockInfo)) { - *status = BLK_DATA_ALL_NEEDED; + if (!tsBufNextPos(pRuntimeEnv->pTsBuf)) { + break; + } } - if ((*status) != BLK_DATA_ALL_NEEDED) { - // the pCtx[i] result is belonged to previous time window since the outputBuf has not been set yet, - // the filter result may be incorrect. So in case of interval query, we need to set the correct time output buffer - if (QUERY_IS_INTERVAL_QUERY(pQuery)) { - SResultRow* pResult = NULL; + // save the cursor status + pRuntimeEnv->pQuery->current->cur = tsBufGetCursor(pRuntimeEnv->pTsBuf); + } else { + for (int32_t i = 0; i < numOfRows; ++i) { + bool qualified = false; + + for (int32_t k = 0; k < numOfFilterCols; ++k) { + char* pElem = (char*)pFilterInfo[k].pData + pFilterInfo[k].info.bytes * i; + + qualified = false; + for (int32_t j = 0; j < pFilterInfo[k].numOfFilters; ++j) { + SColumnFilterElem* pFilterElem = &pFilterInfo[k].pFilters[j]; + + bool isnull = isNull(pElem, pFilterInfo[k].info.type); + if (isnull) { + if (pFilterElem->fp == isNullOperator) { + qualified = true; + break; + } else { + continue; + } + } else { + if (pFilterElem->fp == notNullOperator) { + qualified = true; + break; + } else if (pFilterElem->fp == isNullOperator) { + continue; + } + } - bool masterScan = IS_MASTER_SCAN(pRuntimeEnv); + if (pFilterElem->fp(pFilterElem, pElem, pElem, pFilterInfo[k].info.type)) { + qualified = true; + break; + } + } - TSKEY k = QUERY_IS_ASC_QUERY(pQuery)? pBlockInfo->window.skey:pBlockInfo->window.ekey; - STimeWindow win = getActiveTimeWindow(pWindowResInfo, k, pQuery); - if (setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, &win, masterScan, &pResult, groupId) != TSDB_CODE_SUCCESS) { - // todo handle error in set result for timewindow + if (!qualified) { + break; } } - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - SSqlFuncMsg* pSqlFunc = &pQuery->pExpr1[i].base; + p[i] = qualified ? 1 : 0; + if (!qualified) { + all = false; + } + } + } - int32_t functionId = pSqlFunc->functionId; - int32_t colId = pSqlFunc->colInfo.colId; - (*status) |= aAggs[functionId].dataReqFunc(&pRuntimeEnv->pCtx[i], pBlockInfo->window.skey, pBlockInfo->window.ekey, colId); - if (((*status) & BLK_DATA_ALL_NEEDED) == BLK_DATA_ALL_NEEDED) { - break; + if (!all) { + int32_t start = 0; + int32_t len = 0; + for (int32_t j = 0; j < numOfRows; ++j) { + if (p[j] == 1) { + len++; + } else { + if (len > 0) { + int32_t cstart = j - len; + for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) { + SColumnInfoData *pColumnInfoData = taosArrayGet(pBlock->pDataBlock, i); + + int16_t bytes = pColumnInfoData->info.bytes; + memmove(((char*)pColumnInfoData->pData) + start * bytes, pColumnInfoData->pData + cstart * bytes, len * bytes); + } + + start += len; + len = 0; } } } + + if (len > 0) { + int32_t cstart = numOfRows - len; + for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) { + SColumnInfoData *pColumnInfoData = taosArrayGet(pBlock->pDataBlock, i); + + int16_t bytes = pColumnInfoData->info.bytes; + memmove(pColumnInfoData->pData + start * bytes, pColumnInfoData->pData + cstart * bytes, len * bytes); + } + + start += len; + len = 0; + } + + pBlock->info.rows = start; + pBlock->pBlockStatis = NULL; // clean the block statistics info + + if (start > 0) { + SColumnInfoData* pColumnInfoData = taosArrayGet(pBlock->pDataBlock, 0); + assert(pColumnInfoData->info.type == TSDB_DATA_TYPE_TIMESTAMP && + pColumnInfoData->info.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX); + + pBlock->info.window.skey = *(int64_t*)pColumnInfoData->pData; + pBlock->info.window.ekey = *(int64_t*)(pColumnInfoData->pData + TSDB_KEYSIZE * (start - 1)); + } + } + + tfree(p); +} + +static SColumnInfo* doGetTagColumnInfoById(SColumnInfo* pTagColList, int32_t numOfTags, int16_t colId); +static void doSetTagValueInParam(void* pTable, int32_t tagColId, tVariant *tag, int16_t type, int16_t bytes); + +static uint32_t doFilterByBlockTimeWindow(STableScanInfo* pTableScanInfo, SSDataBlock* pBlock) { + SQLFunctionCtx* pCtx = pTableScanInfo->pCtx; + uint32_t status = BLK_DATA_NO_NEEDED; + + int32_t numOfOutput = pTableScanInfo->numOfOutput; + for (int32_t i = 0; i < numOfOutput; ++i) { + int32_t functionId = pCtx[i].functionId; + int32_t colId = pTableScanInfo->pExpr[i].base.colInfo.colId; + + // group by + first/last should not apply the first/last block filter + status |= aAggs[functionId].dataReqFunc(&pTableScanInfo->pCtx[i], &pBlock->info.window, colId); + if ((status & BLK_DATA_ALL_NEEDED) == BLK_DATA_ALL_NEEDED) { + return status; + } + } + + return status; +} + +static void doSetFilterColumnInfo(SQuery* pQuery, SSDataBlock* pBlock) { + if (pQuery->numOfFilterCols > 0 && pQuery->pFilterInfo[0].pData != NULL) { + return; + } + + // set the initial static data value filter expression + for (int32_t i = 0; i < pQuery->numOfFilterCols; ++i) { + for (int32_t j = 0; j < pBlock->info.numOfCols; ++j) { + SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, j); + + if (pQuery->pFilterInfo[i].info.colId == pColInfo->info.colId) { + pQuery->pFilterInfo[i].pData = pColInfo->pData; + break; + } + } + } +} + +int32_t loadDataBlockOnDemand(SQueryRuntimeEnv* pRuntimeEnv, STableScanInfo* pTableScanInfo, SSDataBlock* pBlock, + uint32_t* status) { + *status = BLK_DATA_NO_NEEDED; + pBlock->pDataBlock = NULL; + pBlock->pBlockStatis = NULL; + + SQuery* pQuery = pRuntimeEnv->pQuery; + int64_t groupId = pQuery->current->groupIndex; + bool ascQuery = QUERY_IS_ASC_QUERY(pQuery); + + SQInfo* pQInfo = pRuntimeEnv->qinfo; + SQueryCostInfo* pCost = &pQInfo->summary; + + if (pRuntimeEnv->pTsBuf != NULL) { + (*status) = BLK_DATA_ALL_NEEDED; + + if (pQuery->stableQuery) { // todo refactor + SExprInfo* pExprInfo = &pTableScanInfo->pExpr[0]; + int16_t tagId = (int16_t)pExprInfo->base.arg->argValue.i64; + SColumnInfo* pColInfo = doGetTagColumnInfoById(pQuery->tagColList, pQuery->numOfTags, tagId); + + // compare tag first + tVariant t = {0}; + doSetTagValueInParam(pQuery->current->pTable, tagId, &t, pColInfo->type, pColInfo->bytes); + setTimestampListJoinInfo(pRuntimeEnv, &t, pQuery->current); + + STSElem elem = tsBufGetElem(pRuntimeEnv->pTsBuf); + if (!tsBufIsValidElem(&elem) || (tsBufIsValidElem(&elem) && (tVariantCompare(&t, elem.tag) != 0))) { + (*status) = BLK_DATA_DISCARD; + return TSDB_CODE_SUCCESS; + } + } + } + + // Calculate all time windows that are overlapping or contain current data block. + // If current data block is contained by all possible time window, do not load current data block. + if (pQuery->numOfFilterCols > 0 || pQuery->groupbyColumn || pQuery->sw.gap > 0 || + (QUERY_IS_INTERVAL_QUERY(pQuery) && overlapWithTimeWindow(pQuery, &pBlock->info))) { + (*status) = BLK_DATA_ALL_NEEDED; + } + + // check if this data block is required to load + if ((*status) != BLK_DATA_ALL_NEEDED) { + // the pCtx[i] result is belonged to previous time window since the outputBuf has not been set yet, + // the filter result may be incorrect. So in case of interval query, we need to set the correct time output buffer + if (QUERY_IS_INTERVAL_QUERY(pQuery)) { + SResultRow* pResult = NULL; + + bool masterScan = IS_MASTER_SCAN(pRuntimeEnv); + TSKEY k = ascQuery? pBlock->info.window.skey : pBlock->info.window.ekey; + + STimeWindow win = getActiveTimeWindow(pTableScanInfo->pResultRowInfo, k, pQuery); + if (setWindowOutputBufByKey(pRuntimeEnv, pTableScanInfo->pResultRowInfo, &win, masterScan, &pResult, groupId, + pTableScanInfo->pCtx, pTableScanInfo->numOfOutput, + pTableScanInfo->rowCellInfoOffset) != TSDB_CODE_SUCCESS) { + longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY); + } + } else if (pQuery->stableQuery && (!isTsCompQuery(pQuery))) { // stable aggregate, not interval aggregate or normal column aggregate + doSetTableGroupOutputBuf(pRuntimeEnv, pTableScanInfo->pResultRowInfo, pTableScanInfo->pCtx, + pTableScanInfo->rowCellInfoOffset, pTableScanInfo->numOfOutput, + pQuery->current->groupIndex); + } + + (*status) = doFilterByBlockTimeWindow(pTableScanInfo, pBlock); } + SDataBlockInfo* pBlockInfo = &pBlock->info; if ((*status) == BLK_DATA_NO_NEEDED) { - qDebug("QInfo:%p data block discard, brange:%"PRId64 "-%"PRId64", rows:%d", GET_QINFO_ADDR(pRuntimeEnv), - pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows); + qDebug("QInfo:%p data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo, pBlockInfo->window.skey, + pBlockInfo->window.ekey, pBlockInfo->rows); pCost->discardBlocks += 1; } else if ((*status) == BLK_DATA_STATIS_NEEDED) { - // this function never returns error? - tsdbRetrieveDataBlockStatisInfo(pQueryHandle, pStatis); pCost->loadBlockStatis += 1; + tsdbRetrieveDataBlockStatisInfo(pTableScanInfo->pQueryHandle, &pBlock->pBlockStatis); - if (*pStatis == NULL) { // data block statistics does not exist, load data block - *pDataBlock = tsdbRetrieveDataBlock(pQueryHandle, NULL); - pCost->totalCheckedRows += pBlockInfo->rows; + if (pBlock->pBlockStatis == NULL) { // data block statistics does not exist, load data block + pBlock->pDataBlock = tsdbRetrieveDataBlock(pTableScanInfo->pQueryHandle, NULL); + pCost->totalCheckedRows += pBlock->info.rows; } } else { assert((*status) == BLK_DATA_ALL_NEEDED); // load the data block statistics to perform further filter pCost->loadBlockStatis += 1; - tsdbRetrieveDataBlockStatisInfo(pQueryHandle, pStatis); + tsdbRetrieveDataBlockStatisInfo(pTableScanInfo->pQueryHandle, &pBlock->pBlockStatis); + + if (pQuery->topBotQuery && pBlock->pBlockStatis != NULL) { + bool load = false; + for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { + int32_t functionId = pTableScanInfo->pCtx[i].functionId; + if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM) { + load = topbot_datablock_filter(&pTableScanInfo->pCtx[i], (char*)&(pBlock->pBlockStatis[i].min), + (char*)&(pBlock->pBlockStatis[i].max)); + if (!load) { // current block has been discard due to filter applied + pCost->discardBlocks += 1; + qDebug("QInfo:%p data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo, + pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows); + (*status) = BLK_DATA_DISCARD; + return TSDB_CODE_SUCCESS; + } + } + } + } - if (!needToLoadDataBlock(pRuntimeEnv, *pStatis, pRuntimeEnv->pCtx, pBlockInfo->rows)) { - // current block has been discard due to filter applied + // current block has been discard due to filter applied + if (!doFilterByBlockStatistics(pRuntimeEnv, pBlock->pBlockStatis, pTableScanInfo->pCtx, pBlockInfo->rows)) { pCost->discardBlocks += 1; - qDebug("QInfo:%p data block discard, brange:%"PRId64 "-%"PRId64", rows:%d", GET_QINFO_ADDR(pRuntimeEnv), - pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows); + qDebug("QInfo:%p data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo, pBlockInfo->window.skey, + pBlockInfo->window.ekey, pBlockInfo->rows); (*status) = BLK_DATA_DISCARD; + return TSDB_CODE_SUCCESS; } pCost->totalCheckedRows += pBlockInfo->rows; pCost->loadBlocks += 1; - *pDataBlock = tsdbRetrieveDataBlock(pQueryHandle, NULL); - if (*pDataBlock == NULL) { + pBlock->pDataBlock = tsdbRetrieveDataBlock(pTableScanInfo->pQueryHandle, NULL); + if (pBlock->pDataBlock == NULL) { return terrno; } + + doSetFilterColumnInfo(pQuery, pBlock); + if (pQuery->numOfFilterCols > 0 || pRuntimeEnv->pTsBuf != NULL) { + filterRowsInDataBlock(pRuntimeEnv, pQuery->pFilterInfo, pQuery->numOfFilterCols, pBlock, ascQuery); + } } return TSDB_CODE_SUCCESS; @@ -2746,148 +2726,6 @@ int32_t binarySearchForKey(char *pValue, int num, TSKEY key, int order) { return midPos; } -static void expandBuffer(SQueryRuntimeEnv* pRuntimeEnv, int32_t newSize, void* qinfo) { - SQuery* pQuery = pRuntimeEnv->pQuery; - SResultRec *pRec = &pQuery->rec; - - assert(newSize > 0); - - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - int32_t bytes = pQuery->pExpr1[i].bytes; - - char *tmp = realloc(pQuery->sdata[i], bytes * newSize + sizeof(tFilePage)); - if (tmp == NULL) { - longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY); - } else { - memset(tmp + sizeof(tFilePage) + bytes * pRec->rows, 0, (size_t)((newSize - pRec->rows) * bytes)); - pQuery->sdata[i] = (tFilePage *)tmp; - } - } - - pRec->capacity = newSize; - qDebug("QInfo:%p realloc output buffer, new size: %d rows, old:%" PRId64 ", remain:%" PRId64, qinfo, newSize, - pRec->capacity, newSize - pRec->rows); -} - -static void ensureOutputBuffer(SQueryRuntimeEnv* pRuntimeEnv, int32_t numOfRows) { - // in case of prj/diff query, ensure the output buffer is sufficient to accommodate the results of current block - SQuery* pQuery = pRuntimeEnv->pQuery; - if (!QUERY_IS_INTERVAL_QUERY(pQuery) && !pRuntimeEnv->groupbyColumn && !isFixedOutputQuery(pRuntimeEnv) && !isTsCompQuery(pQuery)) { - SResultRec *pRec = &pQuery->rec; - - int32_t remain = (int32_t)(pRec->capacity - pRec->rows); - if (remain < numOfRows) { - int32_t newSize = (int32_t)(pRec->capacity + (numOfRows - remain)); - expandBuffer(pRuntimeEnv, newSize, GET_QINFO_ADDR(pRuntimeEnv)); - - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - int32_t bytes = pQuery->pExpr1[i].bytes; - - // set the pCtx output buffer position - pRuntimeEnv->pCtx[i].pOutput = pQuery->sdata[i]->data + pRec->rows * bytes; - - int32_t functionId = pQuery->pExpr1[i].base.functionId; - if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF) { - pRuntimeEnv->pCtx[i].ptsOutputBuf = pRuntimeEnv->pCtx[0].pOutput; - } - } - } - } -} - -static void doSetInitialTimewindow(SQueryRuntimeEnv* pRuntimeEnv, SDataBlockInfo* pBlockInfo) { - SQuery* pQuery = pRuntimeEnv->pQuery; - - if (QUERY_IS_INTERVAL_QUERY(pQuery) && pRuntimeEnv->resultRowInfo.prevSKey == TSKEY_INITIAL_VAL) { - STimeWindow w = TSWINDOW_INITIALIZER; - SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo; - - if (QUERY_IS_ASC_QUERY(pQuery)) { - getAlignQueryTimeWindow(pQuery, pBlockInfo->window.skey, pBlockInfo->window.skey, pQuery->window.ekey, &w); - pWindowResInfo->prevSKey = w.skey; - } else { // the start position of the first time window in the endpoint that spreads beyond the queried last timestamp - getAlignQueryTimeWindow(pQuery, pBlockInfo->window.ekey, pQuery->window.ekey, pBlockInfo->window.ekey, &w); - pWindowResInfo->prevSKey = w.skey; - } - } -} - -static int64_t doScanAllDataBlocks(SQueryRuntimeEnv *pRuntimeEnv) { - SQuery *pQuery = pRuntimeEnv->pQuery; - STableQueryInfo* pTableQueryInfo = pQuery->current; - SQueryCostInfo* summary = &pRuntimeEnv->summary; - - qDebug("QInfo:%p query start, qrange:%" PRId64 "-%" PRId64 ", lastkey:%" PRId64 ", order:%d", - GET_QINFO_ADDR(pRuntimeEnv), pTableQueryInfo->win.skey, pTableQueryInfo->win.ekey, pTableQueryInfo->lastKey, - pQuery->order.order); - - TsdbQueryHandleT pQueryHandle = IS_MASTER_SCAN(pRuntimeEnv)? pRuntimeEnv->pQueryHandle : pRuntimeEnv->pSecQueryHandle; - int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); - - SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER; - while (tsdbNextDataBlock(pQueryHandle)) { - summary->totalBlocks += 1; - - if (IS_MASTER_SCAN(pRuntimeEnv)) { - pQuery->numOfCheckedBlocks += 1; - } - - if (isQueryKilled(GET_QINFO_ADDR(pRuntimeEnv))) { - longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); - } - - tsdbRetrieveDataBlockInfo(pQueryHandle, &blockInfo); - doSetInitialTimewindow(pRuntimeEnv, &blockInfo); - - // in case of prj/diff query, ensure the output buffer is sufficient to accommodate the results of current block - ensureOutputBuffer(pRuntimeEnv, blockInfo.rows); - - SDataStatis *pStatis = NULL; - SArray * pDataBlock = NULL; - uint32_t status = 0; - - int32_t ret = loadDataBlockOnDemand(pRuntimeEnv, &pRuntimeEnv->resultRowInfo, pQueryHandle, &blockInfo, &pStatis, &pDataBlock, &status); - if (ret != TSDB_CODE_SUCCESS) { - break; - } - - if (status == BLK_DATA_DISCARD) { - pQuery->current->lastKey = - QUERY_IS_ASC_QUERY(pQuery) ? blockInfo.window.ekey + step : blockInfo.window.skey + step; - continue; - } - - // query start position can not move into tableApplyFunctionsOnBlock due to limit/offset condition - pQuery->pos = QUERY_IS_ASC_QUERY(pQuery)? 0 : blockInfo.rows - 1; - int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, &blockInfo, pStatis, binarySearchForKey, pDataBlock); - - summary->totalRows += blockInfo.rows; - qDebug("QInfo:%p check data block, brange:%" PRId64 "-%" PRId64 ", numOfRows:%d, numOfRes:%d, lastKey:%"PRId64, GET_QINFO_ADDR(pRuntimeEnv), - blockInfo.window.skey, blockInfo.window.ekey, blockInfo.rows, numOfRes, pQuery->current->lastKey); - - // while the output buffer is full or limit/offset is applied, query may be paused here - if (Q_STATUS_EQUAL(pQuery->status, QUERY_RESBUF_FULL | QUERY_COMPLETED)) { - break; - } - } - - if (terrno != TSDB_CODE_SUCCESS) { - longjmp(pRuntimeEnv->env, terrno); - } - - // if the result buffer is not full, set the query complete - if (!Q_STATUS_EQUAL(pQuery->status, QUERY_RESBUF_FULL)) { - setQueryStatus(pQuery, QUERY_COMPLETED); - } - - if (QUERY_IS_INTERVAL_QUERY(pQuery)) { - closeAllResultRows(&pRuntimeEnv->resultRowInfo); - pRuntimeEnv->resultRowInfo.curIndex = pRuntimeEnv->resultRowInfo.size - 1; // point to the last time window - } - - return 0; -} - /* * set tag value in SQLFunctionCtx * e.g.,tag information into input buffer @@ -2895,35 +2733,26 @@ static int64_t doScanAllDataBlocks(SQueryRuntimeEnv *pRuntimeEnv) { static void doSetTagValueInParam(void* pTable, int32_t tagColId, tVariant *tag, int16_t type, int16_t bytes) { tVariantDestroy(tag); + char* val = NULL; if (tagColId == TSDB_TBNAME_COLUMN_INDEX) { - char* val = tsdbGetTableName(pTable); + val = tsdbGetTableName(pTable); assert(val != NULL); - - tVariantCreateFromBinary(tag, varDataVal(val), varDataLen(val), TSDB_DATA_TYPE_BINARY); } else { - char* val = tsdbGetTableTagVal(pTable, tagColId, type, bytes); - if (val == NULL) { - tag->nType = TSDB_DATA_TYPE_NULL; - return; - } - - if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { - if (isNull(val, type)) { - tag->nType = TSDB_DATA_TYPE_NULL; - return; - } + val = tsdbGetTableTagVal(pTable, tagColId, type, bytes); + } - int32_t maxLen = bytes - VARSTR_HEADER_SIZE; - int32_t len = (varDataLen(val) > maxLen)? maxLen:varDataLen(val); - tVariantCreateFromBinary(tag, varDataVal(val), len, type); - } else { - if (isNull(val, type)) { - tag->nType = TSDB_DATA_TYPE_NULL; - return; - } + if (val == NULL || isNull(val, type)) { + tag->nType = TSDB_DATA_TYPE_NULL; + return; + } - tVariantCreateFromBinary(tag, val, bytes, type); - } + if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { + int32_t maxLen = bytes - VARSTR_HEADER_SIZE; + int32_t len = (varDataLen(val) > maxLen)? maxLen:varDataLen(val); + tVariantCreateFromBinary(tag, varDataVal(val), len, type); + //tVariantCreateFromBinary(tag, varDataVal(val), varDataLen(val), type); + } else { + tVariantCreateFromBinary(tag, val, bytes, type); } } @@ -2939,24 +2768,28 @@ static SColumnInfo* doGetTagColumnInfoById(SColumnInfo* pTagColList, int32_t num return NULL; } -void setTagVal(SQueryRuntimeEnv *pRuntimeEnv, void *pTable) { - SQuery *pQuery = pRuntimeEnv->pQuery; - SQInfo* pQInfo = GET_QINFO_ADDR(pRuntimeEnv); +void setTagValue(SOperatorInfo* pOperatorInfo, void *pTable, SQLFunctionCtx* pCtx, int32_t numOfOutput) { + SQueryRuntimeEnv* pRuntimeEnv = pOperatorInfo->pRuntimeEnv; - SExprInfo *pExprInfo = &pQuery->pExpr1[0]; - if (pQuery->numOfOutput == 1 && pExprInfo->base.functionId == TSDB_FUNC_TS_COMP && pRuntimeEnv->stableQuery) { + SExprInfo *pExpr = pOperatorInfo->pExpr; + SQuery *pQuery = pRuntimeEnv->pQuery; + + SExprInfo* pExprInfo = &pExpr[0]; + if (pQuery->numOfOutput == 1 && pExprInfo->base.functionId == TSDB_FUNC_TS_COMP && pQuery->stableQuery) { assert(pExprInfo->base.numOfParams == 1); - int16_t tagColId = (int16_t)pExprInfo->base.arg->argValue.i64; + int16_t tagColId = (int16_t)pExprInfo->base.arg->argValue.i64; SColumnInfo* pColInfo = doGetTagColumnInfoById(pQuery->tagColList, pQuery->numOfTags, tagColId); - doSetTagValueInParam(pTable, tagColId, &pRuntimeEnv->pCtx[0].tag, pColInfo->type, pColInfo->bytes); + doSetTagValueInParam(pTable, tagColId, &pCtx[0].tag, pColInfo->type, pColInfo->bytes); + return; } else { // set tag value, by which the results are aggregated. int32_t offset = 0; memset(pRuntimeEnv->tagVal, 0, pQuery->tagLen); - for (int32_t idx = 0; idx < pQuery->numOfOutput; ++idx) { - SExprInfo* pLocalExprInfo = &pQuery->pExpr1[idx]; + + for (int32_t idx = 0; idx < numOfOutput; ++idx) { + SExprInfo* pLocalExprInfo = &pExpr[idx]; // ts_comp column required the tag value for join filter if (!TSDB_COL_IS_TAG(pLocalExprInfo->base.colInfo.flag)) { @@ -2964,39 +2797,28 @@ void setTagVal(SQueryRuntimeEnv *pRuntimeEnv, void *pTable) { } // todo use tag column index to optimize performance - doSetTagValueInParam(pTable, pLocalExprInfo->base.colInfo.colId, &pRuntimeEnv->pCtx[idx].tag, - pLocalExprInfo->type, pLocalExprInfo->bytes); + doSetTagValueInParam(pTable, pLocalExprInfo->base.colInfo.colId, &pCtx[idx].tag, pLocalExprInfo->type, + pLocalExprInfo->bytes); if (IS_NUMERIC_TYPE(pLocalExprInfo->type) || pLocalExprInfo->type == TSDB_DATA_TYPE_BOOL) { - memcpy(pRuntimeEnv->tagVal + offset, &pRuntimeEnv->pCtx[idx].tag.i64, pLocalExprInfo->bytes); + memcpy(pRuntimeEnv->tagVal + offset, &pCtx[idx].tag.i64, pLocalExprInfo->bytes); } else { - memcpy(pRuntimeEnv->tagVal + offset, pRuntimeEnv->pCtx[idx].tag.pz, pRuntimeEnv->pCtx[idx].tag.nLen); + memcpy(pRuntimeEnv->tagVal + offset, pCtx[idx].tag.pz, pCtx[idx].tag.nLen); } offset += pLocalExprInfo->bytes; } - // set the join tag for first column - SSqlFuncMsg *pFuncMsg = &pExprInfo->base; - if ((pFuncMsg->functionId == TSDB_FUNC_TS || pFuncMsg->functionId == TSDB_FUNC_PRJ) && pRuntimeEnv->pTsBuf != NULL && - pFuncMsg->colInfo.colIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX) { - assert(pFuncMsg->numOfParams == 1); - - int16_t tagColId = (int16_t)pExprInfo->base.arg->argValue.i64; - SColumnInfo *pColInfo = doGetTagColumnInfoById(pQuery->tagColList, pQuery->numOfTags, tagColId); - - doSetTagValueInParam(pTable, tagColId, &pRuntimeEnv->pCtx[0].tag, pColInfo->type, pColInfo->bytes); - - int16_t tagType = pRuntimeEnv->pCtx[0].tag.nType; - if (tagType == TSDB_DATA_TYPE_BINARY || tagType == TSDB_DATA_TYPE_NCHAR) { - qDebug("QInfo:%p set tag value for join comparison, colId:%" PRId64 ", val:%s", pQInfo, - pExprInfo->base.arg->argValue.i64, pRuntimeEnv->pCtx[0].tag.pz); - } else { - qDebug("QInfo:%p set tag value for join comparison, colId:%" PRId64 ", val:%" PRId64, pQInfo, - pExprInfo->base.arg->argValue.i64, pRuntimeEnv->pCtx[0].tag.i64); - } + //todo : use index to avoid iterator all possible output columns + if (pQuery->stableQuery && pQuery->stabledev && (pRuntimeEnv->prevResult != NULL)) { + setParamForStableStddev(pRuntimeEnv, pCtx, numOfOutput, pExprInfo); } } + + // set the tsBuf start position before check each data block + if (pRuntimeEnv->pTsBuf != NULL) { + setCtxTagForJoin(pRuntimeEnv, &pCtx[0], pExprInfo, pTable); + } } static UNUSED_FUNC void printBinaryData(int32_t functionId, char *data, int32_t srcDataType) { @@ -3102,35 +2924,36 @@ void UNUSED_FUNC displayInterResult(tFilePage **pdata, SQueryRuntimeEnv* pRuntim } } -static int32_t doCopyToSData(SQueryRuntimeEnv* pRuntimeEnv, SGroupResInfo* pGroupResInfo, int32_t orderType); +void copyToSDataBlock(SQueryRuntimeEnv* pRuntimeEnv, int32_t threshold, SSDataBlock* pBlock, int32_t* offset) { + SGroupResInfo* pGroupResInfo = &pRuntimeEnv->groupResInfo; + pBlock->info.rows = 0; -void copyResToQueryResultBuf(SQInfo *pQInfo, SQuery *pQuery) { - SGroupResInfo* pGroupResInfo = &pQInfo->groupResInfo; - - while(pGroupResInfo->currentGroup < pGroupResInfo->totalGroup) { + int32_t code = TSDB_CODE_SUCCESS; + while (pGroupResInfo->currentGroup < pGroupResInfo->totalGroup) { // all results in current group have been returned to client, try next group if ((pGroupResInfo->pRows == NULL) || taosArrayGetSize(pGroupResInfo->pRows) == 0) { assert(pGroupResInfo->index == 0); - if ((pQInfo->code = mergeIntoGroupResult(&pQInfo->groupResInfo, pQInfo)) != TSDB_CODE_SUCCESS) { + if ((code = mergeIntoGroupResult(&pRuntimeEnv->groupResInfo, pRuntimeEnv, offset)) != TSDB_CODE_SUCCESS) { return; } } - pQuery->rec.rows = doCopyToSData(&pQInfo->runtimeEnv, pGroupResInfo, TSDB_ORDER_ASC); + doCopyToSDataBlock(pRuntimeEnv, pGroupResInfo, TSDB_ORDER_ASC, pBlock); // current data are all dumped to result buffer, clear it - if (!hasRemainData(pGroupResInfo)) { + if (!hasRemainDataInCurrentGroup(pGroupResInfo)) { cleanupGroupResInfo(pGroupResInfo); if (!incNextGroup(pGroupResInfo)) { - SET_STABLE_QUERY_OVER(pQInfo); + break; } } - // enough results in data buffer, return - if (pQuery->rec.rows >= pQuery->rec.threshold) { - break; + // enough results in data buffer, return + if (pBlock->info.rows >= threshold) { + break; + } } - } + } static void updateTableQueryInfoForReverseScan(SQuery *pQuery, STableQueryInfo *pTableQueryInfo) { @@ -3144,71 +2967,17 @@ static void updateTableQueryInfoForReverseScan(SQuery *pQuery, STableQueryInfo * SWITCH_ORDER(pTableQueryInfo->cur.order); pTableQueryInfo->cur.vgroupIndex = -1; - // set the index at the end of time window + // set the index to be the end slot of result rows array pTableQueryInfo->resInfo.curIndex = pTableQueryInfo->resInfo.size - 1; } -static void disableFuncInReverseScanImpl(SQueryRuntimeEnv* pRuntimeEnv, SResultRowInfo *pWindowResInfo, int32_t order) { +static void setupQueryRangeForReverseScan(SQueryRuntimeEnv* pRuntimeEnv) { SQuery* pQuery = pRuntimeEnv->pQuery; - for (int32_t i = 0; i < pWindowResInfo->size; ++i) { - bool closed = getResultRowStatus(pWindowResInfo, i); - if (!closed) { - continue; - } - - SResultRow *pRow = getResultRow(pWindowResInfo, i); - - // open/close the specified query for each group result - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - int32_t functId = pQuery->pExpr1[j].base.functionId; - SResultRowCellInfo* pInfo = getResultCell(pRuntimeEnv, pRow, j); - - if (((functId == TSDB_FUNC_FIRST || functId == TSDB_FUNC_FIRST_DST) && order == TSDB_ORDER_ASC) || - ((functId == TSDB_FUNC_LAST || functId == TSDB_FUNC_LAST_DST) && order == TSDB_ORDER_DESC)) { - pInfo->complete = false; - } else if (functId != TSDB_FUNC_TS && functId != TSDB_FUNC_TAG) { - pInfo->complete = true; - } - } - } -} - -void disableFuncInReverseScan(SQInfo *pQInfo) { - SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery *pQuery = pRuntimeEnv->pQuery; - int32_t order = pQuery->order.order; - - // group by normal columns and interval query on normal table - SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo; - if (pRuntimeEnv->groupbyColumn || QUERY_IS_INTERVAL_QUERY(pQuery)) { - disableFuncInReverseScanImpl(pRuntimeEnv, pWindowResInfo, order); - } else { // for simple result of table query, - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { // todo refactor - int32_t functId = pQuery->pExpr1[j].base.functionId; - - SQLFunctionCtx *pCtx = &pRuntimeEnv->pCtx[j]; - if (pCtx->resultInfo == NULL) { - continue; // resultInfo is NULL, means no data checked in previous scan - } - - if (((functId == TSDB_FUNC_FIRST || functId == TSDB_FUNC_FIRST_DST) && order == TSDB_ORDER_ASC) || - ((functId == TSDB_FUNC_LAST || functId == TSDB_FUNC_LAST_DST) && order == TSDB_ORDER_DESC)) { - pCtx->resultInfo->complete = false; - } else if (functId != TSDB_FUNC_TS && functId != TSDB_FUNC_TAG) { - pCtx->resultInfo->complete = true; - } - } - } -} - -static void setupQueryRangeForReverseScan(SQInfo* pQInfo) { - SQuery* pQuery = pQInfo->runtimeEnv.pQuery; - int32_t numOfGroups = (int32_t)(GET_NUM_OF_TABLEGROUP(pQInfo)); - + int32_t numOfGroups = (int32_t)(GET_NUM_OF_TABLEGROUP(pRuntimeEnv)); for(int32_t i = 0; i < numOfGroups; ++i) { - SArray *group = GET_TABLEGROUP(pQInfo, i); - SArray *tableKeyGroup = taosArrayGetP(pQInfo->tableGroupInfo.pGroupList, i); + SArray *group = GET_TABLEGROUP(pRuntimeEnv, i); + SArray *tableKeyGroup = taosArrayGetP(pQuery->tableGroupInfo.pGroupList, i); size_t t = taosArrayGetSize(group); for (int32_t j = 0; j < t; ++j) { @@ -3225,209 +2994,117 @@ static void setupQueryRangeForReverseScan(SQInfo* pQInfo) { } } -void switchCtxOrder(SQueryRuntimeEnv *pRuntimeEnv) { - SQuery *pQuery = pRuntimeEnv->pQuery; - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - SWITCH_ORDER(pRuntimeEnv->pCtx[i].order); +void switchCtxOrder(SQLFunctionCtx* pCtx, int32_t numOfOutput) { + for (int32_t i = 0; i < numOfOutput; ++i) { + SWITCH_ORDER(pCtx[i].order); } } int32_t initResultRow(SResultRow *pResultRow) { pResultRow->pCellInfo = (SResultRowCellInfo*)((char*)pResultRow + sizeof(SResultRow)); pResultRow->pageId = -1; - pResultRow->rowId = -1; + pResultRow->offset = -1; return TSDB_CODE_SUCCESS; } -void resetDefaultResInfoOutputBuf(SQueryRuntimeEnv *pRuntimeEnv) { - SQuery *pQuery = pRuntimeEnv->pQuery; - +/* + * The start of each column SResultRowCellInfo is denote by RowCellInfoOffset. + * Note that in case of top/bottom query, the whole multiple rows of result is treated as only one row of results. + * +------------+-----------------result column 1-----------+-----------------result column 2-----------+ + * + SResultRow | SResultRowCellInfo | intermediate buffer1 | SResultRowCellInfo | intermediate buffer 2| + * +------------+-------------------------------------------+-------------------------------------------+ + * offset[0] offset[1] + */ +void setDefaultOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, SOptrBasicInfo *pInfo, int64_t uid) { + SQLFunctionCtx* pCtx = pInfo->pCtx; + SSDataBlock* pDataBlock = pInfo->pRes; + int32_t* rowCellInfoOffset = pInfo->rowCellInfoOffset; + SResultRowInfo* pResultRowInfo = &pInfo->resultRowInfo; + int32_t tid = 0; - int64_t uid = 0; - SResultRow* pRow = doPrepareResultRowFromKey(pRuntimeEnv, &pRuntimeEnv->resultRowInfo, (char *)&tid, sizeof(tid), true, uid); + SResultRow* pRow = doPrepareResultRowFromKey(pRuntimeEnv, pResultRowInfo, (char *)&tid, sizeof(tid), true, uid); - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - SQLFunctionCtx *pCtx = &pRuntimeEnv->pCtx[i]; - pCtx->pOutput = pQuery->sdata[i]->data; + for (int32_t i = 0; i < pDataBlock->info.numOfCols; ++i) { + SColumnInfoData* pData = taosArrayGet(pDataBlock->pDataBlock, i); /* * set the output buffer information and intermediate buffer * not all queries require the interResultBuf, such as COUNT/TAGPRJ/PRJ/TAG etc. */ - SResultRowCellInfo* pCellInfo = getResultCell(pRuntimeEnv, pRow, i); + SResultRowCellInfo* pCellInfo = getResultCell(pRow, i, rowCellInfoOffset); RESET_RESULT_INFO(pCellInfo); - pCtx->resultInfo = pCellInfo; + + pCtx[i].resultInfo = pCellInfo; + pCtx[i].pOutput = pData->pData; + assert(pCtx[i].pOutput != NULL); // set the timestamp output buffer for top/bottom/diff query - int32_t functionId = pQuery->pExpr1[i].base.functionId; + int32_t functionId = pCtx[i].functionId; if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF) { - pCtx->ptsOutputBuf = pRuntimeEnv->pCtx[0].pOutput; + pCtx[i].ptsOutputBuf = pCtx[0].pOutput; } - - memset(pQuery->sdata[i]->data, 0, (size_t)(pQuery->pExpr1[i].bytes * pQuery->rec.capacity)); } - initCtxOutputBuf(pRuntimeEnv); + initCtxOutputBuffer(pCtx, pDataBlock->info.numOfCols); } -void forwardCtxOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, int64_t output) { - SQuery *pQuery = pRuntimeEnv->pQuery; - - // reset the execution contexts - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - int32_t functionId = pQuery->pExpr1[j].base.functionId; - assert(functionId != TSDB_FUNC_DIFF); +void updateOutputBuf(SArithOperatorInfo* pInfo, int32_t numOfInputRows) { + SOptrBasicInfo* pBInfo = &pInfo->binfo; + SSDataBlock* pDataBlock = pBInfo->pRes; - // set next output position - if (IS_OUTER_FORWARD(aAggs[functionId].status)) { - pRuntimeEnv->pCtx[j].pOutput += pRuntimeEnv->pCtx[j].outputBytes * output; - } + int32_t newSize = pDataBlock->info.rows + numOfInputRows; + if (pInfo->bufCapacity < newSize) { + for(int32_t i = 0; i < pDataBlock->info.numOfCols; ++i) { + SColumnInfoData *pColInfo = taosArrayGet(pDataBlock->pDataBlock, i); + char* p = realloc(pColInfo->pData, newSize * pColInfo->info.bytes); + if (p != NULL) { + pColInfo->pData = p; - if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM) { - /* - * NOTE: for top/bottom query, the value of first column of output (timestamp) are assigned - * in the procedure of top/bottom routine - * the output buffer in top/bottom routine is ptsOutputBuf, so we need to forward the output buffer - * - * diff function is handled in multi-output function - */ - pRuntimeEnv->pCtx[j].ptsOutputBuf = (char*)pRuntimeEnv->pCtx[j].ptsOutputBuf + TSDB_KEYSIZE * output; + // it starts from the tail of the previously generated results. + pBInfo->pCtx[i].pOutput = pColInfo->pData; + pInfo->bufCapacity = newSize; + } else { + // longjmp + } } - - RESET_RESULT_INFO(pRuntimeEnv->pCtx[j].resultInfo); } -} - -void initCtxOutputBuf(SQueryRuntimeEnv *pRuntimeEnv) { - SQuery *pQuery = pRuntimeEnv->pQuery; - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - int32_t functionId = pQuery->pExpr1[j].base.functionId; - pRuntimeEnv->pCtx[j].currentStage = 0; + for (int32_t i = 0; i < pDataBlock->info.numOfCols; ++i) { + SColumnInfoData *pColInfo = taosArrayGet(pDataBlock->pDataBlock, i); + pBInfo->pCtx[i].pOutput = pColInfo->pData + pColInfo->info.bytes * pDataBlock->info.rows; - SResultRowCellInfo* pResInfo = GET_RES_INFO(&pRuntimeEnv->pCtx[j]); - if (pResInfo->initialized) { - continue; + // re-estabilish output buffer pointer. + int32_t functionId = pBInfo->pCtx[i].functionId; + if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF) { + pBInfo->pCtx[i].ptsOutputBuf = pBInfo->pCtx[0].pOutput; } - - aAggs[functionId].init(&pRuntimeEnv->pCtx[j]); } } -void skipResults(SQueryRuntimeEnv *pRuntimeEnv) { - SQuery *pQuery = pRuntimeEnv->pQuery; - if (pQuery->rec.rows == 0 || pQuery->limit.offset == 0) { - return; - } - - if (pQuery->rec.rows <= pQuery->limit.offset) { - qDebug("QInfo:%p skip rows:%" PRId64 ", new offset:%" PRIu64, GET_QINFO_ADDR(pRuntimeEnv), pQuery->rec.rows, - pQuery->limit.offset - pQuery->rec.rows); - - pQuery->limit.offset -= pQuery->rec.rows; - pQuery->rec.rows = 0; - - resetDefaultResInfoOutputBuf(pRuntimeEnv); - - // clear the buffer full flag if exists - CLEAR_QUERY_STATUS(pQuery, QUERY_RESBUF_FULL); - } else { - int64_t numOfSkip = pQuery->limit.offset; - pQuery->rec.rows -= numOfSkip; - pQuery->limit.offset = 0; - - qDebug("QInfo:%p skip row:%"PRId64", new offset:%d, numOfRows remain:%" PRIu64, GET_QINFO_ADDR(pRuntimeEnv), numOfSkip, - 0, pQuery->rec.rows); - - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - int32_t functionId = pQuery->pExpr1[i].base.functionId; - int32_t bytes = pRuntimeEnv->pCtx[i].outputBytes; - - memmove(pQuery->sdata[i]->data, (char*)pQuery->sdata[i]->data + bytes * numOfSkip, (size_t)(pQuery->rec.rows * bytes)); - pRuntimeEnv->pCtx[i].pOutput = ((char*) pQuery->sdata[i]->data) + pQuery->rec.rows * bytes; - - if (functionId == TSDB_FUNC_DIFF || functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM) { - pRuntimeEnv->pCtx[i].ptsOutputBuf = pRuntimeEnv->pCtx[0].pOutput; - } +void initCtxOutputBuffer(SQLFunctionCtx* pCtx, int32_t size) { + for (int32_t j = 0; j < size; ++j) { + SResultRowCellInfo* pResInfo = GET_RES_INFO(&pCtx[j]); + if (pResInfo->initialized) { + continue; } - updateNumOfResult(pRuntimeEnv, (int32_t)pQuery->rec.rows); + aAggs[pCtx[j].functionId].init(&pCtx[j]); } } -void setQueryStatus(SQuery *pQuery, int8_t status) { +void setQueryStatus(SQueryRuntimeEnv *pRuntimeEnv, int8_t status) { if (status == QUERY_NOT_COMPLETED) { - pQuery->status = status; + pRuntimeEnv->status = status; } else { // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first - CLEAR_QUERY_STATUS(pQuery, QUERY_NOT_COMPLETED); - pQuery->status |= status; - } -} - -bool needRepeatScan(SQueryRuntimeEnv *pRuntimeEnv) { - SQuery *pQuery = pRuntimeEnv->pQuery; - - bool toContinue = false; - if (pRuntimeEnv->groupbyColumn || QUERY_IS_INTERVAL_QUERY(pQuery)) { - // for each group result, call the finalize function for each column - SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo; - - for (int32_t i = 0; i < pWindowResInfo->size; ++i) { - SResultRow *pResult = getResultRow(pWindowResInfo, i); - - setResultOutputBuf(pRuntimeEnv, pResult); - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - int16_t functId = pQuery->pExpr1[j].base.functionId; - if (functId == TSDB_FUNC_TS) { - continue; - } - - aAggs[functId].xNextStep(&pRuntimeEnv->pCtx[j]); - SResultRowCellInfo *pResInfo = GET_RES_INFO(&pRuntimeEnv->pCtx[j]); - - toContinue |= (!pResInfo->complete); - } - } - } else { - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - int16_t functId = pQuery->pExpr1[j].base.functionId; - if (functId == TSDB_FUNC_TS) { - continue; - } - - aAggs[functId].xNextStep(&pRuntimeEnv->pCtx[j]); - SResultRowCellInfo *pResInfo = GET_RES_INFO(&pRuntimeEnv->pCtx[j]); - - toContinue |= (!pResInfo->complete); - } + CLEAR_QUERY_STATUS(pRuntimeEnv, QUERY_NOT_COMPLETED); + pRuntimeEnv->status |= status; } - - return toContinue; -} - -static SQueryStatusInfo getQueryStatusInfo(SQueryRuntimeEnv *pRuntimeEnv, TSKEY start) { - SQuery *pQuery = pRuntimeEnv->pQuery; - STableQueryInfo* pTableQueryInfo = pQuery->current; - - assert((start <= pTableQueryInfo->lastKey && QUERY_IS_ASC_QUERY(pQuery)) || - (start >= pTableQueryInfo->lastKey && !QUERY_IS_ASC_QUERY(pQuery))); - - SQueryStatusInfo info = { - .status = pQuery->status, - .windowIndex = pRuntimeEnv->resultRowInfo.curIndex, - .lastKey = start, - }; - - TIME_WINDOW_COPY(info.w, pQuery->window); - return info; } -static void setEnvBeforeReverseScan(SQueryRuntimeEnv *pRuntimeEnv, SQueryStatusInfo *pStatus) { - SQInfo *pQInfo = GET_QINFO_ADDR(pRuntimeEnv); +static void setupEnvForReverseScan(SQueryRuntimeEnv *pRuntimeEnv, SResultRowInfo *pResultRowInfo, SQLFunctionCtx* pCtx, int32_t numOfOutput) { SQuery *pQuery = pRuntimeEnv->pQuery; - pStatus->cur = tsBufGetCursor(pRuntimeEnv->pTsBuf); // save the cursor if (pRuntimeEnv->pTsBuf) { SWITCH_ORDER(pRuntimeEnv->pTsBuf->cur.order); bool ret = tsBufNextPos(pRuntimeEnv->pTsBuf); @@ -3436,228 +3113,49 @@ static void setEnvBeforeReverseScan(SQueryRuntimeEnv *pRuntimeEnv, SQueryStatusI // reverse order time range SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); - SWITCH_ORDER(pQuery->order.order); - - if (QUERY_IS_ASC_QUERY(pQuery)) { - assert(pQuery->window.skey <= pQuery->window.ekey); - } else { - assert(pQuery->window.skey >= pQuery->window.ekey); - } SET_REVERSE_SCAN_FLAG(pRuntimeEnv); - STsdbQueryCond cond = createTsdbQueryCond(pQuery, &pQuery->window); - - setQueryStatus(pQuery, QUERY_NOT_COMPLETED); - switchCtxOrder(pRuntimeEnv); - disableFuncInReverseScan(pQInfo); - setupQueryRangeForReverseScan(pQInfo); - - // clean unused handle - if (pRuntimeEnv->pSecQueryHandle != NULL) { - tsdbCleanupQueryHandle(pRuntimeEnv->pSecQueryHandle); - } - - pRuntimeEnv->pSecQueryHandle = tsdbQueryTables(pQInfo->tsdb, &cond, &pQInfo->tableGroupInfo, pQInfo, &pQInfo->memRef); - if (pRuntimeEnv->pSecQueryHandle == NULL) { - longjmp(pRuntimeEnv->env, terrno); - } -} - -static void clearEnvAfterReverseScan(SQueryRuntimeEnv *pRuntimeEnv, SQueryStatusInfo *pStatus) { - SQuery *pQuery = pRuntimeEnv->pQuery; - STableQueryInfo* pTableQueryInfo = pQuery->current; + setQueryStatus(pRuntimeEnv, QUERY_NOT_COMPLETED); + switchCtxOrder(pCtx, numOfOutput); SWITCH_ORDER(pQuery->order.order); - switchCtxOrder(pRuntimeEnv); - - tsBufSetCursor(pRuntimeEnv->pTsBuf, &pStatus->cur); - if (pRuntimeEnv->pTsBuf) { - pRuntimeEnv->pTsBuf->cur.order = pQuery->order.order; - } - - SET_MASTER_SCAN_FLAG(pRuntimeEnv); - - // update the pQuery->window.skey and pQuery->window.ekey to limit the scan scope of sliding query during reverse scan - pTableQueryInfo->lastKey = pStatus->lastKey; - pQuery->status = pStatus->status; - - pTableQueryInfo->win = pStatus->w; - pQuery->window = pTableQueryInfo->win; -} - -static void restoreTimeWindow(STableGroupInfo* pTableGroupInfo, STsdbQueryCond* pCond) { - assert(pTableGroupInfo->numOfTables == 1); - SArray* pTableKeyGroup = taosArrayGetP(pTableGroupInfo->pGroupList, 0); - STableKeyInfo* pKeyInfo = taosArrayGet(pTableKeyGroup, 0); - pKeyInfo->lastKey = pCond->twindow.skey; -} - -static void handleInterpolationQuery(SQInfo* pQInfo) { - SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; - - SQuery *pQuery = pRuntimeEnv->pQuery; - if (pQuery->numOfCheckedBlocks > 0 || !isPointInterpoQuery(pQuery)) { - return; - } - - SArray *prev = tsdbGetExternalRow(pRuntimeEnv->pQueryHandle, &pQInfo->memRef, TSDB_PREV_ROW); - SArray *next = tsdbGetExternalRow(pRuntimeEnv->pQueryHandle, &pQInfo->memRef, TSDB_NEXT_ROW); - if (prev == NULL || next == NULL) { - return; - } - - // setup the pCtx->start/end info and calculate the interpolation value - SColumnInfoData *startTs = taosArrayGet(prev, 0); - SColumnInfoData *endTs = taosArrayGet(next, 0); - - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - SQLFunctionCtx *pCtx = &pRuntimeEnv->pCtx[i]; - - int32_t functionId = pQuery->pExpr1[i].base.functionId; - SColIndex *pColIndex = &pQuery->pExpr1[i].base.colInfo; - - if (!TSDB_COL_IS_NORMAL_COL(pColIndex->flag)) { - aAggs[functionId].xFunction(pCtx); - continue; - } - - SColumnInfoData *p = taosArrayGet(prev, pColIndex->colIndex); - SColumnInfoData *n = taosArrayGet(next, pColIndex->colIndex); - - assert(p->info.colId == pColIndex->colId); - - pCtx->start.key = *(TSKEY *)startTs->pData; - pCtx->end.key = *(TSKEY *)endTs->pData; - - if (p->info.type != TSDB_DATA_TYPE_BINARY && p->info.type != TSDB_DATA_TYPE_NCHAR) { - GET_TYPED_DATA(pCtx->start.val, double, p->info.type, p->pData); - GET_TYPED_DATA(pCtx->end.val, double, n->info.type, n->pData); - } else { // string pointer - pCtx->start.ptr = p->pData; - pCtx->end.ptr = n->pData; - } - - pCtx->param[2].i64 = (int8_t)pQuery->fillType; - pCtx->startTs = pQuery->window.skey; - if (pQuery->fillVal != NULL) { - if (isNull((const char *)&pQuery->fillVal[i], pCtx->inputType)) { - pCtx->param[1].nType = TSDB_DATA_TYPE_NULL; - } else { // todo refactor, tVariantCreateFromBinary should handle the NULL value - if (pCtx->inputType != TSDB_DATA_TYPE_BINARY && pCtx->inputType != TSDB_DATA_TYPE_NCHAR) { - tVariantCreateFromBinary(&pCtx->param[1], (char *)&pQuery->fillVal[i], pCtx->inputBytes, pCtx->inputType); - } - } - } - - aAggs[functionId].xFunction(pCtx); - } -} - -void scanOneTableDataBlocks(SQueryRuntimeEnv *pRuntimeEnv, TSKEY start) { - SQInfo *pQInfo = (SQInfo *) GET_QINFO_ADDR(pRuntimeEnv); - SQuery *pQuery = pRuntimeEnv->pQuery; - STableQueryInfo *pTableQueryInfo = pQuery->current; - - setQueryStatus(pQuery, QUERY_NOT_COMPLETED); - - // store the start query position - SQueryStatusInfo qstatus = getQueryStatusInfo(pRuntimeEnv, start); - SET_MASTER_SCAN_FLAG(pRuntimeEnv); - - if (!pRuntimeEnv->groupbyColumn && pRuntimeEnv->hasTagResults) { - setTagVal(pRuntimeEnv, pTableQueryInfo->pTable); - } - - while (1) { - doScanAllDataBlocks(pRuntimeEnv); - if (pRuntimeEnv->scanFlag == MASTER_SCAN) { - qstatus.status = pQuery->status; - - // do nothing if no data blocks are found qualified during scan - if (qstatus.lastKey == pTableQueryInfo->lastKey) { - qDebug("QInfo:%p no results generated in this scan", pQInfo); - } - } - - if (!needRepeatScan(pRuntimeEnv)) { - // restore the status code and jump out of loop - if (pRuntimeEnv->scanFlag == REPEAT_SCAN) { - pQuery->status = qstatus.status; - } - - break; - } - - if (pRuntimeEnv->pSecQueryHandle != NULL) { - tsdbCleanupQueryHandle(pRuntimeEnv->pSecQueryHandle); - } - - STsdbQueryCond cond = createTsdbQueryCond(pQuery, &pQuery->window); - restoreTimeWindow(&pQInfo->tableGroupInfo, &cond); - pRuntimeEnv->pSecQueryHandle = tsdbQueryTables(pQInfo->tsdb, &cond, &pQInfo->tableGroupInfo, pQInfo, &pQInfo->memRef); - if (pRuntimeEnv->pSecQueryHandle == NULL) { - longjmp(pRuntimeEnv->env, terrno); - } - - pRuntimeEnv->resultRowInfo.curIndex = qstatus.windowIndex; - setQueryStatus(pQuery, QUERY_NOT_COMPLETED); - pRuntimeEnv->scanFlag = REPEAT_SCAN; - - if (pRuntimeEnv->pTsBuf) { - bool ret = tsBufNextPos(pRuntimeEnv->pTsBuf); - assert(ret); - } - - qDebug("QInfo:%p start to repeat scan data blocks due to query func required, qrange:%"PRId64"-%"PRId64, pQInfo, - cond.twindow.skey, cond.twindow.ekey); - } - - if (needReverseScan(pQuery)) { - setEnvBeforeReverseScan(pRuntimeEnv, &qstatus); - - // reverse scan from current position - qDebug("QInfo:%p start to reverse scan", pQInfo); - doScanAllDataBlocks(pRuntimeEnv); - - clearEnvAfterReverseScan(pRuntimeEnv, &qstatus); - } - - handleInterpolationQuery(pQInfo); + setupQueryRangeForReverseScan(pRuntimeEnv); } -void finalizeQueryResult(SQueryRuntimeEnv *pRuntimeEnv) { +void finalizeQueryResult(SOperatorInfo* pOperator, SQLFunctionCtx* pCtx, SResultRowInfo* pResultRowInfo, int32_t* rowCellInfoOffset) { + SQueryRuntimeEnv *pRuntimeEnv = pOperator->pRuntimeEnv; SQuery *pQuery = pRuntimeEnv->pQuery; - if (pRuntimeEnv->groupbyColumn || QUERY_IS_INTERVAL_QUERY(pQuery)) { + int32_t numOfOutput = pOperator->numOfOutput; + if (pQuery->groupbyColumn || QUERY_IS_INTERVAL_QUERY(pQuery) || pQuery->sw.gap > 0) { // for each group result, call the finalize function for each column - SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo; - if (pRuntimeEnv->groupbyColumn) { - closeAllResultRows(pWindowResInfo); + if (pQuery->groupbyColumn) { + closeAllResultRows(pResultRowInfo); } - for (int32_t i = 0; i < pWindowResInfo->size; ++i) { - SResultRow *buf = pWindowResInfo->pResult[i]; - if (!isResultRowClosed(pWindowResInfo, i)) { + for (int32_t i = 0; i < pResultRowInfo->size; ++i) { + SResultRow *buf = pResultRowInfo->pResult[i]; + if (!isResultRowClosed(pResultRowInfo, i)) { continue; } - setResultOutputBuf(pRuntimeEnv, buf); + setResultOutputBuf(pRuntimeEnv, buf, pCtx, numOfOutput, rowCellInfoOffset); - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - aAggs[pQuery->pExpr1[j].base.functionId].xFinalize(&pRuntimeEnv->pCtx[j]); + for (int32_t j = 0; j < numOfOutput; ++j) { + aAggs[pCtx[j].functionId].xFinalize(&pCtx[j]); } /* * set the number of output results for group by normal columns, the number of output rows usually is 1 except * the top and bottom query */ - buf->numOfRows = (uint16_t)getNumOfResult(pRuntimeEnv); + buf->numOfRows = (uint16_t)getNumOfResult(pRuntimeEnv, pCtx, numOfOutput); } } else { - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - aAggs[pQuery->pExpr1[j].base.functionId].xFinalize(&pRuntimeEnv->pCtx[j]); + for (int32_t j = 0; j < numOfOutput; ++j) { + aAggs[pCtx[j].functionId].xFinalize(&pCtx[j]); } } } @@ -3705,25 +3203,40 @@ void destroyTableQueryInfoImpl(STableQueryInfo *pTableQueryInfo) { cleanupResultRowInfo(&pTableQueryInfo->resInfo); } -/** - * set output buffer for different group - * @param pRuntimeEnv - * @param pDataBlockInfo - */ -void setExecutionContext(SQInfo *pQInfo, int32_t groupIndex, TSKEY nextKey) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - STableQueryInfo *pTableQueryInfo = pRuntimeEnv->pQuery->current; - SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo; +void setResultRowOutputBufInitCtx(SQueryRuntimeEnv *pRuntimeEnv, SResultRow *pResult, SQLFunctionCtx* pCtx, + int32_t numOfOutput, int32_t* rowCellInfoOffset) { + // Note: pResult->pos[i]->num == 0, there is only fixed number of results for each group + tFilePage* bufPage = getResBufPage(pRuntimeEnv->pResultBuf, pResult->pageId); - // lastKey needs to be updated - pTableQueryInfo->lastKey = nextKey; - if (pRuntimeEnv->prevGroupId != INT32_MIN && pRuntimeEnv->prevGroupId == groupIndex) { - return; + int16_t offset = 0; + for (int32_t i = 0; i < numOfOutput; ++i) { + pCtx[i].resultInfo = getResultCell(pResult, i, rowCellInfoOffset); + + SResultRowCellInfo* pResInfo = pCtx[i].resultInfo; + if (pResInfo->initialized && pResInfo->complete) { + offset += pCtx[i].outputBytes; + continue; + } + + pCtx[i].pOutput = getPosInResultPage(pRuntimeEnv->pQuery, bufPage, pResult->offset, offset); + offset += pCtx[i].outputBytes; + + int32_t functionId = pCtx[i].functionId; + if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF) { + pCtx[i].ptsOutputBuf = pCtx[0].pOutput; + } + + if (!pResInfo->initialized) { + aAggs[functionId].init(&pCtx[i]); + } } +} +void doSetTableGroupOutputBuf(SQueryRuntimeEnv* pRuntimeEnv, SResultRowInfo* pResultRowInfo, SQLFunctionCtx* pCtx, + int32_t* rowCellInfoOffset, int32_t numOfOutput, int32_t groupIndex) { int64_t uid = 0; - SResultRow *pResultRow = doPrepareResultRowFromKey(pRuntimeEnv, pWindowResInfo, (char *)&groupIndex, - sizeof(groupIndex), true, uid); + SResultRow* pResultRow = + doPrepareResultRowFromKey(pRuntimeEnv, pResultRowInfo, (char*)&groupIndex, sizeof(groupIndex), true, uid); assert (pResultRow != NULL); /* @@ -3731,142 +3244,146 @@ void setExecutionContext(SQInfo *pQInfo, int32_t groupIndex, TSKEY nextKey) { * all group belong to one result set, and each group result has different group id so set the id to be one */ if (pResultRow->pageId == -1) { - if (addNewWindowResultBuf(pResultRow, pRuntimeEnv->pResultBuf, groupIndex, pRuntimeEnv->numOfRowsPerPage) != - TSDB_CODE_SUCCESS) { + int32_t ret = addNewWindowResultBuf(pResultRow, pRuntimeEnv->pResultBuf, groupIndex, pRuntimeEnv->pQuery->resultRowSize); + if (ret != TSDB_CODE_SUCCESS) { return; } } + setResultRowOutputBufInitCtx(pRuntimeEnv, pResultRow, pCtx, numOfOutput, rowCellInfoOffset); +} + +void setExecutionContext(SQueryRuntimeEnv* pRuntimeEnv, SOptrBasicInfo* pInfo, int32_t numOfOutput, int32_t groupIndex, + TSKEY nextKey) { + STableQueryInfo *pTableQueryInfo = pRuntimeEnv->pQuery->current; + + // lastKey needs to be updated + pTableQueryInfo->lastKey = nextKey; + if (pRuntimeEnv->prevGroupId != INT32_MIN && pRuntimeEnv->prevGroupId == groupIndex) { + return; + } + + doSetTableGroupOutputBuf(pRuntimeEnv, &pInfo->resultRowInfo, pInfo->pCtx, pInfo->rowCellInfoOffset, numOfOutput, groupIndex); + // record the current active group id pRuntimeEnv->prevGroupId = groupIndex; - setResultOutputBuf(pRuntimeEnv, pResultRow); - initCtxOutputBuf(pRuntimeEnv); } -void setResultOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, SResultRow *pResult) { - SQuery *pQuery = pRuntimeEnv->pQuery; - +void setResultOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, SResultRow *pResult, SQLFunctionCtx* pCtx, + int32_t numOfCols, int32_t* rowCellInfoOffset) { // Note: pResult->pos[i]->num == 0, there is only fixed number of results for each group tFilePage *page = getResBufPage(pRuntimeEnv->pResultBuf, pResult->pageId); - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - SQLFunctionCtx *pCtx = &pRuntimeEnv->pCtx[i]; - pCtx->pOutput = getPosInResultPage(pRuntimeEnv, i, pResult, page); + int16_t offset = 0; + for (int32_t i = 0; i < numOfCols; ++i) { + pCtx[i].pOutput = getPosInResultPage(pRuntimeEnv->pQuery, page, pResult->offset, offset); + offset += pCtx[i].outputBytes; - int32_t functionId = pQuery->pExpr1[i].base.functionId; + int32_t functionId = pCtx[i].functionId; if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF) { - pCtx->ptsOutputBuf = pRuntimeEnv->pCtx[0].pOutput; + pCtx[i].ptsOutputBuf = pCtx[0].pOutput; } /* * set the output buffer information and intermediate buffer, * not all queries require the interResultBuf, such as COUNT */ - pCtx->resultInfo = getResultCell(pRuntimeEnv, pResult, i); + pCtx[i].resultInfo = getResultCell(pResult, i, rowCellInfoOffset); } } -void setResultRowOutputBufInitCtx(SQueryRuntimeEnv *pRuntimeEnv, SResultRow *pResult) { - SQuery *pQuery = pRuntimeEnv->pQuery; - - // Note: pResult->pos[i]->num == 0, there is only fixed number of results for each group - tFilePage* bufPage = getResBufPage(pRuntimeEnv->pResultBuf, pResult->pageId); +void setCtxTagForJoin(SQueryRuntimeEnv* pRuntimeEnv, SQLFunctionCtx* pCtx, SExprInfo* pExprInfo, void* pTable) { + SQuery* pQuery = pRuntimeEnv->pQuery; - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - SQLFunctionCtx *pCtx = &pRuntimeEnv->pCtx[i]; + SSqlFuncMsg* pFuncMsg = &pExprInfo->base; + if (pQuery->stableQuery && (pRuntimeEnv->pTsBuf != NULL) && + (pFuncMsg->functionId == TSDB_FUNC_TS || pFuncMsg->functionId == TSDB_FUNC_PRJ) && + (pFuncMsg->colInfo.colIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX)) { + assert(pFuncMsg->numOfParams == 1); - pCtx->resultInfo = getResultCell(pRuntimeEnv, pResult, i); - if (pCtx->resultInfo->initialized && pCtx->resultInfo->complete) { - continue; - } + int16_t tagColId = (int16_t)pExprInfo->base.arg->argValue.i64; + SColumnInfo* pColInfo = doGetTagColumnInfoById(pQuery->tagColList, pQuery->numOfTags, tagColId); - pCtx->pOutput = getPosInResultPage(pRuntimeEnv, i, pResult, bufPage); - pCtx->currentStage = 0; + doSetTagValueInParam(pTable, tagColId, &pCtx->tag, pColInfo->type, pColInfo->bytes); - int32_t functionId = pCtx->functionId; - if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF) { - pCtx->ptsOutputBuf = pRuntimeEnv->pCtx[0].pOutput; - } - - if (!pCtx->resultInfo->initialized) { - aAggs[functionId].init(pCtx); + int16_t tagType = pCtx[0].tag.nType; + if (tagType == TSDB_DATA_TYPE_BINARY || tagType == TSDB_DATA_TYPE_NCHAR) { + qDebug("QInfo:%p set tag value for join comparison, colId:%" PRId64 ", val:%s", pRuntimeEnv->qinfo, + pExprInfo->base.arg->argValue.i64, pCtx[0].tag.pz); + } else { + qDebug("QInfo:%p set tag value for join comparison, colId:%" PRId64 ", val:%" PRId64, pRuntimeEnv->qinfo, + pExprInfo->base.arg->argValue.i64, pCtx[0].tag.i64); } } } -int32_t setTimestampListJoinInfo(SQInfo *pQInfo, STableQueryInfo *pTableQueryInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; +int32_t setTimestampListJoinInfo(SQueryRuntimeEnv* pRuntimeEnv, tVariant* pTag, STableQueryInfo *pTableQueryInfo) { + SQuery* pQuery = pRuntimeEnv->pQuery; + assert(pRuntimeEnv->pTsBuf != NULL); // both the master and supplement scan needs to set the correct ts comp start position - tVariant* pTag = &pRuntimeEnv->pCtx[0].tag; - if (pTableQueryInfo->cur.vgroupIndex == -1) { tVariantAssign(&pTableQueryInfo->tag, pTag); - STSElem elem = tsBufGetElemStartPos(pRuntimeEnv->pTsBuf, pQInfo->vgId, &pTableQueryInfo->tag); + STSElem elem = tsBufGetElemStartPos(pRuntimeEnv->pTsBuf, pQuery->vgId, &pTableQueryInfo->tag); // failed to find data with the specified tag value and vnodeId if (!tsBufIsValidElem(&elem)) { if (pTag->nType == TSDB_DATA_TYPE_BINARY || pTag->nType == TSDB_DATA_TYPE_NCHAR) { - qError("QInfo:%p failed to find tag:%s in ts_comp", pQInfo, pTag->pz); + qError("QInfo:%p failed to find tag:%s in ts_comp", pRuntimeEnv->qinfo, pTag->pz); } else { - qError("QInfo:%p failed to find tag:%" PRId64 " in ts_comp", pQInfo, pTag->i64); + qError("QInfo:%p failed to find tag:%" PRId64 " in ts_comp", pRuntimeEnv->qinfo, pTag->i64); } - return false; + return -1; } - // keep the cursor info of current meter + // Keep the cursor info of current table pTableQueryInfo->cur = tsBufGetCursor(pRuntimeEnv->pTsBuf); if (pTag->nType == TSDB_DATA_TYPE_BINARY || pTag->nType == TSDB_DATA_TYPE_NCHAR) { - qDebug("QInfo:%p find tag:%s start pos in ts_comp, blockIndex:%d, tsIndex:%d", pQInfo, pTag->pz, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex); + qDebug("QInfo:%p find tag:%s start pos in ts_comp, blockIndex:%d, tsIndex:%d", pRuntimeEnv->qinfo, pTag->pz, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex); } else { - qDebug("QInfo:%p find tag:%"PRId64" start pos in ts_comp, blockIndex:%d, tsIndex:%d", pQInfo, pTag->i64, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex); + qDebug("QInfo:%p find tag:%"PRId64" start pos in ts_comp, blockIndex:%d, tsIndex:%d", pRuntimeEnv->qinfo, pTag->i64, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex); } } else { tsBufSetCursor(pRuntimeEnv->pTsBuf, &pTableQueryInfo->cur); - if (pTag->nType == TSDB_DATA_TYPE_BINARY || pTag->nType == TSDB_DATA_TYPE_NCHAR) { - qDebug("QInfo:%p find tag:%s start pos in ts_comp, blockIndex:%d, tsIndex:%d", pQInfo, pTag->pz, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex); + qDebug("QInfo:%p find tag:%s start pos in ts_comp, blockIndex:%d, tsIndex:%d", pRuntimeEnv->qinfo, pTag->pz, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex); } else { - qDebug("QInfo:%p find tag:%"PRId64" start pos in ts_comp, blockIndex:%d, tsIndex:%d", pQInfo, pTag->i64, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex); + qDebug("QInfo:%p find tag:%"PRId64" start pos in ts_comp, blockIndex:%d, tsIndex:%d", pRuntimeEnv->qinfo, pTag->i64, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex); } } return 0; } -int32_t setParamValue(SQueryRuntimeEnv* pRuntimeEnv) { +void setParamForStableStddev(SQueryRuntimeEnv* pRuntimeEnv, SQLFunctionCtx* pCtx, int32_t numOfOutput, SExprInfo* pExpr) { SQuery* pQuery = pRuntimeEnv->pQuery; - if (pRuntimeEnv->prevResult == NULL || pRuntimeEnv->groupbyColumn) { - return TSDB_CODE_SUCCESS; - } - int32_t numOfExprs = pQuery->numOfOutput; for(int32_t i = 0; i < numOfExprs; ++i) { - SExprInfo* pExprInfo = &(pQuery->pExpr1[i]); - if(pExprInfo->base.functionId != TSDB_FUNC_STDDEV_DST) { + SExprInfo* pExprInfo = &(pExpr[i]); + if (pExprInfo->base.functionId != TSDB_FUNC_STDDEV_DST) { continue; } SSqlFuncMsg* pFuncMsg = &pExprInfo->base; - pRuntimeEnv->pCtx[i].param[0].arr = NULL; - pRuntimeEnv->pCtx[i].param[0].nType = TSDB_DATA_TYPE_INT; // avoid freeing the memory by setting the type to be int + pCtx[i].param[0].arr = NULL; + pCtx[i].param[0].nType = TSDB_DATA_TYPE_INT; // avoid freeing the memory by setting the type to be int - int32_t numOfGroup = (int32_t) taosArrayGetSize(pRuntimeEnv->prevResult); - for(int32_t j = 0; j < numOfGroup; ++j) { - SInterResult *p = taosArrayGet(pRuntimeEnv->prevResult, j); + // TODO use hash to speedup this loop + int32_t numOfGroup = (int32_t)taosArrayGetSize(pRuntimeEnv->prevResult); + for (int32_t j = 0; j < numOfGroup; ++j) { + SInterResult* p = taosArrayGet(pRuntimeEnv->prevResult, j); if (pQuery->tagLen == 0 || memcmp(p->tags, pRuntimeEnv->tagVal, pQuery->tagLen) == 0) { - - int32_t numOfCols = (int32_t) taosArrayGetSize(p->pResult); - for(int32_t k = 0; k < numOfCols; ++k) { + int32_t numOfCols = (int32_t)taosArrayGetSize(p->pResult); + for (int32_t k = 0; k < numOfCols; ++k) { SStddevInterResult* pres = taosArrayGet(p->pResult, k); if (pres->colId == pFuncMsg->colInfo.colId) { - pRuntimeEnv->pCtx[i].param[0].arr = pres->pResult; + pCtx[i].param[0].arr = pres->pResult; break; } } @@ -3874,7 +3391,6 @@ int32_t setParamValue(SQueryRuntimeEnv* pRuntimeEnv) { } } - return 0; } /* @@ -3886,84 +3402,61 @@ int32_t setParamValue(SQueryRuntimeEnv* pRuntimeEnv) { * merged during merge stage. In this case, we need the pTableQueryInfo->lastResRows to decide if there * is a previous result generated or not. */ -void setIntervalQueryRange(SQInfo *pQInfo, TSKEY key) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery * pQuery = pRuntimeEnv->pQuery; - STableQueryInfo *pTableQueryInfo = pQuery->current; - - if (pTableQueryInfo->queryRangeSet) { - pTableQueryInfo->lastKey = key; - } else { - pTableQueryInfo->win.skey = key; - STimeWindow win = {.skey = key, .ekey = pQuery->window.ekey}; +void setIntervalQueryRange(SQueryRuntimeEnv *pRuntimeEnv, TSKEY key) { + SQuery *pQuery = pRuntimeEnv->pQuery; + STableQueryInfo *pTableQueryInfo = pQuery->current; + SResultRowInfo *pWindowResInfo = &pTableQueryInfo->resInfo; - // for too small query range, no data in this interval. - if ((QUERY_IS_ASC_QUERY(pQuery) && (pQuery->window.ekey < pQuery->window.skey)) || - (!QUERY_IS_ASC_QUERY(pQuery) && (pQuery->window.skey < pQuery->window.ekey))) { - return; - } + if (pWindowResInfo->prevSKey != TSKEY_INITIAL_VAL) { + return; + } - /** - * In handling the both ascending and descending order super table query, we need to find the first qualified - * timestamp of this table, and then set the first qualified start timestamp. - * In ascending query, the key is the first qualified timestamp. However, in the descending order query, additional - * operations involve. - */ - STimeWindow w = TSWINDOW_INITIALIZER; - SResultRowInfo *pWindowResInfo = &pTableQueryInfo->resInfo; + pTableQueryInfo->win.skey = key; + STimeWindow win = {.skey = key, .ekey = pQuery->window.ekey}; - TSKEY sk = MIN(win.skey, win.ekey); - TSKEY ek = MAX(win.skey, win.ekey); - getAlignQueryTimeWindow(pQuery, win.skey, sk, ek, &w); + /** + * In handling the both ascending and descending order super table query, we need to find the first qualified + * timestamp of this table, and then set the first qualified start timestamp. + * In ascending query, the key is the first qualified timestamp. However, in the descending order query, additional + * operations involve. + */ + STimeWindow w = TSWINDOW_INITIALIZER; - if (pWindowResInfo->prevSKey == TSKEY_INITIAL_VAL) { - if (!QUERY_IS_ASC_QUERY(pQuery)) { - assert(win.ekey == pQuery->window.ekey); - } + TSKEY sk = MIN(win.skey, win.ekey); + TSKEY ek = MAX(win.skey, win.ekey); + getAlignQueryTimeWindow(pQuery, win.skey, sk, ek, &w); - pWindowResInfo->prevSKey = w.skey; + if (pWindowResInfo->prevSKey == TSKEY_INITIAL_VAL) { + if (!QUERY_IS_ASC_QUERY(pQuery)) { + assert(win.ekey == pQuery->window.ekey); } - pTableQueryInfo->queryRangeSet = 1; - pTableQueryInfo->lastKey = pTableQueryInfo->win.skey; + pWindowResInfo->prevSKey = w.skey; } -} -bool requireTimestamp(SQuery *pQuery) { - for (int32_t i = 0; i < pQuery->numOfOutput; i++) { - int32_t functionId = pQuery->pExpr1[i].base.functionId; - if ((aAggs[functionId].status & TSDB_FUNCSTATE_NEED_TS) != 0) { - return true; - } - } - return false; + pTableQueryInfo->lastKey = pTableQueryInfo->win.skey; } -bool needPrimaryTimestampCol(SQuery *pQuery, SDataBlockInfo *pDataBlockInfo) { - /* - * 1. if skey or ekey locates in this block, we need to load the timestamp column to decide the precise position - * 2. if there are top/bottom, first_dst/last_dst functions, we need to load timestamp column in any cases; - */ - STimeWindow *w = &pDataBlockInfo->window; - STableQueryInfo* pTableQueryInfo = pQuery->current; - - bool loadPrimaryTS = (pTableQueryInfo->lastKey >= w->skey && pTableQueryInfo->lastKey <= w->ekey) || - (pQuery->window.ekey >= w->skey && pQuery->window.ekey <= w->ekey) || requireTimestamp(pQuery); - - return loadPrimaryTS; -} +/** + * copyToOutputBuf support copy data in ascending/descending order + * For interval query of both super table and table, copy the data in ascending order, since the output results are + * ordered in SWindowResutl already. While handling the group by query for both table and super table, + * all group result are completed already. + * + * @param pQInfo + * @param result + */ -static int32_t doCopyToSData(SQueryRuntimeEnv* pRuntimeEnv, SGroupResInfo* pGroupResInfo, int32_t orderType) { - void* qinfo = GET_QINFO_ADDR(pRuntimeEnv); +static int32_t doCopyToSDataBlock(SQueryRuntimeEnv* pRuntimeEnv, SGroupResInfo* pGroupResInfo, int32_t orderType, SSDataBlock* pBlock) { SQuery *pQuery = pRuntimeEnv->pQuery; int32_t numOfRows = getNumOfTotalRes(pGroupResInfo); - int32_t numOfResult = (int32_t) pQuery->rec.rows; // there are already exists result rows + int32_t numOfResult = pBlock->info.rows; // there are already exists result rows int32_t start = 0; int32_t step = -1; - qDebug("QInfo:%p start to copy data from windowResInfo to output buf", qinfo); + qDebug("QInfo:%p start to copy data from windowResInfo to output buf", pRuntimeEnv->qinfo); if (orderType == TSDB_ORDER_ASC) { start = pGroupResInfo->index; step = 1; @@ -3981,56 +3474,56 @@ static int32_t doCopyToSData(SQueryRuntimeEnv* pRuntimeEnv, SGroupResInfo* pGrou int32_t numOfRowsToCopy = pRow->numOfRows; - //current output space is not enough to accommodate all data of this page, prepare more space - if (numOfRowsToCopy > (pQuery->rec.capacity - numOfResult)) { - int32_t newSize = (int32_t) (pQuery->rec.capacity + (numOfRowsToCopy - numOfResult)); - expandBuffer(pRuntimeEnv, newSize, GET_QINFO_ADDR(pRuntimeEnv)); - } - pGroupResInfo->index += 1; tFilePage *page = getResBufPage(pRuntimeEnv->pResultBuf, pRow->pageId); - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - int32_t size = pRuntimeEnv->pCtx[j].outputBytes; - char *out = pQuery->sdata[j]->data + numOfResult * size; - char *in = getPosInResultPage(pRuntimeEnv, j, pRow, page); - memcpy(out, in, size * numOfRowsToCopy); + int16_t offset = 0; + for (int32_t j = 0; j < pBlock->info.numOfCols; ++j) { + SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, j); + int32_t bytes = pColInfoData->info.bytes; + + char *out = pColInfoData->pData + numOfResult * bytes; + char *in = getPosInResultPage(pQuery, page, pRow->offset, offset); + memcpy(out, in, bytes * numOfRowsToCopy); + + offset += bytes; } numOfResult += numOfRowsToCopy; - if (numOfResult == pQuery->rec.capacity) { // output buffer is full + if (numOfResult == pRuntimeEnv->resultInfo.capacity) { // output buffer is full break; } } - qDebug("QInfo:%p copy data to query buf completed", qinfo); - return numOfResult; + qDebug("QInfo:%p copy data to query buf completed", pRuntimeEnv->qinfo); + pBlock->info.rows = numOfResult; + return 0; } -/** - * copyToOutputBuf support copy data in ascending/descending order - * For interval query of both super table and table, copy the data in ascending order, since the output results are - * ordered in SWindowResutl already. While handling the group by query for both table and super table, - * all group result are completed already. - * - * @param pQInfo - * @param result - */ -void copyToOutputBuf(SQInfo *pQInfo, SResultRowInfo *pResultInfo) { - SQuery *pQuery = pQInfo->runtimeEnv.pQuery; - SGroupResInfo *pGroupResInfo = &pQInfo->groupResInfo; +static void toSSDataBlock(SGroupResInfo *pGroupResInfo, SQueryRuntimeEnv* pRuntimeEnv, SSDataBlock* pBlock) { + assert(pGroupResInfo->currentGroup <= pGroupResInfo->totalGroup); - assert(pQuery->rec.rows == 0 && pGroupResInfo->currentGroup <= pGroupResInfo->totalGroup); - if (!hasRemainData(pGroupResInfo)) { + pBlock->info.rows = 0; + if (!hasRemainDataInCurrentGroup(pGroupResInfo)) { return; } + SQuery* pQuery = pRuntimeEnv->pQuery; int32_t orderType = (pQuery->pGroupbyExpr != NULL) ? pQuery->pGroupbyExpr->orderType : TSDB_ORDER_ASC; - pQuery->rec.rows = doCopyToSData(&pQInfo->runtimeEnv, pGroupResInfo, orderType); + doCopyToSDataBlock(pRuntimeEnv, pGroupResInfo, orderType, pBlock); + + SColumnInfoData* pInfoData = taosArrayGet(pBlock->pDataBlock, 0); + + if (pInfoData->info.type == TSDB_DATA_TYPE_TIMESTAMP) { + STimeWindow* w = &pBlock->info.window; + w->skey = *(int64_t*)pInfoData->pData; + w->ekey = *(int64_t*)(((char*)pInfoData->pData) + TSDB_KEYSIZE * (pBlock->info.rows - 1)); + } } -static void updateWindowResNumOfRes(SQueryRuntimeEnv *pRuntimeEnv) { +static void updateNumOfRowsInResultRows(SQueryRuntimeEnv *pRuntimeEnv, + SQLFunctionCtx* pCtx, int32_t numOfOutput, SResultRowInfo* pResultRowInfo, int32_t* rowCellInfoOffset) { SQuery *pQuery = pRuntimeEnv->pQuery; // update the number of result for each, only update the number of rows for the corresponding window result. @@ -4038,108 +3531,47 @@ static void updateWindowResNumOfRes(SQueryRuntimeEnv *pRuntimeEnv) { return; } - for (int32_t i = 0; i < pRuntimeEnv->resultRowInfo.size; ++i) { - SResultRow *pResult = pRuntimeEnv->resultRowInfo.pResult[i]; + for (int32_t i = 0; i < pResultRowInfo->size; ++i) { + SResultRow *pResult = pResultRowInfo->pResult[i]; - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - int32_t functionId = pRuntimeEnv->pCtx[j].functionId; + for (int32_t j = 0; j < numOfOutput; ++j) { + int32_t functionId = pCtx[j].functionId; if (functionId == TSDB_FUNC_TS || functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_TAGPRJ) { continue; } - SResultRowCellInfo* pCell = getResultCell(pRuntimeEnv, pResult, j); + SResultRowCellInfo* pCell = getResultCell(pResult, j, rowCellInfoOffset); pResult->numOfRows = (uint16_t)(MAX(pResult->numOfRows, pCell->numOfRes)); } } } -static void stableApplyFunctionsOnBlock(SQueryRuntimeEnv *pRuntimeEnv, SDataBlockInfo *pDataBlockInfo, SDataStatis *pStatis, - SArray *pDataBlock, __block_search_fn_t searchFn) { - SQuery * pQuery = pRuntimeEnv->pQuery; - STableQueryInfo* pTableQueryInfo = pQuery->current; - - SResultRowInfo * pResultRowInfo = &pTableQueryInfo->resInfo; - pQuery->pos = QUERY_IS_ASC_QUERY(pQuery)? 0 : pDataBlockInfo->rows - 1; - - if (pQuery->numOfFilterCols > 0 || pRuntimeEnv->pTsBuf != NULL || pRuntimeEnv->groupbyColumn) { - rowwiseApplyFunctions(pRuntimeEnv, pStatis, pDataBlockInfo, pResultRowInfo, pDataBlock); - } else { - blockwiseApplyFunctions(pRuntimeEnv, pStatis, pDataBlockInfo, pResultRowInfo, searchFn, pDataBlock); - } - - if (QUERY_IS_INTERVAL_QUERY(pQuery)) { - updateResultRowIndex(pResultRowInfo, pTableQueryInfo, QUERY_IS_ASC_QUERY(pQuery), pRuntimeEnv->timeWindowInterpo); - } -} - -bool hasNotReturnedResults(SQueryRuntimeEnv* pRuntimeEnv, SGroupResInfo* pGroupResInfo) { +static void doCopyQueryResultToMsg(SQInfo *pQInfo, int32_t numOfRows, char *data) { + SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; SQuery *pQuery = pRuntimeEnv->pQuery; - SFillInfo *pFillInfo = pRuntimeEnv->pFillInfo; - - if (!Q_STATUS_EQUAL(pQuery->status, QUERY_COMPLETED)) { - return false; - } - - if (pQuery->limit.limit > 0 && pQuery->rec.total >= pQuery->limit.limit) { - return false; - } - - if (pQuery->fillType != TSDB_FILL_NONE && !isPointInterpoQuery(pQuery)) { - // There are results not returned to client yet, so filling applied to the remain result is required firstly. - if (taosFillHasMoreResults(pFillInfo)) { - return true; - } - - /* - * While the code reaches here, there are no results remains now. - * If query is not completed yet, the gaps between two results blocks need to be handled after next data block - * is retrieved from TSDB. - * - * NOTE: If the result set is not the first block, the gap in front of the result set will be filled. If the result - * set is the FIRST result block, the gap between the start time of query time window and the timestamp of the - * first result row in the actual result set will fill nothing. - */ - int32_t numOfTotal = (int32_t)getNumOfResultsAfterFillGap(pFillInfo, pQuery->window.ekey, (int32_t)pQuery->rec.capacity); - return numOfTotal > 0; - } else { // there are results waiting for returned to client. - if (Q_STATUS_EQUAL(pQuery->status, QUERY_COMPLETED) && hasRemainData(pGroupResInfo) && - (pRuntimeEnv->groupbyColumn || QUERY_IS_INTERVAL_QUERY(pQuery))) { - return true; - } - } - - return false; -} - -static int16_t getNumOfFinalResCol(SQuery* pQuery) { - return pQuery->pExpr2 == NULL? pQuery->numOfOutput:pQuery->numOfExpr2; -} -static void doCopyQueryResultToMsg(SQInfo *pQInfo, int32_t numOfRows, char *data) { - SQuery *pQuery = pQInfo->runtimeEnv.pQuery; + SSDataBlock* pRes = pRuntimeEnv->outputBuf; if (pQuery->pExpr2 == NULL) { for (int32_t col = 0; col < pQuery->numOfOutput; ++col) { - int32_t bytes = pQuery->pExpr1[col].bytes; - - memmove(data, pQuery->sdata[col]->data, bytes * numOfRows); - data += bytes * numOfRows; + SColumnInfoData* pColRes = taosArrayGet(pRes->pDataBlock, col); + memmove(data, pColRes->pData, pColRes->info.bytes * pRes->info.rows); + data += pColRes->info.bytes * pRes->info.rows; } } else { for (int32_t col = 0; col < pQuery->numOfExpr2; ++col) { - int32_t bytes = pQuery->pExpr2[col].bytes; - - memmove(data, pQuery->sdata[col]->data, bytes * numOfRows); - data += bytes * numOfRows; + SColumnInfoData* pColRes = taosArrayGet(pRes->pDataBlock, col); + memmove(data, pColRes->pData, pColRes->info.bytes * numOfRows); + data += pColRes->info.bytes * numOfRows; } } - int32_t numOfTables = (int32_t) taosHashGetSize(pQInfo->arrTableIdInfo); + int32_t numOfTables = (int32_t) taosHashGetSize(pRuntimeEnv->pTableRetrieveTsMap); *(int32_t*)data = htonl(numOfTables); data += sizeof(int32_t); int32_t total = 0; - STableIdInfo* item = taosHashIterate(pQInfo->arrTableIdInfo, NULL); + STableIdInfo* item = taosHashIterate(pRuntimeEnv->pTableRetrieveTsMap, NULL); while(item) { STableIdInfo* pDst = (STableIdInfo*)data; @@ -4151,75 +3583,34 @@ static void doCopyQueryResultToMsg(SQInfo *pQInfo, int32_t numOfRows, char *data total++; qDebug("QInfo:%p set subscribe info, tid:%d, uid:%"PRIu64", skey:%"PRId64, pQInfo, item->tid, item->uid, item->key); - item = taosHashIterate(pQInfo->arrTableIdInfo, item); + item = taosHashIterate(pRuntimeEnv->pTableRetrieveTsMap, item); } qDebug("QInfo:%p set %d subscribe info", pQInfo, total); - // Check if query is completed or not for stable query or normal table query respectively. - if (Q_STATUS_EQUAL(pQuery->status, QUERY_COMPLETED)) { - if (pQInfo->runtimeEnv.stableQuery) { - if (IS_STASBLE_QUERY_OVER(pQInfo)) { - setQueryStatus(pQuery, QUERY_OVER); - } - } else { - if (!hasNotReturnedResults(&pQInfo->runtimeEnv, &pQInfo->groupResInfo)) { - setQueryStatus(pQuery, QUERY_OVER); - } - } + if (Q_STATUS_EQUAL(pRuntimeEnv->status, QUERY_COMPLETED) && pRuntimeEnv->proot->status == OP_EXEC_DONE) { + setQueryStatus(pRuntimeEnv, QUERY_OVER); } } -int32_t doFillGapsInResults(SQueryRuntimeEnv* pRuntimeEnv, tFilePage **pDst) { - SQInfo *pQInfo = GET_QINFO_ADDR(pRuntimeEnv); - SQuery *pQuery = pRuntimeEnv->pQuery; - SFillInfo* pFillInfo = pRuntimeEnv->pFillInfo; - - while (1) { - int32_t ret = (int32_t)taosFillResultDataBlock(pFillInfo, (tFilePage**)pQuery->sdata, (int32_t)pQuery->rec.capacity); - - // todo apply limit output function - /* reached the start position of according to offset value, return immediately */ - if (pQuery->limit.offset == 0) { - qDebug("QInfo:%p initial numOfRows:%d, generate filled result:%d rows", pQInfo, pFillInfo->numOfRows, ret); - return ret; - } - - if (pQuery->limit.offset < ret) { - qDebug("QInfo:%p initial numOfRows:%d, generate filled result:%d rows, offset:%" PRId64 ". Discard due to offset, remain:%" PRId64 ", new offset:%d", - pQInfo, pFillInfo->numOfRows, ret, pQuery->limit.offset, ret - pQuery->limit.offset, 0); - - ret -= (int32_t)pQuery->limit.offset; - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { //???pExpr1 or pExpr2 - memmove(pDst[i]->data, pDst[i]->data + pQuery->pExpr1[i].bytes * pQuery->limit.offset, - ret * pQuery->pExpr1[i].bytes); - } - - pQuery->limit.offset = 0; - return ret; - } else { - qDebug("QInfo:%p initial numOfRows:%d, generate filled result:%d rows, offset:%" PRId64 ". Discard due to offset, " - "remain:%d, new offset:%" PRId64, pQInfo, pFillInfo->numOfRows, ret, pQuery->limit.offset, 0, - pQuery->limit.offset - ret); - - pQuery->limit.offset -= ret; - ret = 0; - } - - // no data in current data after fill - int32_t numOfTotal = (int32_t)getNumOfResultsAfterFillGap(pFillInfo, pFillInfo->end, (int32_t)pQuery->rec.capacity); - if (numOfTotal == 0) { - return 0; - } +int32_t doFillTimeIntervalGapsInResults(SFillInfo* pFillInfo, SSDataBlock *pOutput, int32_t capacity) { + void** p = calloc(pFillInfo->numOfCols, POINTER_BYTES); + for(int32_t i = 0; i < pFillInfo->numOfCols; ++i) { + SColumnInfoData* pColInfoData = taosArrayGet(pOutput->pDataBlock, i); + p[i] = pColInfoData->pData; } + + pOutput->info.rows = (int32_t)taosFillResultDataBlock(pFillInfo, p, capacity); + tfree(p); + return pOutput->info.rows; } void queryCostStatis(SQInfo *pQInfo) { SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - SQueryCostInfo *pSummary = &pRuntimeEnv->summary; + SQueryCostInfo *pSummary = &pQInfo->summary; uint64_t hashSize = taosHashGetMemSize(pQInfo->runtimeEnv.pResultRowHashTable); - hashSize += taosHashGetMemSize(pQInfo->tableqinfoGroupInfo.map); + hashSize += taosHashGetMemSize(pRuntimeEnv->tableqinfoGroupInfo.map); pSummary->hashSize = hashSize; // add the merge time @@ -4243,293 +3634,241 @@ void queryCostStatis(SQInfo *pQInfo) { pSummary->numOfTimeWindows, pSummary->tableInfoSize/1024.0, pSummary->hashSize/1024.0); } -static void updateOffsetVal(SQueryRuntimeEnv *pRuntimeEnv, SDataBlockInfo *pBlockInfo) { - SQuery *pQuery = pRuntimeEnv->pQuery; - STableQueryInfo* pTableQueryInfo = pQuery->current; - - int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); - - if (pQuery->limit.offset == pBlockInfo->rows) { // current block will ignore completed - pTableQueryInfo->lastKey = QUERY_IS_ASC_QUERY(pQuery) ? pBlockInfo->window.ekey + step : pBlockInfo->window.skey + step; - pQuery->limit.offset = 0; - return; - } - - if (QUERY_IS_ASC_QUERY(pQuery)) { - pQuery->pos = (int32_t)pQuery->limit.offset; - } else { - pQuery->pos = pBlockInfo->rows - (int32_t)pQuery->limit.offset - 1; - } - - assert(pQuery->pos >= 0 && pQuery->pos <= pBlockInfo->rows - 1); - - SArray * pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pQueryHandle, NULL); - SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0); - - // update the pQuery->limit.offset value, and pQuery->pos value - TSKEY *keys = (TSKEY *) pColInfoData->pData; - - // update the offset value - pTableQueryInfo->lastKey = keys[pQuery->pos]; - pQuery->limit.offset = 0; - - int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock); - - qDebug("QInfo:%p check data block, brange:%" PRId64 "-%" PRId64 ", numOfRows:%d, numOfRes:%d, lastKey:%"PRId64, GET_QINFO_ADDR(pRuntimeEnv), - pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows, numOfRes, pQuery->current->lastKey); -} -static void freeTableBlockDist(STableBlockDist *pTableBlockDist) { - if (pTableBlockDist != NULL) { - taosArrayDestroy(pTableBlockDist->dataBlockInfos); - free(pTableBlockDist->result); - free(pTableBlockDist); - } -} -static int32_t getPercentileFromSortedArray(const SArray* pArray, double rate) { - int32_t len = (int32_t)taosArrayGetSize(pArray); - if (len <= 0) { - return 0; - } - assert(rate >= 0 && rate <= 1.0); - int idx = (int32_t)((len - 1) * rate); - return ((SDataBlockInfo *)(taosArrayGet(pArray, idx)))->rows; -} -static int compareBlockInfo(const void *pLeft, const void *pRight) { - int32_t left = ((SDataBlockInfo *)pLeft)->rows; - int32_t right = ((SDataBlockInfo *)pRight)->rows; - if (left > right) return 1; - if (left < right) return -1; - return 0; -} - -static void generateBlockDistResult(STableBlockDist *pTableBlockDist) { - if (pTableBlockDist == NULL) { - return; - } - int64_t min = 0, max = 0, avg = 0; - SArray* blockInfos= pTableBlockDist->dataBlockInfos; - int64_t totalRows = 0, totalBlocks = taosArrayGetSize(blockInfos); - for (size_t i = 0; i < taosArrayGetSize(blockInfos); i++) { - SDataBlockInfo *blockInfo = taosArrayGet(blockInfos, i); - int64_t rows = blockInfo->rows; - min = MIN(min, rows); - max = MAX(max, rows); - totalRows += rows; - } - avg = totalBlocks > 0 ? (int64_t)(totalRows/totalBlocks) : 0; - - taosArraySort(blockInfos, compareBlockInfo); - - int sz = sprintf(pTableBlockDist->result, - "summery: \n\t 5th=[%d], 25th=[%d], 50th=[%d],75th=[%d], 95th=[%d], 99th=[%d] \n\t min=[%"PRId64"], max=[%"PRId64"], avg = [%"PRId64"] \n\t totalRows=[%"PRId64"], totalBlocks=[%"PRId64"] \n\t seekHeaderTimeCost=[%"PRId64"(us)] \n\t rowsInMem=[%"PRId64"]", - getPercentileFromSortedArray(blockInfos, 0.05), getPercentileFromSortedArray(blockInfos, 0.25), getPercentileFromSortedArray(blockInfos, 0.50), - getPercentileFromSortedArray(blockInfos, 0.75), getPercentileFromSortedArray(blockInfos, 0.95), getPercentileFromSortedArray(blockInfos, 0.99), - min, max, avg, - totalRows, totalBlocks, - pTableBlockDist->firstSeekTimeUs, - pTableBlockDist->numOfRowsInMemTable); - UNUSED(sz); - return; -} -void skipBlocks(SQueryRuntimeEnv *pRuntimeEnv) { - SQuery *pQuery = pRuntimeEnv->pQuery; - - if (pQuery->limit.offset <= 0 || pQuery->numOfFilterCols > 0) { - return; - } - - pQuery->pos = 0; - int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); - - STableQueryInfo* pTableQueryInfo = pQuery->current; - TsdbQueryHandleT pQueryHandle = pRuntimeEnv->pQueryHandle; - - SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER; - while (tsdbNextDataBlock(pQueryHandle)) { - if (isQueryKilled(GET_QINFO_ADDR(pRuntimeEnv))) { - longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); - } - - tsdbRetrieveDataBlockInfo(pQueryHandle, &blockInfo); - - if (pQuery->limit.offset > blockInfo.rows) { - pQuery->limit.offset -= blockInfo.rows; - pTableQueryInfo->lastKey = (QUERY_IS_ASC_QUERY(pQuery)) ? blockInfo.window.ekey : blockInfo.window.skey; - pTableQueryInfo->lastKey += step; - - qDebug("QInfo:%p skip rows:%d, offset:%" PRId64, GET_QINFO_ADDR(pRuntimeEnv), blockInfo.rows, - pQuery->limit.offset); - } else { // find the appropriated start position in current block - updateOffsetVal(pRuntimeEnv, &blockInfo); - break; - } - } - - if (terrno != TSDB_CODE_SUCCESS) { - longjmp(pRuntimeEnv->env, terrno); - } -} - -static TSKEY doSkipIntervalProcess(SQueryRuntimeEnv* pRuntimeEnv, STimeWindow* win, SDataBlockInfo* pBlockInfo, STableQueryInfo* pTableQueryInfo) { - SQuery *pQuery = pRuntimeEnv->pQuery; - SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo; - - assert(pQuery->limit.offset == 0); - STimeWindow tw = *win; - getNextTimeWindow(pQuery, &tw); - - if ((tw.skey <= pBlockInfo->window.ekey && QUERY_IS_ASC_QUERY(pQuery)) || - (tw.ekey >= pBlockInfo->window.skey && !QUERY_IS_ASC_QUERY(pQuery))) { - - // load the data block and check data remaining in current data block - // TODO optimize performance - SArray * pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pQueryHandle, NULL); - SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0); - - tw = *win; - int32_t startPos = - getNextQualifiedWindow(pRuntimeEnv, &tw, pBlockInfo, pColInfoData->pData, binarySearchForKey, -1); - assert(startPos >= 0); - - // set the abort info - pQuery->pos = startPos; - - // reset the query start timestamp - pTableQueryInfo->win.skey = ((TSKEY *)pColInfoData->pData)[startPos]; - pQuery->window.skey = pTableQueryInfo->win.skey; - TSKEY key = pTableQueryInfo->win.skey; - - pWindowResInfo->prevSKey = tw.skey; - int32_t index = pRuntimeEnv->resultRowInfo.curIndex; - - int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock); - pRuntimeEnv->resultRowInfo.curIndex = index; // restore the window index - - qDebug("QInfo:%p check data block, brange:%" PRId64 "-%" PRId64 ", numOfRows:%d, numOfRes:%d, lastKey:%" PRId64, - GET_QINFO_ADDR(pRuntimeEnv), pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows, numOfRes, - pQuery->current->lastKey); - - return key; - } else { // do nothing - pQuery->window.skey = tw.skey; - pWindowResInfo->prevSKey = tw.skey; - pTableQueryInfo->lastKey = tw.skey; - - return tw.skey; - } - - return true; -} - -static bool skipTimeInterval(SQueryRuntimeEnv *pRuntimeEnv, TSKEY* start) { - SQuery *pQuery = pRuntimeEnv->pQuery; - if (QUERY_IS_ASC_QUERY(pQuery)) { - assert(*start <= pQuery->current->lastKey); - } else { - assert(*start >= pQuery->current->lastKey); - } - - // if queried with value filter, do NOT forward query start position - if (pQuery->limit.offset <= 0 || pQuery->numOfFilterCols > 0 || pRuntimeEnv->pTsBuf != NULL || pRuntimeEnv->pFillInfo != NULL) { - return true; - } - - /* - * 1. for interval without interpolation query we forward pQuery->interval.interval at a time for - * pQuery->limit.offset times. Since hole exists, pQuery->interval.interval*pQuery->limit.offset value is - * not valid. otherwise, we only forward pQuery->limit.offset number of points - */ - assert(pRuntimeEnv->resultRowInfo.prevSKey == TSKEY_INITIAL_VAL); - - STimeWindow w = TSWINDOW_INITIALIZER; - bool ascQuery = QUERY_IS_ASC_QUERY(pQuery); - - SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo; - STableQueryInfo *pTableQueryInfo = pQuery->current; - - SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER; - while (tsdbNextDataBlock(pRuntimeEnv->pQueryHandle)) { - tsdbRetrieveDataBlockInfo(pRuntimeEnv->pQueryHandle, &blockInfo); - - if (QUERY_IS_ASC_QUERY(pQuery)) { - if (pWindowResInfo->prevSKey == TSKEY_INITIAL_VAL) { - getAlignQueryTimeWindow(pQuery, blockInfo.window.skey, blockInfo.window.skey, pQuery->window.ekey, &w); - pWindowResInfo->prevSKey = w.skey; - } - } else { - getAlignQueryTimeWindow(pQuery, blockInfo.window.ekey, pQuery->window.ekey, blockInfo.window.ekey, &w); - pWindowResInfo->prevSKey = w.skey; - } - - // the first time window - STimeWindow win = getActiveTimeWindow(pWindowResInfo, pWindowResInfo->prevSKey, pQuery); - - while (pQuery->limit.offset > 0) { - STimeWindow tw = win; - - if ((win.ekey <= blockInfo.window.ekey && ascQuery) || (win.ekey >= blockInfo.window.skey && !ascQuery)) { - pQuery->limit.offset -= 1; - pWindowResInfo->prevSKey = win.skey; - - // current time window is aligned with blockInfo.window.ekey - // restart it from next data block by set prevSKey to be TSKEY_INITIAL_VAL; - if ((win.ekey == blockInfo.window.ekey && ascQuery) || (win.ekey == blockInfo.window.skey && !ascQuery)) { - pWindowResInfo->prevSKey = TSKEY_INITIAL_VAL; - } - } - - if (pQuery->limit.offset == 0) { - *start = doSkipIntervalProcess(pRuntimeEnv, &win, &blockInfo, pTableQueryInfo); - return true; - } - - // current window does not ended in current data block, try next data block - getNextTimeWindow(pQuery, &tw); - - /* - * If the next time window still starts from current data block, - * load the primary timestamp column first, and then find the start position for the next queried time window. - * Note that only the primary timestamp column is required. - * TODO: Optimize for this cases. All data blocks are not needed to be loaded, only if the first actually required - * time window resides in current data block. - */ - if ((tw.skey <= blockInfo.window.ekey && ascQuery) || (tw.ekey >= blockInfo.window.skey && !ascQuery)) { - - SArray *pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pQueryHandle, NULL); - SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0); - - if ((win.ekey > blockInfo.window.ekey && ascQuery) || (win.ekey < blockInfo.window.skey && !ascQuery)) { - pQuery->limit.offset -= 1; - } - - if (pQuery->limit.offset == 0) { - *start = doSkipIntervalProcess(pRuntimeEnv, &win, &blockInfo, pTableQueryInfo); - return true; - } else { - tw = win; - int32_t startPos = - getNextQualifiedWindow(pRuntimeEnv, &tw, &blockInfo, pColInfoData->pData, binarySearchForKey, -1); - assert(startPos >= 0); - - // set the abort info - pQuery->pos = startPos; - pTableQueryInfo->lastKey = ((TSKEY *)pColInfoData->pData)[startPos]; - pWindowResInfo->prevSKey = tw.skey; - win = tw; - } - } else { - break; // offset is not 0, and next time window begins or ends in the next block. - } - } - } - - // check for error - if (terrno != TSDB_CODE_SUCCESS) { - longjmp(pRuntimeEnv->env, terrno); - } - - return true; -} +//static void updateOffsetVal(SQueryRuntimeEnv *pRuntimeEnv, SDataBlockInfo *pBlockInfo) { +// SQuery *pQuery = pRuntimeEnv->pQuery; +// STableQueryInfo* pTableQueryInfo = pQuery->current; +// +// int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); +// +// if (pQuery->limit.offset == pBlockInfo->rows) { // current block will ignore completed +// pTableQueryInfo->lastKey = QUERY_IS_ASC_QUERY(pQuery) ? pBlockInfo->window.ekey + step : pBlockInfo->window.skey + step; +// pQuery->limit.offset = 0; +// return; +// } +// +// if (QUERY_IS_ASC_QUERY(pQuery)) { +// pQuery->pos = (int32_t)pQuery->limit.offset; +// } else { +// pQuery->pos = pBlockInfo->rows - (int32_t)pQuery->limit.offset - 1; +// } +// +// assert(pQuery->pos >= 0 && pQuery->pos <= pBlockInfo->rows - 1); +// +// SArray * pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pQueryHandle, NULL); +// SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0); +// +// // update the pQuery->limit.offset value, and pQuery->pos value +// TSKEY *keys = (TSKEY *) pColInfoData->pData; +// +// // update the offset value +// pTableQueryInfo->lastKey = keys[pQuery->pos]; +// pQuery->limit.offset = 0; +// +// int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock); +// +// qDebug("QInfo:%p check data block, brange:%" PRId64 "-%" PRId64 ", numOfRows:%d, numOfRes:%d, lastKey:%"PRId64, pRuntimeEnv->qinfo, +// pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows, numOfRes, pQuery->current->lastKey); +//} + +//void skipBlocks(SQueryRuntimeEnv *pRuntimeEnv) { +// SQuery *pQuery = pRuntimeEnv->pQuery; +// +// if (pQuery->limit.offset <= 0 || pQuery->numOfFilterCols > 0) { +// return; +// } +// +// pQuery->pos = 0; +// int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); +// +// STableQueryInfo* pTableQueryInfo = pQuery->current; +// TsdbQueryHandleT pQueryHandle = pRuntimeEnv->pQueryHandle; +// +// SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER; +// while (tsdbNextDataBlock(pQueryHandle)) { +// if (isQueryKilled(pRuntimeEnv->qinfo)) { +// longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); +// } +// +// tsdbRetrieveDataBlockInfo(pQueryHandle, &blockInfo); +// +// if (pQuery->limit.offset > blockInfo.rows) { +// pQuery->limit.offset -= blockInfo.rows; +// pTableQueryInfo->lastKey = (QUERY_IS_ASC_QUERY(pQuery)) ? blockInfo.window.ekey : blockInfo.window.skey; +// pTableQueryInfo->lastKey += step; +// +// qDebug("QInfo:%p skip rows:%d, offset:%" PRId64, pRuntimeEnv->qinfo, blockInfo.rows, +// pQuery->limit.offset); +// } else { // find the appropriated start position in current block +// updateOffsetVal(pRuntimeEnv, &blockInfo); +// break; +// } +// } +// +// if (terrno != TSDB_CODE_SUCCESS) { +// longjmp(pRuntimeEnv->env, terrno); +// } +//} + +//static TSKEY doSkipIntervalProcess(SQueryRuntimeEnv* pRuntimeEnv, STimeWindow* win, SDataBlockInfo* pBlockInfo, STableQueryInfo* pTableQueryInfo) { +// SQuery *pQuery = pRuntimeEnv->pQuery; +// SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo; +// +// assert(pQuery->limit.offset == 0); +// STimeWindow tw = *win; +// getNextTimeWindow(pQuery, &tw); +// +// if ((tw.skey <= pBlockInfo->window.ekey && QUERY_IS_ASC_QUERY(pQuery)) || +// (tw.ekey >= pBlockInfo->window.skey && !QUERY_IS_ASC_QUERY(pQuery))) { +// +// // load the data block and check data remaining in current data block +// // TODO optimize performance +// SArray * pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pQueryHandle, NULL); +// SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0); +// +// tw = *win; +// int32_t startPos = +// getNextQualifiedWindow(pQuery, &tw, pBlockInfo, pColInfoData->pData, binarySearchForKey, -1); +// assert(startPos >= 0); +// +// // set the abort info +// pQuery->pos = startPos; +// +// // reset the query start timestamp +// pTableQueryInfo->win.skey = ((TSKEY *)pColInfoData->pData)[startPos]; +// pQuery->window.skey = pTableQueryInfo->win.skey; +// TSKEY key = pTableQueryInfo->win.skey; +// +// pWindowResInfo->prevSKey = tw.skey; +// int32_t index = pRuntimeEnv->resultRowInfo.curIndex; +// +// int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock); +// pRuntimeEnv->resultRowInfo.curIndex = index; // restore the window index +// +// qDebug("QInfo:%p check data block, brange:%" PRId64 "-%" PRId64 ", numOfRows:%d, numOfRes:%d, lastKey:%" PRId64, +// pRuntimeEnv->qinfo, pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows, numOfRes, +// pQuery->current->lastKey); +// +// return key; +// } else { // do nothing +// pQuery->window.skey = tw.skey; +// pWindowResInfo->prevSKey = tw.skey; +// pTableQueryInfo->lastKey = tw.skey; +// +// return tw.skey; +// } +// +// return true; +//} + +//static bool skipTimeInterval(SQueryRuntimeEnv *pRuntimeEnv, TSKEY* start) { +// SQuery *pQuery = pRuntimeEnv->pQuery; +// if (QUERY_IS_ASC_QUERY(pQuery)) { +// assert(*start <= pQuery->current->lastKey); +// } else { +// assert(*start >= pQuery->current->lastKey); +// } +// +// // if queried with value filter, do NOT forward query start position +// if (pQuery->limit.offset <= 0 || pQuery->numOfFilterCols > 0 || pRuntimeEnv->pTsBuf != NULL || pRuntimeEnv->pFillInfo != NULL) { +// return true; +// } +// +// /* +// * 1. for interval without interpolation query we forward pQuery->interval.interval at a time for +// * pQuery->limit.offset times. Since hole exists, pQuery->interval.interval*pQuery->limit.offset value is +// * not valid. otherwise, we only forward pQuery->limit.offset number of points +// */ +// assert(pRuntimeEnv->resultRowInfo.prevSKey == TSKEY_INITIAL_VAL); +// +// STimeWindow w = TSWINDOW_INITIALIZER; +// bool ascQuery = QUERY_IS_ASC_QUERY(pQuery); +// +// SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo; +// STableQueryInfo *pTableQueryInfo = pQuery->current; +// +// SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER; +// while (tsdbNextDataBlock(pRuntimeEnv->pQueryHandle)) { +// tsdbRetrieveDataBlockInfo(pRuntimeEnv->pQueryHandle, &blockInfo); +// +// if (QUERY_IS_ASC_QUERY(pQuery)) { +// if (pWindowResInfo->prevSKey == TSKEY_INITIAL_VAL) { +// getAlignQueryTimeWindow(pQuery, blockInfo.window.skey, blockInfo.window.skey, pQuery->window.ekey, &w); +// pWindowResInfo->prevSKey = w.skey; +// } +// } else { +// getAlignQueryTimeWindow(pQuery, blockInfo.window.ekey, pQuery->window.ekey, blockInfo.window.ekey, &w); +// pWindowResInfo->prevSKey = w.skey; +// } +// +// // the first time window +// STimeWindow win = getActiveTimeWindow(pWindowResInfo, pWindowResInfo->prevSKey, pQuery); +// +// while (pQuery->limit.offset > 0) { +// STimeWindow tw = win; +// +// if ((win.ekey <= blockInfo.window.ekey && ascQuery) || (win.ekey >= blockInfo.window.skey && !ascQuery)) { +// pQuery->limit.offset -= 1; +// pWindowResInfo->prevSKey = win.skey; +// +// // current time window is aligned with blockInfo.window.ekey +// // restart it from next data block by set prevSKey to be TSKEY_INITIAL_VAL; +// if ((win.ekey == blockInfo.window.ekey && ascQuery) || (win.ekey == blockInfo.window.skey && !ascQuery)) { +// pWindowResInfo->prevSKey = TSKEY_INITIAL_VAL; +// } +// } +// +// if (pQuery->limit.offset == 0) { +// *start = doSkipIntervalProcess(pRuntimeEnv, &win, &blockInfo, pTableQueryInfo); +// return true; +// } +// +// // current window does not ended in current data block, try next data block +// getNextTimeWindow(pQuery, &tw); +// +// /* +// * If the next time window still starts from current data block, +// * load the primary timestamp column first, and then find the start position for the next queried time window. +// * Note that only the primary timestamp column is required. +// * TODO: Optimize for this cases. All data blocks are not needed to be loaded, only if the first actually required +// * time window resides in current data block. +// */ +// if ((tw.skey <= blockInfo.window.ekey && ascQuery) || (tw.ekey >= blockInfo.window.skey && !ascQuery)) { +// +// SArray *pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pQueryHandle, NULL); +// SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0); +// +// if ((win.ekey > blockInfo.window.ekey && ascQuery) || (win.ekey < blockInfo.window.skey && !ascQuery)) { +// pQuery->limit.offset -= 1; +// } +// +// if (pQuery->limit.offset == 0) { +// *start = doSkipIntervalProcess(pRuntimeEnv, &win, &blockInfo, pTableQueryInfo); +// return true; +// } else { +// tw = win; +// int32_t startPos = +// getNextQualifiedWindow(pQuery, &tw, &blockInfo, pColInfoData->pData, binarySearchForKey, -1); +// assert(startPos >= 0); +// +// // set the abort info +// pQuery->pos = startPos; +// pTableQueryInfo->lastKey = ((TSKEY *)pColInfoData->pData)[startPos]; +// pWindowResInfo->prevSKey = tw.skey; +// win = tw; +// } +// } else { +// break; // offset is not 0, and next time window begins or ends in the next block. +// } +// } +// } +// +// // check for error +// if (terrno != TSDB_CODE_SUCCESS) { +// longjmp(pRuntimeEnv->env, terrno); +// } +// +// return true; +//} static void doDestroyTableQueryInfo(STableGroupInfo* pTableqinfoGroupInfo); @@ -4537,40 +3876,40 @@ static int32_t setupQueryHandle(void* tsdb, SQInfo* pQInfo, bool isSTableQuery) SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; SQuery *pQuery = pQInfo->runtimeEnv.pQuery; + // TODO set the tags scan handle if (onlyQueryTags(pQuery)) { return TSDB_CODE_SUCCESS; } - if (isSTableQuery && (!QUERY_IS_INTERVAL_QUERY(pQuery)) && (!isFixedOutputQuery(pRuntimeEnv))) { - return TSDB_CODE_SUCCESS; - } - STsdbQueryCond cond = createTsdbQueryCond(pQuery, &pQuery->window); + if (isTsCompQuery(pQuery) || isPointInterpoQuery(pQuery)) { + cond.type = BLOCK_LOAD_TABLE_SEQ_ORDER; + } if (!isSTableQuery - && (pQInfo->tableqinfoGroupInfo.numOfTables == 1) + && (pRuntimeEnv->tableqinfoGroupInfo.numOfTables == 1) && (cond.order == TSDB_ORDER_ASC) && (!QUERY_IS_INTERVAL_QUERY(pQuery)) && (!isGroupbyColumn(pQuery->pGroupbyExpr)) - && (!isFixedOutputQuery(pRuntimeEnv)) + && (!isFixedOutputQuery(pQuery)) ) { - SArray* pa = GET_TABLEGROUP(pQInfo, 0); + SArray* pa = GET_TABLEGROUP(pRuntimeEnv, 0); STableQueryInfo* pCheckInfo = taosArrayGetP(pa, 0); cond.twindow = pCheckInfo->win; } terrno = TSDB_CODE_SUCCESS; if (isFirstLastRowQuery(pQuery)) { - pRuntimeEnv->pQueryHandle = tsdbQueryLastRow(tsdb, &cond, &pQInfo->tableGroupInfo, pQInfo, &pQInfo->memRef); + pRuntimeEnv->pQueryHandle = tsdbQueryLastRow(tsdb, &cond, &pQuery->tableGroupInfo, pQInfo, &pQuery->memRef); // update the query time window pQuery->window = cond.twindow; - if (pQInfo->tableGroupInfo.numOfTables == 0) { - pQInfo->tableqinfoGroupInfo.numOfTables = 0; + if (pQuery->tableGroupInfo.numOfTables == 0) { + pRuntimeEnv->tableqinfoGroupInfo.numOfTables = 0; } else { - size_t numOfGroups = GET_NUM_OF_TABLEGROUP(pQInfo); + size_t numOfGroups = GET_NUM_OF_TABLEGROUP(pRuntimeEnv); for(int32_t i = 0; i < numOfGroups; ++i) { - SArray *group = GET_TABLEGROUP(pQInfo, i); + SArray *group = GET_TABLEGROUP(pRuntimeEnv, i); size_t t = taosArrayGetSize(group); for (int32_t j = 0; j < t; ++j) { @@ -4582,26 +3921,24 @@ static int32_t setupQueryHandle(void* tsdb, SQInfo* pQInfo, bool isSTableQuery) } } } else if (isPointInterpoQuery(pQuery)) { - pRuntimeEnv->pQueryHandle = tsdbQueryRowsInExternalWindow(tsdb, &cond, &pQInfo->tableGroupInfo, pQInfo, &pQInfo->memRef); + pRuntimeEnv->pQueryHandle = tsdbQueryRowsInExternalWindow(tsdb, &cond, &pQuery->tableGroupInfo, pQInfo, &pQuery->memRef); } else { - pRuntimeEnv->pQueryHandle = tsdbQueryTables(tsdb, &cond, &pQInfo->tableGroupInfo, pQInfo, &pQInfo->memRef); + pRuntimeEnv->pQueryHandle = tsdbQueryTables(tsdb, &cond, &pQuery->tableGroupInfo, pQInfo, &pQuery->memRef); } return terrno; } -static SFillColInfo* createFillColInfo(SQuery* pQuery) { - int32_t numOfCols = getNumOfFinalResCol(pQuery); +static SFillColInfo* createFillColInfo(SExprInfo* pExpr, int32_t numOfOutput, int64_t* fillVal) { int32_t offset = 0; - SFillColInfo* pFillCol = calloc(numOfCols, sizeof(SFillColInfo)); + SFillColInfo* pFillCol = calloc(numOfOutput, sizeof(SFillColInfo)); if (pFillCol == NULL) { return NULL; } - // TODO refactor - for(int32_t i = 0; i < numOfCols; ++i) { - SExprInfo* pExprInfo = (pQuery->pExpr2 == NULL)? &pQuery->pExpr1[i]:&pQuery->pExpr2[i]; + for(int32_t i = 0; i < numOfOutput; ++i) { + SExprInfo* pExprInfo = &pExpr[i]; pFillCol[i].col.bytes = pExprInfo->bytes; pFillCol[i].col.type = (int8_t)pExprInfo->type; @@ -4609,7 +3946,7 @@ static SFillColInfo* createFillColInfo(SQuery* pQuery) { pFillCol[i].tagIndex = -2; pFillCol[i].flag = TSDB_COL_NORMAL; // always be ta normal column for table query pFillCol[i].functionId = pExprInfo->base.functionId; - pFillCol[i].fillVal.i = pQuery->fillVal[i]; + pFillCol[i].fillVal.i = fillVal[i]; offset += pExprInfo->bytes; } @@ -4621,11 +3958,15 @@ int32_t doInitQInfo(SQInfo *pQInfo, STSBuf *pTsBuf, SArray* prevResult, void *ts SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; SQuery *pQuery = pQInfo->runtimeEnv.pQuery; + pQuery->tsdb = tsdb; + + pQuery->topBotQuery = isTopBottomQuery(pQuery); + pQuery->hasTagResults = hasTagValOutput(pQuery); + pQuery->timeWindowInterpo = timeWindowInterpoRequired(pQuery); + pQuery->stabledev = isStabledev(pQuery); - pRuntimeEnv->topBotQuery = isTopBottomQuery(pQuery); - pRuntimeEnv->hasTagResults = hasTagValOutput(pQuery); - pRuntimeEnv->timeWindowInterpo = timeWindowInterpoRequired(pQuery); pRuntimeEnv->prevResult = prevResult; + pRuntimeEnv->qinfo = pQInfo; setScanLimitationByResultBuffer(pQuery); @@ -4634,17 +3975,31 @@ int32_t doInitQInfo(SQInfo *pQInfo, STSBuf *pTsBuf, SArray* prevResult, void *ts return code; } - pQInfo->tsdb = tsdb; - pQInfo->vgId = vgId; - pQInfo->groupResInfo.totalGroup = (int32_t) (isSTableQuery? GET_NUM_OF_TABLEGROUP(pQInfo):0); + pQuery->tsdb = tsdb; + pQuery->vgId = vgId; + pQuery->stableQuery = isSTableQuery; + pQuery->groupbyColumn = isGroupbyColumn(pQuery->pGroupbyExpr); + pQuery->interBufSize = getOutputInterResultBufSize(pQuery); + + pRuntimeEnv->groupResInfo.totalGroup = (int32_t) (isSTableQuery? GET_NUM_OF_TABLEGROUP(pRuntimeEnv):0); pRuntimeEnv->pQuery = pQuery; pRuntimeEnv->pTsBuf = pTsBuf; pRuntimeEnv->cur.vgroupIndex = -1; - pRuntimeEnv->stableQuery = isSTableQuery; - pRuntimeEnv->prevGroupId = INT32_MIN; - pRuntimeEnv->groupbyColumn = isGroupbyColumn(pQuery->pGroupbyExpr); - pRuntimeEnv->stabledev = isStabledev(pQuery); + setResultBufSize(pQuery, &pRuntimeEnv->resultInfo); + + if (onlyQueryTags(pQuery)) { + pRuntimeEnv->resultInfo.capacity = 4096; + pRuntimeEnv->proot = createTagScanOperatorInfo(pRuntimeEnv, pQuery->pExpr1, pQuery->numOfOutput); + } else if (pQuery->queryBlockDist) { + pRuntimeEnv->pTableScanner = createTableBlockInfoScanOperator(pRuntimeEnv->pQueryHandle, pRuntimeEnv); + } else if (isTsCompQuery(pQuery) || isPointInterpoQuery(pQuery)) { + pRuntimeEnv->pTableScanner = createTableSeqScanOperator(pRuntimeEnv->pQueryHandle, pRuntimeEnv); + } else if (needReverseScan(pQuery)) { + pRuntimeEnv->pTableScanner = createDataBlocksOptScanInfo(pRuntimeEnv->pQueryHandle, pRuntimeEnv, getNumOfScanTimes(pQuery), 1); + } else { + pRuntimeEnv->pTableScanner = createTableScanOperator(pRuntimeEnv->pQueryHandle, pRuntimeEnv, getNumOfScanTimes(pQuery)); + } if (pTsBuf != NULL) { int16_t order = (pQuery->order.order == pRuntimeEnv->pTsBuf->tsOrder) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC; @@ -4652,109 +4007,26 @@ int32_t doInitQInfo(SQInfo *pQInfo, STSBuf *pTsBuf, SArray* prevResult, void *ts } int32_t ps = DEFAULT_PAGE_SIZE; - int32_t rowsize = 0; - getIntermediateBufInfo(pRuntimeEnv, &ps, &rowsize); - int32_t TENMB = 1024*1024*10; - - if (isSTableQuery && !onlyQueryTags(pRuntimeEnv->pQuery)) { - code = createDiskbasedResultBuffer(&pRuntimeEnv->pResultBuf, rowsize, ps, TENMB, pQInfo); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - if (!QUERY_IS_INTERVAL_QUERY(pQuery)) { - int16_t type = TSDB_DATA_TYPE_NULL; - if (pRuntimeEnv->groupbyColumn) { // group by columns not tags; - type = getGroupbyColumnType(pQuery, pQuery->pGroupbyExpr); - } else { - type = TSDB_DATA_TYPE_INT; // group id - } - - code = initResultRowInfo(&pRuntimeEnv->resultRowInfo, 8, type); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - } - } else if (pRuntimeEnv->groupbyColumn || QUERY_IS_INTERVAL_QUERY(pQuery) || (!isSTableQuery)) { - int32_t numOfResultRows = getInitialPageNum(pQInfo); - getIntermediateBufInfo(pRuntimeEnv, &ps, &rowsize); - code = createDiskbasedResultBuffer(&pRuntimeEnv->pResultBuf, rowsize, ps, TENMB, pQInfo); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - - int16_t type = TSDB_DATA_TYPE_NULL; - if (pRuntimeEnv->groupbyColumn) { - type = getGroupbyColumnType(pQuery, pQuery->pGroupbyExpr); - } else { - type = TSDB_DATA_TYPE_TIMESTAMP; - } + getIntermediateBufInfo(pRuntimeEnv, &ps, &pQuery->intermediateResultRowSize); - code = initResultRowInfo(&pRuntimeEnv->resultRowInfo, numOfResultRows, type); - if (code != TSDB_CODE_SUCCESS) { - return code; - } + int32_t TENMB = 1024*1024*10; + code = createDiskbasedResultBuffer(&pRuntimeEnv->pResultBuf, ps, TENMB, pQInfo); + if (code != TSDB_CODE_SUCCESS) { + return code; } // create runtime environment - code = setupQueryRuntimeEnv(pRuntimeEnv, (int32_t) pQInfo->tableGroupInfo.numOfTables, pQuery->order.order, pQInfo->vgId); + int32_t numOfTables = (int32_t)pQuery->tableGroupInfo.numOfTables; + pQInfo->summary.tableInfoSize += (numOfTables * sizeof(STableQueryInfo)); + code = setupQueryRuntimeEnv(pRuntimeEnv, (int32_t) pQuery->tableGroupInfo.numOfTables); if (code != TSDB_CODE_SUCCESS) { return code; } - if (pQuery->fillType != TSDB_FILL_NONE && !isPointInterpoQuery(pQuery)) { - SFillColInfo* pColInfo = createFillColInfo(pQuery); - STimeWindow w = TSWINDOW_INITIALIZER; - - TSKEY sk = MIN(pQuery->window.skey, pQuery->window.ekey); - TSKEY ek = MAX(pQuery->window.skey, pQuery->window.ekey); - getAlignQueryTimeWindow(pQuery, pQuery->window.skey, sk, ek, &w); - - int32_t numOfCols = getNumOfFinalResCol(pQuery); - pRuntimeEnv->pFillInfo = taosCreateFillInfo(pQuery->order.order, w.skey, 0, (int32_t)pQuery->rec.capacity, numOfCols, - pQuery->interval.sliding, pQuery->interval.slidingUnit, (int8_t)pQuery->precision, - pQuery->fillType, pColInfo, pQInfo); - } - - setQueryStatus(pQuery, QUERY_NOT_COMPLETED); + setQueryStatus(pRuntimeEnv, QUERY_NOT_COMPLETED); return TSDB_CODE_SUCCESS; } -static void enableExecutionForNextTable(SQueryRuntimeEnv *pRuntimeEnv) { - SQuery *pQuery = pRuntimeEnv->pQuery; - - for (int32_t i = 0; i < pQuery->numOfOutput; ++i) { - SResultRowCellInfo *pResInfo = GET_RES_INFO(&pRuntimeEnv->pCtx[i]); - if (pResInfo != NULL) { - pResInfo->complete = false; - } - } -} - -static FORCE_INLINE void setEnvForEachBlock(SQInfo* pQInfo, STableQueryInfo* pTableQueryInfo, SDataBlockInfo* pBlockInfo) { - SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery* pQuery = pQInfo->runtimeEnv.pQuery; - int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); - - if (pRuntimeEnv->hasTagResults || pRuntimeEnv->pTsBuf != NULL) { - setTagVal(pRuntimeEnv, pTableQueryInfo->pTable); - } - - if (pRuntimeEnv->pTsBuf != NULL) { - setTimestampListJoinInfo(pQInfo, pTableQueryInfo); - } - - if (QUERY_IS_INTERVAL_QUERY(pQuery)) { - if (!QUERY_IS_ASC_QUERY(pRuntimeEnv->pQuery)) { - setIntervalQueryRange(pQInfo, pBlockInfo->window.ekey); - } else { - setIntervalQueryRange(pQInfo, pBlockInfo->window.skey); - } - } else { // non-interval query - setExecutionContext(pQInfo, pTableQueryInfo->groupIndex, pBlockInfo->window.ekey + step); - } -} - static void doTableQueryInfoTimeWindowCheck(SQuery* pQuery, STableQueryInfo* pTableQueryInfo) { if (QUERY_IS_ASC_QUERY(pQuery)) { assert( @@ -4769,1123 +4041,1297 @@ static void doTableQueryInfoTimeWindowCheck(SQuery* pQuery, STableQueryInfo* pTa } } -static int64_t scanMultiTableDataBlocks(SQInfo *pQInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery* pQuery = pRuntimeEnv->pQuery; - SQueryCostInfo* summary = &pRuntimeEnv->summary; - - int64_t st = taosGetTimestampMs(); +STsdbQueryCond createTsdbQueryCond(SQuery* pQuery, STimeWindow* win) { + STsdbQueryCond cond = { + .colList = pQuery->colList, + .order = pQuery->order.order, + .numOfCols = pQuery->numOfCols, + .type = BLOCK_LOAD_OFFSET_SEQ_ORDER, + .loadExternalRows = false, + }; - TsdbQueryHandleT pQueryHandle = IS_MASTER_SCAN(pRuntimeEnv)? pRuntimeEnv->pQueryHandle : pRuntimeEnv->pSecQueryHandle; - SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER; + TIME_WINDOW_COPY(cond.twindow, *win); + return cond; +} - int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); +static STableIdInfo createTableIdInfo(STableQueryInfo* pTableQueryInfo) { + STableIdInfo tidInfo; + STableId* id = TSDB_TABLEID(pTableQueryInfo->pTable); - while (tsdbNextDataBlock(pQueryHandle)) { - summary->totalBlocks += 1; + tidInfo.uid = id->uid; + tidInfo.tid = id->tid; + tidInfo.key = pTableQueryInfo->lastKey; - if (isQueryKilled(pQInfo)) { - longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); - } + return tidInfo; +} - tsdbRetrieveDataBlockInfo(pQueryHandle, &blockInfo); - STableQueryInfo **pTableQueryInfo = (STableQueryInfo**) taosHashGet(pQInfo->tableqinfoGroupInfo.map, &blockInfo.tid, sizeof(blockInfo.tid)); - if(pTableQueryInfo == NULL) { - break; +static void updateTableIdInfo(STableQueryInfo* pTableQueryInfo, SSDataBlock* pBlock, SHashObj* pTableIdInfo, int32_t order) { + int32_t step = GET_FORWARD_DIRECTION_FACTOR(order); + pTableQueryInfo->lastKey = ((order == TSDB_ORDER_ASC)? pBlock->info.window.ekey:pBlock->info.window.skey) + step; + + STableIdInfo tidInfo = createTableIdInfo(pTableQueryInfo); + STableIdInfo *idinfo = taosHashGet(pTableIdInfo, &tidInfo.tid, sizeof(tidInfo.tid)); + if (idinfo != NULL) { + assert(idinfo->tid == tidInfo.tid && idinfo->uid == tidInfo.uid); + idinfo->key = tidInfo.key; + } else { + taosHashPut(pTableIdInfo, &tidInfo.tid, sizeof(tidInfo.tid), &tidInfo, sizeof(STableIdInfo)); + } +} + +static void doCloseAllTimeWindow(SQueryRuntimeEnv* pRuntimeEnv) { + size_t numOfGroup = GET_NUM_OF_TABLEGROUP(pRuntimeEnv); + for (int32_t i = 0; i < numOfGroup; ++i) { + SArray* group = GET_TABLEGROUP(pRuntimeEnv, i); + + size_t num = taosArrayGetSize(group); + for (int32_t j = 0; j < num; ++j) { + STableQueryInfo* item = taosArrayGetP(group, j); + closeAllResultRows(&item->resInfo); } + } +} + +static SSDataBlock* doTableScanImpl(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; - pQuery->current = *pTableQueryInfo; - doTableQueryInfoTimeWindowCheck(pQuery, *pTableQueryInfo); + STableScanInfo* pTableScanInfo = pOperator->info; + SSDataBlock* pBlock = &pTableScanInfo->block; + SQuery* pQuery = pOperator->pRuntimeEnv->pQuery; + STableGroupInfo* pTableGroupInfo = &pOperator->pRuntimeEnv->tableqinfoGroupInfo; - if (!pRuntimeEnv->groupbyColumn) { - setEnvForEachBlock(pQInfo, *pTableQueryInfo, &blockInfo); + while (tsdbNextDataBlock(pTableScanInfo->pQueryHandle)) { + if (isQueryKilled(pOperator->pRuntimeEnv->qinfo)) { + longjmp(pOperator->pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); } - if (pRuntimeEnv->stabledev) { - for(int32_t i = 0; i < pQuery->numOfOutput; ++i) { - if (pQuery->pExpr1[i].base.functionId == TSDB_FUNC_STDDEV_DST) { - setParamValue(pRuntimeEnv); - break; - } + pTableScanInfo->numOfBlocks += 1; + tsdbRetrieveDataBlockInfo(pTableScanInfo->pQueryHandle, &pBlock->info); + + // todo opt + if (pTableGroupInfo->numOfTables > 1 || (pQuery->current == NULL && pTableGroupInfo->numOfTables == 1)) { + STableQueryInfo** pTableQueryInfo = + (STableQueryInfo**)taosHashGet(pTableGroupInfo->map, &pBlock->info.tid, sizeof(pBlock->info.tid)); + if (pTableQueryInfo == NULL) { + break; } - } - uint32_t status = 0; - SDataStatis *pStatis = NULL; - SArray *pDataBlock = NULL; + pQuery->current = *pTableQueryInfo; + doTableQueryInfoTimeWindowCheck(pQuery, *pTableQueryInfo); + } - int32_t ret = loadDataBlockOnDemand(pRuntimeEnv, &pQuery->current->resInfo, pQueryHandle, &blockInfo, &pStatis, &pDataBlock, &status); - if (ret != TSDB_CODE_SUCCESS) { - break; + // this function never returns error? + uint32_t status; + int32_t code = loadDataBlockOnDemand(pOperator->pRuntimeEnv, pTableScanInfo, pBlock, &status); + if (code != TSDB_CODE_SUCCESS) { + longjmp(pOperator->pRuntimeEnv->env, code); } - if (status == BLK_DATA_DISCARD) { - pQuery->current->lastKey = QUERY_IS_ASC_QUERY(pQuery)? blockInfo.window.ekey + step : blockInfo.window.skey + step; + // current block is ignored according to filter result by block statistics data, continue load the next block + if (status == BLK_DATA_DISCARD || pBlock->info.rows == 0) { continue; } - summary->totalRows += blockInfo.rows; - stableApplyFunctionsOnBlock(pRuntimeEnv, &blockInfo, pStatis, pDataBlock, binarySearchForKey); - - qDebug("QInfo:%p check data block completed, uid:%"PRId64", tid:%d, brange:%" PRId64 "-%" PRId64 ", numOfRows:%d, " - "lastKey:%" PRId64, - pQInfo, blockInfo.uid, blockInfo.tid, blockInfo.window.skey, blockInfo.window.ekey, blockInfo.rows, - pQuery->current->lastKey); - } - - if (terrno != TSDB_CODE_SUCCESS) { - longjmp(pRuntimeEnv->env, terrno); + return pBlock; } - updateWindowResNumOfRes(pRuntimeEnv); - - int64_t et = taosGetTimestampMs(); - return et - st; + return NULL; } -static bool multiTableMultioutputHelper(SQInfo *pQInfo, int32_t index) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery * pQuery = pRuntimeEnv->pQuery; +static SSDataBlock* doTableScan(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; - setQueryStatus(pQuery, QUERY_NOT_COMPLETED); - SArray *group = GET_TABLEGROUP(pQInfo, 0); - STableQueryInfo* pCheckInfo = taosArrayGetP(group, index); + STableScanInfo *pTableScanInfo = pOperator->info; + SQueryRuntimeEnv *pRuntimeEnv = pOperator->pRuntimeEnv; + SQuery* pQuery = pRuntimeEnv->pQuery; - if (pRuntimeEnv->hasTagResults || pRuntimeEnv->pTsBuf != NULL) { - setTagVal(pRuntimeEnv, pCheckInfo->pTable); - } + SResultRowInfo* pResultRowInfo = pTableScanInfo->pResultRowInfo; - STableId* id = TSDB_TABLEID(pCheckInfo->pTable); - qDebug("QInfo:%p query on (%d): uid:%" PRIu64 ", tid:%d, qrange:%" PRId64 "-%" PRId64, pQInfo, index, - id->uid, id->tid, pCheckInfo->lastKey, pCheckInfo->win.ekey); + while (pTableScanInfo->current < pTableScanInfo->times) { + SSDataBlock* p = doTableScanImpl(pOperator); + if (p != NULL) { + return p; + } - STsdbQueryCond cond = { - .twindow = {pCheckInfo->lastKey, pCheckInfo->win.ekey}, - .order = pQuery->order.order, - .colList = pQuery->colList, - .numOfCols = pQuery->numOfCols, - .loadExternalRows = false, - }; + if (++pTableScanInfo->current >= pTableScanInfo->times) { + if (pTableScanInfo->reverseTimes <= 0) { + return NULL; + } else { + break; + } + } - // todo refactor - SArray *g1 = taosArrayInit(1, POINTER_BYTES); - SArray *tx = taosArrayInit(1, sizeof(STableKeyInfo)); + // do prepare for the next round table scan operation + STsdbQueryCond cond = createTsdbQueryCond(pQuery, &pQuery->window); + tsdbResetQueryHandle(pTableScanInfo->pQueryHandle, &cond); - STableKeyInfo info = {.pTable = pCheckInfo->pTable, .lastKey = pCheckInfo->lastKey}; - taosArrayPush(tx, &info); + setQueryStatus(pRuntimeEnv, QUERY_NOT_COMPLETED); + pRuntimeEnv->scanFlag = REPEAT_SCAN; - taosArrayPush(g1, &tx); - STableGroupInfo gp = {.numOfTables = 1, .pGroupList = g1}; + if (pRuntimeEnv->pTsBuf) { + bool ret = tsBufNextPos(pRuntimeEnv->pTsBuf); + assert(ret); + } - // include only current table - if (pRuntimeEnv->pQueryHandle != NULL) { - tsdbCleanupQueryHandle(pRuntimeEnv->pQueryHandle); - pRuntimeEnv->pQueryHandle = NULL; - } + pResultRowInfo->curIndex = 0; + pResultRowInfo->prevSKey = pResultRowInfo->pResult[0]->win.skey; - pRuntimeEnv->pQueryHandle = tsdbQueryTables(pQInfo->tsdb, &cond, &gp, pQInfo, &pQInfo->memRef); - taosArrayDestroy(tx); - taosArrayDestroy(g1); - if (pRuntimeEnv->pQueryHandle == NULL) { - longjmp(pRuntimeEnv->env, terrno); + qDebug("QInfo:%p start to repeat scan data blocks due to query func required, qrange:%" PRId64 "-%" PRId64, + pRuntimeEnv->qinfo, cond.twindow.skey, cond.twindow.ekey); } - if (pRuntimeEnv->pTsBuf != NULL) { - tVariant* pTag = &pRuntimeEnv->pCtx[0].tag; - - if (pRuntimeEnv->cur.vgroupIndex == -1) { - STSElem elem = tsBufGetElemStartPos(pRuntimeEnv->pTsBuf, pQInfo->vgId, pTag); - // failed to find data with the specified tag value and vnodeId - if (!tsBufIsValidElem(&elem)) { - if (pTag->nType == TSDB_DATA_TYPE_BINARY || pTag->nType == TSDB_DATA_TYPE_NCHAR) { - qError("QInfo:%p failed to find tag:%s in ts_comp", pQInfo, pTag->pz); - } else { - qError("QInfo:%p failed to find tag:%"PRId64" in ts_comp", pQInfo, pTag->i64); - } + if (pTableScanInfo->reverseTimes > 0) { + setupEnvForReverseScan(pRuntimeEnv, pTableScanInfo->pResultRowInfo, pTableScanInfo->pCtx, pTableScanInfo->numOfOutput); - return false; - } else { - STSCursor cur = tsBufGetCursor(pRuntimeEnv->pTsBuf); + STsdbQueryCond cond = createTsdbQueryCond(pQuery, &pQuery->window); + tsdbResetQueryHandle(pTableScanInfo->pQueryHandle, &cond); - if (pTag->nType == TSDB_DATA_TYPE_BINARY || pTag->nType == TSDB_DATA_TYPE_NCHAR) { - qDebug("QInfo:%p find tag:%s start pos in ts_comp, blockIndex:%d, tsIndex:%d", pQInfo, pTag->pz, - cur.blockIndex, cur.tsIndex); - } else { - qDebug("QInfo:%p find tag:%"PRId64" start pos in ts_comp, blockIndex:%d, tsIndex:%d", pQInfo, pTag->i64, - cur.blockIndex, cur.tsIndex); - } - } - } else { - STSElem elem = tsBufGetElem(pRuntimeEnv->pTsBuf); - if (tVariantCompare(elem.tag, &pRuntimeEnv->pCtx[0].tag) != 0) { + qDebug("QInfo:%p start to reverse scan data blocks due to query func required, qrange:%" PRId64 "-%" PRId64, + pRuntimeEnv->qinfo, cond.twindow.skey, cond.twindow.ekey); - STSElem elem1 = tsBufGetElemStartPos(pRuntimeEnv->pTsBuf, pQInfo->vgId, pTag); - // failed to find data with the specified tag value and vnodeId - if (!tsBufIsValidElem(&elem1)) { - if (pTag->nType == TSDB_DATA_TYPE_BINARY || pTag->nType == TSDB_DATA_TYPE_NCHAR) { - qError("QInfo:%p failed to find tag:%s in ts_comp", pQInfo, pTag->pz); - } else { - qError("QInfo:%p failed to find tag:%"PRId64" in ts_comp", pQInfo, pTag->i64); - } + pRuntimeEnv->scanFlag = REVERSE_SCAN; - return false; - } else { - STSCursor cur = tsBufGetCursor(pRuntimeEnv->pTsBuf); - if (pTag->nType == TSDB_DATA_TYPE_BINARY || pTag->nType == TSDB_DATA_TYPE_NCHAR) { - qDebug("QInfo:%p find tag:%s start pos in ts_comp, blockIndex:%d, tsIndex:%d", pQInfo, pTag->pz, cur.blockIndex, cur.tsIndex); - } else { - qDebug("QInfo:%p find tag:%"PRId64" start pos in ts_comp, blockIndex:%d, tsIndex:%d", pQInfo, pTag->i64, cur.blockIndex, cur.tsIndex); - } - } + pTableScanInfo->times = 1; + pTableScanInfo->current = 0; + pTableScanInfo->reverseTimes = 0; + pTableScanInfo->order = cond.order; - } else { - tsBufSetCursor(pRuntimeEnv->pTsBuf, &pRuntimeEnv->cur); - STSCursor cur = tsBufGetCursor(pRuntimeEnv->pTsBuf); - if (pTag->nType == TSDB_DATA_TYPE_BINARY || pTag->nType == TSDB_DATA_TYPE_NCHAR) { - qDebug("QInfo:%p continue scan ts_comp file, tag:%s blockIndex:%d, tsIndex:%d", pQInfo, pTag->pz, cur.blockIndex, cur.tsIndex); - } else { - qDebug("QInfo:%p continue scan ts_comp file, tag:%"PRId64" blockIndex:%d, tsIndex:%d", pQInfo, pTag->i64, cur.blockIndex, cur.tsIndex); - } - } + if (pResultRowInfo->size > 0) { + pResultRowInfo->curIndex = pResultRowInfo->size-1; + pResultRowInfo->prevSKey = pResultRowInfo->pResult[pResultRowInfo->size-1]->win.skey; + } + + SSDataBlock* p = doTableScanImpl(pOperator); + if (p != NULL) { + return p; } } - initCtxOutputBuf(pRuntimeEnv); - return true; + return NULL; } -STsdbQueryCond createTsdbQueryCond(SQuery* pQuery, STimeWindow* win) { - STsdbQueryCond cond = { - .colList = pQuery->colList, - .order = pQuery->order.order, - .numOfCols = pQuery->numOfCols, - .loadExternalRows = false, - }; +static SSDataBlock* doBlockInfoScan(void* param) { + SOperatorInfo *pOperator = (SOperatorInfo*)param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; + } - TIME_WINDOW_COPY(cond.twindow, *win); - return cond; -} + STableScanInfo *pTableScanInfo = pOperator->info; -static STableIdInfo createTableIdInfo(SQuery* pQuery) { - assert(pQuery != NULL && pQuery->current != NULL); + STableBlockDist tableBlockDist = {0}; + tableBlockDist.numOfTables = (int32_t)pOperator->pRuntimeEnv->tableqinfoGroupInfo.numOfTables; + tableBlockDist.dataBlockInfos = taosArrayInit(512, sizeof(SFileBlockInfo)); - STableIdInfo tidInfo; - STableId* id = TSDB_TABLEID(pQuery->current->pTable); + tsdbGetFileBlocksDistInfo(pTableScanInfo->pQueryHandle, &tableBlockDist); + tableBlockDist.numOfRowsInMemTable = (int32_t) tsdbGetNumOfRowsInMemTable(pTableScanInfo->pQueryHandle); - tidInfo.uid = id->uid; - tidInfo.tid = id->tid; - tidInfo.key = pQuery->current->lastKey; + SSDataBlock* pBlock = &pTableScanInfo->block; + pBlock->info.rows = 1; + pBlock->info.numOfCols = 1; - return tidInfo; -} + SBufferWriter bw = tbufInitWriter(NULL, false); + blockDistInfoToBinary(&tableBlockDist, &bw); + SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, 0); -static void updateTableIdInfo(SQuery* pQuery, SHashObj* pTableIdInfo) { - STableIdInfo tidInfo = createTableIdInfo(pQuery); - STableIdInfo* idinfo = taosHashGet(pTableIdInfo, &tidInfo.tid, sizeof(tidInfo.tid)); - if (idinfo != NULL) { - assert(idinfo->tid == tidInfo.tid && idinfo->uid == tidInfo.uid); - idinfo->key = tidInfo.key; - } else { - taosHashPut(pTableIdInfo, &tidInfo.tid, sizeof(tidInfo.tid), &tidInfo, sizeof(STableIdInfo)); - } -} + int32_t len = (int32_t) tbufTell(&bw); + pColInfo->pData = malloc(len + sizeof(int32_t)); -/** - * super table query handler - * 1. super table projection query, group-by on normal columns query, ts-comp query - * 2. point interpolation query, last row query - * - * @param pQInfo - */ -static void sequentialTableProcess(SQInfo *pQInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery * pQuery = pRuntimeEnv->pQuery; - setQueryStatus(pQuery, QUERY_COMPLETED); + *(int32_t*) pColInfo->pData = len; + memcpy(pColInfo->pData + sizeof(int32_t), tbufGetData(&bw, false), len); - size_t numOfGroups = GET_NUM_OF_TABLEGROUP(pQInfo); + tbufCloseWriter(&bw); - if (isPointInterpoQuery(pQuery)) { - resetDefaultResInfoOutputBuf(pRuntimeEnv); - assert(pQuery->limit.offset == 0 && pQuery->limit.limit != 0); + SArray* g = GET_TABLEGROUP(pOperator->pRuntimeEnv, 0); + pOperator->pRuntimeEnv->pQuery->current = taosArrayGetP(g, 0); - while (pQInfo->groupIndex < numOfGroups) { - SArray *group = taosArrayGetP(pQInfo->tableGroupInfo.pGroupList, pQInfo->groupIndex); + pOperator->status = OP_EXEC_DONE; + return pBlock; +} - qDebug("QInfo:%p point interpolation query on group:%d, total group:%" PRIzu ", current group:%p", pQInfo, - pQInfo->groupIndex, numOfGroups, group); - STsdbQueryCond cond = createTsdbQueryCond(pQuery, &pQuery->window); +SOperatorInfo* createTableScanOperator(void* pTsdbQueryHandle, SQueryRuntimeEnv* pRuntimeEnv, int32_t repeatTime) { + assert(repeatTime > 0); - SArray *g1 = taosArrayInit(1, POINTER_BYTES); - SArray *tx = taosArrayDup(group); - taosArrayPush(g1, &tx); + STableScanInfo* pInfo = calloc(1, sizeof(STableScanInfo)); + pInfo->pQueryHandle = pTsdbQueryHandle; + pInfo->times = repeatTime; + pInfo->reverseTimes = 0; + pInfo->order = pRuntimeEnv->pQuery->order.order; + pInfo->current = 0; - STableGroupInfo gp = {.numOfTables = taosArrayGetSize(tx), .pGroupList = g1}; + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + pOperator->name = "TableScanOperator"; + pOperator->blockingOptr = false; + pOperator->status = OP_IN_EXECUTING; + pOperator->info = pInfo; + pOperator->numOfOutput = pRuntimeEnv->pQuery->numOfCols; + pOperator->pRuntimeEnv = pRuntimeEnv; + pOperator->exec = doTableScan; - // include only current table - if (pRuntimeEnv->pQueryHandle != NULL) { - tsdbCleanupQueryHandle(pRuntimeEnv->pQueryHandle); - pRuntimeEnv->pQueryHandle = NULL; - } + return pOperator; +} - pRuntimeEnv->pQueryHandle = tsdbQueryRowsInExternalWindow(pQInfo->tsdb, &cond, &gp, pQInfo, &pQInfo->memRef); +SOperatorInfo* createTableSeqScanOperator(void* pTsdbQueryHandle, SQueryRuntimeEnv* pRuntimeEnv) { + STableScanInfo* pInfo = calloc(1, sizeof(STableScanInfo)); - taosArrayDestroy(tx); - taosArrayDestroy(g1); - if (pRuntimeEnv->pQueryHandle == NULL) { - longjmp(pRuntimeEnv->env, terrno); - } + pInfo->pQueryHandle = pTsdbQueryHandle; + pInfo->times = 1; + pInfo->reverseTimes = 0; + pInfo->order = pRuntimeEnv->pQuery->order.order; + pInfo->current = 0; - initCtxOutputBuf(pRuntimeEnv); + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + pOperator->name = "TableSeqScanOperator"; + pOperator->operatorType = OP_TableSeqScan; + pOperator->blockingOptr = false; + pOperator->status = OP_IN_EXECUTING; + pOperator->info = pInfo; + pOperator->numOfOutput = pRuntimeEnv->pQuery->numOfCols; + pOperator->pRuntimeEnv = pRuntimeEnv; + pOperator->exec = doTableScanImpl; - SArray *s = tsdbGetQueriedTableList(pRuntimeEnv->pQueryHandle); - assert(taosArrayGetSize(s) >= 1); + return pOperator; +} - setTagVal(pRuntimeEnv, taosArrayGetP(s, 0)); - taosArrayDestroy(s); +SOperatorInfo* createTableBlockInfoScanOperator(void* pTsdbQueryHandle, SQueryRuntimeEnv* pRuntimeEnv) { + STableScanInfo* pInfo = calloc(1, sizeof(STableScanInfo)); - // here we simply set the first table as current table - SArray *first = GET_TABLEGROUP(pQInfo, pQInfo->groupIndex); - pQuery->current = taosArrayGetP(first, 0); + pInfo->pQueryHandle = pTsdbQueryHandle; + pInfo->block.pDataBlock = taosArrayInit(1, sizeof(SColumnInfoData)); - scanOneTableDataBlocks(pRuntimeEnv, pQuery->current->lastKey); + SColumnInfoData infoData = {{0}}; + infoData.info.type = TSDB_DATA_TYPE_BINARY; + infoData.info.bytes = 1024; + infoData.info.colId = TSDB_BLOCK_DIST_COLUMN_INDEX; + taosArrayPush(pInfo->block.pDataBlock, &infoData); - int64_t numOfRes = getNumOfResult(pRuntimeEnv); - if (numOfRes > 0) { - pQuery->rec.rows += numOfRes; - forwardCtxOutputBuf(pRuntimeEnv, numOfRes); - } + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + pOperator->name = "TableBlockInfoScanOperator"; + pOperator->operatorType = OP_TableBlockInfoScan; + pOperator->blockingOptr = false; + pOperator->status = OP_IN_EXECUTING; + pOperator->info = pInfo; + pOperator->pRuntimeEnv = pRuntimeEnv; + pOperator->numOfOutput = pRuntimeEnv->pQuery->numOfCols; + pOperator->exec = doBlockInfoScan; - skipResults(pRuntimeEnv); - pQInfo->groupIndex += 1; + return pOperator; +} - // enable execution for next table, when handling the projection query - enableExecutionForNextTable(pRuntimeEnv); +void setTableScanFilterOperatorInfo(STableScanInfo* pTableScanInfo, SOperatorInfo* pDownstream) { + assert(pTableScanInfo != NULL && pDownstream != NULL); - if (pQuery->rec.rows >= pQuery->rec.capacity) { - setQueryStatus(pQuery, QUERY_RESBUF_FULL); - break; - } - } - } else if (pRuntimeEnv->groupbyColumn) { // group-by on normal columns query - while (pQInfo->groupIndex < numOfGroups) { - SArray *group = taosArrayGetP(pQInfo->tableGroupInfo.pGroupList, pQInfo->groupIndex); + pTableScanInfo->pExpr = pDownstream->pExpr; // TODO refactor to use colId instead of pExpr + pTableScanInfo->numOfOutput = pDownstream->numOfOutput; - qDebug("QInfo:%p group by normal columns group:%d, total group:%" PRIzu "", pQInfo, pQInfo->groupIndex, - numOfGroups); + if (pDownstream->operatorType == OP_Aggregate || pDownstream->operatorType == OP_MultiTableAggregate) { + SAggOperatorInfo* pAggInfo = pDownstream->info; - STsdbQueryCond cond = createTsdbQueryCond(pQuery, &pQuery->window); + pTableScanInfo->pCtx = pAggInfo->binfo.pCtx; + pTableScanInfo->pResultRowInfo = &pAggInfo->binfo.resultRowInfo; + pTableScanInfo->rowCellInfoOffset = pAggInfo->binfo.rowCellInfoOffset; + } else if (pDownstream->operatorType == OP_TimeWindow) { + STableIntervalOperatorInfo *pIntervalInfo = pDownstream->info; - SArray *g1 = taosArrayInit(1, POINTER_BYTES); - SArray *tx = taosArrayDup(group); - taosArrayPush(g1, &tx); + pTableScanInfo->pCtx = pIntervalInfo->pCtx; + pTableScanInfo->pResultRowInfo = &pIntervalInfo->resultRowInfo; + pTableScanInfo->rowCellInfoOffset = pIntervalInfo->rowCellInfoOffset; - STableGroupInfo gp = {.numOfTables = taosArrayGetSize(tx), .pGroupList = g1}; + } else if (pDownstream->operatorType == OP_Groupby) { + SGroupbyOperatorInfo *pGroupbyInfo = pDownstream->info; - // include only current table - if (pRuntimeEnv->pQueryHandle != NULL) { - tsdbCleanupQueryHandle(pRuntimeEnv->pQueryHandle); - pRuntimeEnv->pQueryHandle = NULL; - } + pTableScanInfo->pCtx = pGroupbyInfo->binfo.pCtx; + pTableScanInfo->pResultRowInfo = &pGroupbyInfo->binfo.resultRowInfo; + pTableScanInfo->rowCellInfoOffset = pGroupbyInfo->binfo.rowCellInfoOffset; - // no need to update the lastkey for each table - pRuntimeEnv->pQueryHandle = tsdbQueryTables(pQInfo->tsdb, &cond, &gp, pQInfo, &pQInfo->memRef); + } else if (pDownstream->operatorType == OP_MultiTableTimeInterval) { + STableIntervalOperatorInfo *pInfo = pDownstream->info; - taosArrayDestroy(g1); - taosArrayDestroy(tx); - if (pRuntimeEnv->pQueryHandle == NULL) { - longjmp(pRuntimeEnv->env, terrno); - } + pTableScanInfo->pCtx = pInfo->pCtx; + pTableScanInfo->pResultRowInfo = &pInfo->resultRowInfo; + pTableScanInfo->rowCellInfoOffset = pInfo->rowCellInfoOffset; + + } else if (pDownstream->operatorType == OP_Arithmetic) { + SArithOperatorInfo *pInfo = pDownstream->info; + + pTableScanInfo->pCtx = pInfo->binfo.pCtx; + pTableScanInfo->pResultRowInfo = &pInfo->binfo.resultRowInfo; + pTableScanInfo->rowCellInfoOffset = pInfo->binfo.rowCellInfoOffset; + } else if (pDownstream->operatorType == OP_SessionWindow) { + SSWindowOperatorInfo* pInfo = pDownstream->info; + + pTableScanInfo->pCtx = pInfo->binfo.pCtx; + pTableScanInfo->pResultRowInfo = &pInfo->binfo.resultRowInfo; + pTableScanInfo->rowCellInfoOffset = pInfo->binfo.rowCellInfoOffset; + } else { + assert(0); + } +} - SArray *s = tsdbGetQueriedTableList(pRuntimeEnv->pQueryHandle); - assert(taosArrayGetSize(s) >= 1); +static SOperatorInfo* createDataBlocksOptScanInfo(void* pTsdbQueryHandle, SQueryRuntimeEnv* pRuntimeEnv, int32_t repeatTime, int32_t reverseTime) { + assert(repeatTime > 0); - setTagVal(pRuntimeEnv, taosArrayGetP(s, 0)); + STableScanInfo* pInfo = calloc(1, sizeof(STableScanInfo)); + pInfo->pQueryHandle = pTsdbQueryHandle; + pInfo->times = repeatTime; + pInfo->reverseTimes = reverseTime; + pInfo->current = 0; + pInfo->order = pRuntimeEnv->pQuery->order.order; - // here we simply set the first table as current table - scanMultiTableDataBlocks(pQInfo); - pQInfo->groupIndex += 1; + SOperatorInfo* pOptr = calloc(1, sizeof(SOperatorInfo)); + pOptr->name = "DataBlocksOptimizedScanOperator"; + pOptr->operatorType = OP_DataBlocksOptScan; + pOptr->pRuntimeEnv = pRuntimeEnv; + pOptr->blockingOptr = false; + pOptr->info = pInfo; + pOptr->exec = doTableScan; - taosArrayDestroy(s); + return pOptr; +} - // no results generated for current group, continue to try the next group - SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo; - if (pWindowResInfo->size <= 0) { - continue; - } +static int32_t getTableScanOrder(STableScanInfo* pTableScanInfo) { + return pTableScanInfo->order; +} - for (int32_t i = 0; i < pWindowResInfo->size; ++i) { - pWindowResInfo->pResult[i]->closed = true; // enable return all results for group by normal columns +// this is a blocking operator +static SSDataBlock* doAggregate(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; + } - SResultRow *pResult = pWindowResInfo->pResult[i]; - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - SResultRowCellInfo *pCell = getResultCell(pRuntimeEnv, pResult, j); - pResult->numOfRows = (uint16_t)(MAX(pResult->numOfRows, pCell->numOfRes)); - } - } + SAggOperatorInfo* pAggInfo = pOperator->info; + SOptrBasicInfo* pInfo = &pAggInfo->binfo; - qDebug("QInfo:%p generated groupby columns results %d rows for group %d completed", pQInfo, pWindowResInfo->size, - pQInfo->groupIndex); + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; - pQuery->rec.rows = 0; - if (pWindowResInfo->size > pQuery->rec.capacity) { - expandBuffer(pRuntimeEnv, pWindowResInfo->size, pQInfo); - } + SQuery* pQuery = pRuntimeEnv->pQuery; + int32_t order = pQuery->order.order; - initGroupResInfo(&pQInfo->groupResInfo, &pRuntimeEnv->resultRowInfo, 0); - copyToOutputBuf(pQInfo, pWindowResInfo); - assert(pQuery->rec.rows == pWindowResInfo->size); + SOperatorInfo* upstream = pOperator->upstream; - resetResultRowInfo(pRuntimeEnv, &pRuntimeEnv->resultRowInfo); - cleanupGroupResInfo(&pQInfo->groupResInfo); + while(1) { + SSDataBlock* pBlock = upstream->exec(upstream); + if (pBlock == NULL) { break; } - } else if (pRuntimeEnv->queryWindowIdentical && pRuntimeEnv->pTsBuf == NULL && !isTsCompQuery(pQuery)) { - //super table projection query with identical query time range for all tables. - SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER; - resetDefaultResInfoOutputBuf(pRuntimeEnv); - SArray *group = GET_TABLEGROUP(pQInfo, 0); - assert(taosArrayGetSize(group) == pQInfo->tableqinfoGroupInfo.numOfTables && - 1 == taosArrayGetSize(pQInfo->tableqinfoGroupInfo.pGroupList)); + setTagValue(pOperator, pQuery->current->pTable, pInfo->pCtx, pOperator->numOfOutput); - void *pQueryHandle = pRuntimeEnv->pQueryHandle; - if (pQueryHandle == NULL) { - STsdbQueryCond con = createTsdbQueryCond(pQuery, &pQuery->window); - pRuntimeEnv->pQueryHandle = tsdbQueryTables(pQInfo->tsdb, &con, &pQInfo->tableGroupInfo, pQInfo, &pQInfo->memRef); - pQueryHandle = pRuntimeEnv->pQueryHandle; + if (upstream->operatorType == OP_DataBlocksOptScan) { + STableScanInfo* pScanInfo = upstream->info; + order = getTableScanOrder(pScanInfo); } - // skip blocks without load the actual data block from file if no filter condition present - // skipBlocks(&pQInfo->runtimeEnv); - // if (pQuery->limit.offset > 0 && pQuery->numOfFilterCols == 0) { - // setQueryStatus(pQuery, QUERY_COMPLETED); - // return; - // } + // the pDataBlock are always the same one, no need to call this again + setInputDataBlock(pOperator, pInfo->pCtx, pBlock, order); + doAggregateImpl(pOperator, pQuery->window.skey, pInfo->pCtx, pBlock); + } - if (pQuery->prjInfo.vgroupLimit != -1) { - assert(pQuery->limit.limit == -1 && pQuery->limit.offset == 0); - } else if (pQuery->limit.limit != -1) { - assert(pQuery->prjInfo.vgroupLimit == -1); - } + pOperator->status = OP_EXEC_DONE; + setQueryStatus(pRuntimeEnv, QUERY_COMPLETED); - setQueryStatus(pQuery, QUERY_NOT_COMPLETED); + finalizeQueryResult(pOperator, pInfo->pCtx, &pInfo->resultRowInfo, pInfo->rowCellInfoOffset); + pInfo->pRes->info.rows = getNumOfResult(pRuntimeEnv, pInfo->pCtx, pOperator->numOfOutput); - bool hasMoreBlock = true; - int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); - SQueryCostInfo *summary = &pRuntimeEnv->summary; - while ((hasMoreBlock = tsdbNextDataBlock(pQueryHandle)) == true) { - summary->totalBlocks += 1; + return pInfo->pRes; +} - if (isQueryKilled(pQInfo)) { - longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); - } +static SSDataBlock* doSTableAggregate(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; + } - tsdbRetrieveDataBlockInfo(pQueryHandle, &blockInfo); - STableQueryInfo **pTableQueryInfo = - (STableQueryInfo **) taosHashGet(pQInfo->tableqinfoGroupInfo.map, &blockInfo.tid, sizeof(blockInfo.tid)); - if (pTableQueryInfo == NULL) { - break; - } + SAggOperatorInfo* pAggInfo = pOperator->info; + SOptrBasicInfo* pInfo = &pAggInfo->binfo; - pQuery->current = *pTableQueryInfo; - doTableQueryInfoTimeWindowCheck(pQuery, *pTableQueryInfo); + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; - if (pRuntimeEnv->hasTagResults) { - setTagVal(pRuntimeEnv, pQuery->current->pTable); - } + if (pOperator->status == OP_RES_TO_RETURN) { + toSSDataBlock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pInfo->pRes); - if (pQuery->prjInfo.vgroupLimit > 0 && pQuery->current->resInfo.size > pQuery->prjInfo.vgroupLimit) { - pQuery->current->lastKey = - QUERY_IS_ASC_QUERY(pQuery) ? blockInfo.window.ekey + step : blockInfo.window.skey + step; - continue; - } + if (pInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pRuntimeEnv->groupResInfo)) { + pOperator->status = OP_EXEC_DONE; + } - // it is a super table ordered projection query, check for the number of output for each vgroup - if (pQuery->prjInfo.vgroupLimit > 0 && pQuery->rec.rows >= pQuery->prjInfo.vgroupLimit) { - if (QUERY_IS_ASC_QUERY(pQuery) && blockInfo.window.skey >= pQuery->prjInfo.ts) { - pQuery->current->lastKey = - QUERY_IS_ASC_QUERY(pQuery) ? blockInfo.window.ekey + step : blockInfo.window.skey + step; - continue; - } else if (!QUERY_IS_ASC_QUERY(pQuery) && blockInfo.window.ekey <= pQuery->prjInfo.ts) { - pQuery->current->lastKey = - QUERY_IS_ASC_QUERY(pQuery) ? blockInfo.window.ekey + step : blockInfo.window.skey + step; - continue; - } - } + return pInfo->pRes; + } - uint32_t status = 0; - SDataStatis *pStatis = NULL; - SArray *pDataBlock = NULL; + SQuery* pQuery = pRuntimeEnv->pQuery; + int32_t order = pQuery->order.order; - int32_t ret = loadDataBlockOnDemand(pRuntimeEnv, &pQuery->current->resInfo, pQueryHandle, &blockInfo, - &pStatis, &pDataBlock, &status); - if (ret != TSDB_CODE_SUCCESS) { - break; - } + SOperatorInfo* upstream = pOperator->upstream; - if(status == BLK_DATA_DISCARD) { - pQuery->current->lastKey = - QUERY_IS_ASC_QUERY(pQuery) ? blockInfo.window.ekey + step : blockInfo.window.skey + step; - continue; - } + while(1) { + SSDataBlock* pBlock = upstream->exec(upstream); + if (pBlock == NULL) { + break; + } - ensureOutputBuffer(pRuntimeEnv, blockInfo.rows); - int64_t prev = getNumOfResult(pRuntimeEnv); + setTagValue(pOperator, pRuntimeEnv->pQuery->current->pTable, pInfo->pCtx, pOperator->numOfOutput); - pQuery->pos = QUERY_IS_ASC_QUERY(pQuery) ? 0 : blockInfo.rows - 1; - int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, &blockInfo, pStatis, binarySearchForKey, pDataBlock); + if (upstream->operatorType == OP_DataBlocksOptScan) { + STableScanInfo* pScanInfo = upstream->info; + order = getTableScanOrder(pScanInfo); + } - summary->totalRows += blockInfo.rows; - qDebug("QInfo:%p check data block, brange:%" PRId64 "-%" PRId64 ", numOfRows:%d, numOfRes:%d, lastKey:%" PRId64, - GET_QINFO_ADDR(pRuntimeEnv), blockInfo.window.skey, blockInfo.window.ekey, blockInfo.rows, numOfRes, - pQuery->current->lastKey); + // the pDataBlock are always the same one, no need to call this again + setInputDataBlock(pOperator, pInfo->pCtx, pBlock, order); - pQuery->rec.rows = getNumOfResult(pRuntimeEnv); + TSKEY key = QUERY_IS_ASC_QUERY(pQuery)? pBlock->info.window.ekey + 1:pBlock->info.window.skey-1; + setExecutionContext(pRuntimeEnv, pInfo, pOperator->numOfOutput, pQuery->current->groupIndex, key); + doAggregateImpl(pOperator, pQuery->window.skey, pInfo->pCtx, pBlock); + } - int64_t inc = pQuery->rec.rows - prev; - pQuery->current->resInfo.size += (int32_t) inc; + pOperator->status = OP_RES_TO_RETURN; + closeAllResultRows(&pInfo->resultRowInfo); - // the flag may be set by tableApplyFunctionsOnBlock, clear it here - CLEAR_QUERY_STATUS(pQuery, QUERY_COMPLETED); + updateNumOfRowsInResultRows(pRuntimeEnv, pInfo->pCtx, pOperator->numOfOutput, &pInfo->resultRowInfo, + pInfo->rowCellInfoOffset); - updateTableIdInfo(pQuery, pQInfo->arrTableIdInfo); + initGroupResInfo(&pRuntimeEnv->groupResInfo, &pInfo->resultRowInfo); - if (pQuery->prjInfo.vgroupLimit >= 0) { - if (((pQuery->rec.rows + pQuery->rec.total) < pQuery->prjInfo.vgroupLimit) || ((pQuery->rec.rows + pQuery->rec.total) > pQuery->prjInfo.vgroupLimit && prev < pQuery->prjInfo.vgroupLimit)) { - if (QUERY_IS_ASC_QUERY(pQuery) && pQuery->prjInfo.ts < blockInfo.window.ekey) { - pQuery->prjInfo.ts = blockInfo.window.ekey; - } else if (!QUERY_IS_ASC_QUERY(pQuery) && pQuery->prjInfo.ts > blockInfo.window.skey) { - pQuery->prjInfo.ts = blockInfo.window.skey; - } - } - } else { - // the limitation of output result is reached, set the query completed - skipResults(pRuntimeEnv); - if (limitOperator(pQuery, pQInfo)) { - SET_STABLE_QUERY_OVER(pQInfo); - break; - } - } + toSSDataBlock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pInfo->pRes); + if (pInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pRuntimeEnv->groupResInfo)) { + pOperator->status = OP_EXEC_DONE; + } - // while the output buffer is full or limit/offset is applied, query may be paused here - if (Q_STATUS_EQUAL(pQuery->status, QUERY_RESBUF_FULL|QUERY_COMPLETED)) { - break; - } - } + return pInfo->pRes; +} - if (!hasMoreBlock) { - setQueryStatus(pQuery, QUERY_COMPLETED); - SET_STABLE_QUERY_OVER(pQInfo); - } - } else { - /* - * the following two cases handled here. - * 1. ts-comp query, and 2. the super table projection query with different query time range for each table. - * If the subgroup index is larger than 0, results generated by group by tbname,k is existed. - * we need to return it to client in the first place. - */ - if (hasRemainData(&pQInfo->groupResInfo)) { - copyToOutputBuf(pQInfo, &pRuntimeEnv->resultRowInfo); - pQuery->rec.total += pQuery->rec.rows; +static SSDataBlock* doArithmeticOperation(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; - if (pQuery->rec.rows > 0) { - return; - } + SArithOperatorInfo* pArithInfo = pOperator->info; + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; + SOptrBasicInfo *pInfo = &pArithInfo->binfo; + + SSDataBlock* pRes = pInfo->pRes; + int32_t order = pRuntimeEnv->pQuery->order.order; + + pRes->info.rows = 0; + + while(1) { + SSDataBlock* pBlock = pOperator->upstream->exec(pOperator->upstream); + if (pBlock == NULL) { + setQueryStatus(pRuntimeEnv, QUERY_COMPLETED); + break; } - // all data have returned already - if (pQInfo->tableIndex >= pQInfo->tableqinfoGroupInfo.numOfTables) { - return; + STableQueryInfo* pTableQueryInfo = pRuntimeEnv->pQuery->current; + + // todo dynamic set tags + setTagValue(pOperator, pTableQueryInfo->pTable, pInfo->pCtx, pOperator->numOfOutput); + + // the pDataBlock are always the same one, no need to call this again + setInputDataBlock(pOperator, pInfo->pCtx, pBlock, order); + updateOutputBuf(pArithInfo, pBlock->info.rows); + + arithmeticApplyFunctions(pRuntimeEnv, pInfo->pCtx, pOperator->numOfOutput); + updateTableIdInfo(pTableQueryInfo, pBlock, pRuntimeEnv->pTableRetrieveTsMap, order); + + pRes->info.rows = getNumOfResult(pRuntimeEnv, pInfo->pCtx, pOperator->numOfOutput); + if (pRes->info.rows >= pRuntimeEnv->resultInfo.threshold) { + break; } + } - resetDefaultResInfoOutputBuf(pRuntimeEnv); - resetResultRowInfo(pRuntimeEnv, &pRuntimeEnv->resultRowInfo); + clearNumOfRes(pInfo->pCtx, pOperator->numOfOutput); + return (pInfo->pRes->info.rows > 0)? pInfo->pRes:NULL; +} - SArray *group = GET_TABLEGROUP(pQInfo, 0); - assert(taosArrayGetSize(group) == pQInfo->tableqinfoGroupInfo.numOfTables && - 1 == taosArrayGetSize(pQInfo->tableqinfoGroupInfo.pGroupList)); +static SSDataBlock* doLimit(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; + } - while (pQInfo->tableIndex < pQInfo->tableqinfoGroupInfo.numOfTables) { - if (isQueryKilled(pQInfo)) { - longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); - } + SLimitOperatorInfo* pInfo = pOperator->info; - pQuery->current = taosArrayGetP(group, pQInfo->tableIndex); - if (!multiTableMultioutputHelper(pQInfo, pQInfo->tableIndex)) { - pQInfo->tableIndex++; - continue; - } + SSDataBlock* pBlock = pOperator->upstream->exec(pOperator->upstream); + if (pBlock == NULL) { + setQueryStatus(pOperator->pRuntimeEnv, QUERY_COMPLETED); + pOperator->status = OP_EXEC_DONE; + return NULL; + } - // TODO handle the limit offset problem - if (pQuery->numOfFilterCols == 0 && pQuery->limit.offset > 0) { - if (Q_STATUS_EQUAL(pQuery->status, QUERY_COMPLETED)) { - pQInfo->tableIndex++; - continue; - } - } + if (pInfo->total + pBlock->info.rows >= pInfo->limit) { + pBlock->info.rows = (int32_t) (pInfo->limit - pInfo->total); - scanOneTableDataBlocks(pRuntimeEnv, pQuery->current->lastKey); - skipResults(pRuntimeEnv); + pInfo->total = pInfo->limit; - // the limitation of output result is reached, set the query completed - if (limitOperator(pQuery, pQInfo)) { - SET_STABLE_QUERY_OVER(pQInfo); - break; - } + setQueryStatus(pOperator->pRuntimeEnv, QUERY_COMPLETED); + pOperator->status = OP_EXEC_DONE; + } else { + pInfo->total += pBlock->info.rows; + } - // enable execution for next table, when handling the projection query - enableExecutionForNextTable(pRuntimeEnv); - - if (Q_STATUS_EQUAL(pQuery->status, QUERY_COMPLETED)) { - /* - * query range is identical in terms of all meters involved in query, - * so we need to restore them at the *beginning* of query on each meter, - * not the consecutive query on meter on which is aborted due to buffer limitation - * to ensure that, we can reset the query range once query on a meter is completed. - */ - pQInfo->tableIndex++; - updateTableIdInfo(pQuery, pQInfo->arrTableIdInfo); - - // if the buffer is full or group by each table, we need to jump out of the loop - if (Q_STATUS_EQUAL(pQuery->status, QUERY_RESBUF_FULL)) { - break; - } + return pBlock; +} - if (pRuntimeEnv->pTsBuf != NULL) { - pRuntimeEnv->cur = pRuntimeEnv->pTsBuf->cur; - } +// TODO add log +static SSDataBlock* doOffset(void* param) { + SOperatorInfo *pOperator = (SOperatorInfo *)param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; + } - } else { - // all data in the result buffer are skipped due to the offset, continue to retrieve data from current meter - if (pQuery->rec.rows == 0) { - assert(!Q_STATUS_EQUAL(pQuery->status, QUERY_RESBUF_FULL)); - continue; - } else { - // buffer is full, wait for the next round to retrieve data from current meter - assert(Q_STATUS_EQUAL(pQuery->status, QUERY_RESBUF_FULL)); - break; - } - } - } + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; - if (pQInfo->tableIndex >= pQInfo->tableqinfoGroupInfo.numOfTables) { - setQueryStatus(pQuery, QUERY_COMPLETED); + while (1) { + SSDataBlock *pBlock = pOperator->upstream->exec(pOperator->upstream); + if (pBlock == NULL) { + setQueryStatus(pRuntimeEnv, QUERY_COMPLETED); + pOperator->status = OP_EXEC_DONE; + return NULL; } - /* - * 1. super table projection query, group-by on normal columns query, ts-comp query - * 2. point interpolation query, last row query - * - * group-by on normal columns query and last_row query do NOT invoke the finalizer here, - * since the finalize stage will be done at the client side. - * - * projection query, point interpolation query do not need the finalizer. - * - * Only the ts-comp query requires the finalizer function to be executed here. - */ - if (isTsCompQuery(pQuery)) { - finalizeQueryResult(pRuntimeEnv); - } + if (pRuntimeEnv->currentOffset == 0) { + return pBlock; + } else if (pRuntimeEnv->currentOffset >= pBlock->info.rows) { + pRuntimeEnv->currentOffset -= pBlock->info.rows; + } else { + int32_t remain = (int32_t)(pBlock->info.rows - pRuntimeEnv->currentOffset); + pBlock->info.rows = remain; - if (pRuntimeEnv->pTsBuf != NULL) { - pRuntimeEnv->cur = pRuntimeEnv->pTsBuf->cur; - } + for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) { + SColumnInfoData *pColInfoData = taosArrayGet(pBlock->pDataBlock, i); + + int16_t bytes = pColInfoData->info.bytes; + memmove(pColInfoData->pData, pColInfoData->pData + bytes * pRuntimeEnv->currentOffset, remain * bytes); + } - qDebug("QInfo %p numOfTables:%" PRIu64 ", index:%d, numOfGroups:%" PRIzu ", %" PRId64 - " points returned, total:%" PRId64 ", offset:%" PRId64, - pQInfo, (uint64_t)pQInfo->tableqinfoGroupInfo.numOfTables, pQInfo->tableIndex, numOfGroups, pQuery->rec.rows, - pQuery->rec.total, pQuery->limit.offset); + pRuntimeEnv->currentOffset = 0; + return pBlock; + } } } -static int32_t doSaveContext(SQInfo *pQInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery * pQuery = pRuntimeEnv->pQuery; +static SSDataBlock* doIntervalAgg(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; + } - SET_REVERSE_SCAN_FLAG(pRuntimeEnv); - SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); - SWITCH_ORDER(pQuery->order.order); + STableIntervalOperatorInfo* pIntervalInfo = pOperator->info; - if (pRuntimeEnv->pTsBuf != NULL) { - SWITCH_ORDER(pRuntimeEnv->pTsBuf->cur.order); - } + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; + if (pOperator->status == OP_RES_TO_RETURN) { + toSSDataBlock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pIntervalInfo->pRes); - STsdbQueryCond cond = createTsdbQueryCond(pQuery, &pQuery->window); + if (pIntervalInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pRuntimeEnv->groupResInfo)) { + pOperator->status = OP_EXEC_DONE; + } - // clean unused handle - if (pRuntimeEnv->pSecQueryHandle != NULL) { - tsdbCleanupQueryHandle(pRuntimeEnv->pSecQueryHandle); + return pIntervalInfo->pRes; } - setQueryStatus(pQuery, QUERY_NOT_COMPLETED); - switchCtxOrder(pRuntimeEnv); - disableFuncInReverseScan(pQInfo); - setupQueryRangeForReverseScan(pQInfo); + SQuery* pQuery = pRuntimeEnv->pQuery; + int32_t order = pQuery->order.order; + STimeWindow win = pQuery->window; - pRuntimeEnv->prevGroupId = INT32_MIN; - pRuntimeEnv->pSecQueryHandle = tsdbQueryTables(pQInfo->tsdb, &cond, &pQInfo->tableGroupInfo, pQInfo, &pQInfo->memRef); - return (pRuntimeEnv->pSecQueryHandle == NULL)? -1:0; -} + SOperatorInfo* upstream = pOperator->upstream; -static void doRestoreContext(SQInfo *pQInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery * pQuery = pRuntimeEnv->pQuery; + while(1) { + SSDataBlock* pBlock = upstream->exec(upstream); + if (pBlock == NULL) { + break; + } - SWAP(pQuery->window.skey, pQuery->window.ekey, TSKEY); - SWITCH_ORDER(pQuery->order.order); + // the pDataBlock are always the same one, no need to call this again + setInputDataBlock(pOperator, pIntervalInfo->pCtx, pBlock, pQuery->order.order); + hashIntervalAgg(pOperator, &pIntervalInfo->resultRowInfo, pBlock, 0); + } - if (pRuntimeEnv->pTsBuf != NULL) { - SWITCH_ORDER(pRuntimeEnv->pTsBuf->cur.order); + // restore the value + pQuery->order.order = order; + pQuery->window = win; + + pOperator->status = OP_RES_TO_RETURN; + closeAllResultRows(&pIntervalInfo->resultRowInfo); + setQueryStatus(pRuntimeEnv, QUERY_COMPLETED); + finalizeQueryResult(pOperator, pIntervalInfo->pCtx, &pIntervalInfo->resultRowInfo, pIntervalInfo->rowCellInfoOffset); + + initGroupResInfo(&pRuntimeEnv->groupResInfo, &pIntervalInfo->resultRowInfo); + toSSDataBlock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pIntervalInfo->pRes); + + if (pIntervalInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pRuntimeEnv->groupResInfo)) { + pOperator->status = OP_EXEC_DONE; } - switchCtxOrder(pRuntimeEnv); - SET_MASTER_SCAN_FLAG(pRuntimeEnv); + return pIntervalInfo->pRes->info.rows == 0? NULL:pIntervalInfo->pRes; } -static void doCloseAllTimeWindow(SQInfo *pQInfo) { - SQuery *pQuery = pQInfo->runtimeEnv.pQuery; +static SSDataBlock* doSTableIntervalAgg(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; + } - if (QUERY_IS_INTERVAL_QUERY(pQuery)) { - size_t numOfGroup = GET_NUM_OF_TABLEGROUP(pQInfo); - for (int32_t i = 0; i < numOfGroup; ++i) { - SArray *group = GET_TABLEGROUP(pQInfo, i); - - size_t num = taosArrayGetSize(group); - for (int32_t j = 0; j < num; ++j) { - STableQueryInfo* item = taosArrayGetP(group, j); - closeAllResultRows(&item->resInfo); - } + STableIntervalOperatorInfo* pIntervalInfo = pOperator->info; + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; + + if (pOperator->status == OP_RES_TO_RETURN) { + copyToSDataBlock(pRuntimeEnv, 3000, pIntervalInfo->pRes, pIntervalInfo->rowCellInfoOffset); + if (pIntervalInfo->pRes->info.rows == 0 || !hasRemainData(&pRuntimeEnv->groupResInfo)) { + pOperator->status = OP_EXEC_DONE; } - } else { // close results for group result - closeAllResultRows(&pQInfo->runtimeEnv.resultRowInfo); + + return pIntervalInfo->pRes; } -} -static void multiTableQueryProcess(SQInfo *pQInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery *pQuery = pRuntimeEnv->pQuery; + SQuery* pQuery = pRuntimeEnv->pQuery; + int32_t order = pQuery->order.order; - if (Q_STATUS_EQUAL(pQuery->status, QUERY_COMPLETED)) { - if (QUERY_IS_INTERVAL_QUERY(pQuery)) { - copyResToQueryResultBuf(pQInfo, pQuery); - } else { - copyToOutputBuf(pQInfo, &pRuntimeEnv->resultRowInfo); + SOperatorInfo* upstream = pOperator->upstream; + + while(1) { + SSDataBlock* pBlock = upstream->exec(upstream); + if (pBlock == NULL) { + break; } - qDebug("QInfo:%p current:%"PRId64", total:%"PRId64, pQInfo, pQuery->rec.rows, pQuery->rec.total); - return; + // the pDataBlock are always the same one, no need to call this again + STableQueryInfo* pTableQueryInfo = pRuntimeEnv->pQuery->current; + + setTagValue(pOperator, pTableQueryInfo->pTable, pIntervalInfo->pCtx, pOperator->numOfOutput); + setInputDataBlock(pOperator, pIntervalInfo->pCtx, pBlock, pQuery->order.order); + setIntervalQueryRange(pRuntimeEnv, pBlock->info.window.skey); + + hashIntervalAgg(pOperator, &pTableQueryInfo->resInfo, pBlock, pTableQueryInfo->groupIndex); } - qDebug("QInfo:%p query start, qrange:%" PRId64 "-%" PRId64 ", order:%d, forward scan start", pQInfo, - pQuery->window.skey, pQuery->window.ekey, pQuery->order.order); + pOperator->status = OP_RES_TO_RETURN; + pQuery->order.order = order; // TODO : restore the order + doCloseAllTimeWindow(pRuntimeEnv); + setQueryStatus(pRuntimeEnv, QUERY_COMPLETED); - // do check all qualified data blocks - int64_t el = scanMultiTableDataBlocks(pQInfo); - qDebug("QInfo:%p master scan completed, elapsed time: %" PRId64 "ms, reverse scan start", pQInfo, el); + copyToSDataBlock(pRuntimeEnv, 3000, pIntervalInfo->pRes, pIntervalInfo->rowCellInfoOffset); + if (pIntervalInfo->pRes->info.rows == 0 || !hasRemainData(&pRuntimeEnv->groupResInfo)) { + pOperator->status = OP_EXEC_DONE; + } - // query error occurred or query is killed, abort current execution - if (pQInfo->code != TSDB_CODE_SUCCESS || isQueryKilled(pQInfo)) { - qDebug("QInfo:%p query killed or error occurred, code:%s, abort", pQInfo, tstrerror(pQInfo->code)); - longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); + return pIntervalInfo->pRes; +} + +static SSDataBlock* doSessionWindowAgg(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; } - // close all time window results - doCloseAllTimeWindow(pQInfo); + SSWindowOperatorInfo* pWindowInfo = pOperator->info; + SOptrBasicInfo* pBInfo = &pWindowInfo->binfo; - if (needReverseScan(pQuery)) { - int32_t code = doSaveContext(pQInfo); - if (code == TSDB_CODE_SUCCESS) { - el = scanMultiTableDataBlocks(pQInfo); - qDebug("QInfo:%p reversed scan completed, elapsed time: %" PRId64 "ms", pQInfo, el); - doRestoreContext(pQInfo); - } else { - pQInfo->code = code; + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; + if (pOperator->status == OP_RES_TO_RETURN) { + toSSDataBlock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pBInfo->pRes); + + if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pRuntimeEnv->groupResInfo)) { + pOperator->status = OP_EXEC_DONE; } - } else { - qDebug("QInfo:%p no need to do reversed scan, query completed", pQInfo); + + return pBInfo->pRes; } - setQueryStatus(pQuery, QUERY_COMPLETED); + SQuery* pQuery = pRuntimeEnv->pQuery; + int32_t order = pQuery->order.order; + STimeWindow win = pQuery->window; - if (pQInfo->code != TSDB_CODE_SUCCESS || isQueryKilled(pQInfo)) { - qDebug("QInfo:%p query killed or error occurred, code:%s, abort", pQInfo, tstrerror(pQInfo->code)); - //TODO finalizeQueryResult may cause SEGSEV, since the memory may not allocated yet, add a cleanup function instead - longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); + SOperatorInfo* upstream = pOperator->upstream; + + while(1) { + SSDataBlock* pBlock = upstream->exec(upstream); + if (pBlock == NULL) { + break; + } + + // the pDataBlock are always the same one, no need to call this again + setInputDataBlock(pOperator, pBInfo->pCtx, pBlock, pQuery->order.order); + doSessionWindowAggImpl(pOperator, pWindowInfo, pBlock); } - if (QUERY_IS_INTERVAL_QUERY(pQuery) || isSumAvgRateQuery(pQuery)) { - copyResToQueryResultBuf(pQInfo, pQuery); - } else { // not a interval query - initGroupResInfo(&pQInfo->groupResInfo, &pRuntimeEnv->resultRowInfo, 0); - copyToOutputBuf(pQInfo, &pRuntimeEnv->resultRowInfo); + // restore the value + pQuery->order.order = order; + pQuery->window = win; + + pOperator->status = OP_RES_TO_RETURN; + closeAllResultRows(&pBInfo->resultRowInfo); + setQueryStatus(pRuntimeEnv, QUERY_COMPLETED); + finalizeQueryResult(pOperator, pBInfo->pCtx, &pBInfo->resultRowInfo, pBInfo->rowCellInfoOffset); + + initGroupResInfo(&pRuntimeEnv->groupResInfo, &pBInfo->resultRowInfo); + toSSDataBlock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pBInfo->pRes); + + if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pRuntimeEnv->groupResInfo)) { + pOperator->status = OP_EXEC_DONE; } - // handle the limitation of output buffer - qDebug("QInfo:%p points returned:%" PRId64 ", total:%" PRId64, pQInfo, pQuery->rec.rows, pQuery->rec.total + pQuery->rec.rows); + return pBInfo->pRes->info.rows == 0? NULL:pBInfo->pRes; } -static char *getArithemicInputSrc(void *param, const char *name, int32_t colId) { - SArithmeticSupport *pSupport = (SArithmeticSupport *) param; - SExprInfo* pExprInfo = (SExprInfo*) pSupport->exprList; +static SSDataBlock* hashGroupbyAggregate(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; + } + + SGroupbyOperatorInfo *pInfo = pOperator->info; + + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; + if (pOperator->status == OP_RES_TO_RETURN) { + toSSDataBlock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pInfo->binfo.pRes); + + if (pInfo->binfo.pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pRuntimeEnv->groupResInfo)) { + pOperator->status = OP_EXEC_DONE; + } + + return pInfo->binfo.pRes; + } + + SOperatorInfo* upstream = pOperator->upstream; - int32_t index = -1; - for (int32_t i = 0; i < pSupport->numOfCols; ++i) { - if (colId == pExprInfo[i].base.resColId) { - index = i; + while(1) { + SSDataBlock* pBlock = upstream->exec(upstream); + if (pBlock == NULL) { break; } + + // the pDataBlock are always the same one, no need to call this again + setInputDataBlock(pOperator, pInfo->binfo.pCtx, pBlock, pRuntimeEnv->pQuery->order.order); + setTagValue(pOperator, pRuntimeEnv->pQuery->current->pTable, pInfo->binfo.pCtx, pOperator->numOfOutput); + if (pInfo->colIndex == -1) { + pInfo->colIndex = getGroupbyColumnIndex(pRuntimeEnv->pQuery->pGroupbyExpr, pBlock); + } + + doHashGroupbyAgg(pOperator, pInfo, pBlock); } - assert(index >= 0 && index < pSupport->numOfCols); - return pSupport->data[index] + pSupport->offset * pExprInfo[index].bytes; -} + pOperator->status = OP_RES_TO_RETURN; + closeAllResultRows(&pInfo->binfo.resultRowInfo); + setQueryStatus(pRuntimeEnv, QUERY_COMPLETED); -static void doSecondaryArithmeticProcess(SQuery* pQuery) { - if (pQuery->numOfExpr2 == 0) { - return; + if (!pRuntimeEnv->pQuery->stableQuery) { // finalize include the update of result rows + finalizeQueryResult(pOperator, pInfo->binfo.pCtx, &pInfo->binfo.resultRowInfo, pInfo->binfo.rowCellInfoOffset); + } else { + updateNumOfRowsInResultRows(pRuntimeEnv, pInfo->binfo.pCtx, pOperator->numOfOutput, &pInfo->binfo.resultRowInfo, pInfo->binfo.rowCellInfoOffset); } - SArithmeticSupport arithSup = {0}; - tFilePage **data = calloc(pQuery->numOfExpr2, POINTER_BYTES); - for (int32_t i = 0; i < pQuery->numOfExpr2; ++i) { - int32_t bytes = pQuery->pExpr2[i].bytes; - data[i] = (tFilePage *)malloc((size_t)(bytes * pQuery->rec.rows) + sizeof(tFilePage)); + initGroupResInfo(&pRuntimeEnv->groupResInfo, &pInfo->binfo.resultRowInfo); + toSSDataBlock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pInfo->binfo.pRes); + + if (pInfo->binfo.pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pRuntimeEnv->groupResInfo)) { + pOperator->status = OP_EXEC_DONE; } - arithSup.offset = 0; - arithSup.numOfCols = (int32_t)pQuery->numOfOutput; - arithSup.exprList = pQuery->pExpr1; - arithSup.data = calloc(arithSup.numOfCols, POINTER_BYTES); + return pInfo->binfo.pRes; +} - for (int32_t k = 0; k < arithSup.numOfCols; ++k) { - arithSup.data[k] = pQuery->sdata[k]->data; +static SSDataBlock* doFill(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; } - for (int i = 0; i < pQuery->numOfExpr2; ++i) { - SExprInfo *pExpr = &pQuery->pExpr2[i]; + SFillOperatorInfo *pInfo = pOperator->info; + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; - // calculate the result from several other columns - SSqlFuncMsg* pSqlFunc = &pExpr->base; - if (pSqlFunc->functionId != TSDB_FUNC_ARITHM) { + if (taosFillHasMoreResults(pInfo->pFillInfo)) { + doFillTimeIntervalGapsInResults(pInfo->pFillInfo, pInfo->pRes, (int32_t)pRuntimeEnv->resultInfo.capacity); + return pInfo->pRes; + } - for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { - if (pSqlFunc->functionId == pQuery->pExpr1[j].base.functionId && - pSqlFunc->colInfo.colId == pQuery->pExpr1[j].base.colInfo.colId) { - memcpy(data[i]->data, pQuery->sdata[j]->data, (size_t)(pQuery->pExpr1[j].bytes * pQuery->rec.rows)); - break; - } + while(1) { + SSDataBlock *pBlock = pOperator->upstream->exec(pOperator->upstream); + if (pBlock == NULL) { + if (pInfo->totalInputRows == 0) { + pOperator->status = OP_EXEC_DONE; + return NULL; } + + taosFillSetStartInfo(pInfo->pFillInfo, 0, pRuntimeEnv->pQuery->window.ekey); } else { - arithSup.pArithExpr = pExpr; - arithmeticTreeTraverse(arithSup.pArithExpr->pExpr, (int32_t)pQuery->rec.rows, data[i]->data, &arithSup, TSDB_ORDER_ASC, - getArithemicInputSrc); + pInfo->totalInputRows += pBlock->info.rows; + + int64_t ekey = Q_STATUS_EQUAL(pRuntimeEnv->status, QUERY_COMPLETED)?pRuntimeEnv->pQuery->window.ekey:pBlock->info.window.ekey; + + taosFillSetStartInfo(pInfo->pFillInfo, pBlock->info.rows, ekey); + taosFillSetInputDataBlock(pInfo->pFillInfo, pBlock); } - } - for (int32_t i = 0; i < pQuery->numOfExpr2; ++i) { - memcpy(pQuery->sdata[i]->data, data[i]->data, (size_t)(pQuery->pExpr2[i].bytes * pQuery->rec.rows)); + doFillTimeIntervalGapsInResults(pInfo->pFillInfo, pInfo->pRes, pRuntimeEnv->resultInfo.capacity); + return (pInfo->pRes->info.rows > 0)? pInfo->pRes:NULL; } +} - for (int32_t i = 0; i < pQuery->numOfExpr2; ++i) { - tfree(data[i]); +// todo set the attribute of query scan count +static int32_t getNumOfScanTimes(SQuery* pQuery) { + for(int32_t i = 0; i < pQuery->numOfOutput; ++i) { + int32_t functionId = pQuery->pExpr1[i].base.functionId; + if (functionId == TSDB_FUNC_STDDEV || functionId == TSDB_FUNC_PERCT) { + return 2; + } } - tfree(data); - tfree(arithSup.data); + return 1; } -/* - * in each query, this function will be called only once, no retry for further result. - * - * select count(*)/top(field,k)/avg(field name) from table_name [where ts>now-1a]; - * select count(*) from table_name group by status_column; - */ -static void tableAggregationProcess(SQInfo *pQInfo, STableQueryInfo* pTableInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - - SQuery *pQuery = pRuntimeEnv->pQuery; - if (!pRuntimeEnv->topBotQuery && pQuery->limit.offset > 0) { // no need to execute, since the output will be ignore. +static void destroyOperatorInfo(SOperatorInfo* pOperator) { + if (pOperator == NULL) { return; } - scanOneTableDataBlocks(pRuntimeEnv, pTableInfo->lastKey); - finalizeQueryResult(pRuntimeEnv); + if (pOperator->cleanup != NULL) { + pOperator->cleanup(pOperator->info, pOperator->numOfOutput); + } - // since the numOfRows must be identical for all sql functions that are allowed to be executed simutaneously. - pQuery->rec.rows = getNumOfResult(pRuntimeEnv); - doSecondaryArithmeticProcess(pQuery); + destroyOperatorInfo(pOperator->upstream); + tfree(pOperator->info); + tfree(pOperator); +} - if (isQueryKilled(pQInfo)) { - longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); - } +static SOperatorInfo* createAggregateOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput) { + SAggOperatorInfo* pInfo = calloc(1, sizeof(SAggOperatorInfo)); - // TODO limit/offset refactor to be one operator - skipResults(pRuntimeEnv); - limitOperator(pQuery, pQInfo); + SQuery* pQuery = pRuntimeEnv->pQuery; + int32_t numOfRows = (int32_t)(GET_ROW_PARAM_FOR_MULTIOUTPUT(pQuery, pQuery->topBotQuery, pQuery->stableQuery)); + + pInfo->binfo.pRes = createOutputBuf(pExpr, numOfOutput, numOfRows); + pInfo->binfo.pCtx = createSQLFunctionCtx(pRuntimeEnv, pExpr, numOfOutput, &pInfo->binfo.rowCellInfoOffset); + + initResultRowInfo(&pInfo->binfo.resultRowInfo, 8, TSDB_DATA_TYPE_INT); + + pInfo->seed = rand(); + setDefaultOutputBuf(pRuntimeEnv, &pInfo->binfo, pInfo->seed); + + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + pOperator->name = "TableAggregate"; + pOperator->operatorType = OP_Aggregate; + pOperator->blockingOptr = true; + pOperator->status = OP_IN_EXECUTING; + pOperator->info = pInfo; + pOperator->upstream = upstream; + pOperator->pExpr = pExpr; + pOperator->numOfOutput = numOfOutput; + pOperator->pRuntimeEnv = pRuntimeEnv; + + pOperator->exec = doAggregate; + pOperator->cleanup = destroyBasicOperatorInfo; + return pOperator; } -static void tableProjectionProcess(SQInfo *pQInfo, STableQueryInfo* pTableInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; +static void doDestroyBasicInfo(SOptrBasicInfo* pInfo, int32_t numOfOutput) { + assert(pInfo != NULL); - // for ts_comp query, re-initialized is not allowed - SQuery *pQuery = pRuntimeEnv->pQuery; - if (!isTsCompQuery(pQuery)) { - resetDefaultResInfoOutputBuf(pRuntimeEnv); - } + destroySQLFunctionCtx(pInfo->pCtx, numOfOutput); + tfree(pInfo->rowCellInfoOffset); - // skip blocks without load the actual data block from file if no filter condition present - skipBlocks(&pQInfo->runtimeEnv); - if (pQuery->limit.offset > 0 && pQuery->numOfFilterCols == 0) { - setQueryStatus(pQuery, QUERY_COMPLETED); - return; - } + cleanupResultRowInfo(&pInfo->resultRowInfo); + pInfo->pRes = destroyOutputBuf(pInfo->pRes); +} - while (1) { - scanOneTableDataBlocks(pRuntimeEnv, pQuery->current->lastKey); - finalizeQueryResult(pRuntimeEnv); +static void destroyBasicOperatorInfo(void* param, int32_t numOfOutput) { + SOptrBasicInfo* pInfo = (SOptrBasicInfo*) param; + doDestroyBasicInfo(pInfo, numOfOutput); +} - pQuery->rec.rows = getNumOfResult(pRuntimeEnv); - if (pQuery->limit.offset > 0 && pQuery->numOfFilterCols > 0 && pQuery->rec.rows > 0) { - skipResults(pRuntimeEnv); - } +static void destroySFillOperatorInfo(void* param, int32_t numOfOutput) { + SFillOperatorInfo* pInfo = (SFillOperatorInfo*) param; + pInfo->pFillInfo = taosDestroyFillInfo(pInfo->pFillInfo); + pInfo->pRes = destroyOutputBuf(pInfo->pRes); +} - /* - * 1. if pQuery->size == 0, pQuery->limit.offset >= 0, still need to check data - * 2. if pQuery->size > 0, pQuery->limit.offset must be 0 - */ - if (pQuery->rec.rows > 0 || Q_STATUS_EQUAL(pQuery->status, QUERY_COMPLETED)) { - break; - } +static void destroyGroupbyOperatorInfo(void* param, int32_t numOfOutput) { + SGroupbyOperatorInfo* pInfo = (SGroupbyOperatorInfo*) param; + doDestroyBasicInfo(&pInfo->binfo, numOfOutput); + tfree(pInfo->prevData); +} - qDebug("QInfo:%p skip current result, offset:%" PRId64 ", next qrange:%" PRId64 "-%" PRId64, - pQInfo, pQuery->limit.offset, pQuery->current->lastKey, pQuery->current->win.ekey); +static void destroyArithOperatorInfo(void* param, int32_t numOfOutput) { + SArithOperatorInfo* pInfo = (SArithOperatorInfo*) param; + doDestroyBasicInfo(&pInfo->binfo, numOfOutput); +} - resetDefaultResInfoOutputBuf(pRuntimeEnv); - } +static void destroyTagScanOperatorInfo(void* param, int32_t numOfOutput) { + STagScanInfo* pInfo = (STagScanInfo*) param; + pInfo->pRes = destroyOutputBuf(pInfo->pRes); +} - limitOperator(pQuery, pQInfo); - if (Q_STATUS_EQUAL(pQuery->status, QUERY_RESBUF_FULL)) { - qDebug("QInfo:%p query paused due to output limitation, next qrange:%" PRId64 "-%" PRId64, pQInfo, - pQuery->current->lastKey, pQuery->window.ekey); - } else if (Q_STATUS_EQUAL(pQuery->status, QUERY_COMPLETED)) { - STableIdInfo tidInfo = createTableIdInfo(pQuery); - taosHashPut(pQInfo->arrTableIdInfo, &tidInfo.tid, sizeof(tidInfo.tid), &tidInfo, sizeof(STableIdInfo)); - } +SOperatorInfo* createMultiTableAggOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput) { + SAggOperatorInfo* pInfo = calloc(1, sizeof(SAggOperatorInfo)); - if (!isTsCompQuery(pQuery)) { - assert(pQuery->rec.rows <= pQuery->rec.capacity); - } + size_t tableGroup = GET_NUM_OF_TABLEGROUP(pRuntimeEnv); + + pInfo->binfo.pRes = createOutputBuf(pExpr, numOfOutput, (int32_t) tableGroup); + pInfo->binfo.pCtx = createSQLFunctionCtx(pRuntimeEnv, pExpr, numOfOutput, &pInfo->binfo.rowCellInfoOffset); + initResultRowInfo(&pInfo->binfo.resultRowInfo, (int32_t)tableGroup, TSDB_DATA_TYPE_INT); + + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + pOperator->name = "MultiTableAggregate"; + pOperator->operatorType = OP_MultiTableAggregate; + pOperator->blockingOptr = true; + pOperator->status = OP_IN_EXECUTING; + pOperator->info = pInfo; + pOperator->upstream = upstream; + pOperator->pExpr = pExpr; + pOperator->numOfOutput = numOfOutput; + pOperator->pRuntimeEnv = pRuntimeEnv; + + pOperator->exec = doSTableAggregate; + pOperator->cleanup = destroyBasicOperatorInfo; + + return pOperator; } -static void copyAndFillResult(SQInfo* pQInfo) { - SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery* pQuery = pRuntimeEnv->pQuery; +SOperatorInfo* createArithOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput) { + SArithOperatorInfo* pInfo = calloc(1, sizeof(SArithOperatorInfo)); - while(1) { - copyToOutputBuf(pQInfo, &pRuntimeEnv->resultRowInfo); - doSecondaryArithmeticProcess(pQuery); + pInfo->seed = rand(); + pInfo->bufCapacity = pRuntimeEnv->resultInfo.capacity; - TSKEY lastKey = 0; - if (!hasRemainData(&pQInfo->groupResInfo)) { - lastKey = pQuery->window.ekey; - } else { - lastKey = ((TSKEY*)pQuery->sdata[0]->data)[pQuery->rec.rows - 1]; - } + SOptrBasicInfo* pBInfo = &pInfo->binfo; + pBInfo->pRes = createOutputBuf(pExpr, numOfOutput, pInfo->bufCapacity); + pBInfo->pCtx = createSQLFunctionCtx(pRuntimeEnv, pExpr, numOfOutput, &pBInfo->rowCellInfoOffset); - assert(lastKey <= pQuery->window.ekey); + initResultRowInfo(&pBInfo->resultRowInfo, 8, TSDB_DATA_TYPE_INT); + setDefaultOutputBuf(pRuntimeEnv, pBInfo, pInfo->seed); - taosFillSetStartInfo(pRuntimeEnv->pFillInfo, (int32_t)pQuery->rec.rows, lastKey); - taosFillSetDataBlockFromFilePage(pRuntimeEnv->pFillInfo, (const tFilePage **)pQuery->sdata); + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + pOperator->name = "ArithmeticOperator"; + pOperator->operatorType = OP_Arithmetic; + pOperator->blockingOptr = false; + pOperator->status = OP_IN_EXECUTING; + pOperator->info = pInfo; + pOperator->upstream = upstream; + pOperator->pExpr = pExpr; + pOperator->numOfOutput = numOfOutput; + pOperator->pRuntimeEnv = pRuntimeEnv; - pQuery->rec.rows = doFillGapsInResults(pRuntimeEnv, (tFilePage **)pQuery->sdata); + pOperator->exec = doArithmeticOperation; + pOperator->cleanup = destroyArithOperatorInfo; - if (pQuery->rec.rows > 0) { - limitOperator(pQuery, pQInfo); - break; - } + return pOperator; +} - // here the pQuery->rec.rows == 0 - if (!hasRemainData(&pQInfo->groupResInfo) && !taosFillHasMoreResults(pRuntimeEnv->pFillInfo)) { - break; - } - } +SOperatorInfo* createLimitOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream) { + SLimitOperatorInfo* pInfo = calloc(1, sizeof(SLimitOperatorInfo)); + pInfo->limit = pRuntimeEnv->pQuery->limit.limit; + + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + + pOperator->name = "LimitOperator"; + pOperator->operatorType = OP_Limit; + pOperator->blockingOptr = false; + pOperator->status = OP_IN_EXECUTING; + pOperator->upstream = upstream; + pOperator->exec = doLimit; + pOperator->info = pInfo; + pOperator->pRuntimeEnv = pRuntimeEnv; + + return pOperator; } -// handle time interval query on table -static void tableIntervalProcess(SQInfo *pQInfo, STableQueryInfo* pTableInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &(pQInfo->runtimeEnv); - SQuery *pQuery = pRuntimeEnv->pQuery; +SOperatorInfo* createOffsetOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream) { + SOffsetOperatorInfo* pInfo = calloc(1, sizeof(SOffsetOperatorInfo)); - TSKEY newStartKey = QUERY_IS_ASC_QUERY(pQuery)? INT64_MIN:INT64_MAX; + pInfo->offset = pRuntimeEnv->pQuery->limit.offset; + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); - // skip blocks without load the actual data block from file if no filter condition present - if (!pRuntimeEnv->groupbyColumn) { - skipTimeInterval(pRuntimeEnv, &newStartKey); - if (pQuery->limit.offset > 0 && pQuery->numOfFilterCols == 0 && pRuntimeEnv->pFillInfo == NULL) { - setQueryStatus(pQuery, QUERY_COMPLETED); - return; - } - } + pOperator->name = "OffsetOperator"; + pOperator->operatorType = OP_Offset; + pOperator->blockingOptr = false; + pOperator->status = OP_IN_EXECUTING; + pOperator->upstream = upstream; + pOperator->exec = doOffset; + pOperator->info = pInfo; + pOperator->pRuntimeEnv = pRuntimeEnv; - scanOneTableDataBlocks(pRuntimeEnv, newStartKey); - finalizeQueryResult(pRuntimeEnv); + return pOperator; +} - // skip offset result rows - pQuery->rec.rows = 0; +SOperatorInfo* createTimeIntervalOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput) { + STableIntervalOperatorInfo* pInfo = calloc(1, sizeof(STableIntervalOperatorInfo)); - // not fill or no result generated during this query - if (pQuery->fillType == TSDB_FILL_NONE || pRuntimeEnv->resultRowInfo.size == 0 || isPointInterpoQuery(pQuery)) { - // all data scanned, the group by normal column can return - int32_t numOfClosed = numOfClosedResultRows(&pRuntimeEnv->resultRowInfo); - if (pQuery->limit.offset > numOfClosed || numOfClosed == 0) { - return; - } + pInfo->pCtx = createSQLFunctionCtx(pRuntimeEnv, pExpr, numOfOutput, &pInfo->rowCellInfoOffset); + pInfo->pRes = createOutputBuf(pExpr, numOfOutput, pRuntimeEnv->resultInfo.capacity); + initResultRowInfo(&pInfo->resultRowInfo, 8, TSDB_DATA_TYPE_INT); - initGroupResInfo(&pQInfo->groupResInfo, &pRuntimeEnv->resultRowInfo, (int32_t) pQuery->limit.offset); - copyToOutputBuf(pQInfo, &pRuntimeEnv->resultRowInfo); - doSecondaryArithmeticProcess(pQuery); + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); - limitOperator(pQuery, pQInfo); - } else { - initGroupResInfo(&pQInfo->groupResInfo, &pRuntimeEnv->resultRowInfo, 0); - copyAndFillResult(pQInfo); + pOperator->name = "TimeIntervalAggOperator"; + pOperator->operatorType = OP_TimeWindow; + pOperator->blockingOptr = true; + pOperator->status = OP_IN_EXECUTING; + pOperator->upstream = upstream; + pOperator->pExpr = pExpr; + pOperator->numOfOutput = numOfOutput; + pOperator->info = pInfo; + pOperator->pRuntimeEnv = pRuntimeEnv; + pOperator->exec = doIntervalAgg; + pOperator->cleanup = destroyBasicOperatorInfo; + + return pOperator; +} + +SOperatorInfo* createSWindowOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput) { + SSWindowOperatorInfo* pInfo = calloc(1, sizeof(SSWindowOperatorInfo)); + + pInfo->binfo.pCtx = createSQLFunctionCtx(pRuntimeEnv, pExpr, numOfOutput, &pInfo->binfo.rowCellInfoOffset); + pInfo->binfo.pRes = createOutputBuf(pExpr, numOfOutput, pRuntimeEnv->resultInfo.capacity); + initResultRowInfo(&pInfo->binfo.resultRowInfo, 8, TSDB_DATA_TYPE_INT); + + pInfo->prevTs = INT64_MIN; + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + + pOperator->name = "SessionWindowAggOperator"; + pOperator->operatorType = OP_SessionWindow; + pOperator->blockingOptr = true; + pOperator->status = OP_IN_EXECUTING; + pOperator->upstream = upstream; + pOperator->pExpr = pExpr; + pOperator->numOfOutput = numOfOutput; + pOperator->info = pInfo; + pOperator->pRuntimeEnv = pRuntimeEnv; + pOperator->exec = doSessionWindowAgg; + pOperator->cleanup = destroyBasicOperatorInfo; + + return pOperator; +} + +SOperatorInfo* createMultiTableTimeIntervalOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput) { + STableIntervalOperatorInfo* pInfo = calloc(1, sizeof(STableIntervalOperatorInfo)); + + pInfo->pCtx = createSQLFunctionCtx(pRuntimeEnv, pExpr, numOfOutput, &pInfo->rowCellInfoOffset); + pInfo->pRes = createOutputBuf(pExpr, numOfOutput, pRuntimeEnv->resultInfo.capacity); + initResultRowInfo(&pInfo->resultRowInfo, 8, TSDB_DATA_TYPE_INT); + + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + pOperator->name = "MultiTableTimeIntervalOperator"; + pOperator->operatorType = OP_MultiTableTimeInterval; + pOperator->blockingOptr = true; + pOperator->status = OP_IN_EXECUTING; + pOperator->upstream = upstream; + pOperator->pExpr = pExpr; + pOperator->numOfOutput = numOfOutput; + pOperator->info = pInfo; + pOperator->pRuntimeEnv = pRuntimeEnv; + + pOperator->exec = doSTableIntervalAgg; + pOperator->cleanup = destroyBasicOperatorInfo; + + return pOperator; +} + +SOperatorInfo* createGroupbyOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, int32_t numOfOutput) { + SGroupbyOperatorInfo* pInfo = calloc(1, sizeof(SGroupbyOperatorInfo)); + pInfo->colIndex = -1; // group by column index + + pInfo->binfo.pCtx = createSQLFunctionCtx(pRuntimeEnv, pExpr, numOfOutput, &pInfo->binfo.rowCellInfoOffset); + pInfo->binfo.pRes = createOutputBuf(pExpr, numOfOutput, pRuntimeEnv->resultInfo.capacity); + initResultRowInfo(&pInfo->binfo.resultRowInfo, 8, TSDB_DATA_TYPE_INT); + + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + pOperator->name = "GroupbyAggOperator"; + pOperator->blockingOptr = true; + pOperator->status = OP_IN_EXECUTING; + pOperator->operatorType = OP_Groupby; + pOperator->upstream = upstream; + pOperator->pExpr = pExpr; + pOperator->numOfOutput = numOfOutput; + pOperator->info = pInfo; + pOperator->pRuntimeEnv = pRuntimeEnv; + pOperator->exec = hashGroupbyAggregate; + pOperator->cleanup = destroyGroupbyOperatorInfo; + + return pOperator; +} + +SOperatorInfo* createFillOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SOperatorInfo* upstream, SExprInfo* pExpr, + int32_t numOfOutput) { + SFillOperatorInfo* pInfo = calloc(1, sizeof(SFillOperatorInfo)); + pInfo->pRes = createOutputBuf(pExpr, numOfOutput, pRuntimeEnv->resultInfo.capacity); + + { + SQuery* pQuery = pRuntimeEnv->pQuery; + SFillColInfo* pColInfo = createFillColInfo(pExpr, numOfOutput, pQuery->fillVal); + STimeWindow w = TSWINDOW_INITIALIZER; + + TSKEY sk = MIN(pQuery->window.skey, pQuery->window.ekey); + TSKEY ek = MAX(pQuery->window.skey, pQuery->window.ekey); + getAlignQueryTimeWindow(pQuery, pQuery->window.skey, sk, ek, &w); + + pInfo->pFillInfo = taosCreateFillInfo(pQuery->order.order, w.skey, 0, (int32_t)pRuntimeEnv->resultInfo.capacity, numOfOutput, + pQuery->interval.sliding, pQuery->interval.slidingUnit, (int8_t)pQuery->precision, + pQuery->fillType, pColInfo, pRuntimeEnv->qinfo); } + + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + + pOperator->name = "FillOperator"; + pOperator->blockingOptr = false; + pOperator->status = OP_IN_EXECUTING; + pOperator->operatorType = OP_Fill; + + pOperator->upstream = upstream; + pOperator->pExpr = pExpr; + pOperator->numOfOutput = numOfOutput; + pOperator->info = pInfo; + pOperator->pRuntimeEnv = pRuntimeEnv; + + pOperator->exec = doFill; + pOperator->cleanup = destroySFillOperatorInfo; + + return pOperator; } -void tableQueryImpl(SQInfo *pQInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery * pQuery = pRuntimeEnv->pQuery; +static SSDataBlock* doTagScan(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; + } - if (hasNotReturnedResults(pRuntimeEnv, &pQInfo->groupResInfo)) { - if (pQuery->fillType != TSDB_FILL_NONE && !isPointInterpoQuery(pQuery)) { - /* - * There are remain results that are not returned due to result interpolation - * So, we do keep in this procedure instead of launching retrieve procedure for next results. - */ - pQuery->rec.rows = doFillGapsInResults(pRuntimeEnv, (tFilePage **)pQuery->sdata); - if (pQuery->rec.rows > 0) { - limitOperator(pQuery, pQInfo); - qDebug("QInfo:%p current:%" PRId64 " returned, total:%" PRId64, pQInfo, pQuery->rec.rows, pQuery->rec.total); - } else { - copyAndFillResult(pQInfo); + SQueryRuntimeEnv* pRuntimeEnv = pOperator->pRuntimeEnv; + + int32_t maxNumOfTables = (int32_t)pRuntimeEnv->resultInfo.capacity; + + STagScanInfo *pInfo = pOperator->info; + SSDataBlock *pRes = pInfo->pRes; + + int32_t count = 0; + SArray* pa = GET_TABLEGROUP(pRuntimeEnv, 0); + + int32_t functionId = pOperator->pExpr[0].base.functionId; + if (functionId == TSDB_FUNC_TID_TAG) { // return the tags & table Id + SQuery* pQuery = pRuntimeEnv->pQuery; + assert(pQuery->numOfOutput == 1); + + SExprInfo* pExprInfo = &pOperator->pExpr[0]; + int32_t rsize = pExprInfo->bytes; + + count = 0; + + int16_t bytes = pExprInfo->bytes; + int16_t type = pExprInfo->type; + + for(int32_t i = 0; i < pQuery->numOfTags; ++i) { + if (pQuery->tagColList[i].colId == pExprInfo->base.colInfo.colId) { + bytes = pQuery->tagColList[i].bytes; + type = pQuery->tagColList[i].type; + break; } + } + + SColumnInfoData* pColInfo = taosArrayGet(pRes->pDataBlock, 0); + + while(pInfo->currentIndex < pInfo->totalTables && count < maxNumOfTables) { + int32_t i = pInfo->currentIndex++; + STableQueryInfo *item = taosArrayGetP(pa, i); + + char *output = pColInfo->pData + count * rsize; + varDataSetLen(output, rsize - VARSTR_HEADER_SIZE); + + output = varDataVal(output); + STableId* id = TSDB_TABLEID(item->pTable); + + *(int16_t *)output = 0; + output += sizeof(int16_t); + + *(int64_t *)output = id->uid; // memory align problem, todo serialize + output += sizeof(id->uid); - } else { - pQuery->rec.rows = 0; - assert(pRuntimeEnv->resultRowInfo.size > 0); - copyToOutputBuf(pQInfo, &pRuntimeEnv->resultRowInfo); - doSecondaryArithmeticProcess(pQuery); + *(int32_t *)output = id->tid; + output += sizeof(id->tid); - if (pQuery->rec.rows > 0) { - limitOperator(pQuery, pQInfo); - } + *(int32_t *)output = pQuery->vgId; + output += sizeof(pQuery->vgId); - if (pQuery->rec.rows > 0) { - qDebug("QInfo:%p %" PRId64 " rows returned from group results, total:%" PRId64 "", pQInfo, pQuery->rec.rows, - pQuery->rec.total); + char* data = NULL; + if (pExprInfo->base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) { + data = tsdbGetTableName(item->pTable); } else { - qDebug("QInfo:%p query over, %" PRId64 " rows are returned", pQInfo, pQuery->rec.total); + data = tsdbGetTableTagVal(item->pTable, pExprInfo->base.colInfo.colId, type, bytes); } - } - return; - } + doSetTagValueToResultBuf(output, data, type, bytes); + count += 1; + } - // number of points returned during this query - pQuery->rec.rows = 0; - int64_t st = taosGetTimestampUs(); + qDebug("QInfo:%p create (tableId, tag) info completed, rows:%d", pRuntimeEnv->qinfo, count); + } else if (functionId == TSDB_FUNC_COUNT) {// handle the "count(tbname)" query + SColumnInfoData* pColInfo = taosArrayGet(pRes->pDataBlock, 0); + *(int64_t*)pColInfo->pData = pInfo->totalTables; + count = 1; - assert(pQInfo->tableqinfoGroupInfo.numOfTables == 1); - SArray* g = GET_TABLEGROUP(pQInfo, 0); + pOperator->status = OP_EXEC_DONE; + qDebug("QInfo:%p create count(tbname) query, res:%d rows:1", pRuntimeEnv->qinfo, count); + } else { // return only the tags|table name etc. + SExprInfo* pExprInfo = pOperator->pExpr; // todo use the column list instead of exprinfo - STableQueryInfo* item = taosArrayGetP(g, 0); - pQuery->current = item; + count = 0; + while(pInfo->currentIndex < pInfo->totalTables && count < maxNumOfTables) { + int32_t i = pInfo->currentIndex++; - // group by normal column, sliding window query, interval query are handled by interval query processor - if (QUERY_IS_INTERVAL_QUERY(pQuery) || pRuntimeEnv->groupbyColumn) { // interval (down sampling operation) - tableIntervalProcess(pQInfo, item); - } else if (isFixedOutputQuery(pRuntimeEnv)) { - tableAggregationProcess(pQInfo, item); - } else { // diff/add/multiply/subtract/division - assert(pQuery->checkResultBuf == 1); - tableProjectionProcess(pQInfo, item); - } + STableQueryInfo* item = taosArrayGetP(pa, i); - // record the total elapsed time - pRuntimeEnv->summary.elapsedTime += (taosGetTimestampUs() - st); - assert(pQInfo->tableqinfoGroupInfo.numOfTables == 1); -} + char *data = NULL, *dst = NULL; + int16_t type = 0, bytes = 0; + for(int32_t j = 0; j < pOperator->numOfOutput; ++j) { + // not assign value in case of user defined constant output column + if (TSDB_COL_IS_UD_COL(pExprInfo[j].base.colInfo.flag)) { + continue; + } -void buildTableBlockDistResult(SQInfo *pQInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery *pQuery = pRuntimeEnv->pQuery; - pQuery->pos = 0; + SColumnInfoData* pColInfo = taosArrayGet(pRes->pDataBlock, j); + type = pExprInfo[j].type; + bytes = pExprInfo[j].bytes; - STableBlockDist *pTableBlockDist = calloc(1, sizeof(STableBlockDist)); - pTableBlockDist->dataBlockInfos = taosArrayInit(512, sizeof(SDataBlockInfo)); - pTableBlockDist->result = (char *)malloc(512); + if (pExprInfo[j].base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) { + data = tsdbGetTableName(item->pTable); + } else { + data = tsdbGetTableTagVal(item->pTable, pExprInfo[j].base.colInfo.colId, type, bytes); + } - TsdbQueryHandleT pQueryHandle = pRuntimeEnv->pQueryHandle; - SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER; - SSchema blockDistSchema = tGetBlockDistColumnSchema(); + dst = pColInfo->pData + count * pExprInfo[j].bytes; + doSetTagValueToResultBuf(dst, data, type, bytes); + } - int64_t startTime = taosGetTimestampUs(); - while (tsdbNextDataBlockWithoutMerge(pQueryHandle)) { - if (isQueryKilled(GET_QINFO_ADDR(pRuntimeEnv))) { - freeTableBlockDist(pTableBlockDist); - longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED); + count += 1; } - if (pTableBlockDist->firstSeekTimeUs == 0) { - pTableBlockDist->firstSeekTimeUs = taosGetTimestampUs() - startTime; - } - - tsdbRetrieveDataBlockInfo(pQueryHandle, &blockInfo); - taosArrayPush(pTableBlockDist->dataBlockInfos, &blockInfo); - } - if (terrno != TSDB_CODE_SUCCESS) { - freeTableBlockDist(pTableBlockDist); - longjmp(pRuntimeEnv->env, terrno); - } - - pTableBlockDist->numOfRowsInMemTable = tsdbGetNumOfRowsInMemTable(pQueryHandle); - - generateBlockDistResult(pTableBlockDist); - int type = -1; - assert(pQuery->numOfOutput == 1); - SExprInfo* pExprInfo = pQuery->pExpr1; - for (int32_t j = 0; j < pQuery->numOfOutput; j++) { - if (pExprInfo[j].base.colInfo.colId == TSDB_BLOCK_DIST_COLUMN_INDEX) { - type = blockDistSchema.type; + if (pInfo->currentIndex >= pInfo->totalTables) { + pOperator->status = OP_EXEC_DONE; } - assert(type == TSDB_DATA_TYPE_BINARY); - STR_WITH_SIZE_TO_VARSTR(pQuery->sdata[j]->data, pTableBlockDist->result, (VarDataLenT)strlen(pTableBlockDist->result)); - } - freeTableBlockDist(pTableBlockDist); + qDebug("QInfo:%p create tag values results completed, rows:%d", pRuntimeEnv->qinfo, count); + } - pQuery->rec.rows = 1; - setQueryStatus(pQuery, QUERY_COMPLETED); - return; + pRes->info.rows = count; + return (pRes->info.rows == 0)? NULL:pInfo->pRes; } -void stableQueryImpl(SQInfo *pQInfo) { - SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery *pQuery = pRuntimeEnv->pQuery; - pQuery->rec.rows = 0; +SOperatorInfo* createTagScanOperatorInfo(SQueryRuntimeEnv* pRuntimeEnv, SExprInfo* pExpr, int32_t numOfOutput) { + STagScanInfo* pInfo = calloc(1, sizeof(STagScanInfo)); + pInfo->pRes = createOutputBuf(pExpr, numOfOutput, pRuntimeEnv->resultInfo.capacity); - int64_t st = taosGetTimestampUs(); + size_t numOfGroup = GET_NUM_OF_TABLEGROUP(pRuntimeEnv); + assert(numOfGroup == 0 || numOfGroup == 1); - if (QUERY_IS_INTERVAL_QUERY(pQuery) || - (isFixedOutputQuery(pRuntimeEnv) && (!isPointInterpoQuery(pQuery)) && (!pRuntimeEnv->groupbyColumn))) { - multiTableQueryProcess(pQInfo); - } else { - assert(pQuery->checkResultBuf == 1 || isPointInterpoQuery(pQuery) || pRuntimeEnv->groupbyColumn); - sequentialTableProcess(pQInfo); - } + pInfo->totalTables = pRuntimeEnv->tableqinfoGroupInfo.numOfTables; + pInfo->currentIndex = 0; + + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + pOperator->name = "SeqTableTagScan"; + pOperator->operatorType = OP_TagScan; + pOperator->blockingOptr = false; + pOperator->status = OP_IN_EXECUTING; + pOperator->info = pInfo; + pOperator->exec = doTagScan; + pOperator->pExpr = pExpr; + pOperator->numOfOutput = numOfOutput; + pOperator->pRuntimeEnv = pRuntimeEnv; + pOperator->cleanup = destroyTagScanOperatorInfo; - // record the total elapsed time - pQInfo->runtimeEnv.summary.elapsedTime += (taosGetTimestampUs() - st); + return pOperator; } static int32_t getColumnIndexInSource(SQueryTableMsg *pQueryMsg, SSqlFuncMsg *pExprMsg, SColumnInfo* pTagCols) { @@ -5933,6 +5379,17 @@ static bool validateQueryMsg(SQueryTableMsg *pQueryMsg) { return false; } + if (pQueryMsg->sw.gap < 0 || pQueryMsg->sw.primaryColId != PRIMARYKEY_TIMESTAMP_COL_INDEX) { + qError("qmsg:%p illegal value of session window time %" PRId64, pQueryMsg, pQueryMsg->sw.gap); + return false; + } + + if (pQueryMsg->sw.gap > 0 && pQueryMsg->interval.interval > 0) { + qError("qmsg:%p illegal value of session window time %" PRId64" and interval value %"PRId64, pQueryMsg, + pQueryMsg->sw.gap, pQueryMsg->interval.interval); + return false; + } + if (pQueryMsg->numOfTables <= 0) { qError("qmsg:%p illegal value of numOfTables %d", pQueryMsg, pQueryMsg->numOfTables); return false; @@ -5964,7 +5421,8 @@ static bool validateQuerySourceCols(SQueryTableMsg *pQueryMsg, SSqlFuncMsg** pEx if ((pFuncMsg->functionId == TSDB_FUNC_TAGPRJ) || (pFuncMsg->functionId == TSDB_FUNC_TID_TAG && pFuncMsg->colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) || - (pFuncMsg->functionId == TSDB_FUNC_COUNT && pFuncMsg->colInfo.colId == TSDB_TBNAME_COLUMN_INDEX)) { + (pFuncMsg->functionId == TSDB_FUNC_COUNT && pFuncMsg->colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) || + (pFuncMsg->functionId == TSDB_FUNC_BLKINFO)) { continue; } @@ -6044,6 +5502,8 @@ int32_t convertQueryMsg(SQueryTableMsg *pQueryMsg, SQueryParam* param) { pQueryMsg->secondStageOutput = htonl(pQueryMsg->secondStageOutput); pQueryMsg->sqlstrLen = htonl(pQueryMsg->sqlstrLen); pQueryMsg->prevResultLen = htonl(pQueryMsg->prevResultLen); + pQueryMsg->sw.gap = htobe64(pQueryMsg->sw.gap); + pQueryMsg->sw.primaryColId = htonl(pQueryMsg->sw.primaryColId); // query msg safety check if (!validateQueryMsg(pQueryMsg)) { @@ -6118,6 +5578,9 @@ int32_t convertQueryMsg(SQueryTableMsg *pQueryMsg, SQueryParam* param) { pExprMsg->colInfo.colIndex = htons(pExprMsg->colInfo.colIndex); pExprMsg->colInfo.colId = htons(pExprMsg->colInfo.colId); pExprMsg->colInfo.flag = htons(pExprMsg->colInfo.flag); + pExprMsg->colType = htons(pExprMsg->colType); + pExprMsg->colBytes = htons(pExprMsg->colBytes); + pExprMsg->functionId = htons(pExprMsg->functionId); pExprMsg->numOfParams = htons(pExprMsg->numOfParams); pExprMsg->resColId = htons(pExprMsg->resColId); @@ -6156,7 +5619,10 @@ int32_t convertQueryMsg(SQueryTableMsg *pQueryMsg, SQueryParam* param) { pExprMsg->colInfo.colIndex = htons(pExprMsg->colInfo.colIndex); pExprMsg->colInfo.colId = htons(pExprMsg->colInfo.colId); - pExprMsg->colInfo.flag = htons(pExprMsg->colInfo.flag); + pExprMsg->colInfo.flag = htons(pExprMsg->colInfo.flag); + pExprMsg->colType = htons(pExprMsg->colType); + pExprMsg->colBytes = htons(pExprMsg->colBytes); + pExprMsg->functionId = htons(pExprMsg->functionId); pExprMsg->numOfParams = htons(pExprMsg->numOfParams); @@ -6326,8 +5792,29 @@ static int32_t buildArithmeticExprFromMsg(SExprInfo *pArithExprInfo, SQueryTable return TSDB_CODE_SUCCESS; } -int32_t createQueryFuncExprFromMsg(SQueryTableMsg *pQueryMsg, int32_t numOfOutput, SExprInfo **pExprInfo, SSqlFuncMsg **pExprMsg, - SColumnInfo* pTagCols) { +static int32_t updateOutputBufForTopBotQuery(SQueryTableMsg* pQueryMsg, SColumnInfo* pTagCols, SExprInfo* pExprs, int32_t numOfOutput, int32_t tagLen, bool superTable) { + for (int32_t i = 0; i < numOfOutput; ++i) { + int16_t functId = pExprs[i].base.functionId; + + if (functId == TSDB_FUNC_TOP || functId == TSDB_FUNC_BOTTOM) { + int32_t j = getColumnIndexInSource(pQueryMsg, &pExprs[i].base, pTagCols); + if (j < 0 || j >= pQueryMsg->numOfCols) { + return TSDB_CODE_QRY_INVALID_MSG; + } else { + SColumnInfo* pCol = &pQueryMsg->colList[j]; + int32_t ret = getResultDataInfo(pCol->type, pCol->bytes, functId, (int32_t)pExprs[i].base.arg[0].argValue.i64, + &pExprs[i].type, &pExprs[i].bytes, &pExprs[i].interBytes, tagLen, superTable); + assert(ret == TSDB_CODE_SUCCESS); + } + } + } + + return TSDB_CODE_SUCCESS; +} + +// TODO tag length should be passed from client +int32_t createQueryFuncExprFromMsg(SQueryTableMsg* pQueryMsg, int32_t numOfOutput, SExprInfo** pExprInfo, + SSqlFuncMsg** pExprMsg, SColumnInfo* pTagCols) { *pExprInfo = NULL; int32_t code = TSDB_CODE_SUCCESS; @@ -6400,6 +5887,12 @@ int32_t createQueryFuncExprFromMsg(SQueryTableMsg *pQueryMsg, int32_t numOfOutpu } int32_t param = (int32_t)pExprs[i].base.arg[0].argValue.i64; + if (pExprs[i].base.functionId != TSDB_FUNC_ARITHM && + (type != pExprs[i].base.colType || bytes != pExprs[i].base.colBytes)) { + tfree(pExprs); + return TSDB_CODE_QRY_INVALID_MSG; + } + if (getResultDataInfo(type, bytes, pExprs[i].base.functionId, param, &pExprs[i].type, &pExprs[i].bytes, &pExprs[i].interBytes, 0, isSuperTable) != TSDB_CODE_SUCCESS) { tfree(pExprs); @@ -6413,23 +5906,59 @@ int32_t createQueryFuncExprFromMsg(SQueryTableMsg *pQueryMsg, int32_t numOfOutpu assert(isValidDataType(pExprs[i].type)); } - // TODO refactor + // the tag length is affected by other tag columns, so this should be update. + updateOutputBufForTopBotQuery(pQueryMsg, pTagCols, pExprs, numOfOutput, tagLen, isSuperTable); + + *pExprInfo = pExprs; + return TSDB_CODE_SUCCESS; +} + +int32_t createIndirectQueryFuncExprFromMsg(SQueryTableMsg *pQueryMsg, int32_t numOfOutput, SExprInfo **pExprInfo, + SSqlFuncMsg **pExprMsg, SExprInfo *prevExpr) { + *pExprInfo = NULL; + int32_t code = TSDB_CODE_SUCCESS; + + SExprInfo *pExprs = (SExprInfo *)calloc(numOfOutput, sizeof(SExprInfo)); + if (pExprs == NULL) { + return TSDB_CODE_QRY_OUT_OF_MEMORY; + } + + bool isSuperTable = QUERY_IS_STABLE_QUERY(pQueryMsg->queryType); + for (int32_t i = 0; i < numOfOutput; ++i) { pExprs[i].base = *pExprMsg[i]; - int16_t functId = pExprs[i].base.functionId; + pExprs[i].bytes = 0; - if (functId == TSDB_FUNC_TOP || functId == TSDB_FUNC_BOTTOM) { - int32_t j = getColumnIndexInSource(pQueryMsg, &pExprs[i].base, pTagCols); - if (j < 0 || j >= pQueryMsg->numOfCols) { - return TSDB_CODE_QRY_INVALID_MSG; - } else { - SColumnInfo *pCol = &pQueryMsg->colList[j]; - int32_t ret = - getResultDataInfo(pCol->type, pCol->bytes, functId, (int32_t)pExprs[i].base.arg[0].argValue.i64, - &pExprs[i].type, &pExprs[i].bytes, &pExprs[i].interBytes, tagLen, isSuperTable); - assert(ret == TSDB_CODE_SUCCESS); + int16_t type = 0; + int16_t bytes = 0; + + // parse the arithmetic expression + if (pExprs[i].base.functionId == TSDB_FUNC_ARITHM) { + code = buildArithmeticExprFromMsg(&pExprs[i], pQueryMsg); + + if (code != TSDB_CODE_SUCCESS) { + tfree(pExprs); + return code; } + + type = TSDB_DATA_TYPE_DOUBLE; + bytes = tDataTypes[type].bytes; + } else { + int32_t index = pExprs[i].base.colInfo.colIndex; + assert(prevExpr[index].base.resColId == pExprs[i].base.colInfo.colId); + + type = prevExpr[index].type; + bytes = prevExpr[index].bytes; } + + int32_t param = (int32_t)pExprs[i].base.arg[0].argValue.i64; + if (getResultDataInfo(type, bytes, pExprs[i].base.functionId, param, &pExprs[i].type, &pExprs[i].bytes, + &pExprs[i].interBytes, 0, isSuperTable) != TSDB_CODE_SUCCESS) { + tfree(pExprs); + return TSDB_CODE_QRY_INVALID_MSG; + } + + assert(isValidDataType(pExprs[i].type)); } *pExprInfo = pExprs; @@ -6539,6 +6068,9 @@ static void doUpdateExprColumnIndex(SQuery *pQuery) { assert(f < pQuery->numOfCols); } else if (pColIndex->colId <= TSDB_UD_COLUMN_INDEX) { // do nothing for user-defined constant value result columns + } else if (pColIndex->colId == TSDB_BLOCK_DIST_COLUMN_INDEX) { + pColIndex->colIndex = 0;// only one source column, so it must be 0; + assert(pQuery->numOfOutput == 1); } else { int32_t f = 0; for (f = 0; f < pQuery->numOfTags; ++f) { @@ -6553,27 +6085,33 @@ static void doUpdateExprColumnIndex(SQuery *pQuery) { } } -static void calResultBufSize(SQuery* pQuery) { - const int32_t RESULT_MSG_MIN_SIZE = 1024 * (1024 + 512); // bytes - const int32_t RESULT_MSG_MIN_ROWS = 8192; - const float RESULT_THRESHOLD_RATIO = 0.85f; +void setResultBufSize(SQuery* pQuery, SRspResultInfo* pResultInfo) { + const int32_t DEFAULT_RESULT_MSG_SIZE = 1024 * (1024 + 512); + + // the minimum number of rows for projection query + const int32_t MIN_ROWS_FOR_PRJ_QUERY = 8192; + const int32_t DEFAULT_MIN_ROWS = 4096; + + const float THRESHOLD_RATIO = 0.85f; if (isProjQuery(pQuery)) { - int32_t numOfRes = RESULT_MSG_MIN_SIZE / pQuery->resultRowSize; - if (numOfRes < RESULT_MSG_MIN_ROWS) { - numOfRes = RESULT_MSG_MIN_ROWS; + int32_t numOfRes = DEFAULT_RESULT_MSG_SIZE / pQuery->resultRowSize; + if (numOfRes < MIN_ROWS_FOR_PRJ_QUERY) { + numOfRes = MIN_ROWS_FOR_PRJ_QUERY; } - pQuery->rec.capacity = numOfRes; - pQuery->rec.threshold = (int32_t)(numOfRes * RESULT_THRESHOLD_RATIO); + pResultInfo->capacity = numOfRes; } else { // in case of non-prj query, a smaller output buffer will be used. - pQuery->rec.capacity = 4096; - pQuery->rec.threshold = (int32_t)(pQuery->rec.capacity * RESULT_THRESHOLD_RATIO); + pResultInfo->capacity = DEFAULT_MIN_ROWS; } + + pResultInfo->threshold = (int32_t)(pResultInfo->capacity * THRESHOLD_RATIO); + pResultInfo->total = 0; } -SQInfo *createQInfoImpl(SQueryTableMsg *pQueryMsg, SSqlGroupbyExpr *pGroupbyExpr, SExprInfo *pExprs, - SExprInfo *pSecExprs, STableGroupInfo *pTableGroupInfo, SColumnInfo* pTagCols, bool stableQuery, char* sql) { +SQInfo* createQInfoImpl(SQueryTableMsg* pQueryMsg, SSqlGroupbyExpr* pGroupbyExpr, SExprInfo* pExprs, + SExprInfo* pSecExprs, STableGroupInfo* pTableGroupInfo, SColumnInfo* pTagCols, bool stableQuery, + char* sql) { int16_t numOfCols = pQueryMsg->numOfCols; int16_t numOfOutput = pQueryMsg->numOfOutput; @@ -6584,15 +6122,10 @@ SQInfo *createQInfoImpl(SQueryTableMsg *pQueryMsg, SSqlGroupbyExpr *pGroupbyExpr // to make sure third party won't overwrite this structure pQInfo->signature = pQInfo; - pQInfo->tableGroupInfo = *pTableGroupInfo; - - SQuery *pQuery = calloc(1, sizeof(SQuery)); - if (pQuery == NULL) { - goto _cleanup_query; - } - + SQuery* pQuery = &pQInfo->query; pQInfo->runtimeEnv.pQuery = pQuery; + pQuery->tableGroupInfo = *pTableGroupInfo; pQuery->numOfCols = numOfCols; pQuery->numOfOutput = numOfOutput; pQuery->limit.limit = pQueryMsg->limit; @@ -6609,7 +6142,7 @@ SQInfo *createQInfoImpl(SQueryTableMsg *pQueryMsg, SSqlGroupbyExpr *pGroupbyExpr pQuery->tagColList = pTagCols; pQuery->prjInfo.vgroupLimit = pQueryMsg->vgroupLimit; pQuery->prjInfo.ts = (pQueryMsg->order == TSDB_ORDER_ASC)? INT64_MIN:INT64_MAX; - + pQuery->sw = pQueryMsg->sw; pQuery->colList = calloc(numOfCols, sizeof(SSingleColumnFilterInfo)); if (pQuery->colList == NULL) { goto _cleanup; @@ -6639,37 +6172,11 @@ SQInfo *createQInfoImpl(SQueryTableMsg *pQueryMsg, SSqlGroupbyExpr *pGroupbyExpr } doUpdateExprColumnIndex(pQuery); - int32_t ret = createFilterInfo(pQInfo, pQuery); if (ret != TSDB_CODE_SUCCESS) { goto _cleanup; } - // prepare the result buffer - pQuery->sdata = (tFilePage **)calloc(pQuery->numOfOutput, POINTER_BYTES); - if (pQuery->sdata == NULL) { - goto _cleanup; - } - - calResultBufSize(pQuery); - - for (int32_t col = 0; col < pQuery->numOfOutput; ++col) { - // allocate additional memory for interResults that are usually larger then final results - // TODO refactor - int16_t bytes = 0; - if (pQuery->pExpr2 == NULL || col > pQuery->numOfExpr2) { - bytes = pExprs[col].bytes; - } else { - bytes = MAX(pQuery->pExpr2[col].bytes, pExprs[col].bytes); - } - - size_t size = (size_t)((pQuery->rec.capacity + 1) * bytes + pExprs[col].interBytes + sizeof(tFilePage)); - pQuery->sdata[col] = (tFilePage *)calloc(1, size); - if (pQuery->sdata[col] == NULL) { - goto _cleanup; - } - } - if (pQuery->fillType != TSDB_FILL_NONE) { pQuery->fillVal = malloc(sizeof(int64_t) * pQuery->numOfOutput); if (pQuery->fillVal == NULL) { @@ -6683,7 +6190,7 @@ SQInfo *createQInfoImpl(SQueryTableMsg *pQueryMsg, SSqlGroupbyExpr *pGroupbyExpr size_t numOfGroups = 0; if (pTableGroupInfo->pGroupList != NULL) { numOfGroups = taosArrayGetSize(pTableGroupInfo->pGroupList); - STableGroupInfo* pTableqinfo = &pQInfo->tableqinfoGroupInfo; + STableGroupInfo* pTableqinfo = &pQInfo->runtimeEnv.tableqinfoGroupInfo; pTableqinfo->pGroupList = taosArrayInit(numOfGroups, POINTER_BYTES); pTableqinfo->numOfTables = pTableGroupInfo->numOfTables; @@ -6695,26 +6202,23 @@ SQInfo *createQInfoImpl(SQueryTableMsg *pQueryMsg, SSqlGroupbyExpr *pGroupbyExpr goto _cleanup; } - // NOTE: pTableCheckInfo need to update the query time range and the lastKey info - pQInfo->arrTableIdInfo = taosHashInit(pTableGroupInfo->numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK); pQInfo->dataReady = QUERY_RESULT_NOT_READY; pQInfo->rspContext = NULL; pQInfo->sql = sql; pthread_mutex_init(&pQInfo->lock, NULL); tsem_init(&pQInfo->ready, 0, 0); - pQuery->pos = -1; pQuery->window = pQueryMsg->window; changeExecuteScanOrder(pQInfo, pQueryMsg, stableQuery); - pQInfo->runtimeEnv.queryWindowIdentical = true; + SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; bool groupByCol = isGroupbyColumn(pQuery->pGroupbyExpr); STimeWindow window = pQuery->window; int32_t index = 0; for(int32_t i = 0; i < numOfGroups; ++i) { - SArray* pa = taosArrayGetP(pQInfo->tableGroupInfo.pGroupList, i); + SArray* pa = taosArrayGetP(pQuery->tableGroupInfo.pGroupList, i); size_t s = taosArrayGetSize(pa); SArray* p1 = taosArrayInit(s, POINTER_BYTES); @@ -6722,15 +6226,11 @@ SQInfo *createQInfoImpl(SQueryTableMsg *pQueryMsg, SSqlGroupbyExpr *pGroupbyExpr goto _cleanup; } - taosArrayPush(pQInfo->tableqinfoGroupInfo.pGroupList, &p1); + taosArrayPush(pRuntimeEnv->tableqinfoGroupInfo.pGroupList, &p1); for(int32_t j = 0; j < s; ++j) { STableKeyInfo* info = taosArrayGet(pa, j); - window.skey = info->lastKey; - if (info->lastKey != pQuery->window.skey) { - pQInfo->runtimeEnv.queryWindowIdentical = false; - } void* buf = (char*) pQInfo->pBuf + index * sizeof(STableQueryInfo); STableQueryInfo* item = createTableQueryInfo(pQuery, info->pTable, groupByCol, window, buf); @@ -6742,23 +6242,22 @@ SQInfo *createQInfoImpl(SQueryTableMsg *pQueryMsg, SSqlGroupbyExpr *pGroupbyExpr taosArrayPush(p1, &item); STableId* id = TSDB_TABLEID(info->pTable); - taosHashPut(pQInfo->tableqinfoGroupInfo.map, &id->tid, sizeof(id->tid), &item, POINTER_BYTES); + taosHashPut(pRuntimeEnv->tableqinfoGroupInfo.map, &id->tid, sizeof(id->tid), &item, POINTER_BYTES); index += 1; } } - colIdCheck(pQuery); + colIdCheck(pQuery, pQInfo); // todo refactor - pQInfo->runtimeEnv.queryBlockDist = (numOfOutput == 1 && pExprs[0].base.colInfo.colId == TSDB_BLOCK_DIST_COLUMN_INDEX); - + pQInfo->query.queryBlockDist = (numOfOutput == 1 && pExprs[0].base.colInfo.colId == TSDB_BLOCK_DIST_COLUMN_INDEX); + qDebug("qmsg:%p QInfo:%p created", pQueryMsg, pQInfo); return pQInfo; _cleanup_qinfo: tsdbDestroyTableGroup(pTableGroupInfo); -_cleanup_query: if (pGroupbyExpr != NULL) { taosArrayDestroy(pGroupbyExpr->columnInfo); free(pGroupbyExpr); @@ -6796,6 +6295,8 @@ bool isValidQInfo(void *param) { int32_t initQInfo(SQueryTableMsg *pQueryMsg, void *tsdb, int32_t vgId, SQInfo *pQInfo, SQueryParam* param, bool isSTable) { int32_t code = TSDB_CODE_SUCCESS; + + SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; SQuery *pQuery = pQInfo->runtimeEnv.pQuery; STSBuf *pTsBuf = NULL; @@ -6820,15 +6321,15 @@ int32_t initQInfo(SQueryTableMsg *pQueryMsg, void *tsdb, int32_t vgId, SQInfo *p (!QUERY_IS_ASC_QUERY(pQuery) && (pQuery->window.ekey > pQuery->window.skey))) { qDebug("QInfo:%p no result in time range %" PRId64 "-%" PRId64 ", order %d", pQInfo, pQuery->window.skey, pQuery->window.ekey, pQuery->order.order); - setQueryStatus(pQuery, QUERY_COMPLETED); - pQInfo->tableqinfoGroupInfo.numOfTables = 0; + setQueryStatus(pRuntimeEnv, QUERY_COMPLETED); + pRuntimeEnv->tableqinfoGroupInfo.numOfTables = 0; // todo free memory return TSDB_CODE_SUCCESS; } - if (pQInfo->tableqinfoGroupInfo.numOfTables == 0) { + if (pRuntimeEnv->tableqinfoGroupInfo.numOfTables == 0) { qDebug("QInfo:%p no table qualified for tag filter, abort query", pQInfo); - setQueryStatus(pQuery, QUERY_COMPLETED); + setQueryStatus(pRuntimeEnv, QUERY_COMPLETED); return TSDB_CODE_SUCCESS; } @@ -6906,18 +6407,12 @@ void freeQInfo(SQInfo *pQInfo) { qDebug("QInfo:%p start to free QInfo", pQInfo); - releaseQueryBuf(pQInfo->tableqinfoGroupInfo.numOfTables); + SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; + releaseQueryBuf(pRuntimeEnv->tableqinfoGroupInfo.numOfTables); teardownQueryRuntimeEnv(&pQInfo->runtimeEnv); SQuery *pQuery = pQInfo->runtimeEnv.pQuery; if (pQuery != NULL) { - if (pQuery->sdata != NULL) { - for (int32_t col = 0; col < pQuery->numOfOutput; ++col) { - tfree(pQuery->sdata[col]); - } - tfree(pQuery->sdata); - } - if (pQuery->fillVal != NULL) { tfree(pQuery->fillVal); } @@ -6947,20 +6442,16 @@ void freeQInfo(SQInfo *pQInfo) { taosArrayDestroy(pQuery->pGroupbyExpr->columnInfo); tfree(pQuery->pGroupbyExpr); } - - tfree(pQuery); } - doDestroyTableQueryInfo(&pQInfo->tableqinfoGroupInfo); + doDestroyTableQueryInfo(&pRuntimeEnv->tableqinfoGroupInfo); tfree(pQInfo->pBuf); tfree(pQInfo->sql); - tsdbDestroyTableGroup(&pQInfo->tableGroupInfo); - taosHashCleanup(pQInfo->arrTableIdInfo); - - taosArrayDestroy(pQInfo->groupResInfo.pRows); + tsdbDestroyTableGroup(&pQuery->tableGroupInfo); + taosArrayDestroy(pRuntimeEnv->groupResInfo.pRows); pQInfo->signature = 0; qDebug("QInfo:%p QInfo is freed", pQInfo); @@ -6968,41 +6459,20 @@ void freeQInfo(SQInfo *pQInfo) { tfree(pQInfo); } -size_t getResultSize(SQInfo *pQInfo, int64_t *numOfRows) { - SQuery *pQuery = pQInfo->runtimeEnv.pQuery; - - /* - * get the file size and set the numOfRows to be the file size, since for tsComp query, - * the returned row size is equalled to 1 - * TODO handle the case that the file is too large to send back one time - */ - if (isTsCompQuery(pQuery) && (*numOfRows) > 0) { - struct stat fStat; - FILE *f = *(FILE **)pQuery->sdata[0]->data; - if ((f != NULL) && (fstat(fileno(f), &fStat) == 0)) { - *numOfRows = fStat.st_size; - return fStat.st_size; - } else { - qError("QInfo:%p failed to get file info, file:%p, reason:%s", pQInfo, f, strerror(errno)); - return 0; - } - } else { - return (size_t)(pQuery->resultRowSize * (*numOfRows)); - } -} - int32_t doDumpQueryResult(SQInfo *pQInfo, char *data) { // the remained number of retrieved rows, not the interpolated result + SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; SQuery *pQuery = pQInfo->runtimeEnv.pQuery; // load data from file to msg buffer if (isTsCompQuery(pQuery)) { - - FILE *f = *(FILE **)pQuery->sdata[0]->data; // TODO refactor + SColumnInfoData* pColInfoData = taosArrayGet(pRuntimeEnv->outputBuf->pDataBlock, 0); + FILE *f = *(FILE **)pColInfoData->pData; // TODO refactor // make sure file exist if (f) { off_t s = lseek(fileno(f), 0, SEEK_END); + assert(s == pRuntimeEnv->outputBuf->info.rows); qDebug("QInfo:%p ts comp data return, file:%p, size:%"PRId64, pQInfo, f, (uint64_t)s); if (fseek(f, 0, SEEK_SET) >= 0) { @@ -7017,30 +6487,30 @@ int32_t doDumpQueryResult(SQInfo *pQInfo, char *data) { assert(0); } + // dump error info if (s <= (sizeof(STSBufFileHeader) + sizeof(STSGroupBlockInfo) + 6 * sizeof(int32_t))) { qDump(data, s); - assert(0); } fclose(f); - *(FILE **)pQuery->sdata[0]->data = NULL; } // all data returned, set query over - if (Q_STATUS_EQUAL(pQuery->status, QUERY_COMPLETED)) { - setQueryStatus(pQuery, QUERY_OVER); + if (Q_STATUS_EQUAL(pRuntimeEnv->status, QUERY_COMPLETED)) { + setQueryStatus(pRuntimeEnv, QUERY_OVER); } } else { - doCopyQueryResultToMsg(pQInfo, (int32_t)pQuery->rec.rows, data); + doCopyQueryResultToMsg(pQInfo, (int32_t)pRuntimeEnv->outputBuf->info.rows, data); } - pQuery->rec.total += pQuery->rec.rows; - qDebug("QInfo:%p current numOfRes rows:%" PRId64 ", total:%" PRId64, pQInfo, pQuery->rec.rows, pQuery->rec.total); + pRuntimeEnv->resultInfo.total += pRuntimeEnv->outputBuf->info.rows; + qDebug("QInfo:%p current numOfRes rows:%d, total:%" PRId64, pQInfo, + pRuntimeEnv->outputBuf->info.rows, pRuntimeEnv->resultInfo.total); - if (pQuery->limit.limit > 0 && pQuery->limit.limit == pQuery->rec.total) { + if (pQuery->limit.limit > 0 && pQuery->limit.limit == pRuntimeEnv->resultInfo.total) { qDebug("QInfo:%p results limitation reached, limitation:%"PRId64, pQInfo, pQuery->limit.limit); - setQueryStatus(pQuery, QUERY_OVER); + setQueryStatus(pRuntimeEnv, QUERY_OVER); } return TSDB_CODE_SUCCESS; @@ -7075,7 +6545,7 @@ static void doSetTagValueToResultBuf(char* output, const char* val, int16_t type if (IS_VAR_DATA_TYPE(type)) { // Binary data overflows for sort of unknown reasons. Let trim the overflow data if (varDataTLen(val) > bytes) { - int32_t len = bytes; + int32_t len = bytes - VARSTR_HEADER_SIZE; // remain available space memcpy(varDataVal(output), varDataVal(val), len); varDataSetLen(output, len); } else { @@ -7086,139 +6556,6 @@ static void doSetTagValueToResultBuf(char* output, const char* val, int16_t type } } -void buildTagQueryResult(SQInfo* pQInfo) { - SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; - SQuery * pQuery = pRuntimeEnv->pQuery; - - size_t numOfGroup = GET_NUM_OF_TABLEGROUP(pQInfo); - assert(numOfGroup == 0 || numOfGroup == 1); - - if (numOfGroup == 0) { - return; - } - - SArray* pa = GET_TABLEGROUP(pQInfo, 0); - - size_t num = taosArrayGetSize(pa); - assert(num == pQInfo->tableqinfoGroupInfo.numOfTables); - - int32_t count = 0; - int32_t functionId = pQuery->pExpr1[0].base.functionId; - if (functionId == TSDB_FUNC_TID_TAG) { // return the tags & table Id - assert(pQuery->numOfOutput == 1); - - SExprInfo* pExprInfo = &pQuery->pExpr1[0]; - int32_t rsize = pExprInfo->bytes; - count = 0; - - int16_t bytes = pExprInfo->bytes; - int16_t type = pExprInfo->type; - - for(int32_t i = 0; i < pQuery->numOfTags; ++i) { - if (pQuery->tagColList[i].colId == pExprInfo->base.colInfo.colId) { - bytes = pQuery->tagColList[i].bytes; - type = pQuery->tagColList[i].type; - break; - } - } - - while(pQInfo->tableIndex < num && count < pQuery->rec.capacity) { - int32_t i = pQInfo->tableIndex++; - STableQueryInfo *item = taosArrayGetP(pa, i); - - char *output = pQuery->sdata[0]->data + count * rsize; - varDataSetLen(output, rsize - VARSTR_HEADER_SIZE); - - output = varDataVal(output); - STableId* id = TSDB_TABLEID(item->pTable); - - *(int16_t *)output = 0; - output += sizeof(int16_t); - - *(int64_t *)output = id->uid; // memory align problem, todo serialize - output += sizeof(id->uid); - - *(int32_t *)output = id->tid; - output += sizeof(id->tid); - - *(int32_t *)output = pQInfo->vgId; - output += sizeof(pQInfo->vgId); - - if (pExprInfo->base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) { - char* data = tsdbGetTableName(item->pTable); - memcpy(output, data, varDataTLen(data)); - } else { - char* data = tsdbGetTableTagVal(item->pTable, pExprInfo->base.colInfo.colId, type, bytes); - doSetTagValueToResultBuf(output, data, type, bytes); - } - - count += 1; - } - - qDebug("QInfo:%p create (tableId, tag) info completed, rows:%d", pQInfo, count); - - } else if (functionId == TSDB_FUNC_COUNT) {// handle the "count(tbname)" query - *(int64_t*) pQuery->sdata[0]->data = num; - - count = 1; - SET_STABLE_QUERY_OVER(pQInfo); - qDebug("QInfo:%p create count(tbname) query, res:%d rows:1", pQInfo, count); - } else { // return only the tags|table name etc. - count = 0; - SSchema* tbnameSchema = tGetTbnameColumnSchema(); - - int32_t maxNumOfTables = (int32_t)pQuery->rec.capacity; - if (pQuery->limit.limit >= 0 && pQuery->limit.limit < pQuery->rec.capacity) { - maxNumOfTables = (int32_t)pQuery->limit.limit; - } - - while(pQInfo->tableIndex < num && count < maxNumOfTables) { - int32_t i = pQInfo->tableIndex++; - - // discard current result due to offset - if (pQuery->limit.offset > 0) { - pQuery->limit.offset -= 1; - continue; - } - - SExprInfo* pExprInfo = pQuery->pExpr1; - STableQueryInfo* item = taosArrayGetP(pa, i); - - char *data = NULL, *dst = NULL; - int16_t type = 0, bytes = 0; - for(int32_t j = 0; j < pQuery->numOfOutput; ++j) { - // not assign value in case of user defined constant output column - if (TSDB_COL_IS_UD_COL(pExprInfo[j].base.colInfo.flag)) { - continue; - } - - if (pExprInfo[j].base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) { - bytes = tbnameSchema->bytes; - type = tbnameSchema->type; - - data = tsdbGetTableName(item->pTable); - dst = pQuery->sdata[j]->data + count * tbnameSchema->bytes; - } else { - type = pExprInfo[j].type; - bytes = pExprInfo[j].bytes; - - data = tsdbGetTableTagVal(item->pTable, pExprInfo[j].base.colInfo.colId, type, bytes); - dst = pQuery->sdata[j]->data + count * pExprInfo[j].bytes; - - } - - doSetTagValueToResultBuf(dst, data, type, bytes); - } - count += 1; - } - - qDebug("QInfo:%p create tag values results completed, rows:%d", pQInfo, count); - } - - pQuery->rec.rows = count; - setQueryStatus(pQuery, QUERY_COMPLETED); -} - static int64_t getQuerySupportBufSize(size_t numOfTables) { size_t s1 = sizeof(STableQueryInfo); size_t s2 = sizeof(SHashNode); diff --git a/src/query/src/qFill.c b/src/query/src/qFill.c index bc6376b80743bcad3e1b07f903c970eeb9e5dee2..2de1029396fb779798ea7c20da939b2153ca3dd3 100644 --- a/src/query/src/qFill.c +++ b/src/query/src/qFill.c @@ -23,18 +23,19 @@ #include "qFill.h" #include "qExtbuffer.h" #include "queryLog.h" +#include "qExecutor.h" #define FILL_IS_ASC_FILL(_f) ((_f)->order == TSDB_ORDER_ASC) #define DO_INTERPOLATION(_v1, _v2, _k1, _k2, _k) ((_v1) + ((_v2) - (_v1)) * (((double)(_k)) - ((double)(_k1))) / (((double)(_k2)) - ((double)(_k1)))) -static void setTagsValue(SFillInfo* pFillInfo, tFilePage** data, int32_t genRows) { +static void setTagsValue(SFillInfo* pFillInfo, void** data, int32_t genRows) { for(int32_t j = 0; j < pFillInfo->numOfCols; ++j) { SFillColInfo* pCol = &pFillInfo->pFillCol[j]; if (TSDB_COL_IS_NORMAL_COL(pCol->flag)) { continue; } - char* val1 = elePtrAt(data[j]->data, pCol->col.bytes, genRows); + char* val1 = elePtrAt(data[j], pCol->col.bytes, genRows); assert(pCol->tagIndex >= 0 && pCol->tagIndex < pFillInfo->numOfTags); SFillTagColInfo* pTag = &pFillInfo->pTags[pCol->tagIndex]; @@ -44,17 +45,17 @@ static void setTagsValue(SFillInfo* pFillInfo, tFilePage** data, int32_t genRows } } -static void setNullValueForRow(SFillInfo* pFillInfo, tFilePage** data, int32_t numOfCol, int32_t rowIndex) { +static void setNullValueForRow(SFillInfo* pFillInfo, void** data, int32_t numOfCol, int32_t rowIndex) { // the first are always the timestamp column, so start from the second column. for (int32_t i = 1; i < numOfCol; ++i) { SFillColInfo* pCol = &pFillInfo->pFillCol[i]; - char* output = elePtrAt(data[i]->data, pCol->col.bytes, rowIndex); + char* output = elePtrAt(data[i], pCol->col.bytes, rowIndex); setNull(output, pCol->col.type, pCol->col.bytes); } } -static void doFillOneRowResult(SFillInfo* pFillInfo, tFilePage** data, char** srcData, int64_t ts, bool outOfBound) { +static void doFillOneRowResult(SFillInfo* pFillInfo, void** data, char** srcData, int64_t ts, bool outOfBound) { char* prev = pFillInfo->prevValues; char* next = pFillInfo->nextValues; @@ -63,7 +64,7 @@ static void doFillOneRowResult(SFillInfo* pFillInfo, tFilePage** data, char** sr // set the primary timestamp column value int32_t index = pFillInfo->numOfCurrent; - char* val = elePtrAt(data[0]->data, TSDB_KEYSIZE, index); + char* val = elePtrAt(data[0], TSDB_KEYSIZE, index); *(TSKEY*) val = pFillInfo->currentKey; // set the other values @@ -77,7 +78,7 @@ static void doFillOneRowResult(SFillInfo* pFillInfo, tFilePage** data, char** sr continue; } - char* output = elePtrAt(data[i]->data, pCol->col.bytes, index); + char* output = elePtrAt(data[i], pCol->col.bytes, index); assignVal(output, p + pCol->col.offset, pCol->col.bytes, pCol->col.type); } } else { // no prev value yet, set the value for NULL @@ -93,7 +94,7 @@ static void doFillOneRowResult(SFillInfo* pFillInfo, tFilePage** data, char** sr continue; } - char* output = elePtrAt(data[i]->data, pCol->col.bytes, index); + char* output = elePtrAt(data[i], pCol->col.bytes, index); assignVal(output, p + pCol->col.offset, pCol->col.bytes, pCol->col.type); } } else { // no prev value yet, set the value for NULL @@ -111,7 +112,7 @@ static void doFillOneRowResult(SFillInfo* pFillInfo, tFilePage** data, char** sr int16_t type = pCol->col.type; int16_t bytes = pCol->col.bytes; - char *val1 = elePtrAt(data[i]->data, pCol->col.bytes, index); + char *val1 = elePtrAt(data[i], pCol->col.bytes, index); if (type == TSDB_DATA_TYPE_BINARY|| type == TSDB_DATA_TYPE_NCHAR || type == TSDB_DATA_TYPE_BOOL) { setNull(val1, pCol->col.type, bytes); continue; @@ -132,7 +133,7 @@ static void doFillOneRowResult(SFillInfo* pFillInfo, tFilePage** data, char** sr continue; } - char* val1 = elePtrAt(data[i]->data, pCol->col.bytes, index); + char* val1 = elePtrAt(data[i], pCol->col.bytes, index); assignVal(val1, (char*)&pCol->fillVal.i, pCol->col.bytes, pCol->col.type); } } @@ -162,7 +163,7 @@ static void copyCurrentRowIntoBuf(SFillInfo* pFillInfo, char** srcData, char* bu } } -static int32_t fillResultImpl(SFillInfo* pFillInfo, tFilePage** data, int32_t outputRows) { +static int32_t fillResultImpl(SFillInfo* pFillInfo, void** data, int32_t outputRows) { pFillInfo->numOfCurrent = 0; char** srcData = pFillInfo->pData; @@ -213,7 +214,7 @@ static int32_t fillResultImpl(SFillInfo* pFillInfo, tFilePage** data, int32_t ou continue; } - char* output = elePtrAt(data[i]->data, pCol->col.bytes, pFillInfo->numOfCurrent); + char* output = elePtrAt(data[i], pCol->col.bytes, pFillInfo->numOfCurrent); char* src = elePtrAt(srcData[i], pCol->col.bytes, pFillInfo->index); if (i == 0 || (pCol->functionId != TSDB_FUNC_COUNT && !isNull(src, pCol->col.type)) || @@ -255,7 +256,7 @@ static int32_t fillResultImpl(SFillInfo* pFillInfo, tFilePage** data, int32_t ou return pFillInfo->numOfCurrent; } -static int64_t appendFilledResult(SFillInfo* pFillInfo, tFilePage** output, int64_t resultCapacity) { +static int64_t appendFilledResult(SFillInfo* pFillInfo, void** output, int64_t resultCapacity) { /* * These data are generated according to fill strategy, since the current timestamp is out of the time window of * real result set. Note that we need to keep the direct previous result rows, to generated the filled data. @@ -278,7 +279,7 @@ static int32_t setTagColumnInfo(SFillInfo* pFillInfo, int32_t numOfCols, int32_t int32_t k = 0; for (int32_t i = 0; i < numOfCols; ++i) { SFillColInfo* pColInfo = &pFillInfo->pFillCol[i]; - pFillInfo->pData[i] = calloc(1, pColInfo->col.bytes * capacity); + pFillInfo->pData[i] = NULL; if (TSDB_COL_IS_TAG(pColInfo->flag)) { bool exists = false; @@ -356,6 +357,10 @@ SFillInfo* taosCreateFillInfo(int32_t order, TSKEY skey, int32_t numOfTags, int3 pFillInfo->rowSize = setTagColumnInfo(pFillInfo, pFillInfo->numOfCols, pFillInfo->alloc); assert(pFillInfo->rowSize > 0); + for(int32_t i = 0; i < pFillInfo->numOfCols; ++i) { + pFillInfo->pData[i] = malloc(pFillInfo->pFillCol[i].col.bytes * pFillInfo->alloc); + } + return pFillInfo; } @@ -375,11 +380,16 @@ void* taosDestroyFillInfo(SFillInfo* pFillInfo) { tfree(pFillInfo->prevValues); tfree(pFillInfo->nextValues); - tfree(pFillInfo->pTags); - + + for(int32_t i = 0; i < pFillInfo->numOfTags; ++i) { + tfree(pFillInfo->pTags[i].tagVal); + } + for(int32_t i = 0; i < pFillInfo->numOfCols; ++i) { tfree(pFillInfo->pData[i]); } + + tfree(pFillInfo->pTags); tfree(pFillInfo->pData); tfree(pFillInfo->pFillCol); @@ -413,10 +423,19 @@ void taosFillSetStartInfo(SFillInfo* pFillInfo, int32_t numOfRows, TSKEY endKey) } } -// copy the data into source data buffer -void taosFillSetDataBlockFromFilePage(SFillInfo* pFillInfo, const tFilePage** pInput) { +void taosFillSetInputDataBlock(SFillInfo* pFillInfo, const SSDataBlock* pInput) { for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) { - memcpy(pFillInfo->pData[i], pInput[i]->data, pFillInfo->numOfRows * pFillInfo->pFillCol[i].col.bytes); + SColumnInfoData* pColData = taosArrayGet(pInput->pDataBlock, i); +// pFillInfo->pData[i] = pColData->pData; + if (pInput->info.rows > pFillInfo->alloc) { + char* t = realloc(pFillInfo->pData[i], pColData->info.bytes * pInput->info.rows); + assert(t != NULL); + + pFillInfo->pData[i] = t; + pFillInfo->alloc = pInput->info.rows; + } + + memcpy(pFillInfo->pData[i], pColData->pData, pColData->info.bytes * pInput->info.rows); } } @@ -427,12 +446,20 @@ void taosFillCopyInputDataFromOneFilePage(SFillInfo* pFillInfo, const tFilePage* SFillColInfo* pCol = &pFillInfo->pFillCol[i]; const char* data = pInput->data + pCol->col.offset * pInput->num; - memcpy(pFillInfo->pData[i], data, (size_t)(pInput->num * pCol->col.bytes)); + if (pInput->num > pFillInfo->alloc) { + char* t = realloc(pFillInfo->pData[i], (size_t)(pCol->col.bytes * pInput->num)); + assert(t != NULL); + + pFillInfo->pData[i] = t; + pFillInfo->alloc = (int32_t)pInput->num; + } + + memcpy(pFillInfo->pData[i], data, (size_t)(pCol->col.bytes * pInput->num)); if (TSDB_COL_IS_TAG(pCol->flag)) { // copy the tag value to tag value buffer SFillTagColInfo* pTag = &pFillInfo->pTags[pCol->tagIndex]; assert (pTag->col.colId == pCol->col.colId); - memcpy(pTag->tagVal, data, pCol->col.bytes); + memcpy(pTag->tagVal, data, pCol->col.bytes); // TODO not memcpy?? } } } @@ -490,7 +517,7 @@ int32_t taosGetLinearInterpolationVal(SPoint* point, int32_t outputType, SPoint* return TSDB_CODE_SUCCESS; } -int64_t taosFillResultDataBlock(SFillInfo* pFillInfo, tFilePage** output, int32_t capacity) { +int64_t taosFillResultDataBlock(SFillInfo* pFillInfo, void** output, int32_t capacity) { int32_t remain = taosNumOfRemainRows(pFillInfo); int64_t numOfRes = getNumOfResultsAfterFillGap(pFillInfo, pFillInfo->end, capacity); diff --git a/src/query/src/qPercentile.c b/src/query/src/qPercentile.c index 523fa42547e34e9f0bb9ac23868ab3dc67eeec34..d36664617244cb0059b4e291da88260fac9db225 100644 --- a/src/query/src/qPercentile.c +++ b/src/query/src/qPercentile.c @@ -254,7 +254,7 @@ tMemBucket *tMemBucketCreate(int16_t nElemSize, int16_t dataType, double minval, resetSlotInfo(pBucket); - int32_t ret = createDiskbasedResultBuffer(&pBucket->pBuffer, pBucket->bytes, pBucket->bufPageSize, pBucket->bufPageSize * 512, NULL); + int32_t ret = createDiskbasedResultBuffer(&pBucket->pBuffer, pBucket->bufPageSize, pBucket->bufPageSize * 512, NULL); if (ret != TSDB_CODE_SUCCESS) { tMemBucketDestroy(pBucket); return NULL; diff --git a/src/query/src/qResultbuf.c b/src/query/src/qResultbuf.c index d45e76c2fd07f37afa0ffad0f9db65d5e1948c43..ed7f8c67194269b20e21809ff0948662f4f25147 100644 --- a/src/query/src/qResultbuf.c +++ b/src/query/src/qResultbuf.c @@ -9,8 +9,7 @@ #define GET_DATA_PAYLOAD(_p) ((char *)(_p)->pData + POINTER_BYTES) #define NO_IN_MEM_AVAILABLE_PAGES(_b) (listNEles((_b)->lruList) >= (_b)->inMemPages) -int32_t createDiskbasedResultBuffer(SDiskbasedResultBuf** pResultBuf, int32_t rowSize, int32_t pagesize, - int32_t inMemBufSize, const void* handle) { +int32_t createDiskbasedResultBuffer(SDiskbasedResultBuf** pResultBuf, int32_t pagesize, int32_t inMemBufSize, const void* handle) { *pResultBuf = calloc(1, sizeof(SDiskbasedResultBuf)); SDiskbasedResultBuf* pResBuf = *pResultBuf; @@ -31,7 +30,6 @@ int32_t createDiskbasedResultBuffer(SDiskbasedResultBuf** pResultBuf, int32_t ro // at least more than 2 pages must be in memory assert(inMemBufSize >= pagesize * 2); - pResBuf->numOfRowsPerPage = (pagesize - sizeof(tFilePage)) / rowSize; pResBuf->lruList = tdListNew(POINTER_BYTES); // init id hash table @@ -387,8 +385,6 @@ void releaseResBufPageInfo(SDiskbasedResultBuf* pResultBuf, SPageInfo* pi) { pResultBuf->statis.releasePages += 1; } -size_t getNumOfRowsPerPage(const SDiskbasedResultBuf* pResultBuf) { return pResultBuf->numOfRowsPerPage; } - size_t getNumOfResultBufGroupId(const SDiskbasedResultBuf* pResultBuf) { return taosHashGetSize(pResultBuf->groupSet); } size_t getResBufSize(const SDiskbasedResultBuf* pResultBuf) { return (size_t)pResultBuf->totalBufSize; } diff --git a/src/query/src/qParserImpl.c b/src/query/src/qSqlParser.c similarity index 67% rename from src/query/src/qParserImpl.c rename to src/query/src/qSqlParser.c index d18eae033b3ecb4dc2b5870e979c0cba5c28d0a2..e76b78c5238c33fbe6cee6b9d8e81c8e2b30318a 100644 --- a/src/query/src/qParserImpl.c +++ b/src/query/src/qSqlParser.c @@ -23,7 +23,7 @@ #include "ttokendef.h" #include "tutil.h" -SSqlInfo qSQLParse(const char *pStr) { +SSqlInfo qSqlParse(const char *pStr) { void *pParser = ParseAlloc(malloc); SSqlInfo sqlInfo = {0}; @@ -80,53 +80,48 @@ abort_parse: return sqlInfo; } -tSQLExprList *tSqlExprListAppend(tSQLExprList *pList, tSQLExpr *pNode, SStrToken *pDistinct, SStrToken *pToken) { +SArray *tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SStrToken *pDistinct, SStrToken *pToken) { if (pList == NULL) { - pList = calloc(1, sizeof(tSQLExprList)); + pList = taosArrayInit(4, sizeof(tSqlExprItem)); } - if (pList->nAlloc <= pList->nExpr) { - pList->nAlloc = (pList->nAlloc << 1u) + 4; - pList->a = realloc(pList->a, pList->nAlloc * sizeof(pList->a[0])); - if (pList->a == 0) { - pList->nExpr = pList->nAlloc = 0; - return pList; - } - } - assert(pList->a != 0); - if (pNode || pToken) { - struct tSqlExprItem *pItem = &pList->a[pList->nExpr++]; - memset(pItem, 0, sizeof(*pItem)); - pItem->pNode = pNode; + struct tSqlExprItem item = {0}; + + item.pNode = pNode; + item.distinct = (pDistinct != NULL); + if (pToken) { // set the as clause - pItem->aliasName = malloc(pToken->n + 1); - strncpy(pItem->aliasName, pToken->z, pToken->n); - pItem->aliasName[pToken->n] = 0; + item.aliasName = malloc(pToken->n + 1); + strncpy(item.aliasName, pToken->z, pToken->n); + item.aliasName[pToken->n] = 0; - strdequote(pItem->aliasName); + strdequote(item.aliasName); } - pItem->distinct = (pDistinct != NULL); + + taosArrayPush(pList, &item); } + return pList; } -void tSqlExprListDestroy(tSQLExprList *pList) { - if (pList == NULL) return; +static void freeExprElem(void* item) { + tSqlExprItem* exprItem = item; - for (int32_t i = 0; i < pList->nExpr; ++i) { - if (pList->a[i].aliasName != NULL) { - free(pList->a[i].aliasName); - } - tSqlExprDestroy(pList->a[i].pNode); + tfree(exprItem->aliasName); + tSqlExprDestroy(exprItem->pNode); +} + +void tSqlExprListDestroy(SArray *pList) { + if (pList == NULL) { + return; } - free(pList->a); - free(pList); + taosArrayDestroyEx(pList, freeExprElem); } -tSQLExpr *tSqlExprIdValueCreate(SStrToken *pToken, int32_t optrType) { - tSQLExpr *pSqlExpr = calloc(1, sizeof(tSQLExpr)); +tSqlExpr *tSqlExprCreateIdValue(SStrToken *pToken, int32_t optrType) { + tSqlExpr *pSqlExpr = calloc(1, sizeof(tSqlExpr)); if (pToken != NULL) { pSqlExpr->token = *pToken; @@ -135,28 +130,33 @@ tSQLExpr *tSqlExprIdValueCreate(SStrToken *pToken, int32_t optrType) { if (optrType == TK_INTEGER || optrType == TK_STRING || optrType == TK_FLOAT || optrType == TK_BOOL) { toTSDBType(pToken->type); - tVariantCreate(&pSqlExpr->val, pToken); - pSqlExpr->nSQLOptr = optrType; + tVariantCreate(&pSqlExpr->value, pToken); + pSqlExpr->tokenId = optrType; + pSqlExpr->type = SQL_NODE_VALUE; } else if (optrType == TK_NOW) { // use microsecond by default - pSqlExpr->val.i64 = taosGetTimestamp(TSDB_TIME_PRECISION_MICRO); - pSqlExpr->val.nType = TSDB_DATA_TYPE_BIGINT; - pSqlExpr->nSQLOptr = TK_TIMESTAMP; // TK_TIMESTAMP used to denote the time value is in microsecond + pSqlExpr->value.i64 = taosGetTimestamp(TSDB_TIME_PRECISION_MICRO); + pSqlExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + pSqlExpr->tokenId = TK_TIMESTAMP; // TK_TIMESTAMP used to denote the time value is in microsecond + pSqlExpr->type = SQL_NODE_VALUE; } else if (optrType == TK_VARIABLE) { - int32_t ret = parseAbsoluteDuration(pToken->z, pToken->n, &pSqlExpr->val.i64); + int32_t ret = parseAbsoluteDuration(pToken->z, pToken->n, &pSqlExpr->value.i64); if (ret != TSDB_CODE_SUCCESS) { terrno = TSDB_CODE_TSC_SQL_SYNTAX_ERROR; } - pSqlExpr->val.nType = TSDB_DATA_TYPE_BIGINT; - pSqlExpr->nSQLOptr = TK_TIMESTAMP; - } else { // it must be the column name (tk_id) if it is not the number + pSqlExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + pSqlExpr->tokenId = TK_TIMESTAMP; + pSqlExpr->type = SQL_NODE_VALUE; + } else { + // Here it must be the column name (tk_id) if it is not a number or string. assert(optrType == TK_ID || optrType == TK_ALL); if (pToken != NULL) { pSqlExpr->colInfo = *pToken; } - pSqlExpr->nSQLOptr = optrType; + pSqlExpr->tokenId = optrType; + pSqlExpr->type = SQL_NODE_TABLE_COLUMN; } return pSqlExpr; @@ -166,32 +166,36 @@ tSQLExpr *tSqlExprIdValueCreate(SStrToken *pToken, int32_t optrType) { * pList is the parameters for function with id(optType) * function name is denoted by pFunctionToken */ -tSQLExpr *tSqlExprCreateFunction(tSQLExprList *pList, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType) { - if (pFuncToken == NULL) return NULL; +tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType) { + if (pFuncToken == NULL) { + return NULL; + } - tSQLExpr *pExpr = calloc(1, sizeof(tSQLExpr)); - pExpr->nSQLOptr = optType; - pExpr->pParam = pList; + tSqlExpr *pExpr = calloc(1, sizeof(tSqlExpr)); + pExpr->tokenId = optType; + pExpr->type = SQL_NODE_SQLFUNCTION; + pExpr->pParam = pParam; int32_t len = (int32_t)((endToken->z + endToken->n) - pFuncToken->z); - pExpr->operand.z = pFuncToken->z; + pExpr->operand = (*pFuncToken); - pExpr->operand.n = len; // raw field name - pExpr->operand.type = pFuncToken->type; + pExpr->token.n = len; + pExpr->token.z = pFuncToken->z; + pExpr->token.type = pFuncToken->type; - pExpr->token = pExpr->operand; return pExpr; } /* * create binary expression in this procedure - * if the expr is arithmetic, calculate the result and set it to tSQLExpr Object + * if the expr is arithmetic, calculate the result and set it to tSqlExpr Object */ -tSQLExpr *tSqlExprCreate(tSQLExpr *pLeft, tSQLExpr *pRight, int32_t optrType) { - tSQLExpr *pExpr = calloc(1, sizeof(tSQLExpr)); +tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType) { + tSqlExpr *pExpr = calloc(1, sizeof(tSqlExpr)); + pExpr->type = SQL_NODE_EXPR; if (pLeft != NULL && pRight != NULL && (optrType != TK_IN)) { - char* endPos = pRight->token.z + pRight->token.n; + char* endPos = pRight->token.z + pRight->token.n; pExpr->token.z = pLeft->token.z; pExpr->token.n = (uint32_t)(endPos - pExpr->token.z); pExpr->token.type = pLeft->token.type; @@ -203,32 +207,33 @@ tSQLExpr *tSqlExprCreate(tSQLExpr *pLeft, tSQLExpr *pRight, int32_t optrType) { * if a token is noted as the TK_TIMESTAMP, the time precision is microsecond * Otherwise, the time precision is adaptive, determined by the time precision from databases. */ - if ((pLeft->nSQLOptr == TK_INTEGER && pRight->nSQLOptr == TK_INTEGER) || - (pLeft->nSQLOptr == TK_TIMESTAMP && pRight->nSQLOptr == TK_TIMESTAMP)) { - pExpr->val.nType = TSDB_DATA_TYPE_BIGINT; - pExpr->nSQLOptr = pLeft->nSQLOptr; + if ((pLeft->tokenId == TK_INTEGER && pRight->tokenId == TK_INTEGER) || + (pLeft->tokenId == TK_TIMESTAMP && pRight->tokenId == TK_TIMESTAMP)) { + pExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + pExpr->tokenId = pLeft->tokenId; + pExpr->type = SQL_NODE_VALUE; switch (optrType) { case TK_PLUS: { - pExpr->val.i64 = pLeft->val.i64 + pRight->val.i64; + pExpr->value.i64 = pLeft->value.i64 + pRight->value.i64; break; } case TK_MINUS: { - pExpr->val.i64 = pLeft->val.i64 - pRight->val.i64; + pExpr->value.i64 = pLeft->value.i64 - pRight->value.i64; break; } case TK_STAR: { - pExpr->val.i64 = pLeft->val.i64 * pRight->val.i64; + pExpr->value.i64 = pLeft->value.i64 * pRight->value.i64; break; } case TK_DIVIDE: { - pExpr->nSQLOptr = TK_FLOAT; - pExpr->val.nType = TSDB_DATA_TYPE_DOUBLE; - pExpr->val.dKey = (double)pLeft->val.i64 / pRight->val.i64; + pExpr->tokenId = TK_FLOAT; + pExpr->value.nType = TSDB_DATA_TYPE_DOUBLE; + pExpr->value.dKey = (double)pLeft->value.i64 / pRight->value.i64; break; } case TK_REM: { - pExpr->val.i64 = pLeft->val.i64 % pRight->val.i64; + pExpr->value.i64 = pLeft->value.i64 % pRight->value.i64; break; } } @@ -236,33 +241,35 @@ tSQLExpr *tSqlExprCreate(tSQLExpr *pLeft, tSQLExpr *pRight, int32_t optrType) { tSqlExprDestroy(pLeft); tSqlExprDestroy(pRight); - } else if ((pLeft->nSQLOptr == TK_FLOAT && pRight->nSQLOptr == TK_INTEGER) || (pLeft->nSQLOptr == TK_INTEGER && pRight->nSQLOptr == TK_FLOAT) || - (pLeft->nSQLOptr == TK_FLOAT && pRight->nSQLOptr == TK_FLOAT)) { - pExpr->val.nType = TSDB_DATA_TYPE_DOUBLE; - pExpr->nSQLOptr = TK_FLOAT; + } else if ((pLeft->tokenId == TK_FLOAT && pRight->tokenId == TK_INTEGER) || + (pLeft->tokenId == TK_INTEGER && pRight->tokenId == TK_FLOAT) || + (pLeft->tokenId == TK_FLOAT && pRight->tokenId == TK_FLOAT)) { + pExpr->value.nType = TSDB_DATA_TYPE_DOUBLE; + pExpr->tokenId = TK_FLOAT; + pExpr->type = SQL_NODE_VALUE; - double left = (pLeft->val.nType == TSDB_DATA_TYPE_DOUBLE) ? pLeft->val.dKey : pLeft->val.i64; - double right = (pRight->val.nType == TSDB_DATA_TYPE_DOUBLE) ? pRight->val.dKey : pRight->val.i64; + double left = (pLeft->value.nType == TSDB_DATA_TYPE_DOUBLE) ? pLeft->value.dKey : pLeft->value.i64; + double right = (pRight->value.nType == TSDB_DATA_TYPE_DOUBLE) ? pRight->value.dKey : pRight->value.i64; switch (optrType) { case TK_PLUS: { - pExpr->val.dKey = left + right; + pExpr->value.dKey = left + right; break; } case TK_MINUS: { - pExpr->val.dKey = left - right; + pExpr->value.dKey = left - right; break; } case TK_STAR: { - pExpr->val.dKey = left * right; + pExpr->value.dKey = left * right; break; } case TK_DIVIDE: { - pExpr->val.dKey = left / right; + pExpr->value.dKey = left / right; break; } case TK_REM: { - pExpr->val.dKey = left - ((int64_t)(left / right)) * right; + pExpr->value.dKey = left - ((int64_t)(left / right)) * right; break; } } @@ -271,25 +278,25 @@ tSQLExpr *tSqlExprCreate(tSQLExpr *pLeft, tSQLExpr *pRight, int32_t optrType) { tSqlExprDestroy(pRight); } else { - pExpr->nSQLOptr = optrType; + pExpr->tokenId = optrType; pExpr->pLeft = pLeft; pExpr->pRight = pRight; } } else if (optrType == TK_IN) { - pExpr->nSQLOptr = optrType; + pExpr->tokenId = optrType; pExpr->pLeft = pLeft; - tSQLExpr *pRSub = calloc(1, sizeof(tSQLExpr)); - pRSub->nSQLOptr = TK_SET; // TODO refactor ..... - pRSub->pParam = (tSQLExprList *)pRight; + tSqlExpr *pRSub = calloc(1, sizeof(tSqlExpr)); + pRSub->tokenId = TK_SET; // TODO refactor ..... + pRSub->pParam = (SArray *)pRight; pExpr->pRight = pRSub; } else { - pExpr->nSQLOptr = optrType; + pExpr->tokenId = optrType; pExpr->pLeft = pLeft; if (pLeft != NULL && pRight == NULL) { - pRight = calloc(1, sizeof(tSQLExpr)); + pRight = calloc(1, sizeof(tSqlExpr)); } pExpr->pRight = pRight; @@ -298,10 +305,8 @@ tSQLExpr *tSqlExprCreate(tSQLExpr *pLeft, tSQLExpr *pRight, int32_t optrType) { return pExpr; } - - -tSQLExpr *tSqlExprClone(tSQLExpr *pSrc) { - tSQLExpr *pExpr = calloc(1, sizeof(tSQLExpr)); +tSqlExpr *tSqlExprClone(tSqlExpr *pSrc) { + tSqlExpr *pExpr = calloc(1, sizeof(tSqlExpr)); memcpy(pExpr, pSrc, sizeof(*pSrc)); @@ -315,26 +320,64 @@ tSQLExpr *tSqlExprClone(tSQLExpr *pSrc) { //we don't clone pParam now because clone is only used for between/and assert(pSrc->pParam == NULL); - return pExpr; } +void tSqlExprCompact(tSqlExpr** pExpr) { + if (*pExpr == NULL || tSqlExprIsParentOfLeaf(*pExpr)) { + return; + } + + if ((*pExpr)->pLeft) { + tSqlExprCompact(&(*pExpr)->pLeft); + } + + if ((*pExpr)->pRight) { + tSqlExprCompact(&(*pExpr)->pRight); + } + + if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight == NULL && ((*pExpr)->tokenId == TK_OR || (*pExpr)->tokenId == TK_AND)) { + tSqlExprDestroy(*pExpr); + *pExpr = NULL; + } else if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight != NULL) { + tSqlExpr* tmpPtr = (*pExpr)->pRight; + (*pExpr)->pRight = NULL; + + tSqlExprDestroy(*pExpr); + (*pExpr) = tmpPtr; + } else if ((*pExpr)->pRight == NULL && (*pExpr)->pLeft != NULL) { + tSqlExpr* tmpPtr = (*pExpr)->pLeft; + (*pExpr)->pLeft = NULL; + + tSqlExprDestroy(*pExpr); + (*pExpr) = tmpPtr; + } +} + +bool tSqlExprIsLeaf(tSqlExpr* pExpr) { + return (pExpr->pRight == NULL && pExpr->pLeft == NULL) && + (pExpr->tokenId == 0 || pExpr->tokenId == TK_ID || (pExpr->tokenId >= TK_BOOL && pExpr->tokenId <= TK_NCHAR) || pExpr->tokenId == TK_SET); +} + +bool tSqlExprIsParentOfLeaf(tSqlExpr* pExpr) { + return (pExpr->pLeft != NULL && pExpr->pRight != NULL) && + (tSqlExprIsLeaf(pExpr->pLeft) && tSqlExprIsLeaf(pExpr->pRight)); +} -void tSqlExprNodeDestroy(tSQLExpr *pExpr) { +static void doDestroySqlExprNode(tSqlExpr *pExpr) { if (pExpr == NULL) { return; } - if (pExpr->nSQLOptr == TK_STRING) { - tVariantDestroy(&pExpr->val); + if (pExpr->tokenId == TK_STRING) { + tVariantDestroy(&pExpr->value); } tSqlExprListDestroy(pExpr->pParam); - free(pExpr); } -void tSqlExprDestroy(tSQLExpr *pExpr) { +void tSqlExprDestroy(tSqlExpr *pExpr) { if (pExpr == NULL) { return; } @@ -343,7 +386,8 @@ void tSqlExprDestroy(tSQLExpr *pExpr) { pExpr->pLeft = NULL; tSqlExprDestroy(pExpr->pRight); pExpr->pRight = NULL; - tSqlExprNodeDestroy(pExpr); + + doDestroySqlExprNode(pExpr); } SArray *tVariantListAppendToken(SArray *pList, SStrToken *pToken, uint8_t order) { @@ -399,13 +443,59 @@ SArray *tVariantListInsert(SArray *pList, tVariant *pVar, uint8_t sortOrder, int return pList; } -void setDbName(SStrToken *pCpxName, SStrToken *pDb) { +SFromInfo *setTableNameList(SFromInfo* pFromInfo, SStrToken *pName, SStrToken* pAlias) { + if (pFromInfo == NULL) { + pFromInfo = calloc(1, sizeof(SFromInfo)); + pFromInfo->tableList = taosArrayInit(4, sizeof(STableNamePair)); + } + + pFromInfo->type = SQL_NODE_FROM_NAMELIST; + STableNamePair p = {.name = *pName}; + if (pAlias != NULL) { + p.aliasName = *pAlias; + } else { + TPARSER_SET_NONE_TOKEN(p.aliasName); + } + + taosArrayPush(pFromInfo->tableList, &p); + + return pFromInfo; +} + +SFromInfo *setSubquery(SFromInfo* pFromInfo, SQuerySqlNode* pSqlNode) { + if (pFromInfo == NULL) { + pFromInfo = calloc(1, sizeof(SFromInfo)); + } + + pFromInfo->type = SQL_NODE_FROM_SUBQUERY; + pFromInfo->pNode->pClause[pFromInfo->pNode->numOfClause - 1] = pSqlNode; + + return pFromInfo; +} + +void* destroyFromInfo(SFromInfo* pFromInfo) { + if (pFromInfo == NULL) { + return NULL; + } + + if (pFromInfo->type == SQL_NODE_FROM_NAMELIST) { + taosArrayDestroy(pFromInfo->tableList); + } else { + destroyAllSelectClause(pFromInfo->pNode); + } + + tfree(pFromInfo); + return NULL; +} + + +void tSetDbName(SStrToken *pCpxName, SStrToken *pDb) { pCpxName->type = pDb->type; pCpxName->z = pDb->z; pCpxName->n = pDb->n; } -void tSqlSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType) { +void tSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType) { int32_t maxLen = sizeof(pField->name) / sizeof(pField->name[0]); // truncate the column name @@ -473,7 +563,7 @@ static int32_t tryParseNameTwoParts(SStrToken *type) { } } -void tSqlSetColumnType(TAOS_FIELD *pField, SStrToken *type) { +void tSetColumnType(TAOS_FIELD *pField, SStrToken *type) { // set the field type invalid pField->type = -1; pField->name[0] = 0; @@ -538,40 +628,60 @@ void tSqlSetColumnType(TAOS_FIELD *pField, SStrToken *type) { /* * extract the select info out of sql string */ -SQuerySQL *tSetQuerySqlElems(SStrToken *pSelectToken, tSQLExprList *pSelection, SArray *pFrom, tSQLExpr *pWhere, - SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, - SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *pGLimit) { - assert(pSelection != NULL); +SQuerySqlNode *tSetQuerySqlNode(SStrToken *pSelectToken, SArray *pSelectList, SFromInfo *pFrom, tSqlExpr *pWhere, + SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, + SSessionWindowVal *pSession, SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, + SLimitVal *psLimit) { + assert(pSelectList != NULL); + + SQuerySqlNode *pSqlNode = calloc(1, sizeof(SQuerySqlNode)); - SQuerySQL *pQuery = calloc(1, sizeof(SQuerySQL)); - pQuery->selectToken = *pSelectToken; - pQuery->selectToken.n = (uint32_t)strlen(pQuery->selectToken.z); // all later sql string are belonged to the stream sql + // all later sql string are belonged to the stream sql + pSqlNode->sqlstr = *pSelectToken; + pSqlNode->sqlstr.n = (uint32_t)strlen(pSqlNode->sqlstr.z); - pQuery->pSelection = pSelection; - pQuery->from = pFrom; - pQuery->pGroupby = pGroupby; - pQuery->pSortOrder = pSortOrder; - pQuery->pWhere = pWhere; + pSqlNode->pSelectList = pSelectList; + pSqlNode->from = pFrom; + pSqlNode->pGroupby = pGroupby; + pSqlNode->pSortOrder = pSortOrder; + pSqlNode->pWhere = pWhere; + pSqlNode->fillType = pFill; if (pLimit != NULL) { - pQuery->limit = *pLimit; + pSqlNode->limit = *pLimit; + } else { + pSqlNode->limit.limit = -1; + pSqlNode->limit.offset = 0; } - if (pGLimit != NULL) { - pQuery->slimit = *pGLimit; + if (psLimit != NULL) { + pSqlNode->slimit = *psLimit; + } else { + pSqlNode->slimit.limit = -1; + pSqlNode->slimit.offset = 0; } if (pInterval != NULL) { - pQuery->interval = pInterval->interval; - pQuery->offset = pInterval->offset; + pSqlNode->interval = *pInterval; + } else { + TPARSER_SET_NONE_TOKEN(pSqlNode->interval.interval); + TPARSER_SET_NONE_TOKEN(pSqlNode->interval.offset); } if (pSliding != NULL) { - pQuery->sliding = *pSliding; + pSqlNode->sliding = *pSliding; + } else { + TPARSER_SET_NONE_TOKEN(pSqlNode->sliding); + } + + if (pSession != NULL) { + pSqlNode->sessionVal = *pSession; + } else { + TPARSER_SET_NONE_TOKEN(pSqlNode->sessionVal.gap); + TPARSER_SET_NONE_TOKEN(pSqlNode->sessionVal.col); } - pQuery->fillType = pFill; - return pQuery; + return pSqlNode; } static void freeVariant(void *pItem) { @@ -587,14 +697,14 @@ void freeCreateTableInfo(void* p) { tfree(pInfo->tagdata.data); } -void doDestroyQuerySql(SQuerySQL *pQuerySql) { +void destroyQuerySqlNode(SQuerySqlNode *pQuerySql) { if (pQuerySql == NULL) { return; } - tSqlExprListDestroy(pQuerySql->pSelection); + tSqlExprListDestroy(pQuerySql->pSelectList); - pQuerySql->pSelection = NULL; + pQuerySql->pSelectList = NULL; tSqlExprDestroy(pQuerySql->pWhere); pQuerySql->pWhere = NULL; @@ -605,8 +715,7 @@ void doDestroyQuerySql(SQuerySQL *pQuerySql) { taosArrayDestroyEx(pQuerySql->pGroupby, freeVariant); pQuerySql->pGroupby = NULL; - taosArrayDestroyEx(pQuerySql->from, freeVariant); - pQuerySql->from = NULL; + pQuerySql->from = destroyFromInfo(pQuerySql->from); taosArrayDestroyEx(pQuerySql->fillType, freeVariant); pQuerySql->fillType = NULL; @@ -620,15 +729,15 @@ void destroyAllSelectClause(SSubclauseInfo *pClause) { } for(int32_t i = 0; i < pClause->numOfClause; ++i) { - SQuerySQL *pQuerySql = pClause->pClause[i]; - doDestroyQuerySql(pQuerySql); + SQuerySqlNode *pQuerySql = pClause->pClause[i]; + destroyQuerySqlNode(pQuerySql); } tfree(pClause->pClause); } -SCreateTableSQL *tSetCreateSqlElems(SArray *pCols, SArray *pTags, SQuerySQL *pSelect, int32_t type) { - SCreateTableSQL *pCreate = calloc(1, sizeof(SCreateTableSQL)); +SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SQuerySqlNode *pSelect, int32_t type) { + SCreateTableSql *pCreate = calloc(1, sizeof(SCreateTableSql)); switch (type) { case TSQL_CREATE_TABLE: { @@ -672,7 +781,7 @@ SCreatedTableInfo createNewChildTableInfo(SStrToken *pTableName, SArray *pTagNam return info; } -SAlterTableInfo *tAlterTableSqlElems(SStrToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableType) { +SAlterTableInfo *tSetAlterTableInfo(SStrToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableType) { SAlterTableInfo *pAlterTable = calloc(1, sizeof(SAlterTableInfo)); pAlterTable->name = *pTableName; @@ -694,8 +803,8 @@ SAlterTableInfo *tAlterTableSqlElems(SStrToken *pTableName, SArray *pCols, SArra return pAlterTable; } -void* destroyCreateTableSql(SCreateTableSQL* pCreate) { - doDestroyQuerySql(pCreate->pSelect); +void* destroyCreateTableSql(SCreateTableSql* pCreate) { + destroyQuerySqlNode(pCreate->pSelect); taosArrayDestroy(pCreate->colInfo.pColumns); taosArrayDestroy(pCreate->colInfo.pTagColumns); @@ -742,7 +851,7 @@ SSubclauseInfo* setSubclause(SSubclauseInfo* pSubclause, void *pSqlExprInfo) { return pSubclause; } - pSubclause->pClause = (SQuerySQL**) tmp; + pSubclause->pClause = (SQuerySqlNode**) tmp; pSubclause->pClause[newSize - 1] = pSqlExprInfo; pSubclause->numOfClause++; @@ -750,7 +859,7 @@ SSubclauseInfo* setSubclause(SSubclauseInfo* pSubclause, void *pSqlExprInfo) { return pSubclause; } -SSqlInfo*setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type) { +SSqlInfo* setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type) { pInfo->type = type; if (type == TSDB_SQL_SELECT) { @@ -773,7 +882,7 @@ SSubclauseInfo* appendSelectClause(SSubclauseInfo *pQueryInfo, void *pSubclause) return pQueryInfo; } - pQueryInfo->pClause = (SQuerySQL**) tmp; + pQueryInfo->pClause = (SQuerySqlNode**) tmp; pQueryInfo->pClause[pQueryInfo->numOfClause++] = pSubclause; return pQueryInfo; @@ -784,7 +893,7 @@ void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pTableNameToken, SStrToken pInfo->pCreateTableInfo->existCheck = (pIfNotExists->n != 0); } -void setDCLSQLElems(SSqlInfo *pInfo, int32_t type, int32_t nParam, ...) { +void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParam, ...) { pInfo->type = type; if (nParam == 0) { return; diff --git a/src/query/src/qTokenizer.c b/src/query/src/qTokenizer.c index cb411be518e7a40306e9dfbc62b93a949609d279..63ec2bd342d3ddfb3b4158d079eb3d33117f271a 100644 --- a/src/query/src/qTokenizer.c +++ b/src/query/src/qTokenizer.c @@ -139,6 +139,7 @@ static SKeyword keywordTable[] = { {"FROM", TK_FROM}, {"VARIABLE", TK_VARIABLE}, {"INTERVAL", TK_INTERVAL}, + {"SESSION", TK_SESSION}, {"FILL", TK_FILL}, {"SLIDING", TK_SLIDING}, {"ORDER", TK_ORDER}, @@ -200,25 +201,6 @@ static SKeyword keywordTable[] = { {"TRIGGER", TK_TRIGGER}, {"VIEW", TK_VIEW}, {"ALL", TK_ALL}, - {"COUNT", TK_COUNT}, - {"SUM", TK_SUM}, - {"AVG", TK_AVG}, - {"MIN", TK_MIN}, - {"MAX", TK_MAX}, - {"FIRST", TK_FIRST}, - {"LAST", TK_LAST}, - {"TOP", TK_TOP}, - {"BOTTOM", TK_BOTTOM}, - {"STDDEV", TK_STDDEV}, - {"PERCENTILE", TK_PERCENTILE}, - {"APERCENTILE", TK_APERCENTILE}, - {"LEASTSQUARES", TK_LEASTSQUARES}, - {"HISTOGRAM", TK_HISTOGRAM}, - {"DIFF", TK_DIFF}, - {"SPREAD", TK_SPREAD}, - {"TWA", TK_TWA}, - {"INTERP", TK_INTERP}, - {"LAST_ROW", TK_LAST_ROW}, {"SEMI", TK_SEMI}, {"NONE", TK_NONE}, {"PREV", TK_PREV}, @@ -228,17 +210,10 @@ static SKeyword keywordTable[] = { {"TBNAME", TK_TBNAME}, {"JOIN", TK_JOIN}, {"METRICS", TK_METRICS}, - {"TBID", TK_TBID}, {"STABLE", TK_STABLE}, {"FILE", TK_FILE}, {"VNODES", TK_VNODES}, {"UNION", TK_UNION}, - {"RATE", TK_RATE}, - {"IRATE", TK_IRATE}, - {"SUM_RATE", TK_SUM_RATE}, - {"SUM_IRATE", TK_SUM_IRATE}, - {"AVG_RATE", TK_AVG_RATE}, - {"AVG_IRATE", TK_AVG_IRATE}, {"CACHELAST", TK_CACHELAST}, {"DISTINCT", TK_DISTINCT}, {"PARTITIONS", TK_PARTITIONS}, @@ -297,7 +272,7 @@ int tSQLKeywordCode(const char* z, int n) { * Return the length of the token that begins at z[0]. * Store the token type in *type before returning. */ -uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { +uint32_t tSQLGetToken(char* z, uint32_t* tokenId) { uint32_t i; switch (*z) { case ' ': @@ -307,121 +282,121 @@ uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { case '\r': { for (i = 1; isspace(z[i]); i++) { } - *tokenType = TK_SPACE; + *tokenId = TK_SPACE; return i; } case ':': { - *tokenType = TK_COLON; + *tokenId = TK_COLON; return 1; } case '-': { if (z[1] == '-') { for (i = 2; z[i] && z[i] != '\n'; i++) { } - *tokenType = TK_COMMENT; + *tokenId = TK_COMMENT; return i; } - *tokenType = TK_MINUS; + *tokenId = TK_MINUS; return 1; } case '(': { - *tokenType = TK_LP; + *tokenId = TK_LP; return 1; } case ')': { - *tokenType = TK_RP; + *tokenId = TK_RP; return 1; } case ';': { - *tokenType = TK_SEMI; + *tokenId = TK_SEMI; return 1; } case '+': { - *tokenType = TK_PLUS; + *tokenId = TK_PLUS; return 1; } case '*': { - *tokenType = TK_STAR; + *tokenId = TK_STAR; return 1; } case '/': { if (z[1] != '*' || z[2] == 0) { - *tokenType = TK_SLASH; + *tokenId = TK_SLASH; return 1; } for (i = 3; z[i] && (z[i] != '/' || z[i - 1] != '*'); i++) { } if (z[i]) i++; - *tokenType = TK_COMMENT; + *tokenId = TK_COMMENT; return i; } case '%': { - *tokenType = TK_REM; + *tokenId = TK_REM; return 1; } case '=': { - *tokenType = TK_EQ; + *tokenId = TK_EQ; return 1 + (z[1] == '='); } case '<': { if (z[1] == '=') { - *tokenType = TK_LE; + *tokenId = TK_LE; return 2; } else if (z[1] == '>') { - *tokenType = TK_NE; + *tokenId = TK_NE; return 2; } else if (z[1] == '<') { - *tokenType = TK_LSHIFT; + *tokenId = TK_LSHIFT; return 2; } else { - *tokenType = TK_LT; + *tokenId = TK_LT; return 1; } } case '>': { if (z[1] == '=') { - *tokenType = TK_GE; + *tokenId = TK_GE; return 2; } else if (z[1] == '>') { - *tokenType = TK_RSHIFT; + *tokenId = TK_RSHIFT; return 2; } else { - *tokenType = TK_GT; + *tokenId = TK_GT; return 1; } } case '!': { if (z[1] != '=') { - *tokenType = TK_ILLEGAL; + *tokenId = TK_ILLEGAL; return 2; } else { - *tokenType = TK_NE; + *tokenId = TK_NE; return 2; } } case '|': { if (z[1] != '|') { - *tokenType = TK_BITOR; + *tokenId = TK_BITOR; return 1; } else { - *tokenType = TK_CONCAT; + *tokenId = TK_CONCAT; return 2; } } case ',': { - *tokenType = TK_COMMA; + *tokenId = TK_COMMA; return 1; } case '&': { - *tokenType = TK_BITAND; + *tokenId = TK_BITAND; return 1; } case '~': { - *tokenType = TK_BITNOT; + *tokenId = TK_BITNOT; return 1; } case '?': { - *tokenType = TK_QUESTION; + *tokenId = TK_QUESTION; return 1; } case '\'': @@ -447,7 +422,7 @@ uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { if (z[i]) i++; if (strEnd) { - *tokenType = TK_STRING; + *tokenId = TK_STRING; return i; } @@ -471,10 +446,10 @@ uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { } } - *tokenType = TK_FLOAT; + *tokenId = TK_FLOAT; return i; } else { - *tokenType = TK_DOT; + *tokenId = TK_DOT; return 1; } } @@ -483,7 +458,7 @@ uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { char next = z[1]; if (next == 'b') { // bin number - *tokenType = TK_BIN; + *tokenId = TK_BIN; for (i = 2; (z[i] == '0' || z[i] == '1'); ++i) { } @@ -493,7 +468,7 @@ uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { return i; } else if (next == 'x') { //hex number - *tokenType = TK_HEX; + *tokenId = TK_HEX; for (i = 2; isdigit(z[i]) || (z[i] >= 'a' && z[i] <= 'f') || (z[i] >= 'A' && z[i] <= 'F'); ++i) { } @@ -513,7 +488,7 @@ uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { case '7': case '8': case '9': { - *tokenType = TK_INTEGER; + *tokenId = TK_INTEGER; for (i = 1; isdigit(z[i]); i++) { } @@ -523,7 +498,7 @@ uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { z[i] == 'U' || z[i] == 'A' || z[i] == 'S' || z[i] == 'M' || z[i] == 'H' || z[i] == 'D' || z[i] == 'N' || z[i] == 'Y' || z[i] == 'W') && (isIdChar[(uint8_t)z[i + 1]] == 0)) { - *tokenType = TK_VARIABLE; + *tokenId = TK_VARIABLE; i += 1; return i; } @@ -534,12 +509,12 @@ uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { while (isdigit(z[i])) { i++; } - *tokenType = TK_FLOAT; + *tokenId = TK_FLOAT; seg++; } if (seg == 4) { // ip address - *tokenType = TK_IPTOKEN; + *tokenId = TK_IPTOKEN; return i; } @@ -549,14 +524,14 @@ uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { while (isdigit(z[i])) { i++; } - *tokenType = TK_FLOAT; + *tokenId = TK_FLOAT; } return i; } case '[': { for (i = 1; z[i] && z[i - 1] != ']'; i++) { } - *tokenType = TK_ID; + *tokenId = TK_ID; return i; } case 'T': @@ -567,7 +542,7 @@ uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { } if ((i == 4 && strncasecmp(z, "true", 4) == 0) || (i == 5 && strncasecmp(z, "false", 5) == 0)) { - *tokenType = TK_BOOL; + *tokenId = TK_BOOL; return i; } } @@ -577,12 +552,12 @@ uint32_t tSQLGetToken(char* z, uint32_t* tokenType) { } for (i = 1; ((z[i] & 0x80) == 0) && isIdChar[(uint8_t) z[i]]; i++) { } - *tokenType = tSQLKeywordCode(z, i); + *tokenId = tSQLKeywordCode(z, i); return i; } } - *tokenType = TK_ILLEGAL; + *tokenId = TK_ILLEGAL; return 0; } diff --git a/src/query/src/qUtil.c b/src/query/src/qUtil.c index b7a7fd28e9dc2f013457e22446ff7bc41d3fef6c..1e7d3c9b58d3594280edc294e2147e7c908b9d0f 100644 --- a/src/query/src/qUtil.c +++ b/src/query/src/qUtil.c @@ -22,6 +22,7 @@ #include "tbuffer.h" #include "tlosertree.h" #include "queryLog.h" +#include "tscompression.h" typedef struct SCompSupporter { STableQueryInfo **pTableQueryInfo; @@ -41,12 +42,11 @@ int32_t getOutputInterResultBufSize(SQuery* pQuery) { } int32_t initResultRowInfo(SResultRowInfo *pResultRowInfo, int32_t size, int16_t type) { - pResultRowInfo->capacity = size; - - pResultRowInfo->type = type; - pResultRowInfo->curIndex = -1; + pResultRowInfo->type = type; pResultRowInfo->size = 0; pResultRowInfo->prevSKey = TSKEY_INITIAL_VAL; + pResultRowInfo->curIndex = -1; + pResultRowInfo->capacity = size; pResultRowInfo->pResult = calloc(pResultRowInfo->capacity, POINTER_BYTES); if (pResultRowInfo->pResult == NULL) { @@ -135,20 +135,22 @@ void clearResultRow(SQueryRuntimeEnv *pRuntimeEnv, SResultRow *pResultRow, int16 if (pResultRow->pageId >= 0) { tFilePage *page = getResBufPage(pRuntimeEnv->pResultBuf, pResultRow->pageId); + int16_t offset = 0; for (int32_t i = 0; i < pRuntimeEnv->pQuery->numOfOutput; ++i) { SResultRowCellInfo *pResultInfo = &pResultRow->pCellInfo[i]; - char * s = getPosInResultPage(pRuntimeEnv, i, pResultRow, page); - size_t size = pRuntimeEnv->pQuery->pExpr1[i].bytes; + int16_t size = pRuntimeEnv->pQuery->pExpr1[i].bytes; + char * s = getPosInResultPage(pRuntimeEnv->pQuery, page, pResultRow->offset, offset); memset(s, 0, size); + offset += size; RESET_RESULT_INFO(pResultInfo); } } pResultRow->numOfRows = 0; pResultRow->pageId = -1; - pResultRow->rowId = -1; + pResultRow->offset = -1; pResultRow->closed = false; if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { @@ -158,13 +160,15 @@ void clearResultRow(SQueryRuntimeEnv *pRuntimeEnv, SResultRow *pResultRow, int16 } } -SResultRowCellInfo* getResultCell(SQueryRuntimeEnv* pRuntimeEnv, const SResultRow* pRow, int32_t index) { - assert(index >= 0 && index < pRuntimeEnv->pQuery->numOfOutput); - return (SResultRowCellInfo*)((char*) pRow->pCellInfo + pRuntimeEnv->rowCellInfoOffset[index]); +// TODO refactor: use macro +SResultRowCellInfo* getResultCell(const SResultRow* pRow, int32_t index, int32_t* offset) { + assert(index >= 0 && offset != NULL); + return (SResultRowCellInfo*)((char*) pRow->pCellInfo + offset[index]); } size_t getResultRowSize(SQueryRuntimeEnv* pRuntimeEnv) { - return (pRuntimeEnv->pQuery->numOfOutput * sizeof(SResultRowCellInfo)) + pRuntimeEnv->interBufSize + sizeof(SResultRow); + SQuery* pQuery = pRuntimeEnv->pQuery; + return (pQuery->numOfOutput * sizeof(SResultRowCellInfo)) + pQuery->interBufSize + sizeof(SResultRow); } SResultRowPool* initResultRowPool(size_t size) { @@ -340,18 +344,18 @@ void cleanupGroupResInfo(SGroupResInfo* pGroupResInfo) { pGroupResInfo->index = 0; } -void initGroupResInfo(SGroupResInfo* pGroupResInfo, SResultRowInfo* pResultInfo, int32_t offset) { +void initGroupResInfo(SGroupResInfo* pGroupResInfo, SResultRowInfo* pResultInfo) { if (pGroupResInfo->pRows != NULL) { taosArrayDestroy(pGroupResInfo->pRows); } pGroupResInfo->pRows = taosArrayFromList(pResultInfo->pResult, pResultInfo->size, POINTER_BYTES); - pGroupResInfo->index = offset; + pGroupResInfo->index = 0; assert(pGroupResInfo->index <= getNumOfTotalRes(pGroupResInfo)); } -bool hasRemainData(SGroupResInfo* pGroupResInfo) { +bool hasRemainDataInCurrentGroup(SGroupResInfo* pGroupResInfo) { if (pGroupResInfo->pRows == NULL) { return false; } @@ -359,6 +363,14 @@ bool hasRemainData(SGroupResInfo* pGroupResInfo) { return pGroupResInfo->index < taosArrayGetSize(pGroupResInfo->pRows); } +bool hasRemainData(SGroupResInfo* pGroupResInfo) { + if (hasRemainDataInCurrentGroup(pGroupResInfo)) { + return true; + } + + return pGroupResInfo->currentGroup < pGroupResInfo->totalGroup; +} + bool incNextGroup(SGroupResInfo* pGroupResInfo) { return (++pGroupResInfo->currentGroup) < pGroupResInfo->totalGroup; } @@ -372,7 +384,7 @@ int32_t getNumOfTotalRes(SGroupResInfo* pGroupResInfo) { return (int32_t) taosArrayGetSize(pGroupResInfo->pRows); } -static int64_t getNumOfResultWindowRes(SQueryRuntimeEnv* pRuntimeEnv, SResultRow *pResultRow) { +static int64_t getNumOfResultWindowRes(SQueryRuntimeEnv* pRuntimeEnv, SResultRow *pResultRow, int32_t* rowCellInfoOffset) { SQuery* pQuery = pRuntimeEnv->pQuery; for (int32_t j = 0; j < pQuery->numOfOutput; ++j) { @@ -386,7 +398,7 @@ static int64_t getNumOfResultWindowRes(SQueryRuntimeEnv* pRuntimeEnv, SResultRow continue; } - SResultRowCellInfo *pResultInfo = getResultCell(pRuntimeEnv, pResultRow, j); + SResultRowCellInfo *pResultInfo = getResultCell(pResultRow, j, rowCellInfoOffset); assert(pResultInfo != NULL); if (pResultInfo->numOfRes > 0) { @@ -437,7 +449,8 @@ static int32_t tableResultComparFn(const void *pLeft, const void *pRight, void * } } -static int32_t mergeIntoGroupResultImpl(SQueryRuntimeEnv *pRuntimeEnv, SGroupResInfo* pGroupResInfo, SArray *pTableList, void* qinfo) { +static int32_t mergeIntoGroupResultImpl(SQueryRuntimeEnv *pRuntimeEnv, SGroupResInfo* pGroupResInfo, SArray *pTableList, + int32_t* rowCellInfoOffset) { bool ascQuery = QUERY_IS_ASC_QUERY(pRuntimeEnv->pQuery); int32_t code = TSDB_CODE_SUCCESS; @@ -455,7 +468,7 @@ static int32_t mergeIntoGroupResultImpl(SQueryRuntimeEnv *pRuntimeEnv, SGroupRes pTableQueryInfoList = malloc(POINTER_BYTES * size); if (pTableQueryInfoList == NULL || posList == NULL || pGroupResInfo->pRows == NULL || pGroupResInfo->pRows == NULL) { - qError("QInfo:%p failed alloc memory", qinfo); + qError("QInfo:%p failed alloc memory", pRuntimeEnv->qinfo); code = TSDB_CODE_QRY_OUT_OF_MEMORY; goto _end; } @@ -491,7 +504,7 @@ static int32_t mergeIntoGroupResultImpl(SQueryRuntimeEnv *pRuntimeEnv, SGroupRes SResultRowInfo *pWindowResInfo = &pTableQueryInfoList[tableIndex]->resInfo; SResultRow *pWindowRes = getResultRow(pWindowResInfo, cs.rowIndex[tableIndex]); - int64_t num = getNumOfResultWindowRes(pRuntimeEnv, pWindowRes); + int64_t num = getNumOfResultWindowRes(pRuntimeEnv, pWindowRes, rowCellInfoOffset); if (num <= 0) { cs.rowIndex[tableIndex] += 1; @@ -527,7 +540,7 @@ static int32_t mergeIntoGroupResultImpl(SQueryRuntimeEnv *pRuntimeEnv, SGroupRes int64_t endt = taosGetTimestampMs(); - qDebug("QInfo:%p result merge completed for group:%d, elapsed time:%" PRId64 " ms", qinfo, + qDebug("QInfo:%p result merge completed for group:%d, elapsed time:%" PRId64 " ms", pRuntimeEnv->qinfo, pGroupResInfo->currentGroup, endt - startt); _end: @@ -538,13 +551,13 @@ static int32_t mergeIntoGroupResultImpl(SQueryRuntimeEnv *pRuntimeEnv, SGroupRes return code; } -int32_t mergeIntoGroupResult(SGroupResInfo* pGroupResInfo, SQInfo *pQInfo) { +int32_t mergeIntoGroupResult(SGroupResInfo* pGroupResInfo, SQueryRuntimeEnv* pRuntimeEnv, int32_t* offset) { int64_t st = taosGetTimestampUs(); while (pGroupResInfo->currentGroup < pGroupResInfo->totalGroup) { - SArray *group = GET_TABLEGROUP(pQInfo, pGroupResInfo->currentGroup); + SArray *group = GET_TABLEGROUP(pRuntimeEnv, pGroupResInfo->currentGroup); - int32_t ret = mergeIntoGroupResultImpl(&pQInfo->runtimeEnv, pGroupResInfo, group, pQInfo); + int32_t ret = mergeIntoGroupResultImpl(pRuntimeEnv, pGroupResInfo, group, offset); if (ret != TSDB_CODE_SUCCESS) { return ret; } @@ -554,19 +567,83 @@ int32_t mergeIntoGroupResult(SGroupResInfo* pGroupResInfo, SQInfo *pQInfo) { break; } - qDebug("QInfo:%p no result in group %d, continue", pQInfo, pGroupResInfo->currentGroup); + qDebug("QInfo:%p no result in group %d, continue", pRuntimeEnv->qinfo, pGroupResInfo->currentGroup); cleanupGroupResInfo(pGroupResInfo); incNextGroup(pGroupResInfo); } - if (pGroupResInfo->currentGroup >= pGroupResInfo->totalGroup && !hasRemainData(pGroupResInfo)) { - SET_STABLE_QUERY_OVER(pQInfo); - } - int64_t elapsedTime = taosGetTimestampUs() - st; - qDebug("QInfo:%p merge res data into group, index:%d, total group:%d, elapsed time:%" PRId64 "us", pQInfo, + qDebug("QInfo:%p merge res data into group, index:%d, total group:%d, elapsed time:%" PRId64 "us", pRuntimeEnv->qinfo, pGroupResInfo->currentGroup, pGroupResInfo->totalGroup, elapsedTime); - pQInfo->runtimeEnv.summary.firstStageMergeTime += elapsedTime; +// pQInfo->summary.firstStageMergeTime += elapsedTime; return TSDB_CODE_SUCCESS; } + +void blockDistInfoToBinary(STableBlockDist* pDist, struct SBufferWriter* bw) { + tbufWriteUint32(bw, pDist->numOfTables); + tbufWriteUint16(bw, pDist->numOfFiles); + tbufWriteUint64(bw, pDist->totalSize); + tbufWriteUint32(bw, pDist->numOfRowsInMemTable); + tbufWriteUint64(bw, taosArrayGetSize(pDist->dataBlockInfos)); + + // compress the binary string + char* p = TARRAY_GET_START(pDist->dataBlockInfos); + + // compress extra bytes + size_t x = taosArrayGetSize(pDist->dataBlockInfos) * pDist->dataBlockInfos->elemSize; + char* tmp = malloc(x + 2); + + bool comp = false; + int32_t len = tsCompressString(p, (int32_t)x, 1, tmp, (int32_t)x, ONE_STAGE_COMP, NULL, 0); + if (len == -1 || len >= x) { // compress failed, do not compress this binary data + comp = false; + len = (int32_t)x; + } else { + comp = true; + } + + tbufWriteUint8(bw, comp); + tbufWriteUint32(bw, len); + if (comp) { + tbufWriteBinary(bw, tmp, len); + } else { + tbufWriteBinary(bw, p, len); + } + tfree(tmp); +} + +void blockDistInfoFromBinary(const char* data, int32_t len, STableBlockDist* pDist) { + SBufferReader br = tbufInitReader(data, len, false); + + pDist->numOfTables = tbufReadUint32(&br); + pDist->numOfFiles = tbufReadUint16(&br); + pDist->totalSize = tbufReadUint64(&br); + pDist->numOfRowsInMemTable = tbufReadUint32(&br); + int64_t numOfBlocks = tbufReadUint64(&br); + + bool comp = tbufReadUint8(&br); + uint32_t compLen = tbufReadUint32(&br); + + size_t originalLen = (size_t) (numOfBlocks*sizeof(SFileBlockInfo)); + + char* outputBuf = NULL; + if (comp) { + outputBuf = malloc(originalLen); + + size_t actualLen = compLen; + const char* compStr = tbufReadBinary(&br, &actualLen); + + int32_t orignalLen = tsDecompressString(compStr, compLen, 1, outputBuf, + (int32_t)originalLen , ONE_STAGE_COMP, NULL, 0); + assert(orignalLen == numOfBlocks*sizeof(SFileBlockInfo)); + } else { + outputBuf = (char*) tbufReadBinary(&br, &originalLen); + } + + pDist->dataBlockInfos = taosArrayFromList(outputBuf, (uint32_t) numOfBlocks, sizeof(SFileBlockInfo)); + if (comp) { + tfree(outputBuf); + } +} + diff --git a/src/query/src/queryMain.c b/src/query/src/queryMain.c index e262a3ad385273f6e7f4b3c2cbe9c075e3d4e86d..d35d4a5ff859899a6c9521ca0918788307675317 100644 --- a/src/query/src/queryMain.c +++ b/src/query/src/queryMain.c @@ -96,7 +96,7 @@ int32_t qCreateQueryInfo(void* tsdb, int32_t vgId, SQueryTableMsg* pQueryMsg, qi } if (param.pSecExprMsg != NULL) { - if ((code = createQueryFuncExprFromMsg(pQueryMsg, pQueryMsg->secondStageOutput, ¶m.pSecExprs, param.pSecExprMsg, param.pTagColumnInfo)) != TSDB_CODE_SUCCESS) { + if ((code = createIndirectQueryFuncExprFromMsg(pQueryMsg, pQueryMsg->secondStageOutput, ¶m.pSecExprs, param.pSecExprMsg, param.pExprs)) != TSDB_CODE_SUCCESS) { goto _over; } } @@ -144,11 +144,11 @@ int32_t qCreateQueryInfo(void* tsdb, int32_t vgId, SQueryTableMsg* pQueryMsg, qi goto _over; } - qDebug("qmsg:%p query on %" PRIzu " tables in one group from client", pQueryMsg, tableGroupInfo.numOfTables); + qDebug("qmsg:%p query on %u tables in one group from client", pQueryMsg, tableGroupInfo.numOfTables); } int64_t el = taosGetTimestampUs() - st; - qDebug("qmsg:%p tag filter completed, numOfTables:%" PRIzu ", elapsed time:%"PRId64"us", pQueryMsg, tableGroupInfo.numOfTables, el); + qDebug("qmsg:%p tag filter completed, numOfTables:%u, elapsed time:%"PRId64"us", pQueryMsg, tableGroupInfo.numOfTables, el); } else { assert(0); } @@ -209,6 +209,7 @@ bool qTableQuery(qinfo_t qinfo) { return false; } + pQInfo->startExecTs = taosGetTimestampSec(); if (isQueryKilled(pQInfo)) { @@ -216,9 +217,10 @@ bool qTableQuery(qinfo_t qinfo) { return doBuildResCheck(pQInfo); } - if (pQInfo->tableqinfoGroupInfo.numOfTables == 0) { + SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; + if (pRuntimeEnv->tableqinfoGroupInfo.numOfTables == 0) { qDebug("QInfo:%p no table exists for query, abort", pQInfo); - setQueryStatus(pQInfo->runtimeEnv.pQuery, QUERY_COMPLETED); + setQueryStatus(pRuntimeEnv, QUERY_COMPLETED); return doBuildResCheck(pQInfo); } @@ -232,26 +234,16 @@ bool qTableQuery(qinfo_t qinfo) { qDebug("QInfo:%p query task is launched", pQInfo); - SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; - if (onlyQueryTags(pQInfo->runtimeEnv.pQuery)) { - assert(pQInfo->runtimeEnv.pQueryHandle == NULL); - buildTagQueryResult(pQInfo); - } else if (pQInfo->runtimeEnv.stableQuery) { - stableQueryImpl(pQInfo); - } else if (pQInfo->runtimeEnv.queryBlockDist){ - buildTableBlockDistResult(pQInfo); - } else { - tableQueryImpl(pQInfo); - } + pRuntimeEnv->outputBuf = pRuntimeEnv->proot->exec(pRuntimeEnv->proot); - SQuery* pQuery = pRuntimeEnv->pQuery; if (isQueryKilled(pQInfo)) { qDebug("QInfo:%p query is killed", pQInfo); - } else if (pQuery->rec.rows == 0) { - qDebug("QInfo:%p over, %" PRIzu " tables queried, %"PRId64" rows are returned", pQInfo, pQInfo->tableqinfoGroupInfo.numOfTables, pQuery->rec.total); + } else if (GET_NUM_OF_RESULTS(pRuntimeEnv) == 0) { + qDebug("QInfo:%p over, %u tables queried, %"PRId64" rows are returned", pQInfo, pRuntimeEnv->tableqinfoGroupInfo.numOfTables, + pRuntimeEnv->resultInfo.total); } else { - qDebug("QInfo:%p query paused, %" PRId64 " rows returned, numOfTotal:%" PRId64 " rows", - pQInfo, pQuery->rec.rows, pQuery->rec.total + pQuery->rec.rows); + qDebug("QInfo:%p query paused, %d rows returned, numOfTotal:%" PRId64 " rows", + pQInfo, GET_NUM_OF_RESULTS(pRuntimeEnv), pRuntimeEnv->resultInfo.total + GET_NUM_OF_RESULTS(pRuntimeEnv)); } return doBuildResCheck(pQInfo); @@ -279,6 +271,7 @@ int32_t qRetrieveQueryResultInfo(qinfo_t qinfo, bool* buildRes, void* pRspContex *buildRes = true; code = pQInfo->code; } else { + SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; SQuery *pQuery = pQInfo->runtimeEnv.pQuery; pthread_mutex_lock(&pQInfo->lock); @@ -286,8 +279,8 @@ int32_t qRetrieveQueryResultInfo(qinfo_t qinfo, bool* buildRes, void* pRspContex assert(pQInfo->rspContext == NULL); if (pQInfo->dataReady == QUERY_RESULT_READY) { *buildRes = true; - qDebug("QInfo:%p retrieve result info, rowsize:%d, rows:%" PRId64 ", code:%s", pQInfo, pQuery->resultRowSize, - pQuery->rec.rows, tstrerror(pQInfo->code)); + qDebug("QInfo:%p retrieve result info, rowsize:%d, rows:%d, code:%s", pQInfo, pQuery->resultRowSize, + GET_NUM_OF_RESULTS(pRuntimeEnv), tstrerror(pQInfo->code)); } else { *buildRes = false; qDebug("QInfo:%p retrieve req set query return result after paused", pQInfo); @@ -309,12 +302,13 @@ int32_t qDumpRetrieveResult(qinfo_t qinfo, SRetrieveTableRsp **pRsp, int32_t *co return TSDB_CODE_QRY_INVALID_QHANDLE; } - SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; SQuery *pQuery = pQInfo->runtimeEnv.pQuery; - size_t size = getResultSize(pQInfo, &pQuery->rec.rows); + SQueryRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; + int32_t s = GET_NUM_OF_RESULTS(pRuntimeEnv); + size_t size = pQuery->resultRowSize * s; size += sizeof(int32_t); - size += sizeof(STableIdInfo) * taosHashGetSize(pQInfo->arrTableIdInfo); + size += sizeof(STableIdInfo) * taosHashGetSize(pRuntimeEnv->pTableRetrieveTsMap); *contLen = (int32_t)(size + sizeof(SRetrieveTableRsp)); @@ -324,27 +318,27 @@ int32_t qDumpRetrieveResult(qinfo_t qinfo, SRetrieveTableRsp **pRsp, int32_t *co return TSDB_CODE_QRY_OUT_OF_MEMORY; } - (*pRsp)->numOfRows = htonl((int32_t)pQuery->rec.rows); + (*pRsp)->numOfRows = htonl((int32_t)s); if (pQInfo->code == TSDB_CODE_SUCCESS) { - (*pRsp)->offset = htobe64(pQuery->limit.offset); - (*pRsp)->useconds = htobe64(pRuntimeEnv->summary.elapsedTime); + (*pRsp)->offset = htobe64(pQInfo->runtimeEnv.currentOffset); + (*pRsp)->useconds = htobe64(pQInfo->summary.elapsedTime); } else { (*pRsp)->offset = 0; - (*pRsp)->useconds = htobe64(pRuntimeEnv->summary.elapsedTime); + (*pRsp)->useconds = htobe64(pQInfo->summary.elapsedTime); } (*pRsp)->precision = htons(pQuery->precision); - if (pQuery->rec.rows > 0 && pQInfo->code == TSDB_CODE_SUCCESS) { + if (GET_NUM_OF_RESULTS(&(pQInfo->runtimeEnv)) > 0 && pQInfo->code == TSDB_CODE_SUCCESS) { doDumpQueryResult(pQInfo, (*pRsp)->data); } else { - setQueryStatus(pQuery, QUERY_OVER); + setQueryStatus(pRuntimeEnv, QUERY_OVER); } pQInfo->rspContext = NULL; pQInfo->dataReady = QUERY_RESULT_NOT_READY; - if (IS_QUERY_KILLED(pQInfo) || Q_STATUS_EQUAL(pQuery->status, QUERY_OVER)) { + if (IS_QUERY_KILLED(pQInfo) || Q_STATUS_EQUAL(pRuntimeEnv->status, QUERY_OVER)) { // here current thread hold the refcount, so it is safe to free tsdbQueryHandle. *continueExec = false; (*pRsp)->completed = 1; // notify no more result to client @@ -394,8 +388,7 @@ int32_t qQueryCompleted(qinfo_t qinfo) { return TSDB_CODE_QRY_INVALID_QHANDLE; } - SQuery* pQuery = pQInfo->runtimeEnv.pQuery; - return isQueryKilled(pQInfo) || Q_STATUS_EQUAL(pQuery->status, QUERY_OVER); + return isQueryKilled(pQInfo) || Q_STATUS_EQUAL(pQInfo->runtimeEnv.status, QUERY_OVER); } void qDestroyQueryInfo(qinfo_t qHandle) { diff --git a/src/query/src/sql.c b/src/query/src/sql.c index bed9121b0411177788193b15afd10490dc187651..1aafe75c32508cdfef0c341d9f848710093590d4 100644 --- a/src/query/src/sql.c +++ b/src/query/src/sql.c @@ -23,7 +23,6 @@ ** input grammar file: */ #include -#include /************ Begin %include sections from the grammar ************************/ #include @@ -77,10 +76,8 @@ ** zero the stack is dynamically sized using realloc() ** ParseARG_SDECL A static variable declaration for the %extra_argument ** ParseARG_PDECL A parameter declaration for the %extra_argument -** ParseARG_PARAM Code to pass %extra_argument as a subroutine parameter ** ParseARG_STORE Code to store %extra_argument into yypParser ** ParseARG_FETCH Code to extract %extra_argument from yypParser -** ParseCTX_* As ParseARG_ except for %extra_context ** YYERRORSYMBOL is the code number of the error symbol. If not ** defined, then do no error processing. ** YYNSTATE the combined number of states. @@ -100,56 +97,49 @@ #endif /************* Begin control #defines *****************************************/ #define YYCODETYPE unsigned short int -#define YYNOCODE 287 +#define YYNOCODE 265 #define YYACTIONTYPE unsigned short int #define ParseTOKENTYPE SStrToken typedef union { int yyinit; ParseTOKENTYPE yy0; - SCreateDbInfo yy100; - int yy116; - SIntervalVal yy126; - tSQLExprList* yy178; - SArray* yy207; - int64_t yy208; - tVariant yy232; - SLimitVal yy314; - SCreateTableSQL* yy414; - SSubclauseInfo* yy441; - tSQLExpr* yy484; - SCreateAcctInfo yy505; - TAOS_FIELD yy517; - SQuerySQL* yy526; - SCreatedTableInfo yy542; + SCreatedTableInfo yy96; + SFromInfo* yy162; + tSqlExpr* yy178; + SCreateAcctInfo yy187; + SArray* yy285; + TAOS_FIELD yy295; + SQuerySqlNode* yy342; + tVariant yy362; + SIntervalVal yy376; + SLimitVal yy438; + int yy460; + SCreateTableSql* yy470; + SSubclauseInfo* yy513; + SSessionWindowVal yy523; + int64_t yy525; + SCreateDbInfo yy526; } YYMINORTYPE; #ifndef YYSTACKDEPTH #define YYSTACKDEPTH 100 #endif #define ParseARG_SDECL SSqlInfo* pInfo; #define ParseARG_PDECL ,SSqlInfo* pInfo -#define ParseARG_PARAM ,pInfo -#define ParseARG_FETCH SSqlInfo* pInfo=yypParser->pInfo; -#define ParseARG_STORE yypParser->pInfo=pInfo; -#define ParseCTX_SDECL -#define ParseCTX_PDECL -#define ParseCTX_PARAM -#define ParseCTX_FETCH -#define ParseCTX_STORE +#define ParseARG_FETCH SSqlInfo* pInfo = yypParser->pInfo +#define ParseARG_STORE yypParser->pInfo = pInfo #define YYFALLBACK 1 -#define YYNSTATE 306 -#define YYNRULE 263 -#define YYNRULE_WITH_ACTION 263 -#define YYNTOKEN 213 -#define YY_MAX_SHIFT 305 -#define YY_MIN_SHIFTREDUCE 494 -#define YY_MAX_SHIFTREDUCE 756 -#define YY_ERROR_ACTION 757 -#define YY_ACCEPT_ACTION 758 -#define YY_NO_ACTION 759 -#define YY_MIN_REDUCE 760 -#define YY_MAX_REDUCE 1022 +#define YYNSTATE 313 +#define YYNRULE 265 +#define YYNTOKEN 188 +#define YY_MAX_SHIFT 312 +#define YY_MIN_SHIFTREDUCE 502 +#define YY_MAX_SHIFTREDUCE 766 +#define YY_ERROR_ACTION 767 +#define YY_ACCEPT_ACTION 768 +#define YY_NO_ACTION 769 +#define YY_MIN_REDUCE 770 +#define YY_MAX_REDUCE 1034 /************* End control #defines *******************************************/ -#define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0]))) /* Define the yytestcase() macro to be a no-op if is not already defined ** otherwise. @@ -214,258 +204,259 @@ typedef union { ** yy_default[] Default action for each state. ** *********** Begin parsing tables **********************************************/ -#define YY_ACTTAB_COUNT (668) +#define YY_ACTTAB_COUNT (676) static const YYACTIONTYPE yy_action[] = { - /* 0 */ 176, 541, 176, 197, 303, 17, 135, 620, 174, 542, - /* 10 */ 1004, 204, 1005, 47, 48, 30, 51, 52, 135, 201, - /* 20 */ 209, 41, 176, 50, 253, 55, 53, 57, 54, 758, - /* 30 */ 305, 203, 1005, 46, 45, 279, 278, 44, 43, 42, - /* 40 */ 47, 48, 214, 51, 52, 906, 928, 209, 41, 541, - /* 50 */ 50, 253, 55, 53, 57, 54, 199, 542, 660, 903, - /* 60 */ 46, 45, 216, 135, 44, 43, 42, 48, 906, 51, - /* 70 */ 52, 30, 956, 209, 41, 917, 50, 253, 55, 53, - /* 80 */ 57, 54, 250, 130, 75, 289, 46, 45, 906, 236, - /* 90 */ 44, 43, 42, 495, 496, 497, 498, 499, 500, 501, - /* 100 */ 502, 503, 504, 505, 506, 507, 304, 925, 81, 226, - /* 110 */ 70, 541, 212, 47, 48, 903, 51, 52, 30, 542, - /* 120 */ 209, 41, 24, 50, 253, 55, 53, 57, 54, 957, - /* 130 */ 36, 248, 704, 46, 45, 135, 664, 44, 43, 42, - /* 140 */ 47, 49, 894, 51, 52, 241, 892, 209, 41, 229, - /* 150 */ 50, 253, 55, 53, 57, 54, 233, 232, 101, 213, - /* 160 */ 46, 45, 903, 289, 44, 43, 42, 23, 267, 298, - /* 170 */ 297, 266, 265, 264, 296, 263, 295, 294, 293, 262, - /* 180 */ 292, 291, 866, 30, 854, 855, 856, 857, 858, 859, - /* 190 */ 860, 861, 862, 863, 864, 865, 867, 868, 51, 52, - /* 200 */ 917, 76, 209, 41, 900, 50, 253, 55, 53, 57, - /* 210 */ 54, 299, 18, 180, 198, 46, 45, 30, 269, 44, - /* 220 */ 43, 42, 208, 717, 272, 269, 708, 903, 711, 185, - /* 230 */ 714, 1001, 208, 717, 69, 186, 708, 906, 711, 77, - /* 240 */ 714, 114, 113, 184, 1000, 644, 215, 221, 641, 657, - /* 250 */ 642, 71, 643, 12, 205, 206, 25, 80, 252, 145, - /* 260 */ 23, 902, 298, 297, 205, 206, 891, 296, 999, 295, - /* 270 */ 294, 293, 24, 292, 291, 874, 223, 224, 872, 873, - /* 280 */ 36, 193, 904, 875, 805, 877, 878, 876, 161, 879, - /* 290 */ 880, 55, 53, 57, 54, 67, 220, 619, 814, 46, - /* 300 */ 45, 235, 161, 44, 43, 42, 99, 104, 192, 44, - /* 310 */ 43, 42, 93, 103, 109, 112, 102, 239, 78, 254, - /* 320 */ 218, 31, 106, 5, 151, 56, 1, 149, 194, 33, - /* 330 */ 150, 88, 83, 87, 30, 56, 169, 165, 716, 128, - /* 340 */ 30, 30, 167, 164, 117, 116, 115, 36, 716, 889, - /* 350 */ 890, 29, 893, 715, 645, 806, 46, 45, 1014, 161, - /* 360 */ 44, 43, 42, 715, 222, 685, 686, 276, 275, 302, - /* 370 */ 301, 122, 3, 162, 706, 273, 672, 710, 903, 713, - /* 380 */ 132, 277, 281, 676, 903, 903, 652, 60, 219, 677, - /* 390 */ 207, 271, 737, 20, 238, 718, 19, 61, 709, 19, - /* 400 */ 712, 64, 630, 905, 256, 632, 31, 258, 31, 60, - /* 410 */ 707, 79, 631, 178, 28, 720, 60, 259, 62, 179, - /* 420 */ 65, 92, 91, 111, 110, 14, 13, 98, 97, 16, - /* 430 */ 15, 648, 181, 649, 6, 646, 175, 647, 127, 125, - /* 440 */ 182, 183, 189, 190, 967, 188, 173, 187, 966, 177, - /* 450 */ 210, 963, 962, 211, 280, 129, 919, 927, 39, 949, - /* 460 */ 948, 934, 936, 131, 146, 899, 144, 147, 148, 36, - /* 470 */ 817, 261, 37, 171, 34, 270, 813, 237, 1019, 89, - /* 480 */ 1018, 1016, 126, 152, 274, 1013, 95, 671, 242, 1012, - /* 490 */ 200, 1010, 153, 835, 246, 35, 32, 916, 38, 172, - /* 500 */ 63, 66, 802, 136, 58, 251, 137, 138, 249, 247, - /* 510 */ 105, 800, 107, 245, 108, 243, 40, 798, 139, 797, - /* 520 */ 290, 100, 225, 282, 163, 795, 283, 794, 793, 792, - /* 530 */ 791, 790, 166, 168, 787, 785, 783, 781, 779, 284, - /* 540 */ 170, 286, 240, 72, 73, 950, 285, 287, 288, 300, - /* 550 */ 756, 195, 227, 217, 260, 228, 755, 230, 196, 191, - /* 560 */ 231, 84, 85, 754, 742, 234, 796, 238, 74, 654, - /* 570 */ 673, 118, 789, 156, 119, 155, 836, 154, 157, 158, - /* 580 */ 160, 159, 120, 788, 2, 121, 780, 4, 870, 255, - /* 590 */ 8, 901, 68, 133, 202, 244, 140, 141, 142, 143, - /* 600 */ 882, 678, 134, 26, 27, 9, 10, 719, 7, 11, - /* 610 */ 721, 21, 22, 257, 82, 583, 579, 80, 577, 576, - /* 620 */ 575, 572, 545, 268, 86, 90, 31, 94, 59, 96, - /* 630 */ 622, 621, 618, 567, 565, 557, 563, 559, 561, 555, - /* 640 */ 553, 586, 585, 584, 582, 581, 580, 578, 574, 573, - /* 650 */ 60, 543, 511, 509, 760, 759, 759, 759, 759, 759, - /* 660 */ 759, 759, 759, 759, 759, 759, 123, 124, + /* 0 */ 910, 549, 201, 310, 205, 139, 937, 3, 166, 550, + /* 10 */ 768, 312, 17, 47, 48, 139, 51, 52, 30, 180, + /* 20 */ 213, 41, 180, 50, 260, 55, 53, 57, 54, 1016, + /* 30 */ 916, 208, 1017, 46, 45, 178, 180, 44, 43, 42, + /* 40 */ 47, 48, 219, 51, 52, 207, 1017, 213, 41, 549, + /* 50 */ 50, 260, 55, 53, 57, 54, 928, 550, 184, 202, + /* 60 */ 46, 45, 913, 218, 44, 43, 42, 48, 934, 51, + /* 70 */ 52, 240, 968, 213, 41, 549, 50, 260, 55, 53, + /* 80 */ 57, 54, 969, 550, 255, 220, 46, 45, 276, 916, + /* 90 */ 44, 43, 42, 503, 504, 505, 506, 507, 508, 509, + /* 100 */ 510, 511, 512, 513, 514, 515, 311, 628, 84, 230, + /* 110 */ 69, 916, 296, 47, 48, 30, 51, 52, 1013, 30, + /* 120 */ 213, 41, 549, 50, 260, 55, 53, 57, 54, 1012, + /* 130 */ 550, 306, 714, 46, 45, 286, 285, 44, 43, 42, + /* 140 */ 47, 49, 904, 51, 52, 224, 1011, 213, 41, 667, + /* 150 */ 50, 260, 55, 53, 57, 54, 216, 916, 902, 913, + /* 160 */ 46, 45, 222, 912, 44, 43, 42, 23, 274, 305, + /* 170 */ 304, 273, 272, 271, 303, 270, 302, 301, 300, 269, + /* 180 */ 299, 298, 876, 139, 864, 865, 866, 867, 868, 869, + /* 190 */ 870, 871, 872, 873, 874, 875, 877, 878, 51, 52, + /* 200 */ 815, 197, 213, 41, 165, 50, 260, 55, 53, 57, + /* 210 */ 54, 198, 18, 81, 226, 46, 45, 283, 282, 44, + /* 220 */ 43, 42, 212, 727, 928, 25, 718, 671, 721, 189, + /* 230 */ 724, 223, 212, 727, 278, 190, 718, 276, 721, 203, + /* 240 */ 724, 117, 116, 188, 899, 900, 29, 903, 257, 233, + /* 250 */ 77, 44, 43, 42, 209, 210, 237, 236, 259, 139, + /* 260 */ 23, 104, 305, 304, 209, 210, 296, 303, 79, 302, + /* 270 */ 301, 300, 73, 299, 298, 884, 132, 30, 882, 883, + /* 280 */ 36, 70, 225, 885, 824, 887, 888, 886, 165, 889, + /* 290 */ 890, 55, 53, 57, 54, 182, 309, 308, 125, 46, + /* 300 */ 45, 1026, 239, 44, 43, 42, 102, 107, 30, 196, + /* 310 */ 261, 73, 96, 106, 112, 115, 105, 914, 217, 36, + /* 320 */ 674, 913, 109, 5, 155, 56, 78, 30, 243, 33, + /* 330 */ 154, 91, 86, 90, 30, 56, 173, 169, 726, 245, + /* 340 */ 30, 68, 171, 168, 120, 119, 118, 12, 726, 279, + /* 350 */ 183, 83, 913, 80, 725, 28, 46, 45, 266, 816, + /* 360 */ 44, 43, 42, 165, 725, 1, 153, 652, 280, 716, + /* 370 */ 649, 913, 650, 901, 651, 284, 695, 696, 913, 664, + /* 380 */ 720, 288, 723, 241, 913, 680, 24, 31, 686, 134, + /* 390 */ 687, 747, 60, 728, 20, 19, 211, 19, 227, 228, + /* 400 */ 719, 61, 722, 185, 638, 717, 730, 64, 31, 263, + /* 410 */ 640, 265, 639, 31, 60, 82, 60, 179, 95, 94, + /* 420 */ 186, 14, 13, 62, 101, 100, 67, 65, 627, 187, + /* 430 */ 16, 15, 656, 654, 657, 655, 114, 113, 130, 128, + /* 440 */ 6, 193, 194, 192, 177, 191, 181, 915, 979, 131, + /* 450 */ 978, 214, 975, 974, 215, 287, 936, 39, 961, 944, + /* 460 */ 946, 133, 960, 929, 244, 137, 129, 150, 911, 149, + /* 470 */ 242, 909, 679, 246, 151, 204, 653, 247, 258, 256, + /* 480 */ 66, 152, 827, 268, 63, 926, 37, 140, 175, 34, + /* 490 */ 248, 253, 277, 823, 1031, 92, 58, 141, 254, 142, + /* 500 */ 1030, 1028, 156, 281, 1025, 98, 1024, 1022, 157, 845, + /* 510 */ 35, 32, 38, 176, 252, 144, 812, 108, 810, 110, + /* 520 */ 111, 808, 807, 229, 167, 805, 804, 803, 250, 802, + /* 530 */ 801, 800, 170, 172, 797, 795, 793, 791, 789, 174, + /* 540 */ 40, 71, 74, 249, 962, 297, 103, 289, 290, 291, + /* 550 */ 292, 293, 294, 295, 307, 199, 766, 221, 231, 232, + /* 560 */ 267, 765, 234, 235, 764, 752, 200, 195, 238, 87, + /* 570 */ 88, 243, 75, 8, 262, 659, 72, 806, 681, 121, + /* 580 */ 206, 135, 122, 799, 160, 123, 159, 846, 158, 161, + /* 590 */ 162, 164, 163, 798, 684, 124, 880, 790, 2, 136, + /* 600 */ 4, 145, 143, 146, 147, 76, 148, 251, 892, 688, + /* 610 */ 138, 26, 264, 729, 9, 10, 27, 7, 11, 21, + /* 620 */ 731, 22, 85, 591, 83, 587, 585, 584, 583, 580, + /* 630 */ 553, 275, 93, 89, 31, 59, 630, 97, 99, 629, + /* 640 */ 626, 575, 573, 565, 571, 567, 569, 563, 561, 594, + /* 650 */ 593, 592, 590, 589, 588, 586, 582, 581, 60, 519, + /* 660 */ 551, 517, 770, 769, 769, 769, 769, 769, 769, 769, + /* 670 */ 769, 769, 769, 769, 126, 127, }; static const YYCODETYPE yy_lookahead[] = { - /* 0 */ 276, 1, 276, 215, 216, 276, 216, 5, 276, 9, - /* 10 */ 286, 285, 286, 13, 14, 216, 16, 17, 216, 235, - /* 20 */ 20, 21, 276, 23, 24, 25, 26, 27, 28, 213, - /* 30 */ 214, 285, 286, 33, 34, 33, 34, 37, 38, 39, - /* 40 */ 13, 14, 235, 16, 17, 261, 216, 20, 21, 1, - /* 50 */ 23, 24, 25, 26, 27, 28, 257, 9, 37, 260, - /* 60 */ 33, 34, 235, 216, 37, 38, 39, 14, 261, 16, - /* 70 */ 17, 216, 282, 20, 21, 259, 23, 24, 25, 26, - /* 80 */ 27, 28, 280, 216, 282, 81, 33, 34, 261, 273, + /* 0 */ 192, 1, 191, 192, 211, 192, 192, 195, 196, 9, + /* 10 */ 189, 190, 253, 13, 14, 192, 16, 17, 192, 253, + /* 20 */ 20, 21, 253, 23, 24, 25, 26, 27, 28, 263, + /* 30 */ 237, 262, 263, 33, 34, 253, 253, 37, 38, 39, + /* 40 */ 13, 14, 234, 16, 17, 262, 263, 20, 21, 1, + /* 50 */ 23, 24, 25, 26, 27, 28, 235, 9, 253, 233, + /* 60 */ 33, 34, 236, 211, 37, 38, 39, 14, 254, 16, + /* 70 */ 17, 250, 259, 20, 21, 1, 23, 24, 25, 26, + /* 80 */ 27, 28, 259, 9, 261, 211, 33, 34, 79, 237, /* 90 */ 37, 38, 39, 45, 46, 47, 48, 49, 50, 51, - /* 100 */ 52, 53, 54, 55, 56, 57, 58, 277, 222, 61, - /* 110 */ 110, 1, 257, 13, 14, 260, 16, 17, 216, 9, - /* 120 */ 20, 21, 104, 23, 24, 25, 26, 27, 28, 282, - /* 130 */ 112, 284, 105, 33, 34, 216, 115, 37, 38, 39, - /* 140 */ 13, 14, 256, 16, 17, 278, 0, 20, 21, 134, - /* 150 */ 23, 24, 25, 26, 27, 28, 141, 142, 76, 257, - /* 160 */ 33, 34, 260, 81, 37, 38, 39, 88, 89, 90, + /* 100 */ 52, 53, 54, 55, 56, 57, 58, 5, 198, 61, + /* 110 */ 110, 237, 81, 13, 14, 192, 16, 17, 253, 192, + /* 120 */ 20, 21, 1, 23, 24, 25, 26, 27, 28, 253, + /* 130 */ 9, 211, 105, 33, 34, 33, 34, 37, 38, 39, + /* 140 */ 13, 14, 232, 16, 17, 67, 253, 20, 21, 37, + /* 150 */ 23, 24, 25, 26, 27, 28, 233, 237, 0, 236, + /* 160 */ 33, 34, 67, 236, 37, 38, 39, 88, 89, 90, /* 170 */ 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, - /* 180 */ 101, 102, 234, 216, 236, 237, 238, 239, 240, 241, - /* 190 */ 242, 243, 244, 245, 246, 247, 248, 249, 16, 17, - /* 200 */ 259, 282, 20, 21, 216, 23, 24, 25, 26, 27, - /* 210 */ 28, 235, 44, 276, 273, 33, 34, 216, 79, 37, - /* 220 */ 38, 39, 1, 2, 257, 79, 5, 260, 7, 61, - /* 230 */ 9, 276, 1, 2, 222, 67, 5, 261, 7, 262, - /* 240 */ 9, 73, 74, 75, 276, 2, 258, 216, 5, 109, - /* 250 */ 7, 274, 9, 104, 33, 34, 116, 108, 37, 110, - /* 260 */ 88, 260, 90, 91, 33, 34, 254, 95, 276, 97, - /* 270 */ 98, 99, 104, 101, 102, 234, 33, 34, 237, 238, - /* 280 */ 112, 276, 251, 242, 221, 244, 245, 246, 225, 248, - /* 290 */ 249, 25, 26, 27, 28, 104, 67, 106, 221, 33, - /* 300 */ 34, 133, 225, 37, 38, 39, 62, 63, 140, 37, - /* 310 */ 38, 39, 68, 69, 70, 71, 72, 105, 222, 15, - /* 320 */ 67, 109, 78, 62, 63, 104, 223, 224, 276, 68, - /* 330 */ 69, 70, 71, 72, 216, 104, 62, 63, 117, 104, - /* 340 */ 216, 216, 68, 69, 70, 71, 72, 112, 117, 253, - /* 350 */ 254, 255, 256, 132, 111, 221, 33, 34, 261, 225, - /* 360 */ 37, 38, 39, 132, 135, 123, 124, 138, 139, 64, - /* 370 */ 65, 66, 219, 220, 1, 257, 105, 5, 260, 7, - /* 380 */ 109, 257, 257, 105, 260, 260, 105, 109, 135, 105, - /* 390 */ 60, 138, 105, 109, 113, 105, 109, 109, 5, 109, - /* 400 */ 7, 109, 105, 261, 105, 105, 109, 105, 109, 109, - /* 410 */ 37, 109, 105, 276, 104, 111, 109, 107, 130, 276, - /* 420 */ 128, 136, 137, 76, 77, 136, 137, 136, 137, 136, - /* 430 */ 137, 5, 276, 7, 104, 5, 276, 7, 62, 63, - /* 440 */ 276, 276, 276, 276, 252, 276, 276, 276, 252, 276, - /* 450 */ 252, 252, 252, 252, 252, 216, 259, 216, 275, 283, - /* 460 */ 283, 216, 216, 216, 216, 216, 263, 216, 216, 112, - /* 470 */ 216, 216, 216, 216, 216, 216, 216, 259, 216, 216, - /* 480 */ 216, 216, 60, 216, 216, 216, 216, 117, 279, 216, - /* 490 */ 279, 216, 216, 216, 279, 216, 216, 272, 216, 216, - /* 500 */ 129, 127, 216, 271, 126, 121, 270, 269, 125, 120, - /* 510 */ 216, 216, 216, 119, 216, 118, 131, 216, 268, 216, - /* 520 */ 103, 87, 216, 86, 216, 216, 50, 216, 216, 216, - /* 530 */ 216, 216, 216, 216, 216, 216, 216, 216, 216, 83, - /* 540 */ 216, 54, 217, 217, 217, 217, 85, 84, 82, 79, - /* 550 */ 5, 217, 143, 217, 217, 5, 5, 143, 217, 217, - /* 560 */ 5, 222, 222, 5, 89, 134, 217, 113, 109, 105, - /* 570 */ 105, 218, 217, 227, 218, 231, 233, 232, 230, 228, - /* 580 */ 226, 229, 218, 217, 223, 218, 217, 219, 250, 107, - /* 590 */ 104, 259, 114, 104, 1, 104, 267, 266, 265, 264, - /* 600 */ 250, 105, 104, 109, 109, 122, 122, 105, 104, 104, - /* 610 */ 111, 104, 104, 107, 76, 9, 5, 108, 5, 5, - /* 620 */ 5, 5, 80, 15, 76, 137, 109, 137, 16, 137, - /* 630 */ 5, 5, 105, 5, 5, 5, 5, 5, 5, 5, - /* 640 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - /* 650 */ 109, 80, 60, 59, 0, 287, 287, 287, 287, 287, - /* 660 */ 287, 287, 287, 287, 287, 287, 21, 21, 287, 287, - /* 670 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 680 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 690 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 700 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 710 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 720 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 730 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 740 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 750 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 760 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 770 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 780 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 790 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 800 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 810 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 820 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 830 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 840 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 850 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 860 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 870 */ 287, 287, 287, 287, 287, 287, 287, 287, 287, 287, - /* 880 */ 287, + /* 180 */ 101, 102, 210, 192, 212, 213, 214, 215, 216, 217, + /* 190 */ 218, 219, 220, 221, 222, 223, 224, 225, 16, 17, + /* 200 */ 197, 253, 20, 21, 201, 23, 24, 25, 26, 27, + /* 210 */ 28, 253, 44, 198, 136, 33, 34, 139, 140, 37, + /* 220 */ 38, 39, 1, 2, 235, 104, 5, 115, 7, 61, + /* 230 */ 9, 136, 1, 2, 139, 67, 5, 79, 7, 250, + /* 240 */ 9, 73, 74, 75, 229, 230, 231, 232, 257, 135, + /* 250 */ 259, 37, 38, 39, 33, 34, 142, 143, 37, 192, + /* 260 */ 88, 76, 90, 91, 33, 34, 81, 95, 238, 97, + /* 270 */ 98, 99, 104, 101, 102, 210, 192, 192, 213, 214, + /* 280 */ 112, 251, 192, 218, 197, 220, 221, 222, 201, 224, + /* 290 */ 225, 25, 26, 27, 28, 253, 64, 65, 66, 33, + /* 300 */ 34, 237, 134, 37, 38, 39, 62, 63, 192, 141, + /* 310 */ 15, 104, 68, 69, 70, 71, 72, 227, 233, 112, + /* 320 */ 105, 236, 78, 62, 63, 104, 259, 192, 113, 68, + /* 330 */ 69, 70, 71, 72, 192, 104, 62, 63, 117, 255, + /* 340 */ 192, 198, 68, 69, 70, 71, 72, 104, 117, 233, + /* 350 */ 253, 108, 236, 110, 133, 104, 33, 34, 107, 197, + /* 360 */ 37, 38, 39, 201, 133, 199, 200, 2, 233, 1, + /* 370 */ 5, 236, 7, 230, 9, 233, 124, 125, 236, 109, + /* 380 */ 5, 233, 7, 105, 236, 105, 116, 109, 105, 109, + /* 390 */ 105, 105, 109, 105, 109, 109, 60, 109, 33, 34, + /* 400 */ 5, 109, 7, 253, 105, 37, 111, 109, 109, 105, + /* 410 */ 105, 105, 105, 109, 109, 109, 109, 253, 137, 138, + /* 420 */ 253, 137, 138, 131, 137, 138, 104, 129, 106, 253, + /* 430 */ 137, 138, 5, 5, 7, 7, 76, 77, 62, 63, + /* 440 */ 104, 253, 253, 253, 253, 253, 253, 237, 228, 192, + /* 450 */ 228, 228, 228, 228, 228, 228, 192, 252, 260, 192, + /* 460 */ 192, 192, 260, 235, 235, 192, 60, 192, 235, 239, + /* 470 */ 193, 192, 117, 256, 192, 256, 111, 118, 122, 126, + /* 480 */ 128, 192, 192, 192, 130, 249, 192, 248, 192, 192, + /* 490 */ 256, 256, 192, 192, 192, 192, 127, 247, 121, 246, + /* 500 */ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, + /* 510 */ 192, 192, 192, 192, 120, 244, 192, 192, 192, 192, + /* 520 */ 192, 192, 192, 192, 192, 192, 192, 192, 119, 192, + /* 530 */ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, + /* 540 */ 132, 193, 193, 193, 193, 103, 87, 86, 50, 83, + /* 550 */ 85, 54, 84, 82, 79, 193, 5, 193, 144, 5, + /* 560 */ 193, 5, 144, 5, 5, 89, 193, 193, 135, 198, + /* 570 */ 198, 113, 109, 104, 107, 105, 114, 193, 105, 194, + /* 580 */ 1, 104, 194, 193, 203, 194, 207, 209, 208, 206, + /* 590 */ 204, 202, 205, 193, 105, 194, 226, 193, 199, 109, + /* 600 */ 195, 243, 245, 242, 241, 104, 240, 104, 226, 105, + /* 610 */ 104, 109, 107, 105, 123, 123, 109, 104, 104, 104, + /* 620 */ 111, 104, 76, 9, 108, 5, 5, 5, 5, 5, + /* 630 */ 80, 15, 138, 76, 109, 16, 5, 138, 138, 5, + /* 640 */ 105, 5, 5, 5, 5, 5, 5, 5, 5, 5, + /* 650 */ 5, 5, 5, 5, 5, 5, 5, 5, 109, 60, + /* 660 */ 80, 59, 0, 264, 264, 264, 264, 264, 264, 264, + /* 670 */ 264, 264, 264, 264, 21, 21, 264, 264, 264, 264, + /* 680 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 690 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 700 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 710 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 720 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 730 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 740 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 750 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 760 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 770 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 780 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 790 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 800 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 810 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 820 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 830 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 840 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 850 */ 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, + /* 860 */ 264, 264, 264, 264, }; -#define YY_SHIFT_COUNT (305) +#define YY_SHIFT_COUNT (312) #define YY_SHIFT_MIN (0) -#define YY_SHIFT_MAX (654) +#define YY_SHIFT_MAX (662) static const unsigned short int yy_shift_ofst[] = { - /* 0 */ 168, 79, 79, 172, 172, 139, 221, 231, 110, 110, - /* 10 */ 110, 110, 110, 110, 110, 110, 110, 0, 48, 231, - /* 20 */ 243, 243, 243, 243, 18, 110, 110, 110, 110, 146, - /* 30 */ 110, 110, 82, 139, 4, 4, 668, 668, 668, 231, + /* 0 */ 168, 79, 79, 172, 172, 9, 221, 231, 74, 74, + /* 10 */ 74, 74, 74, 74, 74, 74, 74, 0, 48, 231, + /* 20 */ 365, 365, 365, 365, 121, 207, 74, 74, 74, 158, + /* 30 */ 74, 74, 185, 9, 31, 31, 676, 676, 676, 231, /* 40 */ 231, 231, 231, 231, 231, 231, 231, 231, 231, 231, - /* 50 */ 231, 231, 231, 231, 231, 231, 231, 231, 231, 243, - /* 60 */ 243, 2, 2, 2, 2, 2, 2, 2, 235, 110, - /* 70 */ 110, 21, 110, 110, 110, 242, 242, 140, 110, 110, - /* 80 */ 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, - /* 90 */ 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, - /* 100 */ 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, - /* 110 */ 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, - /* 120 */ 110, 110, 110, 110, 110, 110, 110, 110, 357, 422, - /* 130 */ 422, 422, 370, 370, 370, 422, 374, 371, 378, 384, - /* 140 */ 383, 389, 394, 397, 385, 357, 422, 422, 422, 417, - /* 150 */ 139, 139, 422, 422, 434, 437, 476, 456, 461, 487, - /* 160 */ 463, 466, 417, 422, 470, 470, 422, 470, 422, 470, - /* 170 */ 422, 668, 668, 27, 100, 127, 100, 100, 53, 182, - /* 180 */ 266, 266, 266, 266, 244, 261, 274, 323, 323, 323, - /* 190 */ 323, 229, 15, 272, 272, 149, 253, 305, 281, 212, - /* 200 */ 271, 278, 284, 287, 290, 372, 393, 373, 330, 304, - /* 210 */ 288, 292, 297, 299, 300, 302, 307, 310, 285, 289, - /* 220 */ 291, 191, 293, 426, 430, 347, 376, 545, 409, 550, - /* 230 */ 551, 414, 555, 558, 475, 431, 454, 464, 478, 482, - /* 240 */ 486, 459, 465, 489, 593, 491, 496, 498, 494, 483, - /* 250 */ 495, 484, 502, 504, 499, 505, 482, 507, 506, 508, - /* 260 */ 509, 538, 606, 611, 613, 614, 615, 616, 542, 608, - /* 270 */ 548, 488, 517, 517, 612, 490, 492, 517, 625, 626, - /* 280 */ 527, 517, 628, 629, 630, 631, 632, 633, 634, 635, - /* 290 */ 636, 637, 638, 639, 640, 641, 642, 643, 644, 541, - /* 300 */ 571, 645, 646, 592, 594, 654, + /* 50 */ 231, 231, 231, 231, 231, 231, 231, 231, 231, 365, + /* 60 */ 365, 102, 102, 102, 102, 102, 102, 102, 74, 74, + /* 70 */ 112, 74, 207, 207, 74, 74, 74, 252, 252, 270, + /* 80 */ 207, 74, 74, 74, 74, 74, 74, 74, 74, 74, + /* 90 */ 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, + /* 100 */ 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, + /* 110 */ 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, + /* 120 */ 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, + /* 130 */ 74, 406, 406, 406, 355, 355, 355, 406, 355, 406, + /* 140 */ 352, 354, 369, 356, 353, 377, 394, 409, 359, 408, + /* 150 */ 406, 406, 406, 442, 9, 9, 406, 406, 459, 461, + /* 160 */ 498, 466, 465, 497, 468, 471, 442, 406, 475, 475, + /* 170 */ 406, 475, 406, 475, 406, 676, 676, 27, 100, 127, + /* 180 */ 100, 100, 53, 182, 266, 266, 266, 266, 244, 261, + /* 190 */ 274, 323, 323, 323, 323, 78, 114, 214, 214, 243, + /* 200 */ 95, 232, 278, 215, 280, 283, 285, 286, 288, 375, + /* 210 */ 395, 368, 336, 295, 292, 298, 299, 304, 305, 306, + /* 220 */ 307, 251, 281, 284, 287, 322, 293, 427, 428, 360, + /* 230 */ 376, 551, 414, 554, 556, 418, 558, 559, 476, 433, + /* 240 */ 458, 467, 469, 462, 470, 463, 473, 477, 489, 490, + /* 250 */ 501, 579, 503, 504, 506, 502, 491, 507, 492, 508, + /* 260 */ 513, 509, 514, 467, 515, 505, 517, 516, 546, 614, + /* 270 */ 620, 621, 622, 623, 624, 550, 616, 557, 494, 525, + /* 280 */ 525, 619, 499, 500, 525, 631, 634, 535, 525, 636, + /* 290 */ 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, + /* 300 */ 647, 648, 649, 650, 651, 652, 549, 580, 653, 654, + /* 310 */ 599, 602, 662, }; -#define YY_REDUCE_COUNT (172) -#define YY_REDUCE_MIN (-276) -#define YY_REDUCE_MAX (369) +#define YY_REDUCE_COUNT (176) +#define YY_REDUCE_MIN (-241) +#define YY_REDUCE_MAX (405) static const short yy_reduce_ofst[] = { - /* 0 */ -184, -52, -52, 41, 41, 96, -274, -254, -201, -153, - /* 10 */ -198, -145, -98, -33, 118, 124, 125, -170, -212, -276, - /* 20 */ -216, -193, -173, -24, -59, -133, -210, -81, -12, -114, - /* 30 */ 31, 1, 63, 12, 77, 134, -23, 103, 153, -271, - /* 40 */ -268, -63, -45, -32, -8, 5, 52, 137, 143, 156, - /* 50 */ 160, 164, 165, 166, 167, 169, 170, 171, 173, 97, - /* 60 */ 142, 192, 196, 198, 199, 200, 201, 202, 197, 239, - /* 70 */ 241, 183, 245, 246, 247, 176, 177, 203, 248, 249, - /* 80 */ 251, 252, 254, 255, 256, 257, 258, 259, 260, 262, - /* 90 */ 263, 264, 265, 267, 268, 269, 270, 273, 275, 276, - /* 100 */ 277, 279, 280, 282, 283, 286, 294, 295, 296, 298, - /* 110 */ 301, 303, 306, 308, 309, 311, 312, 313, 314, 315, - /* 120 */ 316, 317, 318, 319, 320, 321, 322, 324, 218, 325, - /* 130 */ 326, 327, 209, 211, 215, 328, 225, 232, 236, 238, - /* 140 */ 250, 329, 331, 333, 335, 332, 334, 336, 337, 338, - /* 150 */ 339, 340, 341, 342, 343, 345, 344, 346, 348, 351, - /* 160 */ 352, 354, 350, 349, 353, 356, 355, 364, 366, 367, - /* 170 */ 369, 361, 368, + /* 0 */ -179, -28, -28, 65, 65, 15, -231, -217, -174, -177, + /* 10 */ -9, -77, 85, 116, 135, 142, 148, -186, -189, -234, + /* 20 */ -207, -148, -126, -80, 84, -11, -187, 67, -192, -90, + /* 30 */ 90, -73, 3, 143, 87, 162, 30, 166, -188, -241, + /* 40 */ -218, -195, -135, -124, -107, -52, -42, 42, 97, 150, + /* 50 */ 164, 167, 176, 188, 189, 190, 191, 192, 193, 64, + /* 60 */ 210, 220, 222, 223, 224, 225, 226, 227, 257, 264, + /* 70 */ 205, 267, 228, 229, 268, 269, 273, 198, 202, 230, + /* 80 */ 233, 275, 279, 282, 289, 290, 291, 294, 296, 297, + /* 90 */ 300, 301, 302, 303, 308, 309, 310, 311, 312, 313, + /* 100 */ 314, 315, 316, 317, 318, 319, 320, 321, 324, 325, + /* 110 */ 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, + /* 120 */ 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, + /* 130 */ 347, 277, 348, 349, 217, 219, 234, 350, 235, 351, + /* 140 */ 236, 239, 250, 253, 357, 271, 358, 361, 363, 366, + /* 150 */ 362, 364, 367, 370, 371, 372, 373, 374, 378, 380, + /* 160 */ 379, 381, 383, 386, 387, 389, 382, 384, 385, 388, + /* 170 */ 390, 391, 400, 401, 404, 399, 405, }; static const YYACTIONTYPE yy_default[] = { - /* 0 */ 757, 869, 815, 881, 803, 812, 1007, 1007, 757, 757, - /* 10 */ 757, 757, 757, 757, 757, 757, 757, 929, 776, 1007, - /* 20 */ 757, 757, 757, 757, 757, 757, 757, 757, 757, 812, - /* 30 */ 757, 757, 818, 812, 818, 818, 924, 853, 871, 757, - /* 40 */ 757, 757, 757, 757, 757, 757, 757, 757, 757, 757, - /* 50 */ 757, 757, 757, 757, 757, 757, 757, 757, 757, 757, - /* 60 */ 757, 757, 757, 757, 757, 757, 757, 757, 757, 757, - /* 70 */ 757, 931, 933, 935, 757, 953, 953, 922, 757, 757, - /* 80 */ 757, 757, 757, 757, 757, 757, 757, 757, 757, 757, - /* 90 */ 757, 757, 757, 757, 757, 757, 757, 757, 757, 757, - /* 100 */ 757, 757, 757, 757, 757, 801, 757, 799, 757, 757, - /* 110 */ 757, 757, 757, 757, 757, 757, 757, 757, 757, 757, - /* 120 */ 757, 757, 786, 757, 757, 757, 757, 757, 757, 778, - /* 130 */ 778, 778, 757, 757, 757, 778, 960, 964, 958, 946, - /* 140 */ 954, 945, 941, 940, 968, 757, 778, 778, 778, 816, - /* 150 */ 812, 812, 778, 778, 834, 832, 830, 822, 828, 824, - /* 160 */ 826, 820, 804, 778, 810, 810, 778, 810, 778, 810, - /* 170 */ 778, 853, 871, 757, 969, 757, 1006, 959, 996, 995, - /* 180 */ 1002, 994, 993, 992, 757, 757, 757, 988, 989, 991, - /* 190 */ 990, 757, 757, 998, 997, 757, 757, 757, 757, 757, - /* 200 */ 757, 757, 757, 757, 757, 757, 757, 757, 971, 757, - /* 210 */ 965, 961, 757, 757, 757, 757, 757, 757, 757, 757, - /* 220 */ 757, 883, 757, 757, 757, 757, 757, 757, 757, 757, - /* 230 */ 757, 757, 757, 757, 757, 757, 921, 757, 757, 757, - /* 240 */ 757, 932, 757, 757, 757, 757, 757, 757, 955, 757, - /* 250 */ 947, 757, 757, 757, 757, 757, 895, 757, 757, 757, - /* 260 */ 757, 757, 757, 757, 757, 757, 757, 757, 757, 757, - /* 270 */ 757, 757, 1017, 1015, 757, 757, 757, 1011, 757, 757, - /* 280 */ 757, 1009, 757, 757, 757, 757, 757, 757, 757, 757, - /* 290 */ 757, 757, 757, 757, 757, 757, 757, 757, 757, 837, - /* 300 */ 757, 784, 782, 757, 774, 757, + /* 0 */ 767, 879, 825, 891, 813, 822, 1019, 1019, 767, 767, + /* 10 */ 767, 767, 767, 767, 767, 767, 767, 938, 786, 1019, + /* 20 */ 767, 767, 767, 767, 767, 767, 767, 767, 767, 822, + /* 30 */ 767, 767, 828, 822, 828, 828, 933, 863, 881, 767, + /* 40 */ 767, 767, 767, 767, 767, 767, 767, 767, 767, 767, + /* 50 */ 767, 767, 767, 767, 767, 767, 767, 767, 767, 767, + /* 60 */ 767, 767, 767, 767, 767, 767, 767, 767, 767, 767, + /* 70 */ 940, 943, 767, 767, 945, 767, 767, 965, 965, 931, + /* 80 */ 767, 767, 767, 767, 767, 767, 767, 767, 767, 767, + /* 90 */ 767, 767, 767, 767, 767, 767, 767, 767, 767, 767, + /* 100 */ 767, 767, 767, 767, 767, 767, 767, 767, 811, 767, + /* 110 */ 809, 767, 767, 767, 767, 767, 767, 767, 767, 767, + /* 120 */ 767, 767, 767, 767, 767, 796, 767, 767, 767, 767, + /* 130 */ 767, 788, 788, 788, 767, 767, 767, 788, 767, 788, + /* 140 */ 972, 976, 970, 958, 966, 957, 953, 951, 950, 980, + /* 150 */ 788, 788, 788, 826, 822, 822, 788, 788, 844, 842, + /* 160 */ 840, 832, 838, 834, 836, 830, 814, 788, 820, 820, + /* 170 */ 788, 820, 788, 820, 788, 863, 881, 767, 981, 767, + /* 180 */ 1018, 971, 1008, 1007, 1014, 1006, 1005, 1004, 767, 767, + /* 190 */ 767, 1000, 1001, 1003, 1002, 767, 767, 1010, 1009, 767, + /* 200 */ 767, 767, 767, 767, 767, 767, 767, 767, 767, 767, + /* 210 */ 767, 767, 983, 767, 977, 973, 767, 767, 767, 767, + /* 220 */ 767, 767, 767, 767, 767, 893, 767, 767, 767, 767, + /* 230 */ 767, 767, 767, 767, 767, 767, 767, 767, 767, 767, + /* 240 */ 930, 767, 767, 767, 767, 941, 767, 767, 767, 767, + /* 250 */ 767, 767, 767, 767, 767, 967, 767, 959, 767, 767, + /* 260 */ 767, 767, 767, 905, 767, 767, 767, 767, 767, 767, + /* 270 */ 767, 767, 767, 767, 767, 767, 767, 767, 767, 1029, + /* 280 */ 1027, 767, 767, 767, 1023, 767, 767, 767, 1021, 767, + /* 290 */ 767, 767, 767, 767, 767, 767, 767, 767, 767, 767, + /* 300 */ 767, 767, 767, 767, 767, 767, 847, 767, 794, 792, + /* 310 */ 767, 784, 767, }; /********** End of lemon-generated parsing tables *****************************/ @@ -604,6 +595,7 @@ static const YYCODETYPE yyFallback[] = { 0, /* FROM => nothing */ 0, /* VARIABLE => nothing */ 0, /* INTERVAL => nothing */ + 0, /* SESSION => nothing */ 0, /* FILL => nothing */ 0, /* SLIDING => nothing */ 0, /* ORDER => nothing */ @@ -660,32 +652,6 @@ static const YYCODETYPE yyFallback[] = { 1, /* STATEMENT => ID */ 1, /* TRIGGER => ID */ 1, /* VIEW => ID */ - 1, /* COUNT => ID */ - 1, /* SUM => ID */ - 1, /* AVG => ID */ - 1, /* MIN => ID */ - 1, /* MAX => ID */ - 1, /* FIRST => ID */ - 1, /* LAST => ID */ - 1, /* TOP => ID */ - 1, /* BOTTOM => ID */ - 1, /* STDDEV => ID */ - 1, /* PERCENTILE => ID */ - 1, /* APERCENTILE => ID */ - 1, /* LEASTSQUARES => ID */ - 1, /* HISTOGRAM => ID */ - 1, /* DIFF => ID */ - 1, /* SPREAD => ID */ - 1, /* TWA => ID */ - 1, /* INTERP => ID */ - 1, /* LAST_ROW => ID */ - 1, /* RATE => ID */ - 1, /* IRATE => ID */ - 1, /* SUM_RATE => ID */ - 1, /* SUM_IRATE => ID */ - 1, /* AVG_RATE => ID */ - 1, /* AVG_IRATE => ID */ - 1, /* TBID => ID */ 1, /* SEMI => ID */ 1, /* NONE => ID */ 1, /* PREV => ID */ @@ -737,7 +703,6 @@ struct yyParser { int yyerrcnt; /* Shifts left before out of the error */ #endif ParseARG_SDECL /* A place to hold %extra_argument */ - ParseCTX_SDECL /* A place to hold %extra_context */ #if YYSTACKDEPTH<=0 int yystksz; /* Current side of the stack */ yyStackEntry *yystack; /* The parser's stack */ @@ -904,174 +869,151 @@ static const char *const yyTokenName[] = { /* 116 */ "FROM", /* 117 */ "VARIABLE", /* 118 */ "INTERVAL", - /* 119 */ "FILL", - /* 120 */ "SLIDING", - /* 121 */ "ORDER", - /* 122 */ "BY", - /* 123 */ "ASC", - /* 124 */ "DESC", - /* 125 */ "GROUP", - /* 126 */ "HAVING", - /* 127 */ "LIMIT", - /* 128 */ "OFFSET", - /* 129 */ "SLIMIT", - /* 130 */ "SOFFSET", - /* 131 */ "WHERE", - /* 132 */ "NOW", - /* 133 */ "RESET", - /* 134 */ "QUERY", - /* 135 */ "ADD", - /* 136 */ "COLUMN", - /* 137 */ "TAG", - /* 138 */ "CHANGE", - /* 139 */ "SET", - /* 140 */ "KILL", - /* 141 */ "CONNECTION", - /* 142 */ "STREAM", - /* 143 */ "COLON", - /* 144 */ "ABORT", - /* 145 */ "AFTER", - /* 146 */ "ATTACH", - /* 147 */ "BEFORE", - /* 148 */ "BEGIN", - /* 149 */ "CASCADE", - /* 150 */ "CLUSTER", - /* 151 */ "CONFLICT", - /* 152 */ "COPY", - /* 153 */ "DEFERRED", - /* 154 */ "DELIMITERS", - /* 155 */ "DETACH", - /* 156 */ "EACH", - /* 157 */ "END", - /* 158 */ "EXPLAIN", - /* 159 */ "FAIL", - /* 160 */ "FOR", - /* 161 */ "IGNORE", - /* 162 */ "IMMEDIATE", - /* 163 */ "INITIALLY", - /* 164 */ "INSTEAD", - /* 165 */ "MATCH", - /* 166 */ "KEY", - /* 167 */ "OF", - /* 168 */ "RAISE", - /* 169 */ "REPLACE", - /* 170 */ "RESTRICT", - /* 171 */ "ROW", - /* 172 */ "STATEMENT", - /* 173 */ "TRIGGER", - /* 174 */ "VIEW", - /* 175 */ "COUNT", - /* 176 */ "SUM", - /* 177 */ "AVG", - /* 178 */ "MIN", - /* 179 */ "MAX", - /* 180 */ "FIRST", - /* 181 */ "LAST", - /* 182 */ "TOP", - /* 183 */ "BOTTOM", - /* 184 */ "STDDEV", - /* 185 */ "PERCENTILE", - /* 186 */ "APERCENTILE", - /* 187 */ "LEASTSQUARES", - /* 188 */ "HISTOGRAM", - /* 189 */ "DIFF", - /* 190 */ "SPREAD", - /* 191 */ "TWA", - /* 192 */ "INTERP", - /* 193 */ "LAST_ROW", - /* 194 */ "RATE", - /* 195 */ "IRATE", - /* 196 */ "SUM_RATE", - /* 197 */ "SUM_IRATE", - /* 198 */ "AVG_RATE", - /* 199 */ "AVG_IRATE", - /* 200 */ "TBID", - /* 201 */ "SEMI", - /* 202 */ "NONE", - /* 203 */ "PREV", - /* 204 */ "LINEAR", - /* 205 */ "IMPORT", - /* 206 */ "METRIC", - /* 207 */ "TBNAME", - /* 208 */ "JOIN", - /* 209 */ "METRICS", - /* 210 */ "INSERT", - /* 211 */ "INTO", - /* 212 */ "VALUES", - /* 213 */ "program", - /* 214 */ "cmd", - /* 215 */ "dbPrefix", - /* 216 */ "ids", - /* 217 */ "cpxName", - /* 218 */ "ifexists", - /* 219 */ "alter_db_optr", - /* 220 */ "alter_topic_optr", - /* 221 */ "acct_optr", - /* 222 */ "ifnotexists", - /* 223 */ "db_optr", - /* 224 */ "topic_optr", - /* 225 */ "pps", - /* 226 */ "tseries", - /* 227 */ "dbs", - /* 228 */ "streams", - /* 229 */ "storage", - /* 230 */ "qtime", - /* 231 */ "users", - /* 232 */ "conns", - /* 233 */ "state", - /* 234 */ "keep", - /* 235 */ "tagitemlist", - /* 236 */ "cache", - /* 237 */ "replica", - /* 238 */ "quorum", - /* 239 */ "days", - /* 240 */ "minrows", - /* 241 */ "maxrows", - /* 242 */ "blocks", - /* 243 */ "ctime", - /* 244 */ "wal", - /* 245 */ "fsync", - /* 246 */ "comp", - /* 247 */ "prec", - /* 248 */ "update", - /* 249 */ "cachelast", - /* 250 */ "partitions", - /* 251 */ "typename", - /* 252 */ "signed", - /* 253 */ "create_table_args", - /* 254 */ "create_stable_args", - /* 255 */ "create_table_list", - /* 256 */ "create_from_stable", - /* 257 */ "columnlist", - /* 258 */ "tagNamelist", - /* 259 */ "select", - /* 260 */ "column", - /* 261 */ "tagitem", - /* 262 */ "selcollist", - /* 263 */ "from", - /* 264 */ "where_opt", - /* 265 */ "interval_opt", - /* 266 */ "fill_opt", - /* 267 */ "sliding_opt", - /* 268 */ "groupby_opt", - /* 269 */ "orderby_opt", - /* 270 */ "having_opt", - /* 271 */ "slimit_opt", - /* 272 */ "limit_opt", - /* 273 */ "union", - /* 274 */ "sclp", - /* 275 */ "distinct", - /* 276 */ "expr", - /* 277 */ "as", - /* 278 */ "tablelist", - /* 279 */ "tmvar", - /* 280 */ "sortlist", - /* 281 */ "sortitem", - /* 282 */ "item", - /* 283 */ "sortorder", - /* 284 */ "grouplist", - /* 285 */ "exprlist", - /* 286 */ "expritem", + /* 119 */ "SESSION", + /* 120 */ "FILL", + /* 121 */ "SLIDING", + /* 122 */ "ORDER", + /* 123 */ "BY", + /* 124 */ "ASC", + /* 125 */ "DESC", + /* 126 */ "GROUP", + /* 127 */ "HAVING", + /* 128 */ "LIMIT", + /* 129 */ "OFFSET", + /* 130 */ "SLIMIT", + /* 131 */ "SOFFSET", + /* 132 */ "WHERE", + /* 133 */ "NOW", + /* 134 */ "RESET", + /* 135 */ "QUERY", + /* 136 */ "ADD", + /* 137 */ "COLUMN", + /* 138 */ "TAG", + /* 139 */ "CHANGE", + /* 140 */ "SET", + /* 141 */ "KILL", + /* 142 */ "CONNECTION", + /* 143 */ "STREAM", + /* 144 */ "COLON", + /* 145 */ "ABORT", + /* 146 */ "AFTER", + /* 147 */ "ATTACH", + /* 148 */ "BEFORE", + /* 149 */ "BEGIN", + /* 150 */ "CASCADE", + /* 151 */ "CLUSTER", + /* 152 */ "CONFLICT", + /* 153 */ "COPY", + /* 154 */ "DEFERRED", + /* 155 */ "DELIMITERS", + /* 156 */ "DETACH", + /* 157 */ "EACH", + /* 158 */ "END", + /* 159 */ "EXPLAIN", + /* 160 */ "FAIL", + /* 161 */ "FOR", + /* 162 */ "IGNORE", + /* 163 */ "IMMEDIATE", + /* 164 */ "INITIALLY", + /* 165 */ "INSTEAD", + /* 166 */ "MATCH", + /* 167 */ "KEY", + /* 168 */ "OF", + /* 169 */ "RAISE", + /* 170 */ "REPLACE", + /* 171 */ "RESTRICT", + /* 172 */ "ROW", + /* 173 */ "STATEMENT", + /* 174 */ "TRIGGER", + /* 175 */ "VIEW", + /* 176 */ "SEMI", + /* 177 */ "NONE", + /* 178 */ "PREV", + /* 179 */ "LINEAR", + /* 180 */ "IMPORT", + /* 181 */ "METRIC", + /* 182 */ "TBNAME", + /* 183 */ "JOIN", + /* 184 */ "METRICS", + /* 185 */ "INSERT", + /* 186 */ "INTO", + /* 187 */ "VALUES", + /* 188 */ "error", + /* 189 */ "program", + /* 190 */ "cmd", + /* 191 */ "dbPrefix", + /* 192 */ "ids", + /* 193 */ "cpxName", + /* 194 */ "ifexists", + /* 195 */ "alter_db_optr", + /* 196 */ "alter_topic_optr", + /* 197 */ "acct_optr", + /* 198 */ "ifnotexists", + /* 199 */ "db_optr", + /* 200 */ "topic_optr", + /* 201 */ "pps", + /* 202 */ "tseries", + /* 203 */ "dbs", + /* 204 */ "streams", + /* 205 */ "storage", + /* 206 */ "qtime", + /* 207 */ "users", + /* 208 */ "conns", + /* 209 */ "state", + /* 210 */ "keep", + /* 211 */ "tagitemlist", + /* 212 */ "cache", + /* 213 */ "replica", + /* 214 */ "quorum", + /* 215 */ "days", + /* 216 */ "minrows", + /* 217 */ "maxrows", + /* 218 */ "blocks", + /* 219 */ "ctime", + /* 220 */ "wal", + /* 221 */ "fsync", + /* 222 */ "comp", + /* 223 */ "prec", + /* 224 */ "update", + /* 225 */ "cachelast", + /* 226 */ "partitions", + /* 227 */ "typename", + /* 228 */ "signed", + /* 229 */ "create_table_args", + /* 230 */ "create_stable_args", + /* 231 */ "create_table_list", + /* 232 */ "create_from_stable", + /* 233 */ "columnlist", + /* 234 */ "tagNamelist", + /* 235 */ "select", + /* 236 */ "column", + /* 237 */ "tagitem", + /* 238 */ "selcollist", + /* 239 */ "from", + /* 240 */ "where_opt", + /* 241 */ "interval_opt", + /* 242 */ "session_option", + /* 243 */ "fill_opt", + /* 244 */ "sliding_opt", + /* 245 */ "groupby_opt", + /* 246 */ "orderby_opt", + /* 247 */ "having_opt", + /* 248 */ "slimit_opt", + /* 249 */ "limit_opt", + /* 250 */ "union", + /* 251 */ "sclp", + /* 252 */ "distinct", + /* 253 */ "expr", + /* 254 */ "as", + /* 255 */ "tablelist", + /* 256 */ "tmvar", + /* 257 */ "sortlist", + /* 258 */ "sortitem", + /* 259 */ "item", + /* 260 */ "sortorder", + /* 261 */ "grouplist", + /* 262 */ "exprlist", + /* 263 */ "expritem", }; #endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */ @@ -1235,23 +1177,23 @@ static const char *const yyRuleName[] = { /* 153 */ "tagitem ::= MINUS FLOAT", /* 154 */ "tagitem ::= PLUS INTEGER", /* 155 */ "tagitem ::= PLUS FLOAT", - /* 156 */ "select ::= SELECT selcollist from where_opt interval_opt fill_opt sliding_opt groupby_opt orderby_opt having_opt slimit_opt limit_opt", - /* 157 */ "union ::= select", - /* 158 */ "union ::= LP union RP", + /* 156 */ "select ::= SELECT selcollist from where_opt interval_opt session_option fill_opt sliding_opt groupby_opt orderby_opt having_opt slimit_opt limit_opt", + /* 157 */ "select ::= LP select RP", + /* 158 */ "union ::= select", /* 159 */ "union ::= union UNION ALL select", - /* 160 */ "union ::= union UNION ALL LP select RP", - /* 161 */ "cmd ::= union", - /* 162 */ "select ::= SELECT selcollist", - /* 163 */ "sclp ::= selcollist COMMA", - /* 164 */ "sclp ::=", - /* 165 */ "selcollist ::= sclp distinct expr as", - /* 166 */ "selcollist ::= sclp STAR", - /* 167 */ "as ::= AS ids", - /* 168 */ "as ::= ids", - /* 169 */ "as ::=", - /* 170 */ "distinct ::= DISTINCT", - /* 171 */ "distinct ::=", - /* 172 */ "from ::= FROM tablelist", + /* 160 */ "cmd ::= union", + /* 161 */ "select ::= SELECT selcollist", + /* 162 */ "sclp ::= selcollist COMMA", + /* 163 */ "sclp ::=", + /* 164 */ "selcollist ::= sclp distinct expr as", + /* 165 */ "selcollist ::= sclp STAR", + /* 166 */ "as ::= AS ids", + /* 167 */ "as ::= ids", + /* 168 */ "as ::=", + /* 169 */ "distinct ::= DISTINCT", + /* 170 */ "distinct ::=", + /* 171 */ "from ::= FROM tablelist", + /* 172 */ "from ::= FROM LP union RP", /* 173 */ "tablelist ::= ids cpxName", /* 174 */ "tablelist ::= ids cpxName ids", /* 175 */ "tablelist ::= tablelist COMMA ids cpxName", @@ -1260,88 +1202,90 @@ static const char *const yyRuleName[] = { /* 178 */ "interval_opt ::= INTERVAL LP tmvar RP", /* 179 */ "interval_opt ::= INTERVAL LP tmvar COMMA tmvar RP", /* 180 */ "interval_opt ::=", - /* 181 */ "fill_opt ::=", - /* 182 */ "fill_opt ::= FILL LP ID COMMA tagitemlist RP", - /* 183 */ "fill_opt ::= FILL LP ID RP", - /* 184 */ "sliding_opt ::= SLIDING LP tmvar RP", - /* 185 */ "sliding_opt ::=", - /* 186 */ "orderby_opt ::=", - /* 187 */ "orderby_opt ::= ORDER BY sortlist", - /* 188 */ "sortlist ::= sortlist COMMA item sortorder", - /* 189 */ "sortlist ::= item sortorder", - /* 190 */ "item ::= ids cpxName", - /* 191 */ "sortorder ::= ASC", - /* 192 */ "sortorder ::= DESC", - /* 193 */ "sortorder ::=", - /* 194 */ "groupby_opt ::=", - /* 195 */ "groupby_opt ::= GROUP BY grouplist", - /* 196 */ "grouplist ::= grouplist COMMA item", - /* 197 */ "grouplist ::= item", - /* 198 */ "having_opt ::=", - /* 199 */ "having_opt ::= HAVING expr", - /* 200 */ "limit_opt ::=", - /* 201 */ "limit_opt ::= LIMIT signed", - /* 202 */ "limit_opt ::= LIMIT signed OFFSET signed", - /* 203 */ "limit_opt ::= LIMIT signed COMMA signed", - /* 204 */ "slimit_opt ::=", - /* 205 */ "slimit_opt ::= SLIMIT signed", - /* 206 */ "slimit_opt ::= SLIMIT signed SOFFSET signed", - /* 207 */ "slimit_opt ::= SLIMIT signed COMMA signed", - /* 208 */ "where_opt ::=", - /* 209 */ "where_opt ::= WHERE expr", - /* 210 */ "expr ::= LP expr RP", - /* 211 */ "expr ::= ID", - /* 212 */ "expr ::= ID DOT ID", - /* 213 */ "expr ::= ID DOT STAR", - /* 214 */ "expr ::= INTEGER", - /* 215 */ "expr ::= MINUS INTEGER", - /* 216 */ "expr ::= PLUS INTEGER", - /* 217 */ "expr ::= FLOAT", - /* 218 */ "expr ::= MINUS FLOAT", - /* 219 */ "expr ::= PLUS FLOAT", - /* 220 */ "expr ::= STRING", - /* 221 */ "expr ::= NOW", - /* 222 */ "expr ::= VARIABLE", - /* 223 */ "expr ::= BOOL", - /* 224 */ "expr ::= ID LP exprlist RP", - /* 225 */ "expr ::= ID LP STAR RP", - /* 226 */ "expr ::= expr IS NULL", - /* 227 */ "expr ::= expr IS NOT NULL", - /* 228 */ "expr ::= expr LT expr", - /* 229 */ "expr ::= expr GT expr", - /* 230 */ "expr ::= expr LE expr", - /* 231 */ "expr ::= expr GE expr", - /* 232 */ "expr ::= expr NE expr", - /* 233 */ "expr ::= expr EQ expr", - /* 234 */ "expr ::= expr BETWEEN expr AND expr", - /* 235 */ "expr ::= expr AND expr", - /* 236 */ "expr ::= expr OR expr", - /* 237 */ "expr ::= expr PLUS expr", - /* 238 */ "expr ::= expr MINUS expr", - /* 239 */ "expr ::= expr STAR expr", - /* 240 */ "expr ::= expr SLASH expr", - /* 241 */ "expr ::= expr REM expr", - /* 242 */ "expr ::= expr LIKE expr", - /* 243 */ "expr ::= expr IN LP exprlist RP", - /* 244 */ "exprlist ::= exprlist COMMA expritem", - /* 245 */ "exprlist ::= expritem", - /* 246 */ "expritem ::= expr", - /* 247 */ "expritem ::=", - /* 248 */ "cmd ::= RESET QUERY CACHE", - /* 249 */ "cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist", - /* 250 */ "cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids", - /* 251 */ "cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist", - /* 252 */ "cmd ::= ALTER TABLE ids cpxName DROP TAG ids", - /* 253 */ "cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids", - /* 254 */ "cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem", - /* 255 */ "cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist", - /* 256 */ "cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids", - /* 257 */ "cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist", - /* 258 */ "cmd ::= ALTER STABLE ids cpxName DROP TAG ids", - /* 259 */ "cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids", - /* 260 */ "cmd ::= KILL CONNECTION INTEGER", - /* 261 */ "cmd ::= KILL STREAM INTEGER COLON INTEGER", - /* 262 */ "cmd ::= KILL QUERY INTEGER COLON INTEGER", + /* 181 */ "session_option ::=", + /* 182 */ "session_option ::= SESSION LP ids cpxName COMMA tmvar RP", + /* 183 */ "fill_opt ::=", + /* 184 */ "fill_opt ::= FILL LP ID COMMA tagitemlist RP", + /* 185 */ "fill_opt ::= FILL LP ID RP", + /* 186 */ "sliding_opt ::= SLIDING LP tmvar RP", + /* 187 */ "sliding_opt ::=", + /* 188 */ "orderby_opt ::=", + /* 189 */ "orderby_opt ::= ORDER BY sortlist", + /* 190 */ "sortlist ::= sortlist COMMA item sortorder", + /* 191 */ "sortlist ::= item sortorder", + /* 192 */ "item ::= ids cpxName", + /* 193 */ "sortorder ::= ASC", + /* 194 */ "sortorder ::= DESC", + /* 195 */ "sortorder ::=", + /* 196 */ "groupby_opt ::=", + /* 197 */ "groupby_opt ::= GROUP BY grouplist", + /* 198 */ "grouplist ::= grouplist COMMA item", + /* 199 */ "grouplist ::= item", + /* 200 */ "having_opt ::=", + /* 201 */ "having_opt ::= HAVING expr", + /* 202 */ "limit_opt ::=", + /* 203 */ "limit_opt ::= LIMIT signed", + /* 204 */ "limit_opt ::= LIMIT signed OFFSET signed", + /* 205 */ "limit_opt ::= LIMIT signed COMMA signed", + /* 206 */ "slimit_opt ::=", + /* 207 */ "slimit_opt ::= SLIMIT signed", + /* 208 */ "slimit_opt ::= SLIMIT signed SOFFSET signed", + /* 209 */ "slimit_opt ::= SLIMIT signed COMMA signed", + /* 210 */ "where_opt ::=", + /* 211 */ "where_opt ::= WHERE expr", + /* 212 */ "expr ::= LP expr RP", + /* 213 */ "expr ::= ID", + /* 214 */ "expr ::= ID DOT ID", + /* 215 */ "expr ::= ID DOT STAR", + /* 216 */ "expr ::= INTEGER", + /* 217 */ "expr ::= MINUS INTEGER", + /* 218 */ "expr ::= PLUS INTEGER", + /* 219 */ "expr ::= FLOAT", + /* 220 */ "expr ::= MINUS FLOAT", + /* 221 */ "expr ::= PLUS FLOAT", + /* 222 */ "expr ::= STRING", + /* 223 */ "expr ::= NOW", + /* 224 */ "expr ::= VARIABLE", + /* 225 */ "expr ::= BOOL", + /* 226 */ "expr ::= ID LP exprlist RP", + /* 227 */ "expr ::= ID LP STAR RP", + /* 228 */ "expr ::= expr IS NULL", + /* 229 */ "expr ::= expr IS NOT NULL", + /* 230 */ "expr ::= expr LT expr", + /* 231 */ "expr ::= expr GT expr", + /* 232 */ "expr ::= expr LE expr", + /* 233 */ "expr ::= expr GE expr", + /* 234 */ "expr ::= expr NE expr", + /* 235 */ "expr ::= expr EQ expr", + /* 236 */ "expr ::= expr BETWEEN expr AND expr", + /* 237 */ "expr ::= expr AND expr", + /* 238 */ "expr ::= expr OR expr", + /* 239 */ "expr ::= expr PLUS expr", + /* 240 */ "expr ::= expr MINUS expr", + /* 241 */ "expr ::= expr STAR expr", + /* 242 */ "expr ::= expr SLASH expr", + /* 243 */ "expr ::= expr REM expr", + /* 244 */ "expr ::= expr LIKE expr", + /* 245 */ "expr ::= expr IN LP exprlist RP", + /* 246 */ "exprlist ::= exprlist COMMA expritem", + /* 247 */ "exprlist ::= expritem", + /* 248 */ "expritem ::= expr", + /* 249 */ "expritem ::=", + /* 250 */ "cmd ::= RESET QUERY CACHE", + /* 251 */ "cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist", + /* 252 */ "cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids", + /* 253 */ "cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist", + /* 254 */ "cmd ::= ALTER TABLE ids cpxName DROP TAG ids", + /* 255 */ "cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids", + /* 256 */ "cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem", + /* 257 */ "cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist", + /* 258 */ "cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids", + /* 259 */ "cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist", + /* 260 */ "cmd ::= ALTER STABLE ids cpxName DROP TAG ids", + /* 261 */ "cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids", + /* 262 */ "cmd ::= KILL CONNECTION INTEGER", + /* 263 */ "cmd ::= KILL STREAM INTEGER COLON INTEGER", + /* 264 */ "cmd ::= KILL QUERY INTEGER COLON INTEGER", }; #endif /* NDEBUG */ @@ -1390,29 +1334,28 @@ static int yyGrowStack(yyParser *p){ /* Initialize a new parser that has already been allocated. */ -void ParseInit(void *yypRawParser ParseCTX_PDECL){ - yyParser *yypParser = (yyParser*)yypRawParser; - ParseCTX_STORE +void ParseInit(void *yypParser){ + yyParser *pParser = (yyParser*)yypParser; #ifdef YYTRACKMAXSTACKDEPTH - yypParser->yyhwm = 0; + pParser->yyhwm = 0; #endif #if YYSTACKDEPTH<=0 - yypParser->yytos = NULL; - yypParser->yystack = NULL; - yypParser->yystksz = 0; - if( yyGrowStack(yypParser) ){ - yypParser->yystack = &yypParser->yystk0; - yypParser->yystksz = 1; + pParser->yytos = NULL; + pParser->yystack = NULL; + pParser->yystksz = 0; + if( yyGrowStack(pParser) ){ + pParser->yystack = &pParser->yystk0; + pParser->yystksz = 1; } #endif #ifndef YYNOERRORRECOVERY - yypParser->yyerrcnt = -1; + pParser->yyerrcnt = -1; #endif - yypParser->yytos = yypParser->yystack; - yypParser->yystack[0].stateno = 0; - yypParser->yystack[0].major = 0; + pParser->yytos = pParser->yystack; + pParser->yystack[0].stateno = 0; + pParser->yystack[0].major = 0; #if YYSTACKDEPTH>0 - yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1]; + pParser->yystackEnd = &pParser->yystack[YYSTACKDEPTH-1]; #endif } @@ -1429,14 +1372,11 @@ void ParseInit(void *yypRawParser ParseCTX_PDECL){ ** A pointer to a parser. This pointer is used in subsequent calls ** to Parse and ParseFree. */ -void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) ParseCTX_PDECL){ - yyParser *yypParser; - yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) ); - if( yypParser ){ - ParseCTX_STORE - ParseInit(yypParser ParseCTX_PARAM); - } - return (void*)yypParser; +void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){ + yyParser *pParser; + pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) ); + if( pParser ) ParseInit(pParser); + return pParser; } #endif /* Parse_ENGINEALWAYSONSTACK */ @@ -1453,8 +1393,7 @@ static void yy_destructor( YYCODETYPE yymajor, /* Type code for object to destroy */ YYMINORTYPE *yypminor /* The object to be destroyed */ ){ - ParseARG_FETCH - ParseCTX_FETCH + ParseARG_FETCH; switch( yymajor ){ /* Here is inserted the actions which take place when a ** terminal or non-terminal is destroyed. This can happen @@ -1467,52 +1406,52 @@ static void yy_destructor( ** inside the C code. */ /********* Begin destructor definitions ***************************************/ - case 234: /* keep */ - case 235: /* tagitemlist */ - case 257: /* columnlist */ - case 258: /* tagNamelist */ - case 266: /* fill_opt */ - case 268: /* groupby_opt */ - case 269: /* orderby_opt */ - case 280: /* sortlist */ - case 284: /* grouplist */ + case 210: /* keep */ + case 211: /* tagitemlist */ + case 233: /* columnlist */ + case 234: /* tagNamelist */ + case 243: /* fill_opt */ + case 245: /* groupby_opt */ + case 246: /* orderby_opt */ + case 257: /* sortlist */ + case 261: /* grouplist */ { -taosArrayDestroy((yypminor->yy207)); +taosArrayDestroy((yypminor->yy285)); } break; - case 255: /* create_table_list */ + case 231: /* create_table_list */ { -destroyCreateTableSql((yypminor->yy414)); +destroyCreateTableSql((yypminor->yy470)); } break; - case 259: /* select */ + case 235: /* select */ { -doDestroyQuerySql((yypminor->yy526)); +destroyQuerySqlNode((yypminor->yy342)); } break; - case 262: /* selcollist */ - case 274: /* sclp */ - case 285: /* exprlist */ + case 238: /* selcollist */ + case 251: /* sclp */ + case 262: /* exprlist */ { -tSqlExprListDestroy((yypminor->yy178)); +tSqlExprListDestroy((yypminor->yy285)); } break; - case 264: /* where_opt */ - case 270: /* having_opt */ - case 276: /* expr */ - case 286: /* expritem */ + case 240: /* where_opt */ + case 247: /* having_opt */ + case 253: /* expr */ + case 263: /* expritem */ { -tSqlExprDestroy((yypminor->yy484)); +tSqlExprDestroy((yypminor->yy178)); } break; - case 273: /* union */ + case 250: /* union */ { -destroyAllSelectClause((yypminor->yy441)); +destroyAllSelectClause((yypminor->yy513)); } break; - case 281: /* sortitem */ + case 258: /* sortitem */ { -tVariantDestroy(&(yypminor->yy232)); +tVariantDestroy(&(yypminor->yy362)); } break; /********* End destructor definitions *****************************************/ @@ -1624,12 +1563,13 @@ int ParseCoverage(FILE *out){ ** Find the appropriate action for a parser given the terminal ** look-ahead token iLookAhead. */ -static YYACTIONTYPE yy_find_shift_action( - YYCODETYPE iLookAhead, /* The look-ahead token */ - YYACTIONTYPE stateno /* Current state number */ +static unsigned int yy_find_shift_action( + yyParser *pParser, /* The parser */ + YYCODETYPE iLookAhead /* The look-ahead token */ ){ int i; - + int stateno = pParser->yytos->stateno; + if( stateno>YY_MAX_SHIFT ) return stateno; assert( stateno <= YY_SHIFT_COUNT ); #if defined(YYCOVERAGE) @@ -1637,19 +1577,15 @@ static YYACTIONTYPE yy_find_shift_action( #endif do{ i = yy_shift_ofst[stateno]; - assert( i>=0 ); - assert( i<=YY_ACTTAB_COUNT ); - assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); + assert( i>=0 && i+YYNTOKEN<=sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) ); assert( iLookAhead!=YYNOCODE ); assert( iLookAhead < YYNTOKEN ); i += iLookAhead; - assert( i<(int)YY_NLOOKAHEAD ); if( yy_lookahead[i]!=iLookAhead ){ #ifdef YYFALLBACK YYCODETYPE iFallback; /* Fallback token */ - assert( iLookAhead %s\n", @@ -1664,8 +1600,15 @@ static YYACTIONTYPE yy_find_shift_action( #ifdef YYWILDCARD { int j = i - iLookAhead + YYWILDCARD; - assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) ); - if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){ + if( +#if YY_SHIFT_MIN+YYWILDCARD<0 + j>=0 && +#endif +#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT + j0 + ){ #ifndef NDEBUG if( yyTraceFILE ){ fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", @@ -1679,7 +1622,6 @@ static YYACTIONTYPE yy_find_shift_action( #endif /* YYWILDCARD */ return yy_default[stateno]; }else{ - assert( i>=0 && iyytos; - yytos->stateno = yyNewState; - yytos->major = yyMajor; + yytos->stateno = (YYACTIONTYPE)yyNewState; + yytos->major = (YYCODETYPE)yyMajor; yytos->minor.yy0 = yyMinor; yyTraceShift(yypParser, yyNewState, "Shift"); } -/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side -** of that rule */ -static const YYCODETYPE yyRuleInfoLhs[] = { - 213, /* (0) program ::= cmd */ - 214, /* (1) cmd ::= SHOW DATABASES */ - 214, /* (2) cmd ::= SHOW TOPICS */ - 214, /* (3) cmd ::= SHOW MNODES */ - 214, /* (4) cmd ::= SHOW DNODES */ - 214, /* (5) cmd ::= SHOW ACCOUNTS */ - 214, /* (6) cmd ::= SHOW USERS */ - 214, /* (7) cmd ::= SHOW MODULES */ - 214, /* (8) cmd ::= SHOW QUERIES */ - 214, /* (9) cmd ::= SHOW CONNECTIONS */ - 214, /* (10) cmd ::= SHOW STREAMS */ - 214, /* (11) cmd ::= SHOW VARIABLES */ - 214, /* (12) cmd ::= SHOW SCORES */ - 214, /* (13) cmd ::= SHOW GRANTS */ - 214, /* (14) cmd ::= SHOW VNODES */ - 214, /* (15) cmd ::= SHOW VNODES IPTOKEN */ - 215, /* (16) dbPrefix ::= */ - 215, /* (17) dbPrefix ::= ids DOT */ - 217, /* (18) cpxName ::= */ - 217, /* (19) cpxName ::= DOT ids */ - 214, /* (20) cmd ::= SHOW CREATE TABLE ids cpxName */ - 214, /* (21) cmd ::= SHOW CREATE DATABASE ids */ - 214, /* (22) cmd ::= SHOW dbPrefix TABLES */ - 214, /* (23) cmd ::= SHOW dbPrefix TABLES LIKE ids */ - 214, /* (24) cmd ::= SHOW dbPrefix STABLES */ - 214, /* (25) cmd ::= SHOW dbPrefix STABLES LIKE ids */ - 214, /* (26) cmd ::= SHOW dbPrefix VGROUPS */ - 214, /* (27) cmd ::= SHOW dbPrefix VGROUPS ids */ - 214, /* (28) cmd ::= DROP TABLE ifexists ids cpxName */ - 214, /* (29) cmd ::= DROP STABLE ifexists ids cpxName */ - 214, /* (30) cmd ::= DROP DATABASE ifexists ids */ - 214, /* (31) cmd ::= DROP TOPIC ifexists ids */ - 214, /* (32) cmd ::= DROP DNODE ids */ - 214, /* (33) cmd ::= DROP USER ids */ - 214, /* (34) cmd ::= DROP ACCOUNT ids */ - 214, /* (35) cmd ::= USE ids */ - 214, /* (36) cmd ::= DESCRIBE ids cpxName */ - 214, /* (37) cmd ::= ALTER USER ids PASS ids */ - 214, /* (38) cmd ::= ALTER USER ids PRIVILEGE ids */ - 214, /* (39) cmd ::= ALTER DNODE ids ids */ - 214, /* (40) cmd ::= ALTER DNODE ids ids ids */ - 214, /* (41) cmd ::= ALTER LOCAL ids */ - 214, /* (42) cmd ::= ALTER LOCAL ids ids */ - 214, /* (43) cmd ::= ALTER DATABASE ids alter_db_optr */ - 214, /* (44) cmd ::= ALTER TOPIC ids alter_topic_optr */ - 214, /* (45) cmd ::= ALTER ACCOUNT ids acct_optr */ - 214, /* (46) cmd ::= ALTER ACCOUNT ids PASS ids acct_optr */ - 216, /* (47) ids ::= ID */ - 216, /* (48) ids ::= STRING */ - 218, /* (49) ifexists ::= IF EXISTS */ - 218, /* (50) ifexists ::= */ - 222, /* (51) ifnotexists ::= IF NOT EXISTS */ - 222, /* (52) ifnotexists ::= */ - 214, /* (53) cmd ::= CREATE DNODE ids */ - 214, /* (54) cmd ::= CREATE ACCOUNT ids PASS ids acct_optr */ - 214, /* (55) cmd ::= CREATE DATABASE ifnotexists ids db_optr */ - 214, /* (56) cmd ::= CREATE TOPIC ifnotexists ids topic_optr */ - 214, /* (57) cmd ::= CREATE USER ids PASS ids */ - 225, /* (58) pps ::= */ - 225, /* (59) pps ::= PPS INTEGER */ - 226, /* (60) tseries ::= */ - 226, /* (61) tseries ::= TSERIES INTEGER */ - 227, /* (62) dbs ::= */ - 227, /* (63) dbs ::= DBS INTEGER */ - 228, /* (64) streams ::= */ - 228, /* (65) streams ::= STREAMS INTEGER */ - 229, /* (66) storage ::= */ - 229, /* (67) storage ::= STORAGE INTEGER */ - 230, /* (68) qtime ::= */ - 230, /* (69) qtime ::= QTIME INTEGER */ - 231, /* (70) users ::= */ - 231, /* (71) users ::= USERS INTEGER */ - 232, /* (72) conns ::= */ - 232, /* (73) conns ::= CONNS INTEGER */ - 233, /* (74) state ::= */ - 233, /* (75) state ::= STATE ids */ - 221, /* (76) acct_optr ::= pps tseries storage streams qtime dbs users conns state */ - 234, /* (77) keep ::= KEEP tagitemlist */ - 236, /* (78) cache ::= CACHE INTEGER */ - 237, /* (79) replica ::= REPLICA INTEGER */ - 238, /* (80) quorum ::= QUORUM INTEGER */ - 239, /* (81) days ::= DAYS INTEGER */ - 240, /* (82) minrows ::= MINROWS INTEGER */ - 241, /* (83) maxrows ::= MAXROWS INTEGER */ - 242, /* (84) blocks ::= BLOCKS INTEGER */ - 243, /* (85) ctime ::= CTIME INTEGER */ - 244, /* (86) wal ::= WAL INTEGER */ - 245, /* (87) fsync ::= FSYNC INTEGER */ - 246, /* (88) comp ::= COMP INTEGER */ - 247, /* (89) prec ::= PRECISION STRING */ - 248, /* (90) update ::= UPDATE INTEGER */ - 249, /* (91) cachelast ::= CACHELAST INTEGER */ - 250, /* (92) partitions ::= PARTITIONS INTEGER */ - 223, /* (93) db_optr ::= */ - 223, /* (94) db_optr ::= db_optr cache */ - 223, /* (95) db_optr ::= db_optr replica */ - 223, /* (96) db_optr ::= db_optr quorum */ - 223, /* (97) db_optr ::= db_optr days */ - 223, /* (98) db_optr ::= db_optr minrows */ - 223, /* (99) db_optr ::= db_optr maxrows */ - 223, /* (100) db_optr ::= db_optr blocks */ - 223, /* (101) db_optr ::= db_optr ctime */ - 223, /* (102) db_optr ::= db_optr wal */ - 223, /* (103) db_optr ::= db_optr fsync */ - 223, /* (104) db_optr ::= db_optr comp */ - 223, /* (105) db_optr ::= db_optr prec */ - 223, /* (106) db_optr ::= db_optr keep */ - 223, /* (107) db_optr ::= db_optr update */ - 223, /* (108) db_optr ::= db_optr cachelast */ - 224, /* (109) topic_optr ::= db_optr */ - 224, /* (110) topic_optr ::= topic_optr partitions */ - 219, /* (111) alter_db_optr ::= */ - 219, /* (112) alter_db_optr ::= alter_db_optr replica */ - 219, /* (113) alter_db_optr ::= alter_db_optr quorum */ - 219, /* (114) alter_db_optr ::= alter_db_optr keep */ - 219, /* (115) alter_db_optr ::= alter_db_optr blocks */ - 219, /* (116) alter_db_optr ::= alter_db_optr comp */ - 219, /* (117) alter_db_optr ::= alter_db_optr wal */ - 219, /* (118) alter_db_optr ::= alter_db_optr fsync */ - 219, /* (119) alter_db_optr ::= alter_db_optr update */ - 219, /* (120) alter_db_optr ::= alter_db_optr cachelast */ - 220, /* (121) alter_topic_optr ::= alter_db_optr */ - 220, /* (122) alter_topic_optr ::= alter_topic_optr partitions */ - 251, /* (123) typename ::= ids */ - 251, /* (124) typename ::= ids LP signed RP */ - 251, /* (125) typename ::= ids UNSIGNED */ - 252, /* (126) signed ::= INTEGER */ - 252, /* (127) signed ::= PLUS INTEGER */ - 252, /* (128) signed ::= MINUS INTEGER */ - 214, /* (129) cmd ::= CREATE TABLE create_table_args */ - 214, /* (130) cmd ::= CREATE TABLE create_stable_args */ - 214, /* (131) cmd ::= CREATE STABLE create_stable_args */ - 214, /* (132) cmd ::= CREATE TABLE create_table_list */ - 255, /* (133) create_table_list ::= create_from_stable */ - 255, /* (134) create_table_list ::= create_table_list create_from_stable */ - 253, /* (135) create_table_args ::= ifnotexists ids cpxName LP columnlist RP */ - 254, /* (136) create_stable_args ::= ifnotexists ids cpxName LP columnlist RP TAGS LP columnlist RP */ - 256, /* (137) create_from_stable ::= ifnotexists ids cpxName USING ids cpxName TAGS LP tagitemlist RP */ - 256, /* (138) create_from_stable ::= ifnotexists ids cpxName USING ids cpxName LP tagNamelist RP TAGS LP tagitemlist RP */ - 258, /* (139) tagNamelist ::= tagNamelist COMMA ids */ - 258, /* (140) tagNamelist ::= ids */ - 253, /* (141) create_table_args ::= ifnotexists ids cpxName AS select */ - 257, /* (142) columnlist ::= columnlist COMMA column */ - 257, /* (143) columnlist ::= column */ - 260, /* (144) column ::= ids typename */ - 235, /* (145) tagitemlist ::= tagitemlist COMMA tagitem */ - 235, /* (146) tagitemlist ::= tagitem */ - 261, /* (147) tagitem ::= INTEGER */ - 261, /* (148) tagitem ::= FLOAT */ - 261, /* (149) tagitem ::= STRING */ - 261, /* (150) tagitem ::= BOOL */ - 261, /* (151) tagitem ::= NULL */ - 261, /* (152) tagitem ::= MINUS INTEGER */ - 261, /* (153) tagitem ::= MINUS FLOAT */ - 261, /* (154) tagitem ::= PLUS INTEGER */ - 261, /* (155) tagitem ::= PLUS FLOAT */ - 259, /* (156) select ::= SELECT selcollist from where_opt interval_opt fill_opt sliding_opt groupby_opt orderby_opt having_opt slimit_opt limit_opt */ - 273, /* (157) union ::= select */ - 273, /* (158) union ::= LP union RP */ - 273, /* (159) union ::= union UNION ALL select */ - 273, /* (160) union ::= union UNION ALL LP select RP */ - 214, /* (161) cmd ::= union */ - 259, /* (162) select ::= SELECT selcollist */ - 274, /* (163) sclp ::= selcollist COMMA */ - 274, /* (164) sclp ::= */ - 262, /* (165) selcollist ::= sclp distinct expr as */ - 262, /* (166) selcollist ::= sclp STAR */ - 277, /* (167) as ::= AS ids */ - 277, /* (168) as ::= ids */ - 277, /* (169) as ::= */ - 275, /* (170) distinct ::= DISTINCT */ - 275, /* (171) distinct ::= */ - 263, /* (172) from ::= FROM tablelist */ - 278, /* (173) tablelist ::= ids cpxName */ - 278, /* (174) tablelist ::= ids cpxName ids */ - 278, /* (175) tablelist ::= tablelist COMMA ids cpxName */ - 278, /* (176) tablelist ::= tablelist COMMA ids cpxName ids */ - 279, /* (177) tmvar ::= VARIABLE */ - 265, /* (178) interval_opt ::= INTERVAL LP tmvar RP */ - 265, /* (179) interval_opt ::= INTERVAL LP tmvar COMMA tmvar RP */ - 265, /* (180) interval_opt ::= */ - 266, /* (181) fill_opt ::= */ - 266, /* (182) fill_opt ::= FILL LP ID COMMA tagitemlist RP */ - 266, /* (183) fill_opt ::= FILL LP ID RP */ - 267, /* (184) sliding_opt ::= SLIDING LP tmvar RP */ - 267, /* (185) sliding_opt ::= */ - 269, /* (186) orderby_opt ::= */ - 269, /* (187) orderby_opt ::= ORDER BY sortlist */ - 280, /* (188) sortlist ::= sortlist COMMA item sortorder */ - 280, /* (189) sortlist ::= item sortorder */ - 282, /* (190) item ::= ids cpxName */ - 283, /* (191) sortorder ::= ASC */ - 283, /* (192) sortorder ::= DESC */ - 283, /* (193) sortorder ::= */ - 268, /* (194) groupby_opt ::= */ - 268, /* (195) groupby_opt ::= GROUP BY grouplist */ - 284, /* (196) grouplist ::= grouplist COMMA item */ - 284, /* (197) grouplist ::= item */ - 270, /* (198) having_opt ::= */ - 270, /* (199) having_opt ::= HAVING expr */ - 272, /* (200) limit_opt ::= */ - 272, /* (201) limit_opt ::= LIMIT signed */ - 272, /* (202) limit_opt ::= LIMIT signed OFFSET signed */ - 272, /* (203) limit_opt ::= LIMIT signed COMMA signed */ - 271, /* (204) slimit_opt ::= */ - 271, /* (205) slimit_opt ::= SLIMIT signed */ - 271, /* (206) slimit_opt ::= SLIMIT signed SOFFSET signed */ - 271, /* (207) slimit_opt ::= SLIMIT signed COMMA signed */ - 264, /* (208) where_opt ::= */ - 264, /* (209) where_opt ::= WHERE expr */ - 276, /* (210) expr ::= LP expr RP */ - 276, /* (211) expr ::= ID */ - 276, /* (212) expr ::= ID DOT ID */ - 276, /* (213) expr ::= ID DOT STAR */ - 276, /* (214) expr ::= INTEGER */ - 276, /* (215) expr ::= MINUS INTEGER */ - 276, /* (216) expr ::= PLUS INTEGER */ - 276, /* (217) expr ::= FLOAT */ - 276, /* (218) expr ::= MINUS FLOAT */ - 276, /* (219) expr ::= PLUS FLOAT */ - 276, /* (220) expr ::= STRING */ - 276, /* (221) expr ::= NOW */ - 276, /* (222) expr ::= VARIABLE */ - 276, /* (223) expr ::= BOOL */ - 276, /* (224) expr ::= ID LP exprlist RP */ - 276, /* (225) expr ::= ID LP STAR RP */ - 276, /* (226) expr ::= expr IS NULL */ - 276, /* (227) expr ::= expr IS NOT NULL */ - 276, /* (228) expr ::= expr LT expr */ - 276, /* (229) expr ::= expr GT expr */ - 276, /* (230) expr ::= expr LE expr */ - 276, /* (231) expr ::= expr GE expr */ - 276, /* (232) expr ::= expr NE expr */ - 276, /* (233) expr ::= expr EQ expr */ - 276, /* (234) expr ::= expr BETWEEN expr AND expr */ - 276, /* (235) expr ::= expr AND expr */ - 276, /* (236) expr ::= expr OR expr */ - 276, /* (237) expr ::= expr PLUS expr */ - 276, /* (238) expr ::= expr MINUS expr */ - 276, /* (239) expr ::= expr STAR expr */ - 276, /* (240) expr ::= expr SLASH expr */ - 276, /* (241) expr ::= expr REM expr */ - 276, /* (242) expr ::= expr LIKE expr */ - 276, /* (243) expr ::= expr IN LP exprlist RP */ - 285, /* (244) exprlist ::= exprlist COMMA expritem */ - 285, /* (245) exprlist ::= expritem */ - 286, /* (246) expritem ::= expr */ - 286, /* (247) expritem ::= */ - 214, /* (248) cmd ::= RESET QUERY CACHE */ - 214, /* (249) cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ - 214, /* (250) cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ - 214, /* (251) cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ - 214, /* (252) cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ - 214, /* (253) cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ - 214, /* (254) cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ - 214, /* (255) cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist */ - 214, /* (256) cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids */ - 214, /* (257) cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist */ - 214, /* (258) cmd ::= ALTER STABLE ids cpxName DROP TAG ids */ - 214, /* (259) cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids */ - 214, /* (260) cmd ::= KILL CONNECTION INTEGER */ - 214, /* (261) cmd ::= KILL STREAM INTEGER COLON INTEGER */ - 214, /* (262) cmd ::= KILL QUERY INTEGER COLON INTEGER */ -}; - -/* For rule J, yyRuleInfoNRhs[J] contains the negative of the number -** of symbols on the right-hand side of that rule. */ -static const signed char yyRuleInfoNRhs[] = { - -1, /* (0) program ::= cmd */ - -2, /* (1) cmd ::= SHOW DATABASES */ - -2, /* (2) cmd ::= SHOW TOPICS */ - -2, /* (3) cmd ::= SHOW MNODES */ - -2, /* (4) cmd ::= SHOW DNODES */ - -2, /* (5) cmd ::= SHOW ACCOUNTS */ - -2, /* (6) cmd ::= SHOW USERS */ - -2, /* (7) cmd ::= SHOW MODULES */ - -2, /* (8) cmd ::= SHOW QUERIES */ - -2, /* (9) cmd ::= SHOW CONNECTIONS */ - -2, /* (10) cmd ::= SHOW STREAMS */ - -2, /* (11) cmd ::= SHOW VARIABLES */ - -2, /* (12) cmd ::= SHOW SCORES */ - -2, /* (13) cmd ::= SHOW GRANTS */ - -2, /* (14) cmd ::= SHOW VNODES */ - -3, /* (15) cmd ::= SHOW VNODES IPTOKEN */ - 0, /* (16) dbPrefix ::= */ - -2, /* (17) dbPrefix ::= ids DOT */ - 0, /* (18) cpxName ::= */ - -2, /* (19) cpxName ::= DOT ids */ - -5, /* (20) cmd ::= SHOW CREATE TABLE ids cpxName */ - -4, /* (21) cmd ::= SHOW CREATE DATABASE ids */ - -3, /* (22) cmd ::= SHOW dbPrefix TABLES */ - -5, /* (23) cmd ::= SHOW dbPrefix TABLES LIKE ids */ - -3, /* (24) cmd ::= SHOW dbPrefix STABLES */ - -5, /* (25) cmd ::= SHOW dbPrefix STABLES LIKE ids */ - -3, /* (26) cmd ::= SHOW dbPrefix VGROUPS */ - -4, /* (27) cmd ::= SHOW dbPrefix VGROUPS ids */ - -5, /* (28) cmd ::= DROP TABLE ifexists ids cpxName */ - -5, /* (29) cmd ::= DROP STABLE ifexists ids cpxName */ - -4, /* (30) cmd ::= DROP DATABASE ifexists ids */ - -4, /* (31) cmd ::= DROP TOPIC ifexists ids */ - -3, /* (32) cmd ::= DROP DNODE ids */ - -3, /* (33) cmd ::= DROP USER ids */ - -3, /* (34) cmd ::= DROP ACCOUNT ids */ - -2, /* (35) cmd ::= USE ids */ - -3, /* (36) cmd ::= DESCRIBE ids cpxName */ - -5, /* (37) cmd ::= ALTER USER ids PASS ids */ - -5, /* (38) cmd ::= ALTER USER ids PRIVILEGE ids */ - -4, /* (39) cmd ::= ALTER DNODE ids ids */ - -5, /* (40) cmd ::= ALTER DNODE ids ids ids */ - -3, /* (41) cmd ::= ALTER LOCAL ids */ - -4, /* (42) cmd ::= ALTER LOCAL ids ids */ - -4, /* (43) cmd ::= ALTER DATABASE ids alter_db_optr */ - -4, /* (44) cmd ::= ALTER TOPIC ids alter_topic_optr */ - -4, /* (45) cmd ::= ALTER ACCOUNT ids acct_optr */ - -6, /* (46) cmd ::= ALTER ACCOUNT ids PASS ids acct_optr */ - -1, /* (47) ids ::= ID */ - -1, /* (48) ids ::= STRING */ - -2, /* (49) ifexists ::= IF EXISTS */ - 0, /* (50) ifexists ::= */ - -3, /* (51) ifnotexists ::= IF NOT EXISTS */ - 0, /* (52) ifnotexists ::= */ - -3, /* (53) cmd ::= CREATE DNODE ids */ - -6, /* (54) cmd ::= CREATE ACCOUNT ids PASS ids acct_optr */ - -5, /* (55) cmd ::= CREATE DATABASE ifnotexists ids db_optr */ - -5, /* (56) cmd ::= CREATE TOPIC ifnotexists ids topic_optr */ - -5, /* (57) cmd ::= CREATE USER ids PASS ids */ - 0, /* (58) pps ::= */ - -2, /* (59) pps ::= PPS INTEGER */ - 0, /* (60) tseries ::= */ - -2, /* (61) tseries ::= TSERIES INTEGER */ - 0, /* (62) dbs ::= */ - -2, /* (63) dbs ::= DBS INTEGER */ - 0, /* (64) streams ::= */ - -2, /* (65) streams ::= STREAMS INTEGER */ - 0, /* (66) storage ::= */ - -2, /* (67) storage ::= STORAGE INTEGER */ - 0, /* (68) qtime ::= */ - -2, /* (69) qtime ::= QTIME INTEGER */ - 0, /* (70) users ::= */ - -2, /* (71) users ::= USERS INTEGER */ - 0, /* (72) conns ::= */ - -2, /* (73) conns ::= CONNS INTEGER */ - 0, /* (74) state ::= */ - -2, /* (75) state ::= STATE ids */ - -9, /* (76) acct_optr ::= pps tseries storage streams qtime dbs users conns state */ - -2, /* (77) keep ::= KEEP tagitemlist */ - -2, /* (78) cache ::= CACHE INTEGER */ - -2, /* (79) replica ::= REPLICA INTEGER */ - -2, /* (80) quorum ::= QUORUM INTEGER */ - -2, /* (81) days ::= DAYS INTEGER */ - -2, /* (82) minrows ::= MINROWS INTEGER */ - -2, /* (83) maxrows ::= MAXROWS INTEGER */ - -2, /* (84) blocks ::= BLOCKS INTEGER */ - -2, /* (85) ctime ::= CTIME INTEGER */ - -2, /* (86) wal ::= WAL INTEGER */ - -2, /* (87) fsync ::= FSYNC INTEGER */ - -2, /* (88) comp ::= COMP INTEGER */ - -2, /* (89) prec ::= PRECISION STRING */ - -2, /* (90) update ::= UPDATE INTEGER */ - -2, /* (91) cachelast ::= CACHELAST INTEGER */ - -2, /* (92) partitions ::= PARTITIONS INTEGER */ - 0, /* (93) db_optr ::= */ - -2, /* (94) db_optr ::= db_optr cache */ - -2, /* (95) db_optr ::= db_optr replica */ - -2, /* (96) db_optr ::= db_optr quorum */ - -2, /* (97) db_optr ::= db_optr days */ - -2, /* (98) db_optr ::= db_optr minrows */ - -2, /* (99) db_optr ::= db_optr maxrows */ - -2, /* (100) db_optr ::= db_optr blocks */ - -2, /* (101) db_optr ::= db_optr ctime */ - -2, /* (102) db_optr ::= db_optr wal */ - -2, /* (103) db_optr ::= db_optr fsync */ - -2, /* (104) db_optr ::= db_optr comp */ - -2, /* (105) db_optr ::= db_optr prec */ - -2, /* (106) db_optr ::= db_optr keep */ - -2, /* (107) db_optr ::= db_optr update */ - -2, /* (108) db_optr ::= db_optr cachelast */ - -1, /* (109) topic_optr ::= db_optr */ - -2, /* (110) topic_optr ::= topic_optr partitions */ - 0, /* (111) alter_db_optr ::= */ - -2, /* (112) alter_db_optr ::= alter_db_optr replica */ - -2, /* (113) alter_db_optr ::= alter_db_optr quorum */ - -2, /* (114) alter_db_optr ::= alter_db_optr keep */ - -2, /* (115) alter_db_optr ::= alter_db_optr blocks */ - -2, /* (116) alter_db_optr ::= alter_db_optr comp */ - -2, /* (117) alter_db_optr ::= alter_db_optr wal */ - -2, /* (118) alter_db_optr ::= alter_db_optr fsync */ - -2, /* (119) alter_db_optr ::= alter_db_optr update */ - -2, /* (120) alter_db_optr ::= alter_db_optr cachelast */ - -1, /* (121) alter_topic_optr ::= alter_db_optr */ - -2, /* (122) alter_topic_optr ::= alter_topic_optr partitions */ - -1, /* (123) typename ::= ids */ - -4, /* (124) typename ::= ids LP signed RP */ - -2, /* (125) typename ::= ids UNSIGNED */ - -1, /* (126) signed ::= INTEGER */ - -2, /* (127) signed ::= PLUS INTEGER */ - -2, /* (128) signed ::= MINUS INTEGER */ - -3, /* (129) cmd ::= CREATE TABLE create_table_args */ - -3, /* (130) cmd ::= CREATE TABLE create_stable_args */ - -3, /* (131) cmd ::= CREATE STABLE create_stable_args */ - -3, /* (132) cmd ::= CREATE TABLE create_table_list */ - -1, /* (133) create_table_list ::= create_from_stable */ - -2, /* (134) create_table_list ::= create_table_list create_from_stable */ - -6, /* (135) create_table_args ::= ifnotexists ids cpxName LP columnlist RP */ - -10, /* (136) create_stable_args ::= ifnotexists ids cpxName LP columnlist RP TAGS LP columnlist RP */ - -10, /* (137) create_from_stable ::= ifnotexists ids cpxName USING ids cpxName TAGS LP tagitemlist RP */ - -13, /* (138) create_from_stable ::= ifnotexists ids cpxName USING ids cpxName LP tagNamelist RP TAGS LP tagitemlist RP */ - -3, /* (139) tagNamelist ::= tagNamelist COMMA ids */ - -1, /* (140) tagNamelist ::= ids */ - -5, /* (141) create_table_args ::= ifnotexists ids cpxName AS select */ - -3, /* (142) columnlist ::= columnlist COMMA column */ - -1, /* (143) columnlist ::= column */ - -2, /* (144) column ::= ids typename */ - -3, /* (145) tagitemlist ::= tagitemlist COMMA tagitem */ - -1, /* (146) tagitemlist ::= tagitem */ - -1, /* (147) tagitem ::= INTEGER */ - -1, /* (148) tagitem ::= FLOAT */ - -1, /* (149) tagitem ::= STRING */ - -1, /* (150) tagitem ::= BOOL */ - -1, /* (151) tagitem ::= NULL */ - -2, /* (152) tagitem ::= MINUS INTEGER */ - -2, /* (153) tagitem ::= MINUS FLOAT */ - -2, /* (154) tagitem ::= PLUS INTEGER */ - -2, /* (155) tagitem ::= PLUS FLOAT */ - -12, /* (156) select ::= SELECT selcollist from where_opt interval_opt fill_opt sliding_opt groupby_opt orderby_opt having_opt slimit_opt limit_opt */ - -1, /* (157) union ::= select */ - -3, /* (158) union ::= LP union RP */ - -4, /* (159) union ::= union UNION ALL select */ - -6, /* (160) union ::= union UNION ALL LP select RP */ - -1, /* (161) cmd ::= union */ - -2, /* (162) select ::= SELECT selcollist */ - -2, /* (163) sclp ::= selcollist COMMA */ - 0, /* (164) sclp ::= */ - -4, /* (165) selcollist ::= sclp distinct expr as */ - -2, /* (166) selcollist ::= sclp STAR */ - -2, /* (167) as ::= AS ids */ - -1, /* (168) as ::= ids */ - 0, /* (169) as ::= */ - -1, /* (170) distinct ::= DISTINCT */ - 0, /* (171) distinct ::= */ - -2, /* (172) from ::= FROM tablelist */ - -2, /* (173) tablelist ::= ids cpxName */ - -3, /* (174) tablelist ::= ids cpxName ids */ - -4, /* (175) tablelist ::= tablelist COMMA ids cpxName */ - -5, /* (176) tablelist ::= tablelist COMMA ids cpxName ids */ - -1, /* (177) tmvar ::= VARIABLE */ - -4, /* (178) interval_opt ::= INTERVAL LP tmvar RP */ - -6, /* (179) interval_opt ::= INTERVAL LP tmvar COMMA tmvar RP */ - 0, /* (180) interval_opt ::= */ - 0, /* (181) fill_opt ::= */ - -6, /* (182) fill_opt ::= FILL LP ID COMMA tagitemlist RP */ - -4, /* (183) fill_opt ::= FILL LP ID RP */ - -4, /* (184) sliding_opt ::= SLIDING LP tmvar RP */ - 0, /* (185) sliding_opt ::= */ - 0, /* (186) orderby_opt ::= */ - -3, /* (187) orderby_opt ::= ORDER BY sortlist */ - -4, /* (188) sortlist ::= sortlist COMMA item sortorder */ - -2, /* (189) sortlist ::= item sortorder */ - -2, /* (190) item ::= ids cpxName */ - -1, /* (191) sortorder ::= ASC */ - -1, /* (192) sortorder ::= DESC */ - 0, /* (193) sortorder ::= */ - 0, /* (194) groupby_opt ::= */ - -3, /* (195) groupby_opt ::= GROUP BY grouplist */ - -3, /* (196) grouplist ::= grouplist COMMA item */ - -1, /* (197) grouplist ::= item */ - 0, /* (198) having_opt ::= */ - -2, /* (199) having_opt ::= HAVING expr */ - 0, /* (200) limit_opt ::= */ - -2, /* (201) limit_opt ::= LIMIT signed */ - -4, /* (202) limit_opt ::= LIMIT signed OFFSET signed */ - -4, /* (203) limit_opt ::= LIMIT signed COMMA signed */ - 0, /* (204) slimit_opt ::= */ - -2, /* (205) slimit_opt ::= SLIMIT signed */ - -4, /* (206) slimit_opt ::= SLIMIT signed SOFFSET signed */ - -4, /* (207) slimit_opt ::= SLIMIT signed COMMA signed */ - 0, /* (208) where_opt ::= */ - -2, /* (209) where_opt ::= WHERE expr */ - -3, /* (210) expr ::= LP expr RP */ - -1, /* (211) expr ::= ID */ - -3, /* (212) expr ::= ID DOT ID */ - -3, /* (213) expr ::= ID DOT STAR */ - -1, /* (214) expr ::= INTEGER */ - -2, /* (215) expr ::= MINUS INTEGER */ - -2, /* (216) expr ::= PLUS INTEGER */ - -1, /* (217) expr ::= FLOAT */ - -2, /* (218) expr ::= MINUS FLOAT */ - -2, /* (219) expr ::= PLUS FLOAT */ - -1, /* (220) expr ::= STRING */ - -1, /* (221) expr ::= NOW */ - -1, /* (222) expr ::= VARIABLE */ - -1, /* (223) expr ::= BOOL */ - -4, /* (224) expr ::= ID LP exprlist RP */ - -4, /* (225) expr ::= ID LP STAR RP */ - -3, /* (226) expr ::= expr IS NULL */ - -4, /* (227) expr ::= expr IS NOT NULL */ - -3, /* (228) expr ::= expr LT expr */ - -3, /* (229) expr ::= expr GT expr */ - -3, /* (230) expr ::= expr LE expr */ - -3, /* (231) expr ::= expr GE expr */ - -3, /* (232) expr ::= expr NE expr */ - -3, /* (233) expr ::= expr EQ expr */ - -5, /* (234) expr ::= expr BETWEEN expr AND expr */ - -3, /* (235) expr ::= expr AND expr */ - -3, /* (236) expr ::= expr OR expr */ - -3, /* (237) expr ::= expr PLUS expr */ - -3, /* (238) expr ::= expr MINUS expr */ - -3, /* (239) expr ::= expr STAR expr */ - -3, /* (240) expr ::= expr SLASH expr */ - -3, /* (241) expr ::= expr REM expr */ - -3, /* (242) expr ::= expr LIKE expr */ - -5, /* (243) expr ::= expr IN LP exprlist RP */ - -3, /* (244) exprlist ::= exprlist COMMA expritem */ - -1, /* (245) exprlist ::= expritem */ - -1, /* (246) expritem ::= expr */ - 0, /* (247) expritem ::= */ - -3, /* (248) cmd ::= RESET QUERY CACHE */ - -7, /* (249) cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ - -7, /* (250) cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ - -7, /* (251) cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ - -7, /* (252) cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ - -8, /* (253) cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ - -9, /* (254) cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ - -7, /* (255) cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist */ - -7, /* (256) cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids */ - -7, /* (257) cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist */ - -7, /* (258) cmd ::= ALTER STABLE ids cpxName DROP TAG ids */ - -8, /* (259) cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids */ - -3, /* (260) cmd ::= KILL CONNECTION INTEGER */ - -5, /* (261) cmd ::= KILL STREAM INTEGER COLON INTEGER */ - -5, /* (262) cmd ::= KILL QUERY INTEGER COLON INTEGER */ +/* The following table contains information about every rule that +** is used during the reduce. +*/ +static const struct { + YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */ + signed char nrhs; /* Negative of the number of RHS symbols in the rule */ +} yyRuleInfo[] = { + { 189, -1 }, /* (0) program ::= cmd */ + { 190, -2 }, /* (1) cmd ::= SHOW DATABASES */ + { 190, -2 }, /* (2) cmd ::= SHOW TOPICS */ + { 190, -2 }, /* (3) cmd ::= SHOW MNODES */ + { 190, -2 }, /* (4) cmd ::= SHOW DNODES */ + { 190, -2 }, /* (5) cmd ::= SHOW ACCOUNTS */ + { 190, -2 }, /* (6) cmd ::= SHOW USERS */ + { 190, -2 }, /* (7) cmd ::= SHOW MODULES */ + { 190, -2 }, /* (8) cmd ::= SHOW QUERIES */ + { 190, -2 }, /* (9) cmd ::= SHOW CONNECTIONS */ + { 190, -2 }, /* (10) cmd ::= SHOW STREAMS */ + { 190, -2 }, /* (11) cmd ::= SHOW VARIABLES */ + { 190, -2 }, /* (12) cmd ::= SHOW SCORES */ + { 190, -2 }, /* (13) cmd ::= SHOW GRANTS */ + { 190, -2 }, /* (14) cmd ::= SHOW VNODES */ + { 190, -3 }, /* (15) cmd ::= SHOW VNODES IPTOKEN */ + { 191, 0 }, /* (16) dbPrefix ::= */ + { 191, -2 }, /* (17) dbPrefix ::= ids DOT */ + { 193, 0 }, /* (18) cpxName ::= */ + { 193, -2 }, /* (19) cpxName ::= DOT ids */ + { 190, -5 }, /* (20) cmd ::= SHOW CREATE TABLE ids cpxName */ + { 190, -4 }, /* (21) cmd ::= SHOW CREATE DATABASE ids */ + { 190, -3 }, /* (22) cmd ::= SHOW dbPrefix TABLES */ + { 190, -5 }, /* (23) cmd ::= SHOW dbPrefix TABLES LIKE ids */ + { 190, -3 }, /* (24) cmd ::= SHOW dbPrefix STABLES */ + { 190, -5 }, /* (25) cmd ::= SHOW dbPrefix STABLES LIKE ids */ + { 190, -3 }, /* (26) cmd ::= SHOW dbPrefix VGROUPS */ + { 190, -4 }, /* (27) cmd ::= SHOW dbPrefix VGROUPS ids */ + { 190, -5 }, /* (28) cmd ::= DROP TABLE ifexists ids cpxName */ + { 190, -5 }, /* (29) cmd ::= DROP STABLE ifexists ids cpxName */ + { 190, -4 }, /* (30) cmd ::= DROP DATABASE ifexists ids */ + { 190, -4 }, /* (31) cmd ::= DROP TOPIC ifexists ids */ + { 190, -3 }, /* (32) cmd ::= DROP DNODE ids */ + { 190, -3 }, /* (33) cmd ::= DROP USER ids */ + { 190, -3 }, /* (34) cmd ::= DROP ACCOUNT ids */ + { 190, -2 }, /* (35) cmd ::= USE ids */ + { 190, -3 }, /* (36) cmd ::= DESCRIBE ids cpxName */ + { 190, -5 }, /* (37) cmd ::= ALTER USER ids PASS ids */ + { 190, -5 }, /* (38) cmd ::= ALTER USER ids PRIVILEGE ids */ + { 190, -4 }, /* (39) cmd ::= ALTER DNODE ids ids */ + { 190, -5 }, /* (40) cmd ::= ALTER DNODE ids ids ids */ + { 190, -3 }, /* (41) cmd ::= ALTER LOCAL ids */ + { 190, -4 }, /* (42) cmd ::= ALTER LOCAL ids ids */ + { 190, -4 }, /* (43) cmd ::= ALTER DATABASE ids alter_db_optr */ + { 190, -4 }, /* (44) cmd ::= ALTER TOPIC ids alter_topic_optr */ + { 190, -4 }, /* (45) cmd ::= ALTER ACCOUNT ids acct_optr */ + { 190, -6 }, /* (46) cmd ::= ALTER ACCOUNT ids PASS ids acct_optr */ + { 192, -1 }, /* (47) ids ::= ID */ + { 192, -1 }, /* (48) ids ::= STRING */ + { 194, -2 }, /* (49) ifexists ::= IF EXISTS */ + { 194, 0 }, /* (50) ifexists ::= */ + { 198, -3 }, /* (51) ifnotexists ::= IF NOT EXISTS */ + { 198, 0 }, /* (52) ifnotexists ::= */ + { 190, -3 }, /* (53) cmd ::= CREATE DNODE ids */ + { 190, -6 }, /* (54) cmd ::= CREATE ACCOUNT ids PASS ids acct_optr */ + { 190, -5 }, /* (55) cmd ::= CREATE DATABASE ifnotexists ids db_optr */ + { 190, -5 }, /* (56) cmd ::= CREATE TOPIC ifnotexists ids topic_optr */ + { 190, -5 }, /* (57) cmd ::= CREATE USER ids PASS ids */ + { 201, 0 }, /* (58) pps ::= */ + { 201, -2 }, /* (59) pps ::= PPS INTEGER */ + { 202, 0 }, /* (60) tseries ::= */ + { 202, -2 }, /* (61) tseries ::= TSERIES INTEGER */ + { 203, 0 }, /* (62) dbs ::= */ + { 203, -2 }, /* (63) dbs ::= DBS INTEGER */ + { 204, 0 }, /* (64) streams ::= */ + { 204, -2 }, /* (65) streams ::= STREAMS INTEGER */ + { 205, 0 }, /* (66) storage ::= */ + { 205, -2 }, /* (67) storage ::= STORAGE INTEGER */ + { 206, 0 }, /* (68) qtime ::= */ + { 206, -2 }, /* (69) qtime ::= QTIME INTEGER */ + { 207, 0 }, /* (70) users ::= */ + { 207, -2 }, /* (71) users ::= USERS INTEGER */ + { 208, 0 }, /* (72) conns ::= */ + { 208, -2 }, /* (73) conns ::= CONNS INTEGER */ + { 209, 0 }, /* (74) state ::= */ + { 209, -2 }, /* (75) state ::= STATE ids */ + { 197, -9 }, /* (76) acct_optr ::= pps tseries storage streams qtime dbs users conns state */ + { 210, -2 }, /* (77) keep ::= KEEP tagitemlist */ + { 212, -2 }, /* (78) cache ::= CACHE INTEGER */ + { 213, -2 }, /* (79) replica ::= REPLICA INTEGER */ + { 214, -2 }, /* (80) quorum ::= QUORUM INTEGER */ + { 215, -2 }, /* (81) days ::= DAYS INTEGER */ + { 216, -2 }, /* (82) minrows ::= MINROWS INTEGER */ + { 217, -2 }, /* (83) maxrows ::= MAXROWS INTEGER */ + { 218, -2 }, /* (84) blocks ::= BLOCKS INTEGER */ + { 219, -2 }, /* (85) ctime ::= CTIME INTEGER */ + { 220, -2 }, /* (86) wal ::= WAL INTEGER */ + { 221, -2 }, /* (87) fsync ::= FSYNC INTEGER */ + { 222, -2 }, /* (88) comp ::= COMP INTEGER */ + { 223, -2 }, /* (89) prec ::= PRECISION STRING */ + { 224, -2 }, /* (90) update ::= UPDATE INTEGER */ + { 225, -2 }, /* (91) cachelast ::= CACHELAST INTEGER */ + { 226, -2 }, /* (92) partitions ::= PARTITIONS INTEGER */ + { 199, 0 }, /* (93) db_optr ::= */ + { 199, -2 }, /* (94) db_optr ::= db_optr cache */ + { 199, -2 }, /* (95) db_optr ::= db_optr replica */ + { 199, -2 }, /* (96) db_optr ::= db_optr quorum */ + { 199, -2 }, /* (97) db_optr ::= db_optr days */ + { 199, -2 }, /* (98) db_optr ::= db_optr minrows */ + { 199, -2 }, /* (99) db_optr ::= db_optr maxrows */ + { 199, -2 }, /* (100) db_optr ::= db_optr blocks */ + { 199, -2 }, /* (101) db_optr ::= db_optr ctime */ + { 199, -2 }, /* (102) db_optr ::= db_optr wal */ + { 199, -2 }, /* (103) db_optr ::= db_optr fsync */ + { 199, -2 }, /* (104) db_optr ::= db_optr comp */ + { 199, -2 }, /* (105) db_optr ::= db_optr prec */ + { 199, -2 }, /* (106) db_optr ::= db_optr keep */ + { 199, -2 }, /* (107) db_optr ::= db_optr update */ + { 199, -2 }, /* (108) db_optr ::= db_optr cachelast */ + { 200, -1 }, /* (109) topic_optr ::= db_optr */ + { 200, -2 }, /* (110) topic_optr ::= topic_optr partitions */ + { 195, 0 }, /* (111) alter_db_optr ::= */ + { 195, -2 }, /* (112) alter_db_optr ::= alter_db_optr replica */ + { 195, -2 }, /* (113) alter_db_optr ::= alter_db_optr quorum */ + { 195, -2 }, /* (114) alter_db_optr ::= alter_db_optr keep */ + { 195, -2 }, /* (115) alter_db_optr ::= alter_db_optr blocks */ + { 195, -2 }, /* (116) alter_db_optr ::= alter_db_optr comp */ + { 195, -2 }, /* (117) alter_db_optr ::= alter_db_optr wal */ + { 195, -2 }, /* (118) alter_db_optr ::= alter_db_optr fsync */ + { 195, -2 }, /* (119) alter_db_optr ::= alter_db_optr update */ + { 195, -2 }, /* (120) alter_db_optr ::= alter_db_optr cachelast */ + { 196, -1 }, /* (121) alter_topic_optr ::= alter_db_optr */ + { 196, -2 }, /* (122) alter_topic_optr ::= alter_topic_optr partitions */ + { 227, -1 }, /* (123) typename ::= ids */ + { 227, -4 }, /* (124) typename ::= ids LP signed RP */ + { 227, -2 }, /* (125) typename ::= ids UNSIGNED */ + { 228, -1 }, /* (126) signed ::= INTEGER */ + { 228, -2 }, /* (127) signed ::= PLUS INTEGER */ + { 228, -2 }, /* (128) signed ::= MINUS INTEGER */ + { 190, -3 }, /* (129) cmd ::= CREATE TABLE create_table_args */ + { 190, -3 }, /* (130) cmd ::= CREATE TABLE create_stable_args */ + { 190, -3 }, /* (131) cmd ::= CREATE STABLE create_stable_args */ + { 190, -3 }, /* (132) cmd ::= CREATE TABLE create_table_list */ + { 231, -1 }, /* (133) create_table_list ::= create_from_stable */ + { 231, -2 }, /* (134) create_table_list ::= create_table_list create_from_stable */ + { 229, -6 }, /* (135) create_table_args ::= ifnotexists ids cpxName LP columnlist RP */ + { 230, -10 }, /* (136) create_stable_args ::= ifnotexists ids cpxName LP columnlist RP TAGS LP columnlist RP */ + { 232, -10 }, /* (137) create_from_stable ::= ifnotexists ids cpxName USING ids cpxName TAGS LP tagitemlist RP */ + { 232, -13 }, /* (138) create_from_stable ::= ifnotexists ids cpxName USING ids cpxName LP tagNamelist RP TAGS LP tagitemlist RP */ + { 234, -3 }, /* (139) tagNamelist ::= tagNamelist COMMA ids */ + { 234, -1 }, /* (140) tagNamelist ::= ids */ + { 229, -5 }, /* (141) create_table_args ::= ifnotexists ids cpxName AS select */ + { 233, -3 }, /* (142) columnlist ::= columnlist COMMA column */ + { 233, -1 }, /* (143) columnlist ::= column */ + { 236, -2 }, /* (144) column ::= ids typename */ + { 211, -3 }, /* (145) tagitemlist ::= tagitemlist COMMA tagitem */ + { 211, -1 }, /* (146) tagitemlist ::= tagitem */ + { 237, -1 }, /* (147) tagitem ::= INTEGER */ + { 237, -1 }, /* (148) tagitem ::= FLOAT */ + { 237, -1 }, /* (149) tagitem ::= STRING */ + { 237, -1 }, /* (150) tagitem ::= BOOL */ + { 237, -1 }, /* (151) tagitem ::= NULL */ + { 237, -2 }, /* (152) tagitem ::= MINUS INTEGER */ + { 237, -2 }, /* (153) tagitem ::= MINUS FLOAT */ + { 237, -2 }, /* (154) tagitem ::= PLUS INTEGER */ + { 237, -2 }, /* (155) tagitem ::= PLUS FLOAT */ + { 235, -13 }, /* (156) select ::= SELECT selcollist from where_opt interval_opt session_option fill_opt sliding_opt groupby_opt orderby_opt having_opt slimit_opt limit_opt */ + { 235, -3 }, /* (157) select ::= LP select RP */ + { 250, -1 }, /* (158) union ::= select */ + { 250, -4 }, /* (159) union ::= union UNION ALL select */ + { 190, -1 }, /* (160) cmd ::= union */ + { 235, -2 }, /* (161) select ::= SELECT selcollist */ + { 251, -2 }, /* (162) sclp ::= selcollist COMMA */ + { 251, 0 }, /* (163) sclp ::= */ + { 238, -4 }, /* (164) selcollist ::= sclp distinct expr as */ + { 238, -2 }, /* (165) selcollist ::= sclp STAR */ + { 254, -2 }, /* (166) as ::= AS ids */ + { 254, -1 }, /* (167) as ::= ids */ + { 254, 0 }, /* (168) as ::= */ + { 252, -1 }, /* (169) distinct ::= DISTINCT */ + { 252, 0 }, /* (170) distinct ::= */ + { 239, -2 }, /* (171) from ::= FROM tablelist */ + { 239, -4 }, /* (172) from ::= FROM LP union RP */ + { 255, -2 }, /* (173) tablelist ::= ids cpxName */ + { 255, -3 }, /* (174) tablelist ::= ids cpxName ids */ + { 255, -4 }, /* (175) tablelist ::= tablelist COMMA ids cpxName */ + { 255, -5 }, /* (176) tablelist ::= tablelist COMMA ids cpxName ids */ + { 256, -1 }, /* (177) tmvar ::= VARIABLE */ + { 241, -4 }, /* (178) interval_opt ::= INTERVAL LP tmvar RP */ + { 241, -6 }, /* (179) interval_opt ::= INTERVAL LP tmvar COMMA tmvar RP */ + { 241, 0 }, /* (180) interval_opt ::= */ + { 242, 0 }, /* (181) session_option ::= */ + { 242, -7 }, /* (182) session_option ::= SESSION LP ids cpxName COMMA tmvar RP */ + { 243, 0 }, /* (183) fill_opt ::= */ + { 243, -6 }, /* (184) fill_opt ::= FILL LP ID COMMA tagitemlist RP */ + { 243, -4 }, /* (185) fill_opt ::= FILL LP ID RP */ + { 244, -4 }, /* (186) sliding_opt ::= SLIDING LP tmvar RP */ + { 244, 0 }, /* (187) sliding_opt ::= */ + { 246, 0 }, /* (188) orderby_opt ::= */ + { 246, -3 }, /* (189) orderby_opt ::= ORDER BY sortlist */ + { 257, -4 }, /* (190) sortlist ::= sortlist COMMA item sortorder */ + { 257, -2 }, /* (191) sortlist ::= item sortorder */ + { 259, -2 }, /* (192) item ::= ids cpxName */ + { 260, -1 }, /* (193) sortorder ::= ASC */ + { 260, -1 }, /* (194) sortorder ::= DESC */ + { 260, 0 }, /* (195) sortorder ::= */ + { 245, 0 }, /* (196) groupby_opt ::= */ + { 245, -3 }, /* (197) groupby_opt ::= GROUP BY grouplist */ + { 261, -3 }, /* (198) grouplist ::= grouplist COMMA item */ + { 261, -1 }, /* (199) grouplist ::= item */ + { 247, 0 }, /* (200) having_opt ::= */ + { 247, -2 }, /* (201) having_opt ::= HAVING expr */ + { 249, 0 }, /* (202) limit_opt ::= */ + { 249, -2 }, /* (203) limit_opt ::= LIMIT signed */ + { 249, -4 }, /* (204) limit_opt ::= LIMIT signed OFFSET signed */ + { 249, -4 }, /* (205) limit_opt ::= LIMIT signed COMMA signed */ + { 248, 0 }, /* (206) slimit_opt ::= */ + { 248, -2 }, /* (207) slimit_opt ::= SLIMIT signed */ + { 248, -4 }, /* (208) slimit_opt ::= SLIMIT signed SOFFSET signed */ + { 248, -4 }, /* (209) slimit_opt ::= SLIMIT signed COMMA signed */ + { 240, 0 }, /* (210) where_opt ::= */ + { 240, -2 }, /* (211) where_opt ::= WHERE expr */ + { 253, -3 }, /* (212) expr ::= LP expr RP */ + { 253, -1 }, /* (213) expr ::= ID */ + { 253, -3 }, /* (214) expr ::= ID DOT ID */ + { 253, -3 }, /* (215) expr ::= ID DOT STAR */ + { 253, -1 }, /* (216) expr ::= INTEGER */ + { 253, -2 }, /* (217) expr ::= MINUS INTEGER */ + { 253, -2 }, /* (218) expr ::= PLUS INTEGER */ + { 253, -1 }, /* (219) expr ::= FLOAT */ + { 253, -2 }, /* (220) expr ::= MINUS FLOAT */ + { 253, -2 }, /* (221) expr ::= PLUS FLOAT */ + { 253, -1 }, /* (222) expr ::= STRING */ + { 253, -1 }, /* (223) expr ::= NOW */ + { 253, -1 }, /* (224) expr ::= VARIABLE */ + { 253, -1 }, /* (225) expr ::= BOOL */ + { 253, -4 }, /* (226) expr ::= ID LP exprlist RP */ + { 253, -4 }, /* (227) expr ::= ID LP STAR RP */ + { 253, -3 }, /* (228) expr ::= expr IS NULL */ + { 253, -4 }, /* (229) expr ::= expr IS NOT NULL */ + { 253, -3 }, /* (230) expr ::= expr LT expr */ + { 253, -3 }, /* (231) expr ::= expr GT expr */ + { 253, -3 }, /* (232) expr ::= expr LE expr */ + { 253, -3 }, /* (233) expr ::= expr GE expr */ + { 253, -3 }, /* (234) expr ::= expr NE expr */ + { 253, -3 }, /* (235) expr ::= expr EQ expr */ + { 253, -5 }, /* (236) expr ::= expr BETWEEN expr AND expr */ + { 253, -3 }, /* (237) expr ::= expr AND expr */ + { 253, -3 }, /* (238) expr ::= expr OR expr */ + { 253, -3 }, /* (239) expr ::= expr PLUS expr */ + { 253, -3 }, /* (240) expr ::= expr MINUS expr */ + { 253, -3 }, /* (241) expr ::= expr STAR expr */ + { 253, -3 }, /* (242) expr ::= expr SLASH expr */ + { 253, -3 }, /* (243) expr ::= expr REM expr */ + { 253, -3 }, /* (244) expr ::= expr LIKE expr */ + { 253, -5 }, /* (245) expr ::= expr IN LP exprlist RP */ + { 262, -3 }, /* (246) exprlist ::= exprlist COMMA expritem */ + { 262, -1 }, /* (247) exprlist ::= expritem */ + { 263, -1 }, /* (248) expritem ::= expr */ + { 263, 0 }, /* (249) expritem ::= */ + { 190, -3 }, /* (250) cmd ::= RESET QUERY CACHE */ + { 190, -7 }, /* (251) cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ + { 190, -7 }, /* (252) cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ + { 190, -7 }, /* (253) cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ + { 190, -7 }, /* (254) cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ + { 190, -8 }, /* (255) cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ + { 190, -9 }, /* (256) cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ + { 190, -7 }, /* (257) cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist */ + { 190, -7 }, /* (258) cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids */ + { 190, -7 }, /* (259) cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist */ + { 190, -7 }, /* (260) cmd ::= ALTER STABLE ids cpxName DROP TAG ids */ + { 190, -8 }, /* (261) cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids */ + { 190, -3 }, /* (262) cmd ::= KILL CONNECTION INTEGER */ + { 190, -5 }, /* (263) cmd ::= KILL STREAM INTEGER COLON INTEGER */ + { 190, -5 }, /* (264) cmd ::= KILL QUERY INTEGER COLON INTEGER */ }; static void yy_accept(yyParser*); /* Forward Declaration */ @@ -2346,34 +2024,30 @@ static void yy_accept(yyParser*); /* Forward Declaration */ ** only called from one place, optimizing compilers will in-line it, which ** means that the extra parameters have no performance impact. */ -static YYACTIONTYPE yy_reduce( +static void yy_reduce( yyParser *yypParser, /* The parser */ unsigned int yyruleno, /* Number of the rule by which to reduce */ int yyLookahead, /* Lookahead token, or YYNOCODE if none */ ParseTOKENTYPE yyLookaheadToken /* Value of the lookahead token */ - ParseCTX_PDECL /* %extra_context */ ){ int yygoto; /* The next state */ - YYACTIONTYPE yyact; /* The next action */ + int yyact; /* The next action */ yyStackEntry *yymsp; /* The top of the parser's stack */ int yysize; /* Amount to pop the stack */ - ParseARG_FETCH + ParseARG_FETCH; (void)yyLookahead; (void)yyLookaheadToken; yymsp = yypParser->yytos; #ifndef NDEBUG if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ - yysize = yyRuleInfoNRhs[yyruleno]; + yysize = yyRuleInfo[yyruleno].nrhs; if( yysize ){ - fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n", + fprintf(yyTraceFILE, "%sReduce %d [%s], go to state %d.\n", yyTracePrompt, - yyruleno, yyRuleName[yyruleno], - yyrulenoyytos - yypParser->yystack)>yypParser->yyhwm ){ yypParser->yyhwm++; @@ -2391,19 +2065,13 @@ static YYACTIONTYPE yy_reduce( #if YYSTACKDEPTH>0 if( yypParser->yytos>=yypParser->yystackEnd ){ yyStackOverflow(yypParser); - /* The call to yyStackOverflow() above pops the stack until it is - ** empty, causing the main parser loop to exit. So the return value - ** is never used and does not matter. */ - return 0; + return; } #else if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){ if( yyGrowStack(yypParser) ){ yyStackOverflow(yypParser); - /* The call to yyStackOverflow() above pops the stack until it is - ** empty, causing the main parser loop to exit. So the return value - ** is never used and does not matter. */ - return 0; + return; } yymsp = yypParser->yytos; } @@ -2488,12 +2156,12 @@ static YYACTIONTYPE yy_reduce( case 20: /* cmd ::= SHOW CREATE TABLE ids cpxName */ { yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - setDCLSQLElems(pInfo, TSDB_SQL_SHOW_CREATE_TABLE, 1, &yymsp[-1].minor.yy0); + setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_TABLE, 1, &yymsp[-1].minor.yy0); } break; case 21: /* cmd ::= SHOW CREATE DATABASE ids */ { - setDCLSQLElems(pInfo, TSDB_SQL_SHOW_CREATE_DATABASE, 1, &yymsp[0].minor.yy0); + setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_DATABASE, 1, &yymsp[0].minor.yy0); } break; case 22: /* cmd ::= SHOW dbPrefix TABLES */ @@ -2514,21 +2182,21 @@ static YYACTIONTYPE yy_reduce( case 25: /* cmd ::= SHOW dbPrefix STABLES LIKE ids */ { SStrToken token; - setDbName(&token, &yymsp[-3].minor.yy0); + tSetDbName(&token, &yymsp[-3].minor.yy0); setShowOptions(pInfo, TSDB_MGMT_TABLE_METRIC, &token, &yymsp[0].minor.yy0); } break; case 26: /* cmd ::= SHOW dbPrefix VGROUPS */ { SStrToken token; - setDbName(&token, &yymsp[-1].minor.yy0); + tSetDbName(&token, &yymsp[-1].minor.yy0); setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, 0); } break; case 27: /* cmd ::= SHOW dbPrefix VGROUPS ids */ { SStrToken token; - setDbName(&token, &yymsp[-2].minor.yy0); + tSetDbName(&token, &yymsp[-2].minor.yy0); setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, &yymsp[0].minor.yy0); } break; @@ -2551,21 +2219,21 @@ static YYACTIONTYPE yy_reduce( { setDropDbTableInfo(pInfo, TSDB_SQL_DROP_DB, &yymsp[0].minor.yy0, &yymsp[-1].minor.yy0, TSDB_DB_TYPE_TOPIC, -1); } break; case 32: /* cmd ::= DROP DNODE ids */ -{ setDCLSQLElems(pInfo, TSDB_SQL_DROP_DNODE, 1, &yymsp[0].minor.yy0); } +{ setDCLSqlElems(pInfo, TSDB_SQL_DROP_DNODE, 1, &yymsp[0].minor.yy0); } break; case 33: /* cmd ::= DROP USER ids */ -{ setDCLSQLElems(pInfo, TSDB_SQL_DROP_USER, 1, &yymsp[0].minor.yy0); } +{ setDCLSqlElems(pInfo, TSDB_SQL_DROP_USER, 1, &yymsp[0].minor.yy0); } break; case 34: /* cmd ::= DROP ACCOUNT ids */ -{ setDCLSQLElems(pInfo, TSDB_SQL_DROP_ACCT, 1, &yymsp[0].minor.yy0); } +{ setDCLSqlElems(pInfo, TSDB_SQL_DROP_ACCT, 1, &yymsp[0].minor.yy0); } break; case 35: /* cmd ::= USE ids */ -{ setDCLSQLElems(pInfo, TSDB_SQL_USE_DB, 1, &yymsp[0].minor.yy0);} +{ setDCLSqlElems(pInfo, TSDB_SQL_USE_DB, 1, &yymsp[0].minor.yy0);} break; case 36: /* cmd ::= DESCRIBE ids cpxName */ { yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - setDCLSQLElems(pInfo, TSDB_SQL_DESCRIBE_TABLE, 1, &yymsp[-1].minor.yy0); + setDCLSqlElems(pInfo, TSDB_SQL_DESCRIBE_TABLE, 1, &yymsp[-1].minor.yy0); } break; case 37: /* cmd ::= ALTER USER ids PASS ids */ @@ -2575,26 +2243,26 @@ static YYACTIONTYPE yy_reduce( { setAlterUserSql(pInfo, TSDB_ALTER_USER_PRIVILEGES, &yymsp[-2].minor.yy0, NULL, &yymsp[0].minor.yy0);} break; case 39: /* cmd ::= ALTER DNODE ids ids */ -{ setDCLSQLElems(pInfo, TSDB_SQL_CFG_DNODE, 2, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } +{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_DNODE, 2, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } break; case 40: /* cmd ::= ALTER DNODE ids ids ids */ -{ setDCLSQLElems(pInfo, TSDB_SQL_CFG_DNODE, 3, &yymsp[-2].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } +{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_DNODE, 3, &yymsp[-2].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } break; case 41: /* cmd ::= ALTER LOCAL ids */ -{ setDCLSQLElems(pInfo, TSDB_SQL_CFG_LOCAL, 1, &yymsp[0].minor.yy0); } +{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_LOCAL, 1, &yymsp[0].minor.yy0); } break; case 42: /* cmd ::= ALTER LOCAL ids ids */ -{ setDCLSQLElems(pInfo, TSDB_SQL_CFG_LOCAL, 2, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } +{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_LOCAL, 2, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } break; case 43: /* cmd ::= ALTER DATABASE ids alter_db_optr */ case 44: /* cmd ::= ALTER TOPIC ids alter_topic_optr */ yytestcase(yyruleno==44); -{ SStrToken t = {0}; setCreateDbInfo(pInfo, TSDB_SQL_ALTER_DB, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy100, &t);} +{ SStrToken t = {0}; setCreateDbInfo(pInfo, TSDB_SQL_ALTER_DB, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy526, &t);} break; case 45: /* cmd ::= ALTER ACCOUNT ids acct_optr */ -{ setCreateAcctSql(pInfo, TSDB_SQL_ALTER_ACCT, &yymsp[-1].minor.yy0, NULL, &yymsp[0].minor.yy505);} +{ setCreateAcctSql(pInfo, TSDB_SQL_ALTER_ACCT, &yymsp[-1].minor.yy0, NULL, &yymsp[0].minor.yy187);} break; case 46: /* cmd ::= ALTER ACCOUNT ids PASS ids acct_optr */ -{ setCreateAcctSql(pInfo, TSDB_SQL_ALTER_ACCT, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy505);} +{ setCreateAcctSql(pInfo, TSDB_SQL_ALTER_ACCT, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy187);} break; case 47: /* ids ::= ID */ case 48: /* ids ::= STRING */ yytestcase(yyruleno==48); @@ -2606,21 +2274,21 @@ static YYACTIONTYPE yy_reduce( break; case 50: /* ifexists ::= */ case 52: /* ifnotexists ::= */ yytestcase(yyruleno==52); - case 171: /* distinct ::= */ yytestcase(yyruleno==171); + case 170: /* distinct ::= */ yytestcase(yyruleno==170); { yymsp[1].minor.yy0.n = 0;} break; case 51: /* ifnotexists ::= IF NOT EXISTS */ { yymsp[-2].minor.yy0.n = 1;} break; case 53: /* cmd ::= CREATE DNODE ids */ -{ setDCLSQLElems(pInfo, TSDB_SQL_CREATE_DNODE, 1, &yymsp[0].minor.yy0);} +{ setDCLSqlElems(pInfo, TSDB_SQL_CREATE_DNODE, 1, &yymsp[0].minor.yy0);} break; case 54: /* cmd ::= CREATE ACCOUNT ids PASS ids acct_optr */ -{ setCreateAcctSql(pInfo, TSDB_SQL_CREATE_ACCT, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy505);} +{ setCreateAcctSql(pInfo, TSDB_SQL_CREATE_ACCT, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy187);} break; case 55: /* cmd ::= CREATE DATABASE ifnotexists ids db_optr */ case 56: /* cmd ::= CREATE TOPIC ifnotexists ids topic_optr */ yytestcase(yyruleno==56); -{ setCreateDbInfo(pInfo, TSDB_SQL_CREATE_DB, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy100, &yymsp[-2].minor.yy0);} +{ setCreateDbInfo(pInfo, TSDB_SQL_CREATE_DB, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy526, &yymsp[-2].minor.yy0);} break; case 57: /* cmd ::= CREATE USER ids PASS ids */ { setCreateUserSql(pInfo, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);} @@ -2649,20 +2317,20 @@ static YYACTIONTYPE yy_reduce( break; case 76: /* acct_optr ::= pps tseries storage streams qtime dbs users conns state */ { - yylhsminor.yy505.maxUsers = (yymsp[-2].minor.yy0.n>0)?atoi(yymsp[-2].minor.yy0.z):-1; - yylhsminor.yy505.maxDbs = (yymsp[-3].minor.yy0.n>0)?atoi(yymsp[-3].minor.yy0.z):-1; - yylhsminor.yy505.maxTimeSeries = (yymsp[-7].minor.yy0.n>0)?atoi(yymsp[-7].minor.yy0.z):-1; - yylhsminor.yy505.maxStreams = (yymsp[-5].minor.yy0.n>0)?atoi(yymsp[-5].minor.yy0.z):-1; - yylhsminor.yy505.maxPointsPerSecond = (yymsp[-8].minor.yy0.n>0)?atoi(yymsp[-8].minor.yy0.z):-1; - yylhsminor.yy505.maxStorage = (yymsp[-6].minor.yy0.n>0)?strtoll(yymsp[-6].minor.yy0.z, NULL, 10):-1; - yylhsminor.yy505.maxQueryTime = (yymsp[-4].minor.yy0.n>0)?strtoll(yymsp[-4].minor.yy0.z, NULL, 10):-1; - yylhsminor.yy505.maxConnections = (yymsp[-1].minor.yy0.n>0)?atoi(yymsp[-1].minor.yy0.z):-1; - yylhsminor.yy505.stat = yymsp[0].minor.yy0; -} - yymsp[-8].minor.yy505 = yylhsminor.yy505; + yylhsminor.yy187.maxUsers = (yymsp[-2].minor.yy0.n>0)?atoi(yymsp[-2].minor.yy0.z):-1; + yylhsminor.yy187.maxDbs = (yymsp[-3].minor.yy0.n>0)?atoi(yymsp[-3].minor.yy0.z):-1; + yylhsminor.yy187.maxTimeSeries = (yymsp[-7].minor.yy0.n>0)?atoi(yymsp[-7].minor.yy0.z):-1; + yylhsminor.yy187.maxStreams = (yymsp[-5].minor.yy0.n>0)?atoi(yymsp[-5].minor.yy0.z):-1; + yylhsminor.yy187.maxPointsPerSecond = (yymsp[-8].minor.yy0.n>0)?atoi(yymsp[-8].minor.yy0.z):-1; + yylhsminor.yy187.maxStorage = (yymsp[-6].minor.yy0.n>0)?strtoll(yymsp[-6].minor.yy0.z, NULL, 10):-1; + yylhsminor.yy187.maxQueryTime = (yymsp[-4].minor.yy0.n>0)?strtoll(yymsp[-4].minor.yy0.z, NULL, 10):-1; + yylhsminor.yy187.maxConnections = (yymsp[-1].minor.yy0.n>0)?atoi(yymsp[-1].minor.yy0.z):-1; + yylhsminor.yy187.stat = yymsp[0].minor.yy0; +} + yymsp[-8].minor.yy187 = yylhsminor.yy187; break; case 77: /* keep ::= KEEP tagitemlist */ -{ yymsp[-1].minor.yy207 = yymsp[0].minor.yy207; } +{ yymsp[-1].minor.yy285 = yymsp[0].minor.yy285; } break; case 78: /* cache ::= CACHE INTEGER */ case 79: /* replica ::= REPLICA INTEGER */ yytestcase(yyruleno==79); @@ -2682,234 +2350,234 @@ static YYACTIONTYPE yy_reduce( { yymsp[-1].minor.yy0 = yymsp[0].minor.yy0; } break; case 93: /* db_optr ::= */ -{setDefaultCreateDbOption(&yymsp[1].minor.yy100); yymsp[1].minor.yy100.dbType = TSDB_DB_TYPE_DEFAULT;} +{setDefaultCreateDbOption(&yymsp[1].minor.yy526); yymsp[1].minor.yy526.dbType = TSDB_DB_TYPE_DEFAULT;} break; case 94: /* db_optr ::= db_optr cache */ -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.cacheBlockSize = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.cacheBlockSize = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 95: /* db_optr ::= db_optr replica */ case 112: /* alter_db_optr ::= alter_db_optr replica */ yytestcase(yyruleno==112); -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.replica = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.replica = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 96: /* db_optr ::= db_optr quorum */ case 113: /* alter_db_optr ::= alter_db_optr quorum */ yytestcase(yyruleno==113); -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.quorum = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.quorum = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 97: /* db_optr ::= db_optr days */ -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.daysPerFile = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.daysPerFile = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 98: /* db_optr ::= db_optr minrows */ -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.minRowsPerBlock = strtod(yymsp[0].minor.yy0.z, NULL); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.minRowsPerBlock = strtod(yymsp[0].minor.yy0.z, NULL); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 99: /* db_optr ::= db_optr maxrows */ -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.maxRowsPerBlock = strtod(yymsp[0].minor.yy0.z, NULL); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.maxRowsPerBlock = strtod(yymsp[0].minor.yy0.z, NULL); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 100: /* db_optr ::= db_optr blocks */ case 115: /* alter_db_optr ::= alter_db_optr blocks */ yytestcase(yyruleno==115); -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.numOfBlocks = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.numOfBlocks = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 101: /* db_optr ::= db_optr ctime */ -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.commitTime = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.commitTime = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 102: /* db_optr ::= db_optr wal */ case 117: /* alter_db_optr ::= alter_db_optr wal */ yytestcase(yyruleno==117); -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.walLevel = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.walLevel = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 103: /* db_optr ::= db_optr fsync */ case 118: /* alter_db_optr ::= alter_db_optr fsync */ yytestcase(yyruleno==118); -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.fsyncPeriod = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.fsyncPeriod = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 104: /* db_optr ::= db_optr comp */ case 116: /* alter_db_optr ::= alter_db_optr comp */ yytestcase(yyruleno==116); -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.compressionLevel = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.compressionLevel = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 105: /* db_optr ::= db_optr prec */ -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.precision = yymsp[0].minor.yy0; } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.precision = yymsp[0].minor.yy0; } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 106: /* db_optr ::= db_optr keep */ case 114: /* alter_db_optr ::= alter_db_optr keep */ yytestcase(yyruleno==114); -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.keep = yymsp[0].minor.yy207; } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.keep = yymsp[0].minor.yy285; } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 107: /* db_optr ::= db_optr update */ case 119: /* alter_db_optr ::= alter_db_optr update */ yytestcase(yyruleno==119); -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.update = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.update = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 108: /* db_optr ::= db_optr cachelast */ case 120: /* alter_db_optr ::= alter_db_optr cachelast */ yytestcase(yyruleno==120); -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.cachelast = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.cachelast = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 109: /* topic_optr ::= db_optr */ case 121: /* alter_topic_optr ::= alter_db_optr */ yytestcase(yyruleno==121); -{ yylhsminor.yy100 = yymsp[0].minor.yy100; yylhsminor.yy100.dbType = TSDB_DB_TYPE_TOPIC; } - yymsp[0].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[0].minor.yy526; yylhsminor.yy526.dbType = TSDB_DB_TYPE_TOPIC; } + yymsp[0].minor.yy526 = yylhsminor.yy526; break; case 110: /* topic_optr ::= topic_optr partitions */ case 122: /* alter_topic_optr ::= alter_topic_optr partitions */ yytestcase(yyruleno==122); -{ yylhsminor.yy100 = yymsp[-1].minor.yy100; yylhsminor.yy100.partitions = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[-1].minor.yy100 = yylhsminor.yy100; +{ yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526.partitions = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy526 = yylhsminor.yy526; break; case 111: /* alter_db_optr ::= */ -{ setDefaultCreateDbOption(&yymsp[1].minor.yy100); yymsp[1].minor.yy100.dbType = TSDB_DB_TYPE_DEFAULT;} +{ setDefaultCreateDbOption(&yymsp[1].minor.yy526); yymsp[1].minor.yy526.dbType = TSDB_DB_TYPE_DEFAULT;} break; case 123: /* typename ::= ids */ { yymsp[0].minor.yy0.type = 0; - tSqlSetColumnType (&yylhsminor.yy517, &yymsp[0].minor.yy0); + tSetColumnType (&yylhsminor.yy295, &yymsp[0].minor.yy0); } - yymsp[0].minor.yy517 = yylhsminor.yy517; + yymsp[0].minor.yy295 = yylhsminor.yy295; break; case 124: /* typename ::= ids LP signed RP */ { - if (yymsp[-1].minor.yy208 <= 0) { + if (yymsp[-1].minor.yy525 <= 0) { yymsp[-3].minor.yy0.type = 0; - tSqlSetColumnType(&yylhsminor.yy517, &yymsp[-3].minor.yy0); + tSetColumnType(&yylhsminor.yy295, &yymsp[-3].minor.yy0); } else { - yymsp[-3].minor.yy0.type = -yymsp[-1].minor.yy208; // negative value of name length - tSqlSetColumnType(&yylhsminor.yy517, &yymsp[-3].minor.yy0); + yymsp[-3].minor.yy0.type = -yymsp[-1].minor.yy525; // negative value of name length + tSetColumnType(&yylhsminor.yy295, &yymsp[-3].minor.yy0); } } - yymsp[-3].minor.yy517 = yylhsminor.yy517; + yymsp[-3].minor.yy295 = yylhsminor.yy295; break; case 125: /* typename ::= ids UNSIGNED */ { yymsp[-1].minor.yy0.type = 0; yymsp[-1].minor.yy0.n = ((yymsp[0].minor.yy0.z + yymsp[0].minor.yy0.n) - yymsp[-1].minor.yy0.z); - tSqlSetColumnType (&yylhsminor.yy517, &yymsp[-1].minor.yy0); + tSetColumnType (&yylhsminor.yy295, &yymsp[-1].minor.yy0); } - yymsp[-1].minor.yy517 = yylhsminor.yy517; + yymsp[-1].minor.yy295 = yylhsminor.yy295; break; case 126: /* signed ::= INTEGER */ -{ yylhsminor.yy208 = strtol(yymsp[0].minor.yy0.z, NULL, 10); } - yymsp[0].minor.yy208 = yylhsminor.yy208; +{ yylhsminor.yy525 = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[0].minor.yy525 = yylhsminor.yy525; break; case 127: /* signed ::= PLUS INTEGER */ -{ yymsp[-1].minor.yy208 = strtol(yymsp[0].minor.yy0.z, NULL, 10); } +{ yymsp[-1].minor.yy525 = strtol(yymsp[0].minor.yy0.z, NULL, 10); } break; case 128: /* signed ::= MINUS INTEGER */ -{ yymsp[-1].minor.yy208 = -strtol(yymsp[0].minor.yy0.z, NULL, 10);} +{ yymsp[-1].minor.yy525 = -strtol(yymsp[0].minor.yy0.z, NULL, 10);} break; case 132: /* cmd ::= CREATE TABLE create_table_list */ -{ pInfo->type = TSDB_SQL_CREATE_TABLE; pInfo->pCreateTableInfo = yymsp[0].minor.yy414;} +{ pInfo->type = TSDB_SQL_CREATE_TABLE; pInfo->pCreateTableInfo = yymsp[0].minor.yy470;} break; case 133: /* create_table_list ::= create_from_stable */ { - SCreateTableSQL* pCreateTable = calloc(1, sizeof(SCreateTableSQL)); + SCreateTableSql* pCreateTable = calloc(1, sizeof(SCreateTableSql)); pCreateTable->childTableInfo = taosArrayInit(4, sizeof(SCreatedTableInfo)); - taosArrayPush(pCreateTable->childTableInfo, &yymsp[0].minor.yy542); + taosArrayPush(pCreateTable->childTableInfo, &yymsp[0].minor.yy96); pCreateTable->type = TSQL_CREATE_TABLE_FROM_STABLE; - yylhsminor.yy414 = pCreateTable; + yylhsminor.yy470 = pCreateTable; } - yymsp[0].minor.yy414 = yylhsminor.yy414; + yymsp[0].minor.yy470 = yylhsminor.yy470; break; case 134: /* create_table_list ::= create_table_list create_from_stable */ { - taosArrayPush(yymsp[-1].minor.yy414->childTableInfo, &yymsp[0].minor.yy542); - yylhsminor.yy414 = yymsp[-1].minor.yy414; + taosArrayPush(yymsp[-1].minor.yy470->childTableInfo, &yymsp[0].minor.yy96); + yylhsminor.yy470 = yymsp[-1].minor.yy470; } - yymsp[-1].minor.yy414 = yylhsminor.yy414; + yymsp[-1].minor.yy470 = yylhsminor.yy470; break; case 135: /* create_table_args ::= ifnotexists ids cpxName LP columnlist RP */ { - yylhsminor.yy414 = tSetCreateSqlElems(yymsp[-1].minor.yy207, NULL, NULL, TSQL_CREATE_TABLE); - setSqlInfo(pInfo, yylhsminor.yy414, NULL, TSDB_SQL_CREATE_TABLE); + yylhsminor.yy470 = tSetCreateTableInfo(yymsp[-1].minor.yy285, NULL, NULL, TSQL_CREATE_TABLE); + setSqlInfo(pInfo, yylhsminor.yy470, NULL, TSDB_SQL_CREATE_TABLE); yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; setCreatedTableName(pInfo, &yymsp[-4].minor.yy0, &yymsp[-5].minor.yy0); } - yymsp[-5].minor.yy414 = yylhsminor.yy414; + yymsp[-5].minor.yy470 = yylhsminor.yy470; break; case 136: /* create_stable_args ::= ifnotexists ids cpxName LP columnlist RP TAGS LP columnlist RP */ { - yylhsminor.yy414 = tSetCreateSqlElems(yymsp[-5].minor.yy207, yymsp[-1].minor.yy207, NULL, TSQL_CREATE_STABLE); - setSqlInfo(pInfo, yylhsminor.yy414, NULL, TSDB_SQL_CREATE_TABLE); + yylhsminor.yy470 = tSetCreateTableInfo(yymsp[-5].minor.yy285, yymsp[-1].minor.yy285, NULL, TSQL_CREATE_STABLE); + setSqlInfo(pInfo, yylhsminor.yy470, NULL, TSDB_SQL_CREATE_TABLE); yymsp[-8].minor.yy0.n += yymsp[-7].minor.yy0.n; setCreatedTableName(pInfo, &yymsp[-8].minor.yy0, &yymsp[-9].minor.yy0); } - yymsp[-9].minor.yy414 = yylhsminor.yy414; + yymsp[-9].minor.yy470 = yylhsminor.yy470; break; case 137: /* create_from_stable ::= ifnotexists ids cpxName USING ids cpxName TAGS LP tagitemlist RP */ { yymsp[-5].minor.yy0.n += yymsp[-4].minor.yy0.n; yymsp[-8].minor.yy0.n += yymsp[-7].minor.yy0.n; - yylhsminor.yy542 = createNewChildTableInfo(&yymsp[-5].minor.yy0, NULL, yymsp[-1].minor.yy207, &yymsp[-8].minor.yy0, &yymsp[-9].minor.yy0); + yylhsminor.yy96 = createNewChildTableInfo(&yymsp[-5].minor.yy0, NULL, yymsp[-1].minor.yy285, &yymsp[-8].minor.yy0, &yymsp[-9].minor.yy0); } - yymsp[-9].minor.yy542 = yylhsminor.yy542; + yymsp[-9].minor.yy96 = yylhsminor.yy96; break; case 138: /* create_from_stable ::= ifnotexists ids cpxName USING ids cpxName LP tagNamelist RP TAGS LP tagitemlist RP */ { yymsp[-8].minor.yy0.n += yymsp[-7].minor.yy0.n; yymsp[-11].minor.yy0.n += yymsp[-10].minor.yy0.n; - yylhsminor.yy542 = createNewChildTableInfo(&yymsp[-8].minor.yy0, yymsp[-5].minor.yy207, yymsp[-1].minor.yy207, &yymsp[-11].minor.yy0, &yymsp[-12].minor.yy0); + yylhsminor.yy96 = createNewChildTableInfo(&yymsp[-8].minor.yy0, yymsp[-5].minor.yy285, yymsp[-1].minor.yy285, &yymsp[-11].minor.yy0, &yymsp[-12].minor.yy0); } - yymsp[-12].minor.yy542 = yylhsminor.yy542; + yymsp[-12].minor.yy96 = yylhsminor.yy96; break; case 139: /* tagNamelist ::= tagNamelist COMMA ids */ -{taosArrayPush(yymsp[-2].minor.yy207, &yymsp[0].minor.yy0); yylhsminor.yy207 = yymsp[-2].minor.yy207; } - yymsp[-2].minor.yy207 = yylhsminor.yy207; +{taosArrayPush(yymsp[-2].minor.yy285, &yymsp[0].minor.yy0); yylhsminor.yy285 = yymsp[-2].minor.yy285; } + yymsp[-2].minor.yy285 = yylhsminor.yy285; break; case 140: /* tagNamelist ::= ids */ -{yylhsminor.yy207 = taosArrayInit(4, sizeof(SStrToken)); taosArrayPush(yylhsminor.yy207, &yymsp[0].minor.yy0);} - yymsp[0].minor.yy207 = yylhsminor.yy207; +{yylhsminor.yy285 = taosArrayInit(4, sizeof(SStrToken)); taosArrayPush(yylhsminor.yy285, &yymsp[0].minor.yy0);} + yymsp[0].minor.yy285 = yylhsminor.yy285; break; case 141: /* create_table_args ::= ifnotexists ids cpxName AS select */ { - yylhsminor.yy414 = tSetCreateSqlElems(NULL, NULL, yymsp[0].minor.yy526, TSQL_CREATE_STREAM); - setSqlInfo(pInfo, yylhsminor.yy414, NULL, TSDB_SQL_CREATE_TABLE); + yylhsminor.yy470 = tSetCreateTableInfo(NULL, NULL, yymsp[0].minor.yy342, TSQL_CREATE_STREAM); + setSqlInfo(pInfo, yylhsminor.yy470, NULL, TSDB_SQL_CREATE_TABLE); yymsp[-3].minor.yy0.n += yymsp[-2].minor.yy0.n; setCreatedTableName(pInfo, &yymsp[-3].minor.yy0, &yymsp[-4].minor.yy0); } - yymsp[-4].minor.yy414 = yylhsminor.yy414; + yymsp[-4].minor.yy470 = yylhsminor.yy470; break; case 142: /* columnlist ::= columnlist COMMA column */ -{taosArrayPush(yymsp[-2].minor.yy207, &yymsp[0].minor.yy517); yylhsminor.yy207 = yymsp[-2].minor.yy207; } - yymsp[-2].minor.yy207 = yylhsminor.yy207; +{taosArrayPush(yymsp[-2].minor.yy285, &yymsp[0].minor.yy295); yylhsminor.yy285 = yymsp[-2].minor.yy285; } + yymsp[-2].minor.yy285 = yylhsminor.yy285; break; case 143: /* columnlist ::= column */ -{yylhsminor.yy207 = taosArrayInit(4, sizeof(TAOS_FIELD)); taosArrayPush(yylhsminor.yy207, &yymsp[0].minor.yy517);} - yymsp[0].minor.yy207 = yylhsminor.yy207; +{yylhsminor.yy285 = taosArrayInit(4, sizeof(TAOS_FIELD)); taosArrayPush(yylhsminor.yy285, &yymsp[0].minor.yy295);} + yymsp[0].minor.yy285 = yylhsminor.yy285; break; case 144: /* column ::= ids typename */ { - tSqlSetColumnInfo(&yylhsminor.yy517, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy517); + tSetColumnInfo(&yylhsminor.yy295, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy295); } - yymsp[-1].minor.yy517 = yylhsminor.yy517; + yymsp[-1].minor.yy295 = yylhsminor.yy295; break; case 145: /* tagitemlist ::= tagitemlist COMMA tagitem */ -{ yylhsminor.yy207 = tVariantListAppend(yymsp[-2].minor.yy207, &yymsp[0].minor.yy232, -1); } - yymsp[-2].minor.yy207 = yylhsminor.yy207; +{ yylhsminor.yy285 = tVariantListAppend(yymsp[-2].minor.yy285, &yymsp[0].minor.yy362, -1); } + yymsp[-2].minor.yy285 = yylhsminor.yy285; break; case 146: /* tagitemlist ::= tagitem */ -{ yylhsminor.yy207 = tVariantListAppend(NULL, &yymsp[0].minor.yy232, -1); } - yymsp[0].minor.yy207 = yylhsminor.yy207; +{ yylhsminor.yy285 = tVariantListAppend(NULL, &yymsp[0].minor.yy362, -1); } + yymsp[0].minor.yy285 = yylhsminor.yy285; break; case 147: /* tagitem ::= INTEGER */ case 148: /* tagitem ::= FLOAT */ yytestcase(yyruleno==148); case 149: /* tagitem ::= STRING */ yytestcase(yyruleno==149); case 150: /* tagitem ::= BOOL */ yytestcase(yyruleno==150); -{ toTSDBType(yymsp[0].minor.yy0.type); tVariantCreate(&yylhsminor.yy232, &yymsp[0].minor.yy0); } - yymsp[0].minor.yy232 = yylhsminor.yy232; +{ toTSDBType(yymsp[0].minor.yy0.type); tVariantCreate(&yylhsminor.yy362, &yymsp[0].minor.yy0); } + yymsp[0].minor.yy362 = yylhsminor.yy362; break; case 151: /* tagitem ::= NULL */ -{ yymsp[0].minor.yy0.type = 0; tVariantCreate(&yylhsminor.yy232, &yymsp[0].minor.yy0); } - yymsp[0].minor.yy232 = yylhsminor.yy232; +{ yymsp[0].minor.yy0.type = 0; tVariantCreate(&yylhsminor.yy362, &yymsp[0].minor.yy0); } + yymsp[0].minor.yy362 = yylhsminor.yy362; break; case 152: /* tagitem ::= MINUS INTEGER */ case 153: /* tagitem ::= MINUS FLOAT */ yytestcase(yyruleno==153); @@ -2919,418 +2587,422 @@ static YYACTIONTYPE yy_reduce( yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = yymsp[0].minor.yy0.type; toTSDBType(yymsp[-1].minor.yy0.type); - tVariantCreate(&yylhsminor.yy232, &yymsp[-1].minor.yy0); + tVariantCreate(&yylhsminor.yy362, &yymsp[-1].minor.yy0); } - yymsp[-1].minor.yy232 = yylhsminor.yy232; + yymsp[-1].minor.yy362 = yylhsminor.yy362; break; - case 156: /* select ::= SELECT selcollist from where_opt interval_opt fill_opt sliding_opt groupby_opt orderby_opt having_opt slimit_opt limit_opt */ + case 156: /* select ::= SELECT selcollist from where_opt interval_opt session_option fill_opt sliding_opt groupby_opt orderby_opt having_opt slimit_opt limit_opt */ { - yylhsminor.yy526 = tSetQuerySqlElems(&yymsp[-11].minor.yy0, yymsp[-10].minor.yy178, yymsp[-9].minor.yy207, yymsp[-8].minor.yy484, yymsp[-4].minor.yy207, yymsp[-3].minor.yy207, &yymsp[-7].minor.yy126, &yymsp[-5].minor.yy0, yymsp[-6].minor.yy207, &yymsp[0].minor.yy314, &yymsp[-1].minor.yy314); + yylhsminor.yy342 = tSetQuerySqlNode(&yymsp[-12].minor.yy0, yymsp[-11].minor.yy285, yymsp[-10].minor.yy162, yymsp[-9].minor.yy178, yymsp[-4].minor.yy285, yymsp[-3].minor.yy285, &yymsp[-8].minor.yy376, &yymsp[-7].minor.yy523, &yymsp[-5].minor.yy0, yymsp[-6].minor.yy285, &yymsp[0].minor.yy438, &yymsp[-1].minor.yy438); } - yymsp[-11].minor.yy526 = yylhsminor.yy526; + yymsp[-12].minor.yy342 = yylhsminor.yy342; break; - case 157: /* union ::= select */ -{ yylhsminor.yy441 = setSubclause(NULL, yymsp[0].minor.yy526); } - yymsp[0].minor.yy441 = yylhsminor.yy441; + case 157: /* select ::= LP select RP */ +{yymsp[-2].minor.yy342 = yymsp[-1].minor.yy342;} break; - case 158: /* union ::= LP union RP */ -{ yymsp[-2].minor.yy441 = yymsp[-1].minor.yy441; } + case 158: /* union ::= select */ +{ yylhsminor.yy513 = setSubclause(NULL, yymsp[0].minor.yy342); } + yymsp[0].minor.yy513 = yylhsminor.yy513; break; case 159: /* union ::= union UNION ALL select */ -{ yylhsminor.yy441 = appendSelectClause(yymsp[-3].minor.yy441, yymsp[0].minor.yy526); } - yymsp[-3].minor.yy441 = yylhsminor.yy441; +{ yylhsminor.yy513 = appendSelectClause(yymsp[-3].minor.yy513, yymsp[0].minor.yy342); } + yymsp[-3].minor.yy513 = yylhsminor.yy513; break; - case 160: /* union ::= union UNION ALL LP select RP */ -{ yylhsminor.yy441 = appendSelectClause(yymsp[-5].minor.yy441, yymsp[-1].minor.yy526); } - yymsp[-5].minor.yy441 = yylhsminor.yy441; + case 160: /* cmd ::= union */ +{ setSqlInfo(pInfo, yymsp[0].minor.yy513, NULL, TSDB_SQL_SELECT); } break; - case 161: /* cmd ::= union */ -{ setSqlInfo(pInfo, yymsp[0].minor.yy441, NULL, TSDB_SQL_SELECT); } - break; - case 162: /* select ::= SELECT selcollist */ + case 161: /* select ::= SELECT selcollist */ { - yylhsminor.yy526 = tSetQuerySqlElems(&yymsp[-1].minor.yy0, yymsp[0].minor.yy178, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + yylhsminor.yy342 = tSetQuerySqlNode(&yymsp[-1].minor.yy0, yymsp[0].minor.yy285, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); } - yymsp[-1].minor.yy526 = yylhsminor.yy526; + yymsp[-1].minor.yy342 = yylhsminor.yy342; break; - case 163: /* sclp ::= selcollist COMMA */ -{yylhsminor.yy178 = yymsp[-1].minor.yy178;} - yymsp[-1].minor.yy178 = yylhsminor.yy178; + case 162: /* sclp ::= selcollist COMMA */ +{yylhsminor.yy285 = yymsp[-1].minor.yy285;} + yymsp[-1].minor.yy285 = yylhsminor.yy285; break; - case 164: /* sclp ::= */ -{yymsp[1].minor.yy178 = 0;} + case 163: /* sclp ::= */ + case 188: /* orderby_opt ::= */ yytestcase(yyruleno==188); +{yymsp[1].minor.yy285 = 0;} break; - case 165: /* selcollist ::= sclp distinct expr as */ + case 164: /* selcollist ::= sclp distinct expr as */ { - yylhsminor.yy178 = tSqlExprListAppend(yymsp[-3].minor.yy178, yymsp[-1].minor.yy484, yymsp[-2].minor.yy0.n? &yymsp[-2].minor.yy0:0, yymsp[0].minor.yy0.n?&yymsp[0].minor.yy0:0); + yylhsminor.yy285 = tSqlExprListAppend(yymsp[-3].minor.yy285, yymsp[-1].minor.yy178, yymsp[-2].minor.yy0.n? &yymsp[-2].minor.yy0:0, yymsp[0].minor.yy0.n?&yymsp[0].minor.yy0:0); } - yymsp[-3].minor.yy178 = yylhsminor.yy178; + yymsp[-3].minor.yy285 = yylhsminor.yy285; break; - case 166: /* selcollist ::= sclp STAR */ + case 165: /* selcollist ::= sclp STAR */ { - tSQLExpr *pNode = tSqlExprIdValueCreate(NULL, TK_ALL); - yylhsminor.yy178 = tSqlExprListAppend(yymsp[-1].minor.yy178, pNode, 0, 0); + tSqlExpr *pNode = tSqlExprCreateIdValue(NULL, TK_ALL); + yylhsminor.yy285 = tSqlExprListAppend(yymsp[-1].minor.yy285, pNode, 0, 0); } - yymsp[-1].minor.yy178 = yylhsminor.yy178; + yymsp[-1].minor.yy285 = yylhsminor.yy285; break; - case 167: /* as ::= AS ids */ + case 166: /* as ::= AS ids */ { yymsp[-1].minor.yy0 = yymsp[0].minor.yy0; } break; - case 168: /* as ::= ids */ + case 167: /* as ::= ids */ { yylhsminor.yy0 = yymsp[0].minor.yy0; } yymsp[0].minor.yy0 = yylhsminor.yy0; break; - case 169: /* as ::= */ + case 168: /* as ::= */ { yymsp[1].minor.yy0.n = 0; } break; - case 170: /* distinct ::= DISTINCT */ + case 169: /* distinct ::= DISTINCT */ { yylhsminor.yy0 = yymsp[0].minor.yy0; } yymsp[0].minor.yy0 = yylhsminor.yy0; break; - case 172: /* from ::= FROM tablelist */ -{yymsp[-1].minor.yy207 = yymsp[0].minor.yy207;} + case 171: /* from ::= FROM tablelist */ +{yymsp[-1].minor.yy162 = yymsp[0].minor.yy285;} + break; + case 172: /* from ::= FROM LP union RP */ +{yymsp[-3].minor.yy162 = yymsp[-1].minor.yy513;} break; case 173: /* tablelist ::= ids cpxName */ { toTSDBType(yymsp[-1].minor.yy0.type); yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - yylhsminor.yy207 = tVariantListAppendToken(NULL, &yymsp[-1].minor.yy0, -1); - yylhsminor.yy207 = tVariantListAppendToken(yylhsminor.yy207, &yymsp[-1].minor.yy0, -1); // table alias name + yylhsminor.yy285 = setTableNameList(NULL, &yymsp[-1].minor.yy0, NULL); } - yymsp[-1].minor.yy207 = yylhsminor.yy207; + yymsp[-1].minor.yy285 = yylhsminor.yy285; break; case 174: /* tablelist ::= ids cpxName ids */ { toTSDBType(yymsp[-2].minor.yy0.type); toTSDBType(yymsp[0].minor.yy0.type); yymsp[-2].minor.yy0.n += yymsp[-1].minor.yy0.n; - yylhsminor.yy207 = tVariantListAppendToken(NULL, &yymsp[-2].minor.yy0, -1); - yylhsminor.yy207 = tVariantListAppendToken(yylhsminor.yy207, &yymsp[0].minor.yy0, -1); + yylhsminor.yy285 = setTableNameList(NULL, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0); } - yymsp[-2].minor.yy207 = yylhsminor.yy207; + yymsp[-2].minor.yy285 = yylhsminor.yy285; break; case 175: /* tablelist ::= tablelist COMMA ids cpxName */ { toTSDBType(yymsp[-1].minor.yy0.type); yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - yylhsminor.yy207 = tVariantListAppendToken(yymsp[-3].minor.yy207, &yymsp[-1].minor.yy0, -1); - yylhsminor.yy207 = tVariantListAppendToken(yylhsminor.yy207, &yymsp[-1].minor.yy0, -1); + yylhsminor.yy285 = setTableNameList(yymsp[-3].minor.yy285, &yymsp[-1].minor.yy0, NULL); } - yymsp[-3].minor.yy207 = yylhsminor.yy207; + yymsp[-3].minor.yy285 = yylhsminor.yy285; break; case 176: /* tablelist ::= tablelist COMMA ids cpxName ids */ { toTSDBType(yymsp[-2].minor.yy0.type); toTSDBType(yymsp[0].minor.yy0.type); yymsp[-2].minor.yy0.n += yymsp[-1].minor.yy0.n; - yylhsminor.yy207 = tVariantListAppendToken(yymsp[-4].minor.yy207, &yymsp[-2].minor.yy0, -1); - yylhsminor.yy207 = tVariantListAppendToken(yylhsminor.yy207, &yymsp[0].minor.yy0, -1); + + yylhsminor.yy285 = setTableNameList(yymsp[-4].minor.yy285, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0); } - yymsp[-4].minor.yy207 = yylhsminor.yy207; + yymsp[-4].minor.yy285 = yylhsminor.yy285; break; case 177: /* tmvar ::= VARIABLE */ {yylhsminor.yy0 = yymsp[0].minor.yy0;} yymsp[0].minor.yy0 = yylhsminor.yy0; break; case 178: /* interval_opt ::= INTERVAL LP tmvar RP */ -{yymsp[-3].minor.yy126.interval = yymsp[-1].minor.yy0; yymsp[-3].minor.yy126.offset.n = 0; yymsp[-3].minor.yy126.offset.z = NULL; yymsp[-3].minor.yy126.offset.type = 0;} +{yymsp[-3].minor.yy376.interval = yymsp[-1].minor.yy0; yymsp[-3].minor.yy376.offset.n = 0;} break; case 179: /* interval_opt ::= INTERVAL LP tmvar COMMA tmvar RP */ -{yymsp[-5].minor.yy126.interval = yymsp[-3].minor.yy0; yymsp[-5].minor.yy126.offset = yymsp[-1].minor.yy0;} +{yymsp[-5].minor.yy376.interval = yymsp[-3].minor.yy0; yymsp[-5].minor.yy376.offset = yymsp[-1].minor.yy0;} break; case 180: /* interval_opt ::= */ -{memset(&yymsp[1].minor.yy126, 0, sizeof(yymsp[1].minor.yy126));} +{memset(&yymsp[1].minor.yy376, 0, sizeof(yymsp[1].minor.yy376));} + break; + case 181: /* session_option ::= */ +{yymsp[1].minor.yy523.col.n = 0; yymsp[1].minor.yy523.gap.n = 0;} + break; + case 182: /* session_option ::= SESSION LP ids cpxName COMMA tmvar RP */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + yymsp[-6].minor.yy523.col = yymsp[-4].minor.yy0; + yymsp[-6].minor.yy523.gap = yymsp[-1].minor.yy0; +} break; - case 181: /* fill_opt ::= */ -{yymsp[1].minor.yy207 = 0; } + case 183: /* fill_opt ::= */ +{ yymsp[1].minor.yy285 = 0; } break; - case 182: /* fill_opt ::= FILL LP ID COMMA tagitemlist RP */ + case 184: /* fill_opt ::= FILL LP ID COMMA tagitemlist RP */ { tVariant A = {0}; toTSDBType(yymsp[-3].minor.yy0.type); tVariantCreate(&A, &yymsp[-3].minor.yy0); - tVariantListInsert(yymsp[-1].minor.yy207, &A, -1, 0); - yymsp[-5].minor.yy207 = yymsp[-1].minor.yy207; + tVariantListInsert(yymsp[-1].minor.yy285, &A, -1, 0); + yymsp[-5].minor.yy285 = yymsp[-1].minor.yy285; } break; - case 183: /* fill_opt ::= FILL LP ID RP */ + case 185: /* fill_opt ::= FILL LP ID RP */ { toTSDBType(yymsp[-1].minor.yy0.type); - yymsp[-3].minor.yy207 = tVariantListAppendToken(NULL, &yymsp[-1].minor.yy0, -1); + yymsp[-3].minor.yy285 = tVariantListAppendToken(NULL, &yymsp[-1].minor.yy0, -1); } break; - case 184: /* sliding_opt ::= SLIDING LP tmvar RP */ + case 186: /* sliding_opt ::= SLIDING LP tmvar RP */ {yymsp[-3].minor.yy0 = yymsp[-1].minor.yy0; } break; - case 185: /* sliding_opt ::= */ + case 187: /* sliding_opt ::= */ {yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = NULL; yymsp[1].minor.yy0.type = 0; } break; - case 186: /* orderby_opt ::= */ -{yymsp[1].minor.yy207 = 0;} - break; - case 187: /* orderby_opt ::= ORDER BY sortlist */ -{yymsp[-2].minor.yy207 = yymsp[0].minor.yy207;} + case 189: /* orderby_opt ::= ORDER BY sortlist */ +{yymsp[-2].minor.yy285 = yymsp[0].minor.yy285;} break; - case 188: /* sortlist ::= sortlist COMMA item sortorder */ + case 190: /* sortlist ::= sortlist COMMA item sortorder */ { - yylhsminor.yy207 = tVariantListAppend(yymsp[-3].minor.yy207, &yymsp[-1].minor.yy232, yymsp[0].minor.yy116); + yylhsminor.yy285 = tVariantListAppend(yymsp[-3].minor.yy285, &yymsp[-1].minor.yy362, yymsp[0].minor.yy460); } - yymsp[-3].minor.yy207 = yylhsminor.yy207; + yymsp[-3].minor.yy285 = yylhsminor.yy285; break; - case 189: /* sortlist ::= item sortorder */ + case 191: /* sortlist ::= item sortorder */ { - yylhsminor.yy207 = tVariantListAppend(NULL, &yymsp[-1].minor.yy232, yymsp[0].minor.yy116); + yylhsminor.yy285 = tVariantListAppend(NULL, &yymsp[-1].minor.yy362, yymsp[0].minor.yy460); } - yymsp[-1].minor.yy207 = yylhsminor.yy207; + yymsp[-1].minor.yy285 = yylhsminor.yy285; break; - case 190: /* item ::= ids cpxName */ + case 192: /* item ::= ids cpxName */ { toTSDBType(yymsp[-1].minor.yy0.type); yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; - tVariantCreate(&yylhsminor.yy232, &yymsp[-1].minor.yy0); + tVariantCreate(&yylhsminor.yy362, &yymsp[-1].minor.yy0); } - yymsp[-1].minor.yy232 = yylhsminor.yy232; + yymsp[-1].minor.yy362 = yylhsminor.yy362; break; - case 191: /* sortorder ::= ASC */ -{ yymsp[0].minor.yy116 = TSDB_ORDER_ASC; } + case 193: /* sortorder ::= ASC */ +{ yymsp[0].minor.yy460 = TSDB_ORDER_ASC; } break; - case 192: /* sortorder ::= DESC */ -{ yymsp[0].minor.yy116 = TSDB_ORDER_DESC;} + case 194: /* sortorder ::= DESC */ +{ yymsp[0].minor.yy460 = TSDB_ORDER_DESC;} break; - case 193: /* sortorder ::= */ -{ yymsp[1].minor.yy116 = TSDB_ORDER_ASC; } + case 195: /* sortorder ::= */ +{ yymsp[1].minor.yy460 = TSDB_ORDER_ASC; } break; - case 194: /* groupby_opt ::= */ -{ yymsp[1].minor.yy207 = 0;} + case 196: /* groupby_opt ::= */ +{ yymsp[1].minor.yy285 = 0;} break; - case 195: /* groupby_opt ::= GROUP BY grouplist */ -{ yymsp[-2].minor.yy207 = yymsp[0].minor.yy207;} + case 197: /* groupby_opt ::= GROUP BY grouplist */ +{ yymsp[-2].minor.yy285 = yymsp[0].minor.yy285;} break; - case 196: /* grouplist ::= grouplist COMMA item */ + case 198: /* grouplist ::= grouplist COMMA item */ { - yylhsminor.yy207 = tVariantListAppend(yymsp[-2].minor.yy207, &yymsp[0].minor.yy232, -1); + yylhsminor.yy285 = tVariantListAppend(yymsp[-2].minor.yy285, &yymsp[0].minor.yy362, -1); } - yymsp[-2].minor.yy207 = yylhsminor.yy207; + yymsp[-2].minor.yy285 = yylhsminor.yy285; break; - case 197: /* grouplist ::= item */ + case 199: /* grouplist ::= item */ { - yylhsminor.yy207 = tVariantListAppend(NULL, &yymsp[0].minor.yy232, -1); + yylhsminor.yy285 = tVariantListAppend(NULL, &yymsp[0].minor.yy362, -1); } - yymsp[0].minor.yy207 = yylhsminor.yy207; + yymsp[0].minor.yy285 = yylhsminor.yy285; break; - case 198: /* having_opt ::= */ - case 208: /* where_opt ::= */ yytestcase(yyruleno==208); - case 247: /* expritem ::= */ yytestcase(yyruleno==247); -{yymsp[1].minor.yy484 = 0;} + case 200: /* having_opt ::= */ + case 210: /* where_opt ::= */ yytestcase(yyruleno==210); + case 249: /* expritem ::= */ yytestcase(yyruleno==249); +{yymsp[1].minor.yy178 = 0;} break; - case 199: /* having_opt ::= HAVING expr */ - case 209: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==209); -{yymsp[-1].minor.yy484 = yymsp[0].minor.yy484;} + case 201: /* having_opt ::= HAVING expr */ + case 211: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==211); +{yymsp[-1].minor.yy178 = yymsp[0].minor.yy178;} break; - case 200: /* limit_opt ::= */ - case 204: /* slimit_opt ::= */ yytestcase(yyruleno==204); -{yymsp[1].minor.yy314.limit = -1; yymsp[1].minor.yy314.offset = 0;} + case 202: /* limit_opt ::= */ + case 206: /* slimit_opt ::= */ yytestcase(yyruleno==206); +{yymsp[1].minor.yy438.limit = -1; yymsp[1].minor.yy438.offset = 0;} break; - case 201: /* limit_opt ::= LIMIT signed */ - case 205: /* slimit_opt ::= SLIMIT signed */ yytestcase(yyruleno==205); -{yymsp[-1].minor.yy314.limit = yymsp[0].minor.yy208; yymsp[-1].minor.yy314.offset = 0;} + case 203: /* limit_opt ::= LIMIT signed */ + case 207: /* slimit_opt ::= SLIMIT signed */ yytestcase(yyruleno==207); +{yymsp[-1].minor.yy438.limit = yymsp[0].minor.yy525; yymsp[-1].minor.yy438.offset = 0;} break; - case 202: /* limit_opt ::= LIMIT signed OFFSET signed */ -{ yymsp[-3].minor.yy314.limit = yymsp[-2].minor.yy208; yymsp[-3].minor.yy314.offset = yymsp[0].minor.yy208;} + case 204: /* limit_opt ::= LIMIT signed OFFSET signed */ +{ yymsp[-3].minor.yy438.limit = yymsp[-2].minor.yy525; yymsp[-3].minor.yy438.offset = yymsp[0].minor.yy525;} break; - case 203: /* limit_opt ::= LIMIT signed COMMA signed */ -{ yymsp[-3].minor.yy314.limit = yymsp[0].minor.yy208; yymsp[-3].minor.yy314.offset = yymsp[-2].minor.yy208;} + case 205: /* limit_opt ::= LIMIT signed COMMA signed */ +{ yymsp[-3].minor.yy438.limit = yymsp[0].minor.yy525; yymsp[-3].minor.yy438.offset = yymsp[-2].minor.yy525;} break; - case 206: /* slimit_opt ::= SLIMIT signed SOFFSET signed */ -{yymsp[-3].minor.yy314.limit = yymsp[-2].minor.yy208; yymsp[-3].minor.yy314.offset = yymsp[0].minor.yy208;} + case 208: /* slimit_opt ::= SLIMIT signed SOFFSET signed */ +{yymsp[-3].minor.yy438.limit = yymsp[-2].minor.yy525; yymsp[-3].minor.yy438.offset = yymsp[0].minor.yy525;} break; - case 207: /* slimit_opt ::= SLIMIT signed COMMA signed */ -{yymsp[-3].minor.yy314.limit = yymsp[0].minor.yy208; yymsp[-3].minor.yy314.offset = yymsp[-2].minor.yy208;} + case 209: /* slimit_opt ::= SLIMIT signed COMMA signed */ +{yymsp[-3].minor.yy438.limit = yymsp[0].minor.yy525; yymsp[-3].minor.yy438.offset = yymsp[-2].minor.yy525;} break; - case 210: /* expr ::= LP expr RP */ -{yylhsminor.yy484 = yymsp[-1].minor.yy484; yylhsminor.yy484->token.z = yymsp[-2].minor.yy0.z; yylhsminor.yy484->token.n = (yymsp[0].minor.yy0.z - yymsp[-2].minor.yy0.z + 1);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 212: /* expr ::= LP expr RP */ +{yylhsminor.yy178 = yymsp[-1].minor.yy178; yylhsminor.yy178->token.z = yymsp[-2].minor.yy0.z; yylhsminor.yy178->token.n = (yymsp[0].minor.yy0.z - yymsp[-2].minor.yy0.z + 1);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 211: /* expr ::= ID */ -{ yylhsminor.yy484 = tSqlExprIdValueCreate(&yymsp[0].minor.yy0, TK_ID);} - yymsp[0].minor.yy484 = yylhsminor.yy484; + case 213: /* expr ::= ID */ +{ yylhsminor.yy178 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_ID);} + yymsp[0].minor.yy178 = yylhsminor.yy178; break; - case 212: /* expr ::= ID DOT ID */ -{ yymsp[-2].minor.yy0.n += (1+yymsp[0].minor.yy0.n); yylhsminor.yy484 = tSqlExprIdValueCreate(&yymsp[-2].minor.yy0, TK_ID);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 214: /* expr ::= ID DOT ID */ +{ yymsp[-2].minor.yy0.n += (1+yymsp[0].minor.yy0.n); yylhsminor.yy178 = tSqlExprCreateIdValue(&yymsp[-2].minor.yy0, TK_ID);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 213: /* expr ::= ID DOT STAR */ -{ yymsp[-2].minor.yy0.n += (1+yymsp[0].minor.yy0.n); yylhsminor.yy484 = tSqlExprIdValueCreate(&yymsp[-2].minor.yy0, TK_ALL);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 215: /* expr ::= ID DOT STAR */ +{ yymsp[-2].minor.yy0.n += (1+yymsp[0].minor.yy0.n); yylhsminor.yy178 = tSqlExprCreateIdValue(&yymsp[-2].minor.yy0, TK_ALL);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 214: /* expr ::= INTEGER */ -{ yylhsminor.yy484 = tSqlExprIdValueCreate(&yymsp[0].minor.yy0, TK_INTEGER);} - yymsp[0].minor.yy484 = yylhsminor.yy484; + case 216: /* expr ::= INTEGER */ +{ yylhsminor.yy178 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_INTEGER);} + yymsp[0].minor.yy178 = yylhsminor.yy178; break; - case 215: /* expr ::= MINUS INTEGER */ - case 216: /* expr ::= PLUS INTEGER */ yytestcase(yyruleno==216); -{ yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_INTEGER; yylhsminor.yy484 = tSqlExprIdValueCreate(&yymsp[-1].minor.yy0, TK_INTEGER);} - yymsp[-1].minor.yy484 = yylhsminor.yy484; + case 217: /* expr ::= MINUS INTEGER */ + case 218: /* expr ::= PLUS INTEGER */ yytestcase(yyruleno==218); +{ yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_INTEGER; yylhsminor.yy178 = tSqlExprCreateIdValue(&yymsp[-1].minor.yy0, TK_INTEGER);} + yymsp[-1].minor.yy178 = yylhsminor.yy178; break; - case 217: /* expr ::= FLOAT */ -{ yylhsminor.yy484 = tSqlExprIdValueCreate(&yymsp[0].minor.yy0, TK_FLOAT);} - yymsp[0].minor.yy484 = yylhsminor.yy484; + case 219: /* expr ::= FLOAT */ +{ yylhsminor.yy178 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_FLOAT);} + yymsp[0].minor.yy178 = yylhsminor.yy178; break; - case 218: /* expr ::= MINUS FLOAT */ - case 219: /* expr ::= PLUS FLOAT */ yytestcase(yyruleno==219); -{ yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_FLOAT; yylhsminor.yy484 = tSqlExprIdValueCreate(&yymsp[-1].minor.yy0, TK_FLOAT);} - yymsp[-1].minor.yy484 = yylhsminor.yy484; + case 220: /* expr ::= MINUS FLOAT */ + case 221: /* expr ::= PLUS FLOAT */ yytestcase(yyruleno==221); +{ yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_FLOAT; yylhsminor.yy178 = tSqlExprCreateIdValue(&yymsp[-1].minor.yy0, TK_FLOAT);} + yymsp[-1].minor.yy178 = yylhsminor.yy178; break; - case 220: /* expr ::= STRING */ -{ yylhsminor.yy484 = tSqlExprIdValueCreate(&yymsp[0].minor.yy0, TK_STRING);} - yymsp[0].minor.yy484 = yylhsminor.yy484; + case 222: /* expr ::= STRING */ +{ yylhsminor.yy178 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_STRING);} + yymsp[0].minor.yy178 = yylhsminor.yy178; break; - case 221: /* expr ::= NOW */ -{ yylhsminor.yy484 = tSqlExprIdValueCreate(&yymsp[0].minor.yy0, TK_NOW); } - yymsp[0].minor.yy484 = yylhsminor.yy484; + case 223: /* expr ::= NOW */ +{ yylhsminor.yy178 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_NOW); } + yymsp[0].minor.yy178 = yylhsminor.yy178; break; - case 222: /* expr ::= VARIABLE */ -{ yylhsminor.yy484 = tSqlExprIdValueCreate(&yymsp[0].minor.yy0, TK_VARIABLE);} - yymsp[0].minor.yy484 = yylhsminor.yy484; + case 224: /* expr ::= VARIABLE */ +{ yylhsminor.yy178 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_VARIABLE);} + yymsp[0].minor.yy178 = yylhsminor.yy178; break; - case 223: /* expr ::= BOOL */ -{ yylhsminor.yy484 = tSqlExprIdValueCreate(&yymsp[0].minor.yy0, TK_BOOL);} - yymsp[0].minor.yy484 = yylhsminor.yy484; + case 225: /* expr ::= BOOL */ +{ yylhsminor.yy178 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_BOOL);} + yymsp[0].minor.yy178 = yylhsminor.yy178; break; - case 224: /* expr ::= ID LP exprlist RP */ -{ yylhsminor.yy484 = tSqlExprCreateFunction(yymsp[-1].minor.yy178, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0, yymsp[-3].minor.yy0.type); } - yymsp[-3].minor.yy484 = yylhsminor.yy484; + case 226: /* expr ::= ID LP exprlist RP */ +{ yylhsminor.yy178 = tSqlExprCreateFunction(yymsp[-1].minor.yy285, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0, yymsp[-3].minor.yy0.type); } + yymsp[-3].minor.yy178 = yylhsminor.yy178; break; - case 225: /* expr ::= ID LP STAR RP */ -{ yylhsminor.yy484 = tSqlExprCreateFunction(NULL, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0, yymsp[-3].minor.yy0.type); } - yymsp[-3].minor.yy484 = yylhsminor.yy484; + case 227: /* expr ::= ID LP STAR RP */ +{ yylhsminor.yy178 = tSqlExprCreateFunction(NULL, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0, yymsp[-3].minor.yy0.type); } + yymsp[-3].minor.yy178 = yylhsminor.yy178; break; - case 226: /* expr ::= expr IS NULL */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, NULL, TK_ISNULL);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 228: /* expr ::= expr IS NULL */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, NULL, TK_ISNULL);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; + break; + case 229: /* expr ::= expr IS NOT NULL */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-3].minor.yy178, NULL, TK_NOTNULL);} + yymsp[-3].minor.yy178 = yylhsminor.yy178; break; - case 227: /* expr ::= expr IS NOT NULL */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-3].minor.yy484, NULL, TK_NOTNULL);} - yymsp[-3].minor.yy484 = yylhsminor.yy484; + case 230: /* expr ::= expr LT expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_LT);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 228: /* expr ::= expr LT expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_LT);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 231: /* expr ::= expr GT expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_GT);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 229: /* expr ::= expr GT expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_GT);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 232: /* expr ::= expr LE expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_LE);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 230: /* expr ::= expr LE expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_LE);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 233: /* expr ::= expr GE expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_GE);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 231: /* expr ::= expr GE expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_GE);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 234: /* expr ::= expr NE expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_NE);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 232: /* expr ::= expr NE expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_NE);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 235: /* expr ::= expr EQ expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_EQ);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 233: /* expr ::= expr EQ expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_EQ);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 236: /* expr ::= expr BETWEEN expr AND expr */ +{ tSqlExpr* X2 = tSqlExprClone(yymsp[-4].minor.yy178); yylhsminor.yy178 = tSqlExprCreate(tSqlExprCreate(yymsp[-4].minor.yy178, yymsp[-2].minor.yy178, TK_GE), tSqlExprCreate(X2, yymsp[0].minor.yy178, TK_LE), TK_AND);} + yymsp[-4].minor.yy178 = yylhsminor.yy178; break; - case 234: /* expr ::= expr BETWEEN expr AND expr */ -{ tSQLExpr* X2 = tSqlExprClone(yymsp[-4].minor.yy484); yylhsminor.yy484 = tSqlExprCreate(tSqlExprCreate(yymsp[-4].minor.yy484, yymsp[-2].minor.yy484, TK_GE), tSqlExprCreate(X2, yymsp[0].minor.yy484, TK_LE), TK_AND);} - yymsp[-4].minor.yy484 = yylhsminor.yy484; + case 237: /* expr ::= expr AND expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_AND);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 235: /* expr ::= expr AND expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_AND);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 238: /* expr ::= expr OR expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_OR); } + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 236: /* expr ::= expr OR expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_OR); } - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 239: /* expr ::= expr PLUS expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_PLUS); } + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 237: /* expr ::= expr PLUS expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_PLUS); } - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 240: /* expr ::= expr MINUS expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_MINUS); } + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 238: /* expr ::= expr MINUS expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_MINUS); } - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 241: /* expr ::= expr STAR expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_STAR); } + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 239: /* expr ::= expr STAR expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_STAR); } - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 242: /* expr ::= expr SLASH expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_DIVIDE);} + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 240: /* expr ::= expr SLASH expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_DIVIDE);} - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 243: /* expr ::= expr REM expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_REM); } + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 241: /* expr ::= expr REM expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_REM); } - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 244: /* expr ::= expr LIKE expr */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-2].minor.yy178, yymsp[0].minor.yy178, TK_LIKE); } + yymsp[-2].minor.yy178 = yylhsminor.yy178; break; - case 242: /* expr ::= expr LIKE expr */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-2].minor.yy484, yymsp[0].minor.yy484, TK_LIKE); } - yymsp[-2].minor.yy484 = yylhsminor.yy484; + case 245: /* expr ::= expr IN LP exprlist RP */ +{yylhsminor.yy178 = tSqlExprCreate(yymsp[-4].minor.yy178, (tSqlExpr*)yymsp[-1].minor.yy285, TK_IN); } + yymsp[-4].minor.yy178 = yylhsminor.yy178; break; - case 243: /* expr ::= expr IN LP exprlist RP */ -{yylhsminor.yy484 = tSqlExprCreate(yymsp[-4].minor.yy484, (tSQLExpr*)yymsp[-1].minor.yy178, TK_IN); } - yymsp[-4].minor.yy484 = yylhsminor.yy484; + case 246: /* exprlist ::= exprlist COMMA expritem */ +{yylhsminor.yy285 = tSqlExprListAppend(yymsp[-2].minor.yy285,yymsp[0].minor.yy178,0, 0);} + yymsp[-2].minor.yy285 = yylhsminor.yy285; break; - case 244: /* exprlist ::= exprlist COMMA expritem */ -{yylhsminor.yy178 = tSqlExprListAppend(yymsp[-2].minor.yy178,yymsp[0].minor.yy484,0, 0);} - yymsp[-2].minor.yy178 = yylhsminor.yy178; + case 247: /* exprlist ::= expritem */ +{yylhsminor.yy285 = tSqlExprListAppend(0,yymsp[0].minor.yy178,0, 0);} + yymsp[0].minor.yy285 = yylhsminor.yy285; break; - case 245: /* exprlist ::= expritem */ -{yylhsminor.yy178 = tSqlExprListAppend(0,yymsp[0].minor.yy484,0, 0);} + case 248: /* expritem ::= expr */ +{yylhsminor.yy178 = yymsp[0].minor.yy178;} yymsp[0].minor.yy178 = yylhsminor.yy178; break; - case 246: /* expritem ::= expr */ -{yylhsminor.yy484 = yymsp[0].minor.yy484;} - yymsp[0].minor.yy484 = yylhsminor.yy484; - break; - case 248: /* cmd ::= RESET QUERY CACHE */ -{ setDCLSQLElems(pInfo, TSDB_SQL_RESET_CACHE, 0);} + case 250: /* cmd ::= RESET QUERY CACHE */ +{ setDCLSqlElems(pInfo, TSDB_SQL_RESET_CACHE, 0);} break; - case 249: /* cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ + case 251: /* cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&yymsp[-4].minor.yy0, yymsp[0].minor.yy207, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy285, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 250: /* cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ + case 252: /* cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; toTSDBType(yymsp[0].minor.yy0.type); SArray* K = tVariantListAppendToken(NULL, &yymsp[0].minor.yy0, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&yymsp[-4].minor.yy0, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 251: /* cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ + case 253: /* cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&yymsp[-4].minor.yy0, yymsp[0].minor.yy207, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy285, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 252: /* cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ + case 254: /* cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; toTSDBType(yymsp[0].minor.yy0.type); SArray* A = tVariantListAppendToken(NULL, &yymsp[0].minor.yy0, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&yymsp[-4].minor.yy0, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 253: /* cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ + case 255: /* cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ { yymsp[-5].minor.yy0.n += yymsp[-4].minor.yy0.n; @@ -3340,59 +3012,59 @@ static YYACTIONTYPE yy_reduce( toTSDBType(yymsp[0].minor.yy0.type); A = tVariantListAppendToken(A, &yymsp[0].minor.yy0, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&yymsp[-5].minor.yy0, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-5].minor.yy0, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 254: /* cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ + case 256: /* cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ { yymsp[-6].minor.yy0.n += yymsp[-5].minor.yy0.n; toTSDBType(yymsp[-2].minor.yy0.type); SArray* A = tVariantListAppendToken(NULL, &yymsp[-2].minor.yy0, -1); - A = tVariantListAppend(A, &yymsp[0].minor.yy232, -1); + A = tVariantListAppend(A, &yymsp[0].minor.yy362, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&yymsp[-6].minor.yy0, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL, -1); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-6].minor.yy0, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL, -1); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 255: /* cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist */ + case 257: /* cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&yymsp[-4].minor.yy0, yymsp[0].minor.yy207, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, TSDB_SUPER_TABLE); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy285, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, TSDB_SUPER_TABLE); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 256: /* cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids */ + case 258: /* cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; toTSDBType(yymsp[0].minor.yy0.type); SArray* K = tVariantListAppendToken(NULL, &yymsp[0].minor.yy0, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&yymsp[-4].minor.yy0, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, TSDB_SUPER_TABLE); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, TSDB_SUPER_TABLE); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 257: /* cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist */ + case 259: /* cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&yymsp[-4].minor.yy0, yymsp[0].minor.yy207, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, TSDB_SUPER_TABLE); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy285, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, TSDB_SUPER_TABLE); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 258: /* cmd ::= ALTER STABLE ids cpxName DROP TAG ids */ + case 260: /* cmd ::= ALTER STABLE ids cpxName DROP TAG ids */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; toTSDBType(yymsp[0].minor.yy0.type); SArray* A = tVariantListAppendToken(NULL, &yymsp[0].minor.yy0, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&yymsp[-4].minor.yy0, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, TSDB_SUPER_TABLE); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, TSDB_SUPER_TABLE); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 259: /* cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids */ + case 261: /* cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids */ { yymsp[-5].minor.yy0.n += yymsp[-4].minor.yy0.n; @@ -3402,26 +3074,26 @@ static YYACTIONTYPE yy_reduce( toTSDBType(yymsp[0].minor.yy0.type); A = tVariantListAppendToken(A, &yymsp[0].minor.yy0, -1); - SAlterTableInfo* pAlterTable = tAlterTableSqlElems(&yymsp[-5].minor.yy0, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, TSDB_SUPER_TABLE); + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-5].minor.yy0, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, TSDB_SUPER_TABLE); setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 260: /* cmd ::= KILL CONNECTION INTEGER */ + case 262: /* cmd ::= KILL CONNECTION INTEGER */ {setKillSql(pInfo, TSDB_SQL_KILL_CONNECTION, &yymsp[0].minor.yy0);} break; - case 261: /* cmd ::= KILL STREAM INTEGER COLON INTEGER */ + case 263: /* cmd ::= KILL STREAM INTEGER COLON INTEGER */ {yymsp[-2].minor.yy0.n += (yymsp[-1].minor.yy0.n + yymsp[0].minor.yy0.n); setKillSql(pInfo, TSDB_SQL_KILL_STREAM, &yymsp[-2].minor.yy0);} break; - case 262: /* cmd ::= KILL QUERY INTEGER COLON INTEGER */ + case 264: /* cmd ::= KILL QUERY INTEGER COLON INTEGER */ {yymsp[-2].minor.yy0.n += (yymsp[-1].minor.yy0.n + yymsp[0].minor.yy0.n); setKillSql(pInfo, TSDB_SQL_KILL_QUERY, &yymsp[-2].minor.yy0);} break; default: break; /********** End reduce actions ************************************************/ }; - assert( yyrulenostateno = (YYACTIONTYPE)yyact; yymsp->major = (YYCODETYPE)yygoto; yyTraceShift(yypParser, yyact, "... then shift"); - return yyact; } /* @@ -3446,8 +3117,7 @@ static YYACTIONTYPE yy_reduce( static void yy_parse_failed( yyParser *yypParser /* The parser */ ){ - ParseARG_FETCH - ParseCTX_FETCH + ParseARG_FETCH; #ifndef NDEBUG if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); @@ -3458,8 +3128,7 @@ static void yy_parse_failed( ** parser fails */ /************ Begin %parse_failure code ***************************************/ /************ End %parse_failure code *****************************************/ - ParseARG_STORE /* Suppress warning about unused %extra_argument variable */ - ParseCTX_STORE + ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ } #endif /* YYNOERRORRECOVERY */ @@ -3471,8 +3140,7 @@ static void yy_syntax_error( int yymajor, /* The major type of the error token */ ParseTOKENTYPE yyminor /* The minor type of the error token */ ){ - ParseARG_FETCH - ParseCTX_FETCH + ParseARG_FETCH; #define TOKEN yyminor /************ Begin %syntax_error code ****************************************/ @@ -3498,8 +3166,7 @@ static void yy_syntax_error( assert(len <= outputBufLen); /************ End %syntax_error code ******************************************/ - ParseARG_STORE /* Suppress warning about unused %extra_argument variable */ - ParseCTX_STORE + ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ } /* @@ -3508,8 +3175,7 @@ static void yy_syntax_error( static void yy_accept( yyParser *yypParser /* The parser */ ){ - ParseARG_FETCH - ParseCTX_FETCH + ParseARG_FETCH; #ifndef NDEBUG if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); @@ -3524,8 +3190,7 @@ static void yy_accept( /*********** Begin %parse_accept code *****************************************/ /*********** End %parse_accept code *******************************************/ - ParseARG_STORE /* Suppress warning about unused %extra_argument variable */ - ParseCTX_STORE + ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ } /* The main parser program. @@ -3554,47 +3219,45 @@ void Parse( ParseARG_PDECL /* Optional %extra_argument parameter */ ){ YYMINORTYPE yyminorunion; - YYACTIONTYPE yyact; /* The parser action. */ + unsigned int yyact; /* The parser action. */ #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) int yyendofinput; /* True if we are at the end of input */ #endif #ifdef YYERRORSYMBOL int yyerrorhit = 0; /* True if yymajor has invoked an error */ #endif - yyParser *yypParser = (yyParser*)yyp; /* The parser */ - ParseCTX_FETCH - ParseARG_STORE + yyParser *yypParser; /* The parser */ + yypParser = (yyParser*)yyp; assert( yypParser->yytos!=0 ); #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) yyendofinput = (yymajor==0); #endif + ParseARG_STORE; - yyact = yypParser->yytos->stateno; #ifndef NDEBUG if( yyTraceFILE ){ - if( yyact < YY_MIN_REDUCE ){ + int stateno = yypParser->yytos->stateno; + if( stateno < YY_MIN_REDUCE ){ fprintf(yyTraceFILE,"%sInput '%s' in state %d\n", - yyTracePrompt,yyTokenName[yymajor],yyact); + yyTracePrompt,yyTokenName[yymajor],stateno); }else{ fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n", - yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE); + yyTracePrompt,yyTokenName[yymajor],stateno-YY_MIN_REDUCE); } } #endif do{ - assert( yyact==yypParser->yytos->stateno ); - yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact); + yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor); if( yyact >= YY_MIN_REDUCE ){ - yyact = yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor, - yyminor ParseCTX_PARAM); + yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,yyminor); }else if( yyact <= YY_MAX_SHIFTREDUCE ){ - yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor); + yy_shift(yypParser,yyact,yymajor,yyminor); #ifndef YYNOERRORRECOVERY yypParser->yyerrcnt--; #endif - break; + yymajor = YYNOCODE; }else if( yyact==YY_ACCEPT_ACTION ){ yypParser->yytos--; yy_accept(yypParser); @@ -3645,9 +3308,10 @@ void Parse( yymajor = YYNOCODE; }else{ while( yypParser->yytos >= yypParser->yystack + && yymx != YYERRORSYMBOL && (yyact = yy_find_reduce_action( yypParser->yytos->stateno, - YYERRORSYMBOL)) > YY_MAX_SHIFTREDUCE + YYERRORSYMBOL)) >= YY_MIN_REDUCE ){ yy_pop_parser_stack(yypParser); } @@ -3664,8 +3328,6 @@ void Parse( } yypParser->yyerrcnt = 3; yyerrorhit = 1; - if( yymajor==YYNOCODE ) break; - yyact = yypParser->yytos->stateno; #elif defined(YYNOERRORRECOVERY) /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to ** do any kind of error recovery. Instead, simply invoke the syntax @@ -3676,7 +3338,8 @@ void Parse( */ yy_syntax_error(yypParser,yymajor, yyminor); yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); - break; + yymajor = YYNOCODE; + #else /* YYERRORSYMBOL is not defined */ /* This is what we do if the grammar does not define ERROR: ** @@ -3698,10 +3361,10 @@ void Parse( yypParser->yyerrcnt = -1; #endif } - break; + yymajor = YYNOCODE; #endif } - }while( yypParser->yytos>yypParser->yystack ); + }while( yymajor!=YYNOCODE && yypParser->yytos>yypParser->yystack ); #ifndef NDEBUG if( yyTraceFILE ){ yyStackEntry *i; @@ -3716,17 +3379,3 @@ void Parse( #endif return; } - -/* -** Return the fallback token corresponding to canonical token iToken, or -** 0 if iToken has no fallback. -*/ -int ParseFallback(int iToken){ -#ifdef YYFALLBACK - assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) ); - return yyFallback[iToken]; -#else - (void)iToken; - return 0; -#endif -} diff --git a/src/query/tests/resultBufferTest.cpp b/src/query/tests/resultBufferTest.cpp index 7b946d858913b14dfdbb71eb1b8a8c469e6d930c..19e011774241dffa156a0ecb003041aba8253ba5 100644 --- a/src/query/tests/resultBufferTest.cpp +++ b/src/query/tests/resultBufferTest.cpp @@ -10,7 +10,7 @@ namespace { // simple test void simpleTest() { SDiskbasedResultBuf* pResultBuf = NULL; - int32_t ret = createDiskbasedResultBuffer(&pResultBuf, 64, 1024, 4096, NULL); + int32_t ret = createDiskbasedResultBuffer(&pResultBuf, 1024, 4096, NULL); int32_t pageId = 0; int32_t groupId = 0; @@ -52,7 +52,7 @@ void simpleTest() { void writeDownTest() { SDiskbasedResultBuf* pResultBuf = NULL; - int32_t ret = createDiskbasedResultBuffer(&pResultBuf, 64, 1024, 4*1024, NULL); + int32_t ret = createDiskbasedResultBuffer(&pResultBuf, 1024, 4*1024, NULL); int32_t pageId = 0; int32_t writePageId = 0; @@ -99,7 +99,7 @@ void writeDownTest() { void recyclePageTest() { SDiskbasedResultBuf* pResultBuf = NULL; - int32_t ret = createDiskbasedResultBuffer(&pResultBuf, 64, 1024, 4*1024, NULL); + int32_t ret = createDiskbasedResultBuffer(&pResultBuf, 1024, 4*1024, NULL); int32_t pageId = 0; int32_t writePageId = 0; diff --git a/src/tsdb/src/tsdbCommit.c b/src/tsdb/src/tsdbCommit.c index a4cc316725d60510441c4badb77ce9a2805c08ca..a1ed0796a6e628a55ebe81fd87396ada990b7e73 100644 --- a/src/tsdb/src/tsdbCommit.c +++ b/src/tsdb/src/tsdbCommit.c @@ -15,7 +15,13 @@ #include "tsdbint.h" #define TSDB_MAX_SUBBLOCKS 8 -#define TSDB_KEY_FID(key, days, precision) ((key) / tsMsPerDay[(precision)] / (days)) +static FORCE_INLINE int TSDB_KEY_FID(TSKEY key, int32_t days, int8_t precision) { + if (key < 0) { + return (int)(-((-key) / tsMsPerDay[precision] / days + 1)); + } else { + return (int)((key / tsMsPerDay[precision] / days)); + } +} typedef struct { SRtn rtn; // retention snapshot diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index e0648c33a050fcdb5a6cd83517283092b1061d5e..0bf533d027db284a12ae155dc494fd8f9f35a5f1 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -34,11 +34,9 @@ .tid = (_checkInfo)->tableId.tid, \ .uid = (_checkInfo)->tableId.uid}) - enum { TSDB_QUERY_TYPE_ALL = 1, TSDB_QUERY_TYPE_LAST = 2, - TSDB_QUERY_TYPE_EXTERNAL = 3, }; typedef struct SQueryFilePos { @@ -68,7 +66,7 @@ typedef struct STableCheckInfo { STableId tableId; TSKEY lastKey; STable* pTableObj; - SBlockInfo* pCompInfo; + SBlockInfo* pCompInfo; int32_t compSize; int32_t numOfBlocks:29; // number of qualified data blocks not the original blocks int8_t chosen:2; // indicate which iterator should move forward @@ -78,8 +76,8 @@ typedef struct STableCheckInfo { } STableCheckInfo; typedef struct STableBlockInfo { - SBlock* compBlock; - STableCheckInfo* pTableCheckInfo; + SBlock *compBlock; + STableCheckInfo *pTableCheckInfo; } STableBlockInfo; typedef struct SBlockOrderSupporter { @@ -111,6 +109,8 @@ typedef struct STsdbQueryHandle { bool checkFiles; // check file stage bool cachelastrow; // check if last row cached bool loadExternalRow; // load time window external data rows + bool currentLoadExternalRows; // current load external rows + int32_t loadType; // block load type void* qinfo; // query info handle, for debug purpose int32_t type; // query type: retrieve all data blocks, 2. retrieve only last row, 3. retrieve direct prev|next rows SDFileSet* pFileGroup; @@ -147,6 +147,8 @@ static int32_t tsdbReadRowsFromCache(STableCheckInfo* pCheckInfo, TSKEY maxKey, static int32_t tsdbCheckInfoCompar(const void* key1, const void* key2); static int32_t doGetExternalRow(STsdbQueryHandle* pQueryHandle, int16_t type, SMemRef* pMemRef); static void* doFreeColumnInfoData(SArray* pColumnInfoData); +static void* destroyTableCheckInfo(SArray* pTableCheckInfo); +static bool tsdbGetExternalRow(TsdbQueryHandleT pHandle); static void tsdbInitDataBlockLoadInfo(SDataBlockLoadInfo* pBlockLoadInfo) { pBlockLoadInfo->slot = -1; @@ -294,40 +296,48 @@ static SArray* createCheckInfoFromTableGroup(STsdbQueryHandle* pQueryHandle, STa taosArraySort(pTableCheckInfo, tsdbCheckInfoCompar); size_t gsize = taosArrayGetSize(pTableCheckInfo); - + for (int32_t i = 0; i < gsize; ++i) { STableCheckInfo* pInfo = (STableCheckInfo*) taosArrayGet(pTableCheckInfo, i); - taosArrayPush(pTable, &pInfo->pTableObj); } *psTable = pTable; - return pTableCheckInfo; } -static SArray* createCheckInfoFromCheckInfo(SArray* pTableCheckInfo, TSKEY skey, SArray** psTable) { - size_t si = taosArrayGetSize(pTableCheckInfo); - SArray* pNew = taosArrayInit(si, sizeof(STableCheckInfo)); - if (pNew == NULL) { - return NULL; +static void resetCheckInfo(STsdbQueryHandle* pQueryHandle) { + size_t numOfTables = taosArrayGetSize(pQueryHandle->pTableCheckInfo); + assert(numOfTables >= 1); + + // todo apply the lastkey of table check to avoid to load header file + for (int32_t i = 0; i < numOfTables; ++i) { + STableCheckInfo* pCheckInfo = (STableCheckInfo*) taosArrayGet(pQueryHandle->pTableCheckInfo, i); + pCheckInfo->lastKey = pQueryHandle->window.skey; + pCheckInfo->iter = tSkipListDestroyIter(pCheckInfo->iter); + pCheckInfo->iiter = tSkipListDestroyIter(pCheckInfo->iiter); + pCheckInfo->initBuf = false; + + if (ASCENDING_TRAVERSE(pQueryHandle->order)) { + assert(pCheckInfo->lastKey >= pQueryHandle->window.skey); + } else { + assert(pCheckInfo->lastKey <= pQueryHandle->window.skey); + } } +} - SArray* pTable = taosArrayInit(si, sizeof(STable*)); +// only one table, not need to sort again +static SArray* createCheckInfoFromCheckInfo(STableCheckInfo* pCheckInfo, TSKEY skey, SArray** psTable) { + SArray* pNew = taosArrayInit(1, sizeof(STableCheckInfo)); + SArray* pTable = taosArrayInit(1, sizeof(STable*)); - for (int32_t j = 0; j < si; ++j) { - STableCheckInfo* pCheckInfo = (STableCheckInfo*) taosArrayGet(pTableCheckInfo, j); - STableCheckInfo info = { .lastKey = skey, .pTableObj = pCheckInfo->pTableObj}; + STableCheckInfo info = { .lastKey = skey, .pTableObj = pCheckInfo->pTableObj}; - info.tableId = pCheckInfo->tableId; - taosArrayPush(pNew, &info); - taosArrayPush(pTable, &pCheckInfo->pTableObj); - } + info.tableId = pCheckInfo->tableId; + taosArrayPush(pNew, &info); + taosArrayPush(pTable, &pCheckInfo->pTableObj); *psTable = pTable; - - // it is ordered already, no need to sort again. - taosArraySort(pNew, tsdbCheckInfoCompar); return pNew; } @@ -351,14 +361,15 @@ static STsdbQueryHandle* tsdbQueryTablesImpl(STsdbRepo* tsdb, STsdbQueryCond* pC pQueryHandle->locateStart = false; pQueryHandle->pMemRef = pMemRef; pQueryHandle->loadExternalRow = pCond->loadExternalRows; + pQueryHandle->currentLoadExternalRows = pCond->loadExternalRows; + + pQueryHandle->loadType = pCond->type; if (tsdbInitReadH(&pQueryHandle->rhelper, (STsdbRepo*)tsdb) != 0) { goto out_of_memory; } - //tsdbMayTakeMemSnapshot(pQueryHandle); assert(pCond != NULL && pCond->numOfCols > 0 && pMemRef != NULL); - if (ASCENDING_TRAVERSE(pCond->order)) { assert(pQueryHandle->window.skey <= pQueryHandle->window.ekey); } else { @@ -388,7 +399,9 @@ static STsdbQueryHandle* tsdbQueryTablesImpl(STsdbRepo* tsdb, STsdbQueryCond* pC pQueryHandle->statis[i].colId = colInfo.info.colId; } - pQueryHandle->defaultLoadColumn = getDefaultLoadColumns(pQueryHandle, true); + if (pCond->numOfCols > 0) { + pQueryHandle->defaultLoadColumn = getDefaultLoadColumns(pQueryHandle, true); + } STsdbMeta* pMeta = tsdbGetMeta(tsdb); assert(pMeta != NULL); @@ -433,6 +446,74 @@ TsdbQueryHandleT* tsdbQueryTables(STsdbRepo* tsdb, STsdbQueryCond* pCond, STable return (TsdbQueryHandleT) pQueryHandle; } +void tsdbResetQueryHandle(TsdbQueryHandleT queryHandle, STsdbQueryCond *pCond) { + STsdbQueryHandle* pQueryHandle = queryHandle; + + pQueryHandle->order = pCond->order; + pQueryHandle->window = pCond->twindow; + pQueryHandle->type = TSDB_QUERY_TYPE_ALL; + pQueryHandle->cur.fid = -1; + pQueryHandle->cur.win = TSWINDOW_INITIALIZER; + pQueryHandle->checkFiles = true; + pQueryHandle->activeIndex = 0; // current active table index + pQueryHandle->locateStart = false; + pQueryHandle->loadExternalRow = pCond->loadExternalRows; + + if (ASCENDING_TRAVERSE(pCond->order)) { + assert(pQueryHandle->window.skey <= pQueryHandle->window.ekey); + } else { + assert(pQueryHandle->window.skey >= pQueryHandle->window.ekey); + } + + // allocate buffer in order to load data blocks from file + memset(pQueryHandle->statis, 0, sizeof(SDataStatis)); + + tsdbInitDataBlockLoadInfo(&pQueryHandle->dataBlockLoadInfo); + tsdbInitCompBlockLoadInfo(&pQueryHandle->compBlockLoadInfo); + + resetCheckInfo(pQueryHandle); +} + +void tsdbResetQueryHandleForNewTable(TsdbQueryHandleT queryHandle, STsdbQueryCond *pCond, STableGroupInfo* groupList) { + STsdbQueryHandle* pQueryHandle = queryHandle; + + pQueryHandle->order = pCond->order; + pQueryHandle->window = pCond->twindow; + pQueryHandle->type = TSDB_QUERY_TYPE_ALL; + pQueryHandle->cur.fid = -1; + pQueryHandle->cur.win = TSWINDOW_INITIALIZER; + pQueryHandle->checkFiles = true; + pQueryHandle->activeIndex = 0; // current active table index + pQueryHandle->locateStart = false; + pQueryHandle->loadExternalRow = pCond->loadExternalRows; + + if (ASCENDING_TRAVERSE(pCond->order)) { + assert(pQueryHandle->window.skey <= pQueryHandle->window.ekey); + } else { + assert(pQueryHandle->window.skey >= pQueryHandle->window.ekey); + } + + // allocate buffer in order to load data blocks from file + memset(pQueryHandle->statis, 0, sizeof(SDataStatis)); + + tsdbInitDataBlockLoadInfo(&pQueryHandle->dataBlockLoadInfo); + tsdbInitCompBlockLoadInfo(&pQueryHandle->compBlockLoadInfo); + + SArray* pTable = NULL; + STsdbMeta* pMeta = tsdbGetMeta(pQueryHandle->pTsdb); + + pQueryHandle->pTableCheckInfo = destroyTableCheckInfo(pQueryHandle->pTableCheckInfo); + + pQueryHandle->pTableCheckInfo = createCheckInfoFromTableGroup(pQueryHandle, groupList, pMeta, &pTable); + if (pQueryHandle->pTableCheckInfo == NULL) { + tsdbCleanupQueryHandle(pQueryHandle); + terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; + } + + pQueryHandle->prev = doFreeColumnInfoData(pQueryHandle->prev); + pQueryHandle->next = doFreeColumnInfoData(pQueryHandle->next); +} + TsdbQueryHandleT tsdbQueryLastRow(STsdbRepo *tsdb, STsdbQueryCond *pCond, STableGroupInfo *groupList, void* qinfo, SMemRef* pMemRef) { pCond->twindow = updateLastrowForEachGroup(groupList); @@ -469,12 +550,45 @@ SArray* tsdbGetQueriedTableList(TsdbQueryHandleT *pHandle) { return res; } +// leave only one table for each group +static STableGroupInfo* trimTableGroup(STimeWindow* window, STableGroupInfo* pGroupList) { + assert(pGroupList); + size_t numOfGroup = taosArrayGetSize(pGroupList->pGroupList); + + STableGroupInfo* pNew = calloc(1, sizeof(STableGroupInfo)); + pNew->pGroupList = taosArrayInit(numOfGroup, sizeof(SArray)); + + for(int32_t i = 0; i < numOfGroup; ++i) { + SArray* oneGroup = taosArrayGetP(pGroupList->pGroupList, i); + size_t numOfTables = taosArrayGetSize(oneGroup); + + SArray* px = taosArrayInit(4, sizeof(STableKeyInfo)); + for (int32_t j = 0; j < numOfTables; ++j) { + STableKeyInfo* pInfo = (STableKeyInfo*)taosArrayGet(oneGroup, j); + if (window->skey <= pInfo->lastKey && ((STable*)pInfo->pTable)->lastKey != TSKEY_INITIAL_VAL) { + taosArrayPush(px, pInfo); + pNew->numOfTables += 1; + break; + } + } + + // there are no data in this group + if (taosArrayGetSize(px) == 0) { + taosArrayDestroy(px); + } else { + taosArrayPush(pNew->pGroupList, &px); + } + } + + return pNew; +} + TsdbQueryHandleT tsdbQueryRowsInExternalWindow(STsdbRepo *tsdb, STsdbQueryCond* pCond, STableGroupInfo *groupList, void* qinfo, SMemRef* pRef) { - STsdbQueryHandle *pQueryHandle = (STsdbQueryHandle*) tsdbQueryTables(tsdb, pCond, groupList, qinfo, pRef); + STableGroupInfo* pNew = trimTableGroup(&pCond->twindow, groupList); + + STsdbQueryHandle *pQueryHandle = (STsdbQueryHandle*) tsdbQueryTables(tsdb, pCond, pNew, qinfo, pRef); pQueryHandle->loadExternalRow = true; - if (pQueryHandle != NULL) { - changeQueryHandleForInterpQuery(pQueryHandle); - } + pQueryHandle->currentLoadExternalRows = true; return pQueryHandle; } @@ -769,77 +883,94 @@ static int32_t binarySearchForBlock(SBlock* pBlock, int32_t numOfBlocks, TSKEY s return midSlot; } -static int32_t getFileCompInfo(STsdbQueryHandle* pQueryHandle, int32_t* numOfBlocks) { - // load all the comp offset value for all tables in this file - int32_t code = TSDB_CODE_SUCCESS; +static int32_t loadBlockInfo(STsdbQueryHandle * pQueryHandle, int32_t index, int32_t* numOfBlocks) { + int32_t code = 0; - *numOfBlocks = 0; - size_t numOfTables = taosArrayGetSize(pQueryHandle->pTableCheckInfo); + STableCheckInfo* pCheckInfo = taosArrayGet(pQueryHandle->pTableCheckInfo, index); + pCheckInfo->numOfBlocks = 0; - for (int32_t i = 0; i < numOfTables; ++i) { - STableCheckInfo* pCheckInfo = taosArrayGet(pQueryHandle->pTableCheckInfo, i); - pCheckInfo->numOfBlocks = 0; + if (tsdbSetReadTable(&pQueryHandle->rhelper, pCheckInfo->pTableObj) != TSDB_CODE_SUCCESS) { + code = terrno; + return code; + } - if (tsdbSetReadTable(&pQueryHandle->rhelper, pCheckInfo->pTableObj) != TSDB_CODE_SUCCESS) { - code = terrno; - break; - } + SBlockIdx* compIndex = pQueryHandle->rhelper.pBlkIdx; + + // no data block in this file, try next file + if (compIndex == NULL || compIndex->uid != pCheckInfo->tableId.uid) { + return 0; // no data blocks in the file belongs to pCheckInfo->pTable + } - SBlockIdx* compIndex = pQueryHandle->rhelper.pBlkIdx; + if (pCheckInfo->compSize < (int32_t)compIndex->len) { + assert(compIndex->len > 0); - // no data block in this file, try next file - if (compIndex == NULL || compIndex->uid != pCheckInfo->tableId.uid) { - continue; // no data blocks in the file belongs to pCheckInfo->pTable + char* t = realloc(pCheckInfo->pCompInfo, compIndex->len); + if (t == NULL) { + terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; + code = TSDB_CODE_TDB_OUT_OF_MEMORY; + return code; } - if (pCheckInfo->compSize < (int32_t)compIndex->len) { - assert(compIndex->len > 0); + pCheckInfo->pCompInfo = (SBlockInfo*)t; + pCheckInfo->compSize = compIndex->len; + } - char* t = realloc(pCheckInfo->pCompInfo, compIndex->len); - if (t == NULL) { - terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; - code = TSDB_CODE_TDB_OUT_OF_MEMORY; - break; - } + tsdbLoadBlockInfo(&(pQueryHandle->rhelper), (void*)(pCheckInfo->pCompInfo)); + SBlockInfo* pCompInfo = pCheckInfo->pCompInfo; - pCheckInfo->pCompInfo = (SBlockInfo*) t; - pCheckInfo->compSize = compIndex->len; - } + TSKEY s = TSKEY_INITIAL_VAL, e = TSKEY_INITIAL_VAL; - tsdbLoadBlockInfo(&(pQueryHandle->rhelper), (void *)(pCheckInfo->pCompInfo)); - SBlockInfo* pCompInfo = pCheckInfo->pCompInfo; + if (ASCENDING_TRAVERSE(pQueryHandle->order)) { + assert(pCheckInfo->lastKey <= pQueryHandle->window.ekey && pQueryHandle->window.skey <= pQueryHandle->window.ekey); + } else { + assert(pCheckInfo->lastKey >= pQueryHandle->window.ekey && pQueryHandle->window.skey >= pQueryHandle->window.ekey); + } - TSKEY s = TSKEY_INITIAL_VAL, e = TSKEY_INITIAL_VAL; + s = MIN(pCheckInfo->lastKey, pQueryHandle->window.ekey); + e = MAX(pCheckInfo->lastKey, pQueryHandle->window.ekey); - if (ASCENDING_TRAVERSE(pQueryHandle->order)) { - assert(pCheckInfo->lastKey <= pQueryHandle->window.ekey && pQueryHandle->window.skey <= pQueryHandle->window.ekey); - } else { - assert(pCheckInfo->lastKey >= pQueryHandle->window.ekey && pQueryHandle->window.skey >= pQueryHandle->window.ekey); - } + // discard the unqualified data block based on the query time window + int32_t start = binarySearchForBlock(pCompInfo->blocks, compIndex->numOfBlocks, s, TSDB_ORDER_ASC); + int32_t end = start; - s = MIN(pCheckInfo->lastKey, pQueryHandle->window.ekey); - e = MAX(pCheckInfo->lastKey, pQueryHandle->window.ekey); + if (s > pCompInfo->blocks[start].keyLast) { + return 0; + } - // discard the unqualified data block based on the query time window - int32_t start = binarySearchForBlock(pCompInfo->blocks, compIndex->numOfBlocks, s, TSDB_ORDER_ASC); - int32_t end = start; + // todo speedup the procedure of located end block + while (end < (int32_t)compIndex->numOfBlocks && (pCompInfo->blocks[end].keyFirst <= e)) { + end += 1; + } - if (s > pCompInfo->blocks[start].keyLast) { - continue; - } + pCheckInfo->numOfBlocks = (end - start); - // todo speedup the procedure of located end block - while (end < (int32_t)compIndex->numOfBlocks && (pCompInfo->blocks[end].keyFirst <= e)) { - end += 1; - } + if (start > 0) { + memmove(pCompInfo->blocks, &pCompInfo->blocks[start], pCheckInfo->numOfBlocks * sizeof(SBlock)); + } - pCheckInfo->numOfBlocks = (end - start); + (*numOfBlocks) += pCheckInfo->numOfBlocks; + return 0; +} - if (start > 0) { - memmove(pCompInfo->blocks, &pCompInfo->blocks[start], pCheckInfo->numOfBlocks * sizeof(SBlock)); - } +static int32_t getFileCompInfo(STsdbQueryHandle* pQueryHandle, int32_t* numOfBlocks) { + // load all the comp offset value for all tables in this file + int32_t code = TSDB_CODE_SUCCESS; + *numOfBlocks = 0; + + size_t numOfTables = 0; + if (pQueryHandle->loadType == BLOCK_LOAD_TABLE_SEQ_ORDER) { + code = loadBlockInfo(pQueryHandle, pQueryHandle->activeIndex, numOfBlocks); + } else if (pQueryHandle->loadType == BLOCK_LOAD_OFFSET_SEQ_ORDER) { + numOfTables = taosArrayGetSize(pQueryHandle->pTableCheckInfo); - (*numOfBlocks) += pCheckInfo->numOfBlocks; + for (int32_t i = 0; i < numOfTables; ++i) { + code = loadBlockInfo(pQueryHandle, i, numOfBlocks); + if (code != TSDB_CODE_SUCCESS) { + return code; + } + } + } else { + assert(0); } return code; @@ -1268,7 +1399,7 @@ static void copyOneRowFromMem(STsdbQueryHandle* pQueryHandle, int32_t capacity, case TSDB_DATA_TYPE_DOUBLE: SET_DOUBLE_PTR(pData, value); break; - case TSDB_DATA_TYPE_TIMESTAMP: + case TSDB_DATA_TYPE_TIMESTAMP: *(TSKEY *)pData = tdGetKey(*(TKEY *)value); break; default: @@ -1730,6 +1861,7 @@ static int32_t createDataBlocksInfo(STsdbQueryHandle* pQueryHandle, int32_t numO memset(pQueryHandle->pDataBlockInfo, 0, size); *numOfAllocBlocks = numOfBlocks; + // access data blocks according to the offset of each block in asc/desc order. int32_t numOfTables = (int32_t)taosArrayGetSize(pQueryHandle->pTableCheckInfo); SBlockOrderSupporter sup = {0}; @@ -1959,6 +2091,91 @@ static void moveToNextDataBlockInCurrentFile(STsdbQueryHandle* pQueryHandle) { cur->blockCompleted = false; } +int32_t tsdbGetFileBlocksDistInfo(TsdbQueryHandleT* queryHandle, STableBlockDist* pTableBlockInfo) { + STsdbQueryHandle* pQueryHandle = (STsdbQueryHandle*) queryHandle; + + pTableBlockInfo->totalSize = 0; + STsdbFS* pFileHandle = REPO_FS(pQueryHandle->pTsdb); + + // find the start data block in file + pQueryHandle->locateStart = true; + STsdbCfg* pCfg = &pQueryHandle->pTsdb->config; + int32_t fid = getFileIdFromKey(pQueryHandle->window.skey, pCfg->daysPerFile, pCfg->precision); + + tsdbRLockFS(pFileHandle); + tsdbFSIterInit(&pQueryHandle->fileIter, pFileHandle, pQueryHandle->order); + tsdbFSIterSeek(&pQueryHandle->fileIter, fid); + tsdbUnLockFS(pFileHandle); + + pTableBlockInfo->numOfFiles += 1; + + int32_t code = TSDB_CODE_SUCCESS; + int32_t numOfBlocks = 0; + int32_t numOfTables = (int32_t)taosArrayGetSize(pQueryHandle->pTableCheckInfo); + STimeWindow win = TSWINDOW_INITIALIZER; + + while (true) { + numOfBlocks = 0; + tsdbRLockFS(REPO_FS(pQueryHandle->pTsdb)); + + if ((pQueryHandle->pFileGroup = tsdbFSIterNext(&pQueryHandle->fileIter)) == NULL) { + tsdbUnLockFS(REPO_FS(pQueryHandle->pTsdb)); + break; + } + + tsdbGetFidKeyRange(pCfg->daysPerFile, pCfg->precision, pQueryHandle->pFileGroup->fid, &win.skey, &win.ekey); + + // current file are not overlapped with query time window, ignore remain files + if ((ASCENDING_TRAVERSE(pQueryHandle->order) && win.skey > pQueryHandle->window.ekey) || + (!ASCENDING_TRAVERSE(pQueryHandle->order) && win.ekey < pQueryHandle->window.ekey)) { + tsdbUnLockFS(REPO_FS(pQueryHandle->pTsdb)); + tsdbDebug("%p remain files are not qualified for qrange:%" PRId64 "-%" PRId64 ", ignore, %p", pQueryHandle, + pQueryHandle->window.skey, pQueryHandle->window.ekey, pQueryHandle->qinfo); + pQueryHandle->pFileGroup = NULL; + break; + } + + pTableBlockInfo->numOfFiles += 1; + if (tsdbSetAndOpenReadFSet(&pQueryHandle->rhelper, pQueryHandle->pFileGroup) < 0) { + tsdbUnLockFS(REPO_FS(pQueryHandle->pTsdb)); + code = terrno; + break; + } + + tsdbUnLockFS(REPO_FS(pQueryHandle->pTsdb)); + + if (tsdbLoadBlockIdx(&pQueryHandle->rhelper) < 0) { + code = terrno; + break; + } + + if ((code = getFileCompInfo(pQueryHandle, &numOfBlocks)) != TSDB_CODE_SUCCESS) { + break; + } + + tsdbDebug("%p %d blocks found in file for %d table(s), fid:%d, %p", pQueryHandle, numOfBlocks, numOfTables, + pQueryHandle->pFileGroup->fid, pQueryHandle->qinfo); + + if (numOfBlocks == 0) { + continue; + } + + for (int32_t i = 0; i < numOfTables; ++i) { + STableCheckInfo* pCheckInfo = taosArrayGet(pQueryHandle->pTableCheckInfo, i); + + SBlock* pBlock = pCheckInfo->pCompInfo->blocks; + for (int32_t j = 0; j < pCheckInfo->numOfBlocks; ++j) { + pTableBlockInfo->totalSize += pBlock[j].len; + + int32_t numOfRows = pBlock[j].numOfRows; + taosArrayPush(pTableBlockInfo->dataBlockInfos, &numOfRows); + } + } + } + + return code; +} + static int32_t getDataBlocksInFiles(STsdbQueryHandle* pQueryHandle, bool* exists) { STsdbFS* pFileHandle = REPO_FS(pQueryHandle->pTsdb); SQueryFilePos* cur = &pQueryHandle->cur; @@ -2017,19 +2234,14 @@ static bool doHasDataInBuffer(STsdbQueryHandle* pQueryHandle) { pQueryHandle->activeIndex += 1; } - if (pQueryHandle->loadExternalRow && pQueryHandle->window.skey == pQueryHandle->window.ekey) { - SMemRef* pMemRef = pQueryHandle->pMemRef; - doGetExternalRow(pQueryHandle, TSDB_PREV_ROW, pMemRef); - doGetExternalRow(pQueryHandle, TSDB_NEXT_ROW, pMemRef); - } - // no data in memtable or imemtable, decrease the memory reference. - tsdbMayUnTakeMemSnapshot(pQueryHandle); + // TODO !! +// tsdbMayUnTakeMemSnapshot(pQueryHandle); return false; } //todo not unref yet, since it is not support multi-group interpolation query -static void changeQueryHandleForInterpQuery(TsdbQueryHandleT pHandle) { +static UNUSED_FUNC void changeQueryHandleForInterpQuery(TsdbQueryHandleT pHandle) { // filter the queried time stamp in the first place STsdbQueryHandle* pQueryHandle = (STsdbQueryHandle*) pHandle; @@ -2152,149 +2364,164 @@ static void destroyHelper(void* param) { free(param); } -// handle data in cache situation -bool tsdbNextDataBlock(TsdbQueryHandleT* pHandle) { - STsdbQueryHandle* pQueryHandle = (STsdbQueryHandle*) pHandle; - - int64_t stime = taosGetTimestampUs(); - int64_t elapsedTime = stime; - - size_t numOfTables = taosArrayGetSize(pQueryHandle->pTableCheckInfo); - assert(numOfTables > 0); - - if (pQueryHandle->type == TSDB_QUERY_TYPE_LAST && pQueryHandle->cachelastrow) { - // the last row is cached in buffer, return it directly. - // here note that the pQueryHandle->window must be the TS_INITIALIZER - int32_t numOfCols = (int32_t)(QH_GET_NUM_OF_COLS(pQueryHandle)); - SQueryFilePos* cur = &pQueryHandle->cur; - - SDataRow pRow = NULL; - TSKEY key = TSKEY_INITIAL_VAL; - int32_t step = ASCENDING_TRAVERSE(pQueryHandle->order)? 1:-1; - - if (++pQueryHandle->activeIndex < numOfTables) { - STableCheckInfo* pCheckInfo = taosArrayGet(pQueryHandle->pTableCheckInfo, pQueryHandle->activeIndex); - int32_t ret = tsdbGetCachedLastRow(pCheckInfo->pTableObj, &pRow, &key); - if (ret != TSDB_CODE_SUCCESS) { - return false; - } - - copyOneRowFromMem(pQueryHandle, pQueryHandle->outputCapacity, 0, pRow, numOfCols, pCheckInfo->pTableObj, NULL); - tfree(pRow); - - // update the last key value - pCheckInfo->lastKey = key + step; - - cur->rows = 1; // only one row - cur->lastKey = key + step; - cur->mixBlock = true; - cur->win.skey = key; - cur->win.ekey = key; - - return true; - } - - return false; - } - +static bool loadBlockOfActiveTable(STsdbQueryHandle* pQueryHandle) { if (pQueryHandle->checkFiles) { // check if the query range overlaps with the file data block bool exists = true; int32_t code = getDataBlocksInFiles(pQueryHandle, &exists); if (code != TSDB_CODE_SUCCESS) { - pQueryHandle->activeIndex = 0; pQueryHandle->checkFiles = false; - return false; } if (exists) { - pQueryHandle->cost.checkForNextTime += (taosGetTimestampUs() - stime); + if (pQueryHandle->currentLoadExternalRows && pQueryHandle->window.skey == pQueryHandle->window.ekey) { + SColumnInfoData* pColInfo = taosArrayGet(pQueryHandle->pColumns, 0); + assert(*(int64_t*)pColInfo->pData == pQueryHandle->window.skey); + } + + pQueryHandle->currentLoadExternalRows = false; // clear the flag, since the exact matched row is found. return exists; } - pQueryHandle->activeIndex = 0; pQueryHandle->checkFiles = false; } - // TODO: opt by consider the scan order - bool ret = doHasDataInBuffer(pQueryHandle); - terrno = TSDB_CODE_SUCCESS; + if (hasMoreDataInCache(pQueryHandle)) { + pQueryHandle->currentLoadExternalRows = false; + return true; + } - elapsedTime = taosGetTimestampUs() - stime; - pQueryHandle->cost.checkForNextTime += elapsedTime; - return ret; + // current result is empty + if (pQueryHandle->currentLoadExternalRows && pQueryHandle->window.skey == pQueryHandle->window.ekey && pQueryHandle->cur.rows == 0) { + SMemRef* pMemRef = pQueryHandle->pMemRef; + + doGetExternalRow(pQueryHandle, TSDB_PREV_ROW, pMemRef); + doGetExternalRow(pQueryHandle, TSDB_NEXT_ROW, pMemRef); + + bool result = tsdbGetExternalRow(pQueryHandle); + + pQueryHandle->prev = doFreeColumnInfoData(pQueryHandle->prev); + pQueryHandle->next = doFreeColumnInfoData(pQueryHandle->next); + pQueryHandle->currentLoadExternalRows = false; + + return result; + } + + return false; } -bool tsdbNextDataBlockWithoutMerge(TsdbQueryHandleT* pHandle) { - STsdbQueryHandle* pQueryHandle = (STsdbQueryHandle*) pHandle; +static bool loadCachedLastRow(STsdbQueryHandle* pQueryHandle) { + // the last row is cached in buffer, return it directly. + // here note that the pQueryHandle->window must be the TS_INITIALIZER + int32_t numOfCols = (int32_t)(QH_GET_NUM_OF_COLS(pQueryHandle)); + size_t numOfTables = taosArrayGetSize(pQueryHandle->pTableCheckInfo); + assert(numOfTables > 0 && numOfCols > 0); - int64_t stime = taosGetTimestampUs(); - int64_t elapsedTime = stime; + SQueryFilePos* cur = &pQueryHandle->cur; - size_t numOfTables = taosArrayGetSize(pQueryHandle->pTableCheckInfo); - assert(numOfTables > 0); + SDataRow pRow = NULL; + TSKEY key = TSKEY_INITIAL_VAL; + int32_t step = ASCENDING_TRAVERSE(pQueryHandle->order)? 1:-1; - if (pQueryHandle->type == TSDB_QUERY_TYPE_LAST && pQueryHandle->cachelastrow) { - // the last row is cached in buffer, return it directly. - // here note that the pQueryHandle->window must be the TS_INITIALIZER - int32_t numOfCols = (int32_t)(QH_GET_NUM_OF_COLS(pQueryHandle)); - SQueryFilePos* cur = &pQueryHandle->cur; - - SDataRow pRow = NULL; - TSKEY key = TSKEY_INITIAL_VAL; - int32_t step = ASCENDING_TRAVERSE(pQueryHandle->order)? 1:-1; - - if (++pQueryHandle->activeIndex < numOfTables) { - STableCheckInfo* pCheckInfo = taosArrayGet(pQueryHandle->pTableCheckInfo, pQueryHandle->activeIndex); - int32_t ret = tsdbGetCachedLastRow(pCheckInfo->pTableObj, &pRow, &key); - if (ret != TSDB_CODE_SUCCESS) { - return false; - } + if (++pQueryHandle->activeIndex < numOfTables) { + STableCheckInfo* pCheckInfo = taosArrayGet(pQueryHandle->pTableCheckInfo, pQueryHandle->activeIndex); + int32_t ret = tsdbGetCachedLastRow(pCheckInfo->pTableObj, &pRow, &key); + if (ret != TSDB_CODE_SUCCESS) { + return false; + } - copyOneRowFromMem(pQueryHandle, pQueryHandle->outputCapacity, 0, pRow, numOfCols, pCheckInfo->pTableObj, NULL); - tfree(pRow); + copyOneRowFromMem(pQueryHandle, pQueryHandle->outputCapacity, 0, pRow, numOfCols, pCheckInfo->pTableObj, NULL); + tfree(pRow); - // update the last key value - pCheckInfo->lastKey = key + step; + // update the last key value + pCheckInfo->lastKey = key + step; - cur->rows = 1; // only one row - cur->lastKey = key + step; - cur->mixBlock = true; - cur->win.skey = key; - cur->win.ekey = key; + cur->rows = 1; // only one row + cur->lastKey = key + step; + cur->mixBlock = true; + cur->win.skey = key; + cur->win.ekey = key; + + return true; + } + + return false; +} + +static bool loadDataBlockFromTableSeq(STsdbQueryHandle* pQueryHandle) { + size_t numOfTables = taosArrayGetSize(pQueryHandle->pTableCheckInfo); + assert(numOfTables > 0); + + int64_t stime = taosGetTimestampUs(); + while(pQueryHandle->activeIndex < numOfTables) { + if (loadBlockOfActiveTable(pQueryHandle)) { return true; } - return false; + STableCheckInfo* pCheckInfo = taosArrayGet(pQueryHandle->pTableCheckInfo, pQueryHandle->activeIndex); + pCheckInfo->numOfBlocks = 0; + + pQueryHandle->activeIndex += 1; + pQueryHandle->locateStart = false; + pQueryHandle->checkFiles = true; + pQueryHandle->cur.rows = 0; + pQueryHandle->currentLoadExternalRows = pQueryHandle->loadExternalRow; + + terrno = TSDB_CODE_SUCCESS; + + int64_t elapsedTime = taosGetTimestampUs() - stime; + pQueryHandle->cost.checkForNextTime += elapsedTime; } - if (pQueryHandle->checkFiles) { - // check if the query range overlaps with the file data block - bool exists = true; + return false; +} + +// handle data in cache situation +bool tsdbNextDataBlock(TsdbQueryHandleT pHandle) { + STsdbQueryHandle* pQueryHandle = (STsdbQueryHandle*) pHandle; + + int64_t stime = taosGetTimestampUs(); + int64_t elapsedTime = stime; + + if (pQueryHandle->type == TSDB_QUERY_TYPE_LAST && pQueryHandle->cachelastrow) { + return loadCachedLastRow(pQueryHandle); + } + + if (pQueryHandle->loadType == BLOCK_LOAD_TABLE_SEQ_ORDER) { + return loadDataBlockFromTableSeq(pQueryHandle); + } else { // loadType == RR and Offset Order + if (pQueryHandle->checkFiles) { + // check if the query range overlaps with the file data block + bool exists = true; + + int32_t code = getDataBlocksInFiles(pQueryHandle, &exists); + if (code != TSDB_CODE_SUCCESS) { + pQueryHandle->activeIndex = 0; + pQueryHandle->checkFiles = false; + + return false; + } + + if (exists) { + pQueryHandle->cost.checkForNextTime += (taosGetTimestampUs() - stime); + return exists; + } - int32_t code = getDataBlocksInFiles(pQueryHandle, &exists); - if (code != TSDB_CODE_SUCCESS) { pQueryHandle->activeIndex = 0; pQueryHandle->checkFiles = false; - - return false; } - if (exists) { - pQueryHandle->cost.checkForNextTime += (taosGetTimestampUs() - stime); - return exists; - } + // TODO: opt by consider the scan order + bool ret = doHasDataInBuffer(pQueryHandle); + terrno = TSDB_CODE_SUCCESS; - pQueryHandle->activeIndex = 0; - pQueryHandle->checkFiles = false; + elapsedTime = taosGetTimestampUs() - stime; + pQueryHandle->cost.checkForNextTime += elapsedTime; + return ret; } - - elapsedTime = taosGetTimestampUs() - stime; - pQueryHandle->cost.checkForNextTime += elapsedTime; - return false; } static int32_t doGetExternalRow(STsdbQueryHandle* pQueryHandle, int16_t type, SMemRef* pMemRef) { @@ -2342,7 +2569,7 @@ static int32_t doGetExternalRow(STsdbQueryHandle* pQueryHandle, int16_t type, SM } // load the previous row - STsdbQueryCond cond = {.numOfCols = numOfCols, .loadExternalRows = false,}; + STsdbQueryCond cond = {.numOfCols = numOfCols, .loadExternalRows = false, .type = BLOCK_LOAD_OFFSET_SEQ_ORDER}; if (type == TSDB_PREV_ROW) { cond.order = TSDB_ORDER_DESC; cond.twindow = (STimeWindow){pQueryHandle->window.skey, INT64_MIN}; @@ -2363,20 +2590,20 @@ static int32_t doGetExternalRow(STsdbQueryHandle* pQueryHandle, int16_t type, SM } pSecQueryHandle = tsdbQueryTablesImpl(pQueryHandle->pTsdb, &cond, pQueryHandle->qinfo, pMemRef); - tfree(cond.colList); + // current table, only one table + STableCheckInfo* pCurrent = taosArrayGet(pQueryHandle->pTableCheckInfo, pQueryHandle->activeIndex); + SArray* psTable = NULL; - - pSecQueryHandle->pTableCheckInfo = createCheckInfoFromCheckInfo(pQueryHandle->pTableCheckInfo, pSecQueryHandle->window.skey, &psTable); + pSecQueryHandle->pTableCheckInfo = createCheckInfoFromCheckInfo(pCurrent, pSecQueryHandle->window.skey, &psTable); if (pSecQueryHandle->pTableCheckInfo == NULL) { terrno = TSDB_CODE_QRY_OUT_OF_MEMORY; goto out_of_memory; } - - tsdbMayTakeMemSnapshot(pSecQueryHandle, psTable); + tsdbMayTakeMemSnapshot(pSecQueryHandle, psTable); if (!tsdbNextDataBlock((void*)pSecQueryHandle)) { // no result in current query, free the corresponding result rows structure if (type == TSDB_PREV_ROW) { @@ -2406,10 +2633,35 @@ out_of_memory: return terrno; } -SArray* tsdbGetExternalRow(TsdbQueryHandleT *pHandle, SMemRef* pMemRef, int16_t type) { +bool tsdbGetExternalRow(TsdbQueryHandleT pHandle) { STsdbQueryHandle* pQueryHandle = (STsdbQueryHandle*) pHandle; - assert(type == TSDB_PREV_ROW || type == TSDB_NEXT_ROW); - return (type == TSDB_PREV_ROW)? pQueryHandle->prev:pQueryHandle->next; + SQueryFilePos* cur = &pQueryHandle->cur; + + cur->fid = INT32_MIN; + cur->mixBlock = true; + if (pQueryHandle->prev == NULL || pQueryHandle->next == NULL) { + cur->rows = 0; + return false; + } + + int32_t numOfCols = (int32_t) QH_GET_NUM_OF_COLS(pQueryHandle); + for (int32_t i = 0; i < numOfCols; ++i) { + SColumnInfoData* pColInfoData = taosArrayGet(pQueryHandle->pColumns, i); + SColumnInfoData* first = taosArrayGet(pQueryHandle->prev, i); + + memcpy(pColInfoData->pData, first->pData, pColInfoData->info.bytes); + + SColumnInfoData* sec = taosArrayGet(pQueryHandle->next, i); + memcpy(((char*)pColInfoData->pData) + pColInfoData->info.bytes, sec->pData, pColInfoData->info.bytes); + + if (i == 0 && pColInfoData->info.type == TSDB_DATA_TYPE_TIMESTAMP) { + cur->win.skey = *(TSKEY*)pColInfoData->pData; + cur->win.ekey = *(TSKEY*)(((char*)pColInfoData->pData) + TSDB_KEYSIZE); + } + } + + cur->rows = 2; + return true; } /* @@ -2949,10 +3201,10 @@ int32_t tsdbQuerySTableByTagCond(STsdbRepo* tsdb, uint64_t uid, TSKEY skey, cons goto _error; } - pGroupInfo->numOfTables = taosArrayGetSize(res); + pGroupInfo->numOfTables = (uint32_t) taosArrayGetSize(res); pGroupInfo->pGroupList = createTableGroup(res, pTagSchema, pColIndex, numOfCols, skey); - tsdbDebug("%p no table name/tag condition, all tables belong to one group, numOfTables:%" PRIzu "", tsdb, pGroupInfo->numOfTables); + tsdbDebug("%p no table name/tag condition, all tables belong to one group, numOfTables:%u", tsdb, pGroupInfo->numOfTables); taosArrayDestroy(res); if (tsdbUnlockRepoMeta(tsdb) < 0) goto _error; @@ -2994,10 +3246,10 @@ int32_t tsdbQuerySTableByTagCond(STsdbRepo* tsdb, uint64_t uid, TSKEY skey, cons } END_TRY doQueryTableList(pTable, res, expr); - pGroupInfo->numOfTables = taosArrayGetSize(res); + pGroupInfo->numOfTables = (uint32_t)taosArrayGetSize(res); pGroupInfo->pGroupList = createTableGroup(res, pTagSchema, pColIndex, numOfCols, skey); - tsdbDebug("%p stable tid:%d, uid:%"PRIu64" query, numOfTables:%" PRIzu ", belong to %" PRIzu " groups", tsdb, pTable->tableId.tid, + tsdbDebug("%p stable tid:%d, uid:%"PRIu64" query, numOfTables:%u, belong to %" PRIzu " groups", tsdb, pTable->tableId.tid, pTable->tableId.uid, pGroupInfo->numOfTables, taosArrayGetSize(pGroupInfo->pGroupList)); taosArrayDestroy(res); @@ -3074,7 +3326,7 @@ int32_t tsdbGetTableGroupFromIdList(STsdbRepo* tsdb, SArray* pTableIdList, STabl return terrno; } - pGroupInfo->numOfTables = taosArrayGetSize(group); + pGroupInfo->numOfTables = (uint32_t) taosArrayGetSize(group); if (pGroupInfo->numOfTables > 0) { taosArrayPush(pGroupInfo->pGroupList, &group); } else { @@ -3099,23 +3351,26 @@ static void* doFreeColumnInfoData(SArray* pColumnInfoData) { return NULL; } +static void* destroyTableCheckInfo(SArray* pTableCheckInfo) { + size_t size = taosArrayGetSize(pTableCheckInfo); + for (int32_t i = 0; i < size; ++i) { + STableCheckInfo* p = taosArrayGet(pTableCheckInfo, i); + destroyTableMemIterator(p); + + tfree(p->pCompInfo); + } + + taosArrayDestroy(pTableCheckInfo); + return NULL; +} + void tsdbCleanupQueryHandle(TsdbQueryHandleT queryHandle) { STsdbQueryHandle* pQueryHandle = (STsdbQueryHandle*)queryHandle; if (pQueryHandle == NULL) { return; } - - if (pQueryHandle->pTableCheckInfo != NULL) { - size_t size = taosArrayGetSize(pQueryHandle->pTableCheckInfo); - for (int32_t i = 0; i < size; ++i) { - STableCheckInfo* pTableCheckInfo = taosArrayGet(pQueryHandle->pTableCheckInfo, i); - destroyTableMemIterator(pTableCheckInfo); - - tfree(pTableCheckInfo->pCompInfo); - } - taosArrayDestroy(pQueryHandle->pTableCheckInfo); - } + pQueryHandle->pTableCheckInfo = destroyTableCheckInfo(pQueryHandle->pTableCheckInfo); pQueryHandle->pColumns = doFreeColumnInfoData(pQueryHandle->pColumns); taosArrayDestroy(pQueryHandle->defaultLoadColumn); diff --git a/src/util/inc/tarray.h b/src/util/inc/tarray.h index 63e62a54c2f679920100b7137cb49377da90789f..f2e268c2d4dfe210dfbfd9b94ee74a4f87848361 100644 --- a/src/util/inc/tarray.h +++ b/src/util/inc/tarray.h @@ -25,7 +25,8 @@ extern "C" { #define TARRAY_MIN_SIZE 8 #define TARRAY_GET_ELEM(array, index) ((void*)((char*)((array)->pData) + (index) * (array)->elemSize)) -#define TARRAY_ELEM_IDX(array, ele) (POINTER_DISTANCE(ele, (array)->pData) / (array)->elemSize) +#define TARRAY_ELEM_IDX(array, ele) (POINTER_DISTANCE(ele, (array)->pData) / (array)->elemSize) +#define TARRAY_GET_START(array) ((array)->pData) typedef struct SArray { size_t size; diff --git a/src/util/inc/tbuffer.h b/src/util/inc/tbuffer.h index e2bdb815d7ac8ecd09b62a3a62897b78ebf3c4b7..b19984b4be047108fd9848a2a7eddbac2c0b78a4 100644 --- a/src/util/inc/tbuffer.h +++ b/src/util/inc/tbuffer.h @@ -73,14 +73,14 @@ int main( int argc, char** argv ) { } */ -typedef struct { +typedef struct SBufferReader { bool endian; const char* data; size_t pos; size_t size; } SBufferReader; -typedef struct { +typedef struct SBufferWriter { bool endian; char* data; size_t pos; diff --git a/src/vnode/src/vnodeRead.c b/src/vnode/src/vnodeRead.c index acb1fc78f6554dc7cd4b984107a7bc1b95315b67..8b26f4a8e22e00286a833f88efdf31e1e84b58e9 100644 --- a/src/vnode/src/vnodeRead.c +++ b/src/vnode/src/vnodeRead.c @@ -372,7 +372,7 @@ static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SVReadMsg *pRead) { // kill current query and free corresponding resources. if (pRetrieve->free == 1) { - vWarn("vgId:%d, QInfo:%p, retrieve msg received to kill query and free qhandle", pVnode->vgId, *handle); + vDebug("vgId:%d, QInfo:%p, retrieve msg received to kill query and free qhandle", pVnode->vgId, *handle); qKillQuery(*handle); qReleaseQInfo(pVnode->qMgmt, (void **)&handle, true); diff --git a/tests/pytest/crash_gen/crash_gen_main.py b/tests/pytest/crash_gen/crash_gen_main.py index 43506c68d51b134347df48080ef7f3223b379775..0fe94bad1d6b36aa1be5d47428dae19e918ce6e4 100755 --- a/tests/pytest/crash_gen/crash_gen_main.py +++ b/tests/pytest/crash_gen/crash_gen_main.py @@ -1785,7 +1785,7 @@ class TdSuperTable: 'top(speed, 50)', # TODO: not supported? 'bottom(speed, 50)', # TODO: not supported? 'apercentile(speed, 10)', # TODO: TD-1316 - 'last_row(speed)', + # 'last_row(speed)', # TODO: commented out per TD-3231, we should re-create # Transformation Functions # 'diff(speed)', # TODO: no supported?! 'spread(speed)' diff --git a/tests/pytest/fulltest.sh b/tests/pytest/fulltest.sh index 14f2b6e07e39322a3ce62f6176034fd2fba9edbc..d830d96865a3dfac2a0244c796a01516fba0369c 100755 --- a/tests/pytest/fulltest.sh +++ b/tests/pytest/fulltest.sh @@ -183,6 +183,7 @@ python3 ./test.py -f query/isNullTest.py python3 ./test.py -f query/queryWithTaosdKilled.py python3 ./test.py -f query/floatCompare.py python3 ./test.py -f query/queryGroupbySort.py +python3 ./test.py -f query/queryBetweenAnd.py #stream python3 ./test.py -f stream/metric_1.py @@ -232,6 +233,7 @@ python3 ./test.py -f functions/function_top.py -r 1 python3 ./test.py -f functions/function_twa.py -r 1 python3 ./test.py -f functions/function_twa_test2.py python3 ./test.py -f functions/all_null_value.py +python3 ./test.py -f functions/function_percentile2.py python3 queryCount.py python3 ./test.py -f query/queryGroupbyWithInterval.py python3 client/twoClients.py @@ -241,10 +243,12 @@ python3 ./test.py -f query/last_row_cache.py python3 ./test.py -f query/last_cache.py # tools -python3 test.py -f tools/taosdemoTest.py python3 test.py -f tools/taosdumpTest.py python3 test.py -f tools/lowaTest.py +python3 test.py -f tools/taosdemoTest.py python3 test.py -f tools/taosdemoTest2.py +python3 test.py -f tools/taosdemoTestWithoutMetric.py +python3 test.py -f tools/taosdemoTestLimitOffset.py # subscribe python3 test.py -f subscribe/singlemeter.py @@ -270,4 +274,4 @@ python3 ./test.py -f update/bug_td2279.py python3 ./test.py -f wal/addOldWalTest.py # account -python3 ./test.py -f account/account_create.py \ No newline at end of file +python3 ./test.py -f account/account_create.py diff --git a/tests/pytest/functions/function_percentile2.py b/tests/pytest/functions/function_percentile2.py new file mode 100644 index 0000000000000000000000000000000000000000..227a3cbe0307a8a198fed2e981eba2890f1e5664 --- /dev/null +++ b/tests/pytest/functions/function_percentile2.py @@ -0,0 +1,63 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +from util.log import * +from util.cases import * +from util.sql import * +import numpy as np + + +class TDTestCase: + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor()) + + self.rowNum = 1000000 + self.ts = 1537146000000 + + def run(self): + tdSql.prepare() + + tdSql.execute("create table test(ts timestamp, col1 int, col2 float, col3 double)") + for i in range(1000): + sql = "insert into test values" + batchSize = int (self.rowNum / 1000) + for j in range (batchSize): + currTime = self.ts + batchSize * i + j + sql += "(%d, 1, 2.37, 3.1415926)" % currTime + tdSql.execute(sql) + + tdSql.query("select percentile(col1, 20) from test") + tdSql.checkData(0, 0, 1) + + tdSql.query("select percentile(col2, 20) from test") + tdSql.checkData(0, 0, 2.3699998) + + tdSql.query("select percentile(col3, 20) from test") + tdSql.checkData(0, 0, 3.1415926) + + tdSql.query("select apercentile(col1, 20) from test") + tdSql.checkData(0, 0, 1) + + tdSql.query("select apercentile(col2, 20) from test") + tdSql.checkData(0, 0, 2.3699998) + + tdSql.query("select apercentile(col3, 20) from test") + tdSql.checkData(0, 0, 3.1415926) + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) diff --git a/tests/pytest/functions/function_stddev.py b/tests/pytest/functions/function_stddev.py index 51e005936183853ee2a4c9cc58bd5dd192b99006..afc59ac83d7156253225bf6c111e7b5f0e6f9dcb 100644 --- a/tests/pytest/functions/function_stddev.py +++ b/tests/pytest/functions/function_stddev.py @@ -26,6 +26,8 @@ class TDTestCase: self.rowNum = 10 self.ts = 1537146000000 + self.stb_prefix = 's' + self.subtb_prefix = 't' def run(self): tdSql.prepare() @@ -85,6 +87,39 @@ class TDTestCase: tdSql.query("select stddev(col6) from test1") tdSql.checkData(0, 0, np.std(floatData)) + + #add for td-3276 + sql="create table s (ts timestamp, c1 int, c2 float, c3 bigint, c4 smallint, c5 tinyint, c6 double, c7 bool,c8 binary(20),c9 nchar(20),c11 int unsigned,c12 smallint unsigned,c13 tinyint unsigned,c14 bigint unsigned) \ + tags(t1 int, t2 float, t3 bigint, t4 smallint, t5 tinyint, t6 double, t7 bool,t8 binary(20),t9 nchar(20), t10 int unsigned , t11 smallint unsigned , t12 tinyint unsigned , t13 bigint unsigned)" + tdSql.execute(sql) + for j in range(2): + if j % 2 == 0: + sql = "create table %s using %s tags(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)" % \ + (self.subtb_prefix+str(j)+'_'+str(j),self.stb_prefix) + else: + sql = "create table %s using %s tags(%d,%d,%d,%d,%d,%d,%d,'%s','%s',%d,%d,%d,%d)" % \ + (self.subtb_prefix+str(j)+'_'+str(j),self.stb_prefix,j,j/2.0,j%41,j%51,j%53,j*1.0,j%2,'taos'+str(j),'涛思'+str(j), j%43, j%23 , j%17 , j%3167) + tdSql.execute(sql) + for i in range(10): + if i % 5 == 0 : + ret = tdSql.execute( + "insert into %s values (%d , NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)" % + (self.subtb_prefix+str(j)+'_'+str(j), self.ts+i)) + else: + ret = tdSql.execute( + "insert into %s values (%d , %d,%d,%d,%d,%d,%d,%d,'%s','%s',%d,%d,%d,%d)" % + (self.subtb_prefix+str(j)+'_'+str(j), self.ts+i, i%100, i/2.0, i%41, i%51, i%53, i*1.0, i%2,'taos'+str(i),'涛思'+str(i), i%43, i%23 , i%17 , i%3167)) + + for i in range(13): + tdSql.query('select stddev(c4) from s group by t%s' % str(i+1) ) + + #add for td-3223 + for i in range(13): + if i == 1 or i == 5 or i == 6 or i == 7 or i == 9 or i == 8 :continue + tdSql.query('select stddev(c%d),stddev(c%d) from s group by c%d' %( i+1 , i+1 , i+1 ) ) + + + def stop(self): tdSql.close() diff --git a/tests/pytest/pytest_3.sh b/tests/pytest/pytest_3.sh index b1e2539fa935bcb9ce69482325e1e4c6df0503f2..f2462154bc60f845600d6f819edf01925cb86938 100755 --- a/tests/pytest/pytest_3.sh +++ b/tests/pytest/pytest_3.sh @@ -78,9 +78,11 @@ python3 test.py -f query/queryInterval.py python3 test.py -f query/queryFillTest.py # tools +python3 test.py -f tools/lowaTest.py python3 test.py -f tools/taosdemoTest.py +python3 test.py -f tools/taosdemoTestWithoutMetric.py +python3 test.py -f tools/taosdemoTestLimitOffset.py python3 test.py -f tools/taosdumpTest.py -python3 test.py -f tools/lowaTest.py #python3 test.py -f tools/taosdemoTest2.py # subscribe diff --git a/tests/pytest/query/queryBetweenAnd.py b/tests/pytest/query/queryBetweenAnd.py new file mode 100644 index 0000000000000000000000000000000000000000..cd4320f5235222049f3c1dfc6a7bb8a1ab901b61 --- /dev/null +++ b/tests/pytest/query/queryBetweenAnd.py @@ -0,0 +1,206 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import taos +import sys + +from util.log import * +from util.sql import * +from util.cases import * + + + +class TDTestCase: + + def init(self, conn, logSql): + tdLog.debug(f"start to excute {__file__}") + tdSql.init(conn.cursor()) + + def run(self): + tdSql.prepare() + + tdLog.printNoPrefix("==========step1:create table") + tdSql.execute( + '''create table if not exists supt + (ts timestamp, c1 int, c2 float, c3 bigint, c4 double, c5 smallint, c6 tinyint) + tags(location binary(64), type int, isused bool , family nchar(64))''' + ) + tdSql.execute("create table t1 using supt tags('beijing', 1, 1, '自行车')") + tdSql.execute("create table t2 using supt tags('shanghai', 2, 0, '拖拉机')") + + tdLog.printNoPrefix("==========step2:insert data") + for i in range(10): + tdSql.execute( + f"insert into t1 values (now+{i}m, {32767+i}, {20.0+i/10}, {2**31+i}, {3.4*10**38+i/10}, {127+i}, {i})" + ) + tdSql.execute( + f"insert into t2 values (now-{i}m, {-32767-i}, {20.0-i/10}, {-i-2**31}, {-i/10-3.4*10**38}, {-127-i}, {-i})" + ) + tdSql.execute( + f"insert into t1 values (now+11m, {2**31-1}, {pow(10,37)*34}, {pow(2,63)-1}, {1.7*10**308}, 32767, 127)" + ) + tdSql.execute( + f"insert into t2 values (now-11m, {1-2**31}, {-3.4*10**38}, {1-2**63}, {-1.7*10**308}, -32767, -127)" + ) + tdSql.execute( + f"insert into t2 values (now-12m, null , {-3.4*10**38}, null , {-1.7*10**308}, null , null)" + ) + + tdLog.printNoPrefix("==========step3:query timestamp type") + + tdSql.query("select * from t1 where ts between now-1m and now+10m") + tdSql.checkRows(10) + tdSql.query("select * from t1 where ts between '2021-01-01 00:00:00.000' and '2121-01-01 00:00:00.000'") + tdSql.checkRows(11) + tdSql.query("select * from t1 where ts between '1969-01-01 00:00:00.000' and '1969-12-31 23:59:59.999'") + tdSql.checkRows(0) + tdSql.query("select * from t1 where ts between -2793600 and 31507199") + tdSql.checkRows(0) + tdSql.query("select * from t1 where ts between 1609430400000 and 4765104000000") + tdSql.checkRows(11) + + tdLog.printNoPrefix("==========step4:query int type") + + tdSql.query("select * from t1 where c1 between 32767 and 32776") + tdSql.checkRows(10) + tdSql.query("select * from t1 where c1 between 32766.9 and 32776.1") + tdSql.checkRows(10) + tdSql.query("select * from t1 where c1 between 32776 and 32767") + tdSql.checkRows(0) + tdSql.error("select * from t1 where c1 between 'a' and 'e'") + # tdSql.query("select * from t1 where c1 between 0x64 and 0x69") + # tdSql.checkRows(6) + tdSql.error("select * from t1 where c1 not between 100 and 106") + tdSql.query(f"select * from t1 where c1 between {2**31-2} and {2**31+1}") + tdSql.checkRows(1) + tdSql.error(f"select * from t2 where c1 between null and {1-2**31}") + # tdSql.checkRows(3) + tdSql.query(f"select * from t2 where c1 between {-2**31} and {1-2**31}") + tdSql.checkRows(1) + + tdLog.printNoPrefix("==========step5:query float type") + + tdSql.query("select * from t1 where c2 between 20.0 and 21.0") + tdSql.checkRows(10) + tdSql.query(f"select * from t1 where c2 between {-3.4*10**38-1} and {3.4*10**38+1}") + tdSql.checkRows(11) + tdSql.query("select * from t1 where c2 between 21.0 and 20.0") + tdSql.checkRows(0) + tdSql.error("select * from t1 where c2 between 'DC3' and 'SYN'") + tdSql.error("select * from t1 where c2 not between 0.1 and 0.2") + # tdSql.query(f"select * from t1 where c2 between {pow(10,38)*3.4} and {pow(10,38)*3.4+1}") + # tdSql.checkRows(1) + tdSql.query(f"select * from t2 where c2 between {-3.4*10**38-1} and {-3.4*10**38}") + tdSql.checkRows(0) + tdSql.error(f"select * from t2 where c2 between null and {-3.4*10**38}") + # tdSql.checkRows(3) + + tdLog.printNoPrefix("==========step6:query bigint type") + + tdSql.query(f"select * from t1 where c3 between {2**31} and {2**31+10}") + tdSql.checkRows(10) + tdSql.error(f"select * from t1 where c3 between {-2**63} and {2**63}") + # tdSql.checkRows(11) + tdSql.query(f"select * from t1 where c3 between {2**31+10} and {2**31}") + tdSql.checkRows(0) + tdSql.error("select * from t1 where c3 between 'a' and 'z'") + tdSql.error("select * from t1 where c3 not between 1 and 2") + tdSql.query(f"select * from t1 where c3 between {2**63-2} and {2**63-1}") + tdSql.checkRows(1) + tdSql.error(f"select * from t2 where c3 between {-2**63} and {1-2**63}") + # tdSql.checkRows(3) + tdSql.error(f"select * from t2 where c3 between null and {1-2**63}") + # tdSql.checkRows(2) + + tdLog.printNoPrefix("==========step7:query double type") + + tdSql.query(f"select * from t1 where c4 between {3.4*10**38} and {3.4*10**38+10}") + tdSql.checkRows(10) + tdSql.query(f"select * from t1 where c4 between {1.7*10**308+1} and {1.7*10**308+2}") + # 因为精度原因,在超出bigint边界后,数值不能进行准确的判断 + # tdSql.checkRows(0) + tdSql.query(f"select * from t1 where c4 between {3.4*10**38+10} and {3.4*10**38}") + # tdSql.checkRows(0) + tdSql.error("select * from t1 where c4 between 'a' and 'z'") + tdSql.error("select * from t1 where c4 not between 1 and 2") + tdSql.query(f"select * from t1 where c4 between {1.7*10**308} and {1.7*10**308+1}") + tdSql.checkRows(1) + tdSql.query(f"select * from t2 where c4 between {-1.7*10**308-1} and {-1.7*10**308}") + # tdSql.checkRows(3) + tdSql.error(f"select * from t2 where c4 between null and {-1.7*10**308}") + # tdSql.checkRows(3) + + tdLog.printNoPrefix("==========step8:query smallint type") + + tdSql.query("select * from t1 where c5 between 127 and 136") + tdSql.checkRows(10) + tdSql.query("select * from t1 where c5 between 126.9 and 135.9") + tdSql.checkRows(9) + tdSql.query("select * from t1 where c5 between 136 and 127") + tdSql.checkRows(0) + tdSql.error("select * from t1 where c5 between '~' and 'ˆ'") + tdSql.error("select * from t1 where c5 not between 1 and 2") + tdSql.query("select * from t1 where c5 between 32767 and 32768") + tdSql.checkRows(1) + tdSql.query("select * from t2 where c5 between -32768 and -32767") + tdSql.checkRows(1) + tdSql.error("select * from t2 where c5 between null and -32767") + # tdSql.checkRows(1) + + tdLog.printNoPrefix("==========step9:query tinyint type") + + tdSql.query("select * from t1 where c6 between 0 and 9") + tdSql.checkRows(10) + tdSql.query("select * from t1 where c6 between -1.1 and 8.9") + tdSql.checkRows(9) + tdSql.query("select * from t1 where c6 between 9 and 0") + tdSql.checkRows(0) + tdSql.error("select * from t1 where c6 between 'NUL' and 'HT'") + tdSql.error("select * from t1 where c6 not between 1 and 2") + tdSql.query("select * from t1 where c6 between 127 and 128") + tdSql.checkRows(1) + tdSql.query("select * from t2 where c6 between -128 and -127") + tdSql.checkRows(1) + tdSql.error("select * from t2 where c6 between null and -127") + # tdSql.checkRows(3) + + tdLog.printNoPrefix("==========step10:invalid query type") + + tdSql.query("select * from supt where location between 'beijing' and 'shanghai'") + tdSql.checkRows(23) + # 非0值均解析为1,因此"between 负值 and o"解析为"between 1 and 0" + tdSql.query("select * from supt where isused between 0 and 1") + tdSql.checkRows(23) + tdSql.query("select * from supt where isused between -1 and 0") + tdSql.checkRows(0) + tdSql.error("select * from supt where isused between false and true") + tdSql.query("select * from supt where family between '拖拉机' and '自行车'") + tdSql.checkRows(23) + + tdLog.printNoPrefix("==========step11:query HEX/OCT/BIN type") + + tdSql.error("select * from t1 where c6 between 0x7f and 0x80") # check filter HEX + tdSql.error("select * from t1 where c6 between 0b1 and 0b11111") # check filter BIN + tdSql.error("select * from t1 where c6 between 0b1 and 0x80") + tdSql.error("select * from t1 where c6=0b1") + tdSql.error("select * from t1 where c6=0x1") + # 八进制数据会按照十进制数据进行判定 + tdSql.query("select * from t1 where c6 between 01 and 0200") # check filter OCT + tdSql.checkRows(10) + + def stop(self): + tdSql.close() + tdLog.success(f"{__file__} successfully executed") + +tdCases.addLinux(__file__, TDTestCase()) +tdCases.addWindows(__file__, TDTestCase()) \ No newline at end of file diff --git a/tests/pytest/query/queryNormal.py b/tests/pytest/query/queryNormal.py index 13393117d642ec1fbdf839d59429428ffee79a27..4e3f9dd3bdd6376ac1069c4e084944a577ce4a70 100644 --- a/tests/pytest/query/queryNormal.py +++ b/tests/pytest/query/queryNormal.py @@ -94,8 +94,9 @@ class TDTestCase: tdSql.query("select * from stb1 limit 2 offset 3") tdSql.checkRows(1) - # query ... alias for table ---- bug - tdSql.error("select t.ts from tb1 t") + # query ... alias for table + tdSql.query("select t.ts from tb1 t") + tdSql.checkRows(2) # query ... tbname tdSql.query("select tbname from stb1") diff --git a/tests/pytest/tools/insert-tblimit-tboffset.json b/tests/pytest/tools/insert-tblimit-tboffset.json new file mode 100644 index 0000000000000000000000000000000000000000..5f63660347141e419043da7632b507cf38be4315 --- /dev/null +++ b/tests/pytest/tools/insert-tblimit-tboffset.json @@ -0,0 +1,60 @@ +{ + "filetype": "insert", + "cfgdir": "/etc/taos", + "host": "127.0.0.1", + "port": 6030, + "user": "root", + "password": "taosdata", + "thread_count": 4, + "thread_count_create_tbl": 4, + "result_file": "./insert_res.txt", + "confirm_parameter_prompt": "no", + "insert_interval": 0, + "num_of_records_per_req": 100, + "max_sql_len": 1024000, + "databases": [{ + "dbinfo": { + "name": "db", + "drop": "yes", + "replica": 1, + "days": 10, + "cache": 16, + "blocks": 8, + "precision": "ms", + "keep": 365, + "minRows": 100, + "maxRows": 4096, + "comp":2, + "walLevel":1, + "cachelast":0, + "quorum":1, + "fsync":3000, + "update": 0 + }, + "super_tables": [{ + "name": "stb", + "child_table_exists":"no", + "childtable_count": 100, + "childtable_prefix": "stb_", + "auto_create_table": "no", + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 1000, + "childtable_limit": 33, + "childtable_offset": 33, + "multi_thread_write_one_tbl": "no", + "number_of_tbl_in_one_sql": 0, + "rows_per_tbl": 100, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":10}, {"type": "BINARY", "len": 16, "count":3}, {"type": "BINARY", "len": 32, "count":6}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":5}] + }] + }] +} diff --git a/tests/pytest/tools/insert-tblimit-tboffset0.json b/tests/pytest/tools/insert-tblimit-tboffset0.json new file mode 100644 index 0000000000000000000000000000000000000000..302744cab933297d8455aed9c45210385f1ec56f --- /dev/null +++ b/tests/pytest/tools/insert-tblimit-tboffset0.json @@ -0,0 +1,60 @@ +{ + "filetype": "insert", + "cfgdir": "/etc/taos", + "host": "127.0.0.1", + "port": 6030, + "user": "root", + "password": "taosdata", + "thread_count": 4, + "thread_count_create_tbl": 4, + "result_file": "./insert_res.txt", + "confirm_parameter_prompt": "no", + "insert_interval": 0, + "num_of_records_per_req": 100, + "max_sql_len": 1024000, + "databases": [{ + "dbinfo": { + "name": "db", + "drop": "yes", + "replica": 1, + "days": 10, + "cache": 16, + "blocks": 8, + "precision": "ms", + "keep": 365, + "minRows": 100, + "maxRows": 4096, + "comp":2, + "walLevel":1, + "cachelast":0, + "quorum":1, + "fsync":3000, + "update": 0 + }, + "super_tables": [{ + "name": "stb", + "child_table_exists":"no", + "childtable_count": 100, + "childtable_prefix": "stb_", + "auto_create_table": "no", + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 1000, + "childtable_limit": 20, + "childtable_offset": 0, + "multi_thread_write_one_tbl": "no", + "number_of_tbl_in_one_sql": 0, + "rows_per_tbl": 100, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":10}, {"type": "BINARY", "len": 16, "count":3}, {"type": "BINARY", "len": 32, "count":6}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":5}] + }] + }] +} diff --git a/tests/pytest/tools/taosdemoTestLimitOffset.py b/tests/pytest/tools/taosdemoTestLimitOffset.py new file mode 100644 index 0000000000000000000000000000000000000000..6dbe5a70282975670b6b1813b429e5a07336c6db --- /dev/null +++ b/tests/pytest/tools/taosdemoTestLimitOffset.py @@ -0,0 +1,68 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +import os +from util.log import * +from util.cases import * +from util.sql import * +from util.dnodes import * + + +class TDTestCase: + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + self.numberOfTables = 10000 + self.numberOfRecords = 100 + + def getBuildPath(self): + selfPath = os.path.dirname(os.path.realpath(__file__)) + + if ("community" in selfPath): + projPath = selfPath[:selfPath.find("community")] + else: + projPath = selfPath[:selfPath.find("tests")] + + for root, dirs, files in os.walk(projPath): + if ("taosd" in files): + rootRealPath = os.path.dirname(os.path.realpath(root)) + if ("packaging" not in rootRealPath): + buildPath = root[:len(root)-len("/build/bin")] + break + return buildPath + + def run(self): + tdSql.prepare() + buildPath = self.getBuildPath() + if (buildPath == ""): + tdLog.exit("taosd not found!") + else: + tdLog.info("taosd found in %s" % buildPath) + binPath = buildPath+ "/build/bin/" + os.system("%staosdemo -f tools/insert-tblimit-tboffset.json" % binPath) + + tdSql.execute("use db") + tdSql.query("select count(tbname) from db.stb") + tdSql.checkData(0, 0, 100) + tdSql.query("select count(*) from db.stb") + tdSql.checkData(0, 0, 33000) + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) diff --git a/tests/pytest/tools/taosdemoTestWithoutMetric.py b/tests/pytest/tools/taosdemoTestWithoutMetric.py new file mode 100644 index 0000000000000000000000000000000000000000..647d6a37cb74e613950ccf72e9f2d99d85435c1a --- /dev/null +++ b/tests/pytest/tools/taosdemoTestWithoutMetric.py @@ -0,0 +1,72 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +import os +from util.log import * +from util.cases import * +from util.sql import * +from util.dnodes import * + + +class TDTestCase: + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + self.numberOfTables = 100 + self.numberOfRecords = 1000 + + def getBuildPath(self): + selfPath = os.path.dirname(os.path.realpath(__file__)) + + if ("community" in selfPath): + projPath = selfPath[:selfPath.find("community")] + else: + projPath = selfPath[:selfPath.find("tests")] + + for root, dirs, files in os.walk(projPath): + if ("taosd" in files): + rootRealPath = os.path.dirname(os.path.realpath(root)) + if ("packaging" not in rootRealPath): + buildPath = root[:len(root) - len("/build/bin")] + break + return buildPath + + def run(self): + buildPath = self.getBuildPath() + if (buildPath == ""): + tdLog.exit("taosd not found!") + else: + tdLog.info("taosd found in %s" % buildPath) + binPath = buildPath + "/build/bin/" + os.system("%staosdemo -y -t %d -n %d -x" % + (binPath, self.numberOfTables, self.numberOfRecords)) + + tdSql.query("show databases") + for i in range(18): + print(tdSql.getData(0, i) ) + tdSql.checkData(0, 2, self.numberOfTables) + + tdSql.execute("use test") + tdSql.query( + "select count(*) from test.t%d" % (self.numberOfTables -1)) + tdSql.checkData(0, 0, self.numberOfRecords) + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) diff --git a/tests/pytest/util/dnodes.py b/tests/pytest/util/dnodes.py index 91c75efe9762a18042660423708e6dd9475166dd..6eaf4e18af53eac2b6e3c93749528993d8477e3d 100644 --- a/tests/pytest/util/dnodes.py +++ b/tests/pytest/util/dnodes.py @@ -136,7 +136,8 @@ class TDDnode: "monitorDebugFlag":"135", "udebugFlag":"135", "jnidebugFlag":"135", - "qdebugFlag":"135" + "qdebugFlag":"135", + "maxSQLLength":"1048576" } def init(self, path): diff --git a/tests/script/general/cache/new_metrics.sim b/tests/script/general/cache/new_metrics.sim index 00cbe843f955c618f8dfca56d9633eb911038d5e..eb9b042483251856c2ac7b3f973ab31176a8ff56 100644 --- a/tests/script/general/cache/new_metrics.sim +++ b/tests/script/general/cache/new_metrics.sim @@ -94,8 +94,9 @@ while $i < 10 $i = $i + 1 endw -print ==> sleep 8 seconds to renew cache -sleep 8000 +print ==> sleep 1 seconds to renew cache +sql reset query cache +sleep 1000 print =============== step5 diff --git a/tests/script/general/cache/restart_metrics.sim b/tests/script/general/cache/restart_metrics.sim index 4f2dae5fd95eba03de931234f6a71204948cb78a..a1b2365b2aef739d7bdf97ba82b757648285fe97 100644 --- a/tests/script/general/cache/restart_metrics.sim +++ b/tests/script/general/cache/restart_metrics.sim @@ -55,7 +55,7 @@ system sh/cfg.sh -n dnode1 -c walLevel -v 1 system sh/exec.sh -n dnode1 -s start print =============== step3 -print ==> sleep 8 seconds to renew cache +print ==> sleep 1 seconds to renew cache sql reset query cache sleep 1000 diff --git a/tests/script/general/cache/restart_table.sim b/tests/script/general/cache/restart_table.sim index 9b0d8f9c5b483b79da41fb4e86c17902d328a89d..1f7d982a2848a5df443f32a8c2b5c1edb97874d2 100644 --- a/tests/script/general/cache/restart_table.sim +++ b/tests/script/general/cache/restart_table.sim @@ -39,10 +39,9 @@ system sh/cfg.sh -n dnode1 -c walLevel -v 1 system sh/exec.sh -n dnode1 -s start print =============== step3 -print ==> sleep 8 seconds to renew cache -sleep 2000 +print ==> sleep 1 seconds to renew cache sql reset query cache -sleep 18000 +sleep 1000 print =============== step4 sql create database $db diff --git a/tests/script/general/parser/col_arithmetic_operation.sim b/tests/script/general/parser/col_arithmetic_operation.sim index 7611bd582fc76d0e2c3fe2fe51bc2dcd74e7fbbe..8bb692e3bbe8af3ec9ed179ad29d40b4712d257b 100644 --- a/tests/script/general/parser/col_arithmetic_operation.sim +++ b/tests/script/general/parser/col_arithmetic_operation.sim @@ -93,6 +93,7 @@ $halfTbNum = $tbNum / 2 $nchar = 'nchar . $c $nchar = $nchar . ' + $ts = $ts + 1 sql insert into $tb5 values ( $ts , NULL , $c , NULL , $c , NULL , $c , NULL, NULL , $nchar ) $tb6 values ( $ts , NULL , $c , NULL , $c , NULL , $c , NULL, NULL , $nchar ) $tb7 values ( $ts , NULL , $c , NULL , $c , NULL , $c , NULL, NULL , $nchar ) $tb8 values ( $ts , NULL , $c , NULL , $c , NULL , $c , NULL, NULL , $nchar ) $tb9 values ( $ts , NULL , $c , NULL , $c , NULL , $c , NULL, NULL , $nchar ) $x = $x + 1 endw diff --git a/tests/script/general/parser/col_arithmetic_query.sim b/tests/script/general/parser/col_arithmetic_query.sim index 3f1a430e2bb5bab1112708a9925d74d27f030cc5..2c56c6445fa8134ab28837940a11ff6f4127b7c7 100644 --- a/tests/script/general/parser/col_arithmetic_query.sim +++ b/tests/script/general/parser/col_arithmetic_query.sim @@ -426,7 +426,7 @@ if $data02 != 9.000000020 then endi # all possible function in the arithmetic expression, add more -sql select min(c1) * max(c2) /4, sum(c1) * apercentile(c2, 20), apercentile(c4, 33) + 52/9, spread(c5)/min(c2), count(1)/sum(c1), avg(c2)*count(c2) from $stb where ts >= '2018-09-17 09:00:00.000' and ts <= '2018-11-25 19:30:00.000'; +sql select min(c1) * max(c2) /4, sum(c1) * apercentile(c2, 20), apercentile(c4, 33) + 52/9, spread(c5)/min(c2), count(1)/sum(c1), avg(c2)*count(c2) from $stb where ts >= '2018-09-17 09:00:00.000' and ts <= '2018-11-25 19:30:01.000'; if $rows != 1 then return -1 endi @@ -462,7 +462,7 @@ if $rows != 0 then endi # no result return [d.3] -sql select sum(c2) - avg(c2) from $stb where ts > '2018-11-25 19:30:00.000' +sql select sum(c2) - avg(c2) from $stb where ts > '2018-11-25 19:30:01.000' if $rows != 0 then return -1 endi @@ -520,35 +520,35 @@ if $data91 != 9 then endi # in group by column -sql select apercentile(c6, 50)-first(c6)+last(c5)*12, last(c5)*12 from ca_stb0 group by c2; -if $rows != 10 then - return -1 -endi - -if $data00 != 0.000000000 then - return -1 -endi - -if $data01 != 0.000000000 then - return -1 -endi - -if $data10 != 12.000000000 then - return -1 -endi - -if $data11 != 12.000000000 then - return -1 -endi - -if $data20 != 24.000000000 then - return -1 -endi - -if $data21 != 24.000000000 then - return -1 -endi - +#sql select apercentile(c6, 50)-first(c6)+last(c5)*12, last(c5)*12 from ca_stb0 group by c2; +#if $rows != 10 then +# return -1 +#endi +# +#if $data00 != 0.000000000 then +# return -1 +#endi +# +#if $data01 != 0.000000000 then +# return -1 +#endi +# +#if $data10 != 12.000000000 then +# return -1 +#endi +# +#if $data11 != 12.000000000 then +# return -1 +#endi +# +#if $data20 != 24.000000000 then +# return -1 +#endi +# +#if $data21 != 24.000000000 then +# return -1 +#endi +# sql_error select first(c6) - last(c6) *12 / count(*) from $stb group by c3; sql select first(c6) - last(c6) *12 / count(*) from $stb group by c5; diff --git a/tests/script/general/parser/fill.sim b/tests/script/general/parser/fill.sim index 7bf00145d619f2455a35686b9fd7424efbcc3f2a..d109dd50f7c0ba0684295dbf093ba1b280e04fce 100644 --- a/tests/script/general/parser/fill.sim +++ b/tests/script/general/parser/fill.sim @@ -313,6 +313,7 @@ if $rows != 9 then return -1 endi if $data01 != 0 then + print expect 0, actual:$data01 return -1 endi if $data11 != 6 then @@ -979,10 +980,6 @@ if $data00 != @20-01-01 01:01:00.000@ then return -1 endi -if $data00 != @20-01-01 01:01:00.000@ then - return -1 -endi -if $data1 if $data01 != 2.000000000 then return -1 endi diff --git a/tests/script/general/parser/function.sim b/tests/script/general/parser/function.sim index 8eb6aa55f2d06f4d90e14cd4035d2c460b6b1b00..9165b7e98e0510c3fead45acbc1baeceb0b653a3 100644 --- a/tests/script/general/parser/function.sim +++ b/tests/script/general/parser/function.sim @@ -775,8 +775,24 @@ if $rows != 1 then return -1 endi - if $data00 != 0.000000000 then return -1 endi +sql select count(tbname) from st1 +if $rows != 1 then + return -1 +endi + +if $data00 != 1 then + return -1 +endi + +sql select count(id) from st1 +if $rows != 1 then + return -1 +endi + +if $data00 != 1 then + return -1 +endi diff --git a/tests/script/general/parser/groupby.sim b/tests/script/general/parser/groupby.sim index a83c975be22e24684a50caccca4baa90bcae0b16..dd7331054c2ee6edd3ba07327057404dae7b3ed7 100644 --- a/tests/script/general/parser/groupby.sim +++ b/tests/script/general/parser/groupby.sim @@ -610,6 +610,11 @@ print =================>TD-2665 sql_error create table txx as select avg(c) as t from st; sql_error create table txx1 as select avg(c) as t from t1; +sql select stddev(c),stddev(c) from st group by c; +if $rows != 4 then + return -1 +endi + print =================>TD-2236 sql select first(ts),last(ts) from t1 group by c; if $rows != 4 then diff --git a/tests/script/general/parser/interp_test.sim b/tests/script/general/parser/interp_test.sim index 28601808306334286d2a9691626a2b1c1d211b30..81a77995fb828db00b65d085e0839e3b652385e0 100644 --- a/tests/script/general/parser/interp_test.sim +++ b/tests/script/general/parser/interp_test.sim @@ -579,7 +579,7 @@ $tb = $tbPrefix . 0 ## interp(*) from stb + group by + fill(none) $t = $ts0 + 1000 - sql select interp(*) from $stb where ts = $t fill(NULL) group by tbname +sql select interp(*) from $stb where ts = $t fill(NULL) group by tbname if $rows != $tbNum then return -1 endi diff --git a/tests/script/general/parser/join.sim b/tests/script/general/parser/join.sim index 392228e12122bcbcd0d336ec9dbf32a0cf9ef4c5..2c14a86c3a6532088d0dce584199c0d5ffbed6fd 100644 --- a/tests/script/general/parser/join.sim +++ b/tests/script/general/parser/join.sim @@ -347,6 +347,7 @@ $val = $rowNum + $rowNum print $val print $rows if $rows != $val then + print expect $val , actual:$rows return -1 endi @@ -447,7 +448,7 @@ endi sql select first(join_tb0.c8),first(join_tb0.c9) from join_tb1 , join_tb0 where join_tb1.ts = join_tb0.ts and join_tb1.ts <= 100002 and join_tb0.c7 = true #====================group by========================================= - +print =================>"group by not supported" diff --git a/tests/script/general/parser/join_manyblocks.sim b/tests/script/general/parser/join_manyblocks.sim new file mode 100644 index 0000000000000000000000000000000000000000..fddd59c0a148975fd1bb6d63dbfb01ab46baa376 --- /dev/null +++ b/tests/script/general/parser/join_manyblocks.sim @@ -0,0 +1,93 @@ +system sh/stop_dnodes.sh + +system sh/deploy.sh -n dnode1 -i 1 +system sh/cfg.sh -n dnode1 -c walLevel -v 1 +system sh/cfg.sh -n dnode1 -c maxtablespervnode -v 4 + +system sh/exec.sh -n dnode1 -s start +sql connect +sleep 100 + +$dbPrefix = join_m_db +$tbPrefix = join_tb +$mtPrefix = join_mt +$tbNum = 3 +$rowNum = 20000 +$totalNum = $tbNum * $rowNum + +print =============== join_manyBlocks.sim +$i = 0 +$db = $dbPrefix . $i +$mt = $mtPrefix . $i + +$tstart = 100000 + +sql drop database if exists $db -x step1 +step1: +sql create database if not exists $db keep 36500 + +sql use $db +sql create table $mt (ts timestamp, c1 int, c2 float, c3 bigint, c4 smallint, c5 tinyint, c6 double, c7 bool, c8 binary(10), c9 nchar(9)) TAGS(t1 int, t2 binary(12)) + +$mt1 = $mtPrefix . 1 . $i +sql create table $mt1 (ts timestamp, c1 int, c2 float, c3 bigint, c4 smallint, c5 tinyint, c6 double, c7 bool, c8 binary(10), c9 nchar(9)) TAGS(t1 int, t2 binary(12), t3 int) + +$i = 0 +$tbPrefix1 = join_1_tb + +$i = 0 +while $i < $tbNum + $tb = $tbPrefix . $i + $tg2 = ' . abc + $tg2 = $tg2 . ' + sql create table $tb using $mt tags( $i , $tg2 ) + + $tb1 = $tbPrefix1 . $i + $c = $i + $t3 = $i + 1 + + $binary = ' . abc + $binary = $binary . $i + $binary = $binary . ' + + print $binary + sql create table $tb1 using $mt1 tags( $i , $binary , $t3 ) + + $x = 0 + while $x < $rowNum + $ms = $x . m + $c = $x / 100 + $c = $c * 100 + $c = $x - $c + + $binary = ' . binary + $binary = $binary . $c + $binary = $binary . ' + + $nchar = ' . nchar + $nchar = $nchar . $c + $nchar = $nchar . ' + + sql insert into $tb values ($tstart , $c , $c , $c , $c , $c , $c , $c , $binary , $nchar ) $tb1 values ($tstart , $c , $c , $c , $c , $c , $c , $c , $binary , $nchar ) + + $tstart = $tstart + 1 + $x = $x + 1 + endw + + $i = $i + 1 + $tstart = 100000 +endw + +sleep 100 + +print ===============join_manyblocks.sim +print ==============> td-3313 +sql select join_mt0.ts,join_mt0.ts,join_mt0.t1 from join_mt0, join_mt1 where join_mt0.ts=join_mt1.ts and join_mt0.t1=join_mt1.t1; + +print $row +if $row != 60000 then + print expect 60000, actual: $row + return -1 +endi + +print ======= second tags join diff --git a/tests/script/general/parser/lastrow_query.sim b/tests/script/general/parser/lastrow_query.sim index a87b3cc646871e442cd3540fa163f1d5492c1456..3fd88cfc1bb4e651ae16801c249aa88f0a3797ba 100644 --- a/tests/script/general/parser/lastrow_query.sim +++ b/tests/script/general/parser/lastrow_query.sim @@ -16,6 +16,9 @@ $stb = $stbPrefix . $i sql use $db +print ========>TD-3231 last_row with group by column error +sql_error select last_row(c1) from $stb group by c1; + ##### select lastrow from STable with two vnodes, timestamp decreases from tables in vnode0 to tables in vnode1 sql select last_row(*) from $stb if $rows != 1 then @@ -224,4 +227,4 @@ sql create table tu(ts timestamp, k int) sql select last_row(*) from tu if $row != 0 then return -1 -endi \ No newline at end of file +endi diff --git a/tests/script/general/parser/limit1_stb.sim b/tests/script/general/parser/limit1_stb.sim index a0e3a45d2fc28002e2e64b195191b72659849a53..513e2fac026c0d1d617ba4126d22c01f82527ca0 100644 --- a/tests/script/general/parser/limit1_stb.sim +++ b/tests/script/general/parser/limit1_stb.sim @@ -538,6 +538,7 @@ $offset = $offset + 1 sql select max(c1), min(c2), avg(c3), count(c4), sum(c5), spread(c6), first(c7), last(c8), first(c9) from $stb where ts >= $ts0 and ts <= $tsu and t1 > 1 and t1 < 8 interval(5m) limit $offset offset $offset $val = $rowNum - $offset if $rows != $val then + print expect $val, actual:$rows return -1 endi if $data00 != @18-10-22 02:30:00.000@ then diff --git a/tests/script/general/parser/sliding.sim b/tests/script/general/parser/sliding.sim index be33c905a1786b8c56d21aa973e6eb1fb8698ad6..b2695ff95f27c5fe6ba27c9b977c96b48a5e5801 100644 --- a/tests/script/general/parser/sliding.sim +++ b/tests/script/general/parser/sliding.sim @@ -469,11 +469,12 @@ if $data25 != 33 then endi sql select count(*),stddev(c1),count(c1),first(c2),last(c3) from sliding_tb0 where ts>'2000-1-1 00:00:00' and ts<'2000-1-1 00:00:01.002' and c2 >= 0 interval(30s) sliding(10s) order by ts desc limit 1000; -if $row != 1 then +if $row != 3 then return -1 endi -if $data00 != @99-12-31 23:59:40.000@ then +if $data00 != @00-01-01 00:00:00.000@ then + print expect 00-01-01 00:00:00.000, actual: $data00 return -1 endi @@ -489,7 +490,28 @@ if $data03 != 33 then return -1 endi -print check boundary check crash at client side +if $data10 != @99-12-31 23:59:50.000@ then + return -1 +endi + +if $data11 != 33 then + return -1 +endi +if $data12 != 9.521904571 then + return -1 +endi + +if $data20 != @99-12-31 23:59:40.000@ then + return -1 +endi +if $data21 != 33 then + return -1 +endi +if $data22 != 9.521904571 then + return -1 +endi + +print ====================>check boundary check crash at client side sql select count(*) from sliding_mt0 where ts>now and ts < now-1h; print ========================query on super table diff --git a/tests/script/general/parser/testSuite.sim b/tests/script/general/parser/testSuite.sim index 255389a2df12e470890ca74e9b3872a28bc119c3..c2ce5df12e9e4d60824950f21ac8afb9d64b5235 100644 --- a/tests/script/general/parser/testSuite.sim +++ b/tests/script/general/parser/testSuite.sim @@ -107,3 +107,6 @@ sleep 100 run general/parser/function.sim sleep 100 run general/parser/stableOp.sim +sleep 100 +run general/parser/slimit_alter_tags.sim + diff --git a/tests/script/general/parser/topbot.sim b/tests/script/general/parser/topbot.sim index 3e73e4967be505d80ebf47809e63c89bc2846a04..80a122238ec70136daa444b78764f5881563451d 100644 --- a/tests/script/general/parser/topbot.sim +++ b/tests/script/general/parser/topbot.sim @@ -73,6 +73,60 @@ if $row != 100 then return -1 endi +sql select bottom(c3, 5) from tb_tb1 interval(1y); +if $rows != 5 then + return -1 +endi + +if $data01 != 0.00000 then + print expect 0.00000, actual:$data01 + return -1 +endi + +if $data11 != 0.00000 then + return -1 +endi + +if $data21 != 0.00000 then + return -1 +endi + +if $data31 != 0.00000 then + return -1 +endi + +sql select top(c4, 5) from tb_tb1 interval(1y); +if $rows != 5 then + return -1 +endi + +if $data01 != 9.000000000 then + print expect 9.000000000, acutal:$data01 + return -1 +endi + +if $data11 != 9.000000000 then + return -1 +endi + +if $data21 != 9.000000000 then + return -1 +endi + +if $data31 != 9.000000000 then + return -1 +endi + +sql select top(c3, 5) from tb_tb1 interval(40h) +if $rows != 25 then + return -1 +endi + +if $data01 != 9.00000 then + print expect 9.00000, actual:$data01 + return -1 +endi + sql select last(*) from tb_tb9 if $row != 1 then return -1 diff --git a/tests/script/general/stream/restart_stream.sim b/tests/script/general/stream/restart_stream.sim index 5da69075361da7c2fad3ed9fde7384493fac9519..c8be10103d16a856fb0c083546cfaf8b939a1b98 100644 --- a/tests/script/general/stream/restart_stream.sim +++ b/tests/script/general/stream/restart_stream.sim @@ -100,8 +100,9 @@ system sh/cfg.sh -n dnode1 -c walLevel -v 1 system sh/exec.sh -n dnode1 -s start print =============== step5 -print ==> sleep 8 seconds to renew cache -sleep 8000 +print ==> renew cache +sql reset query cache +sleep 1000 print =============== step6 diff --git a/tests/script/general/table/delete_writing.sim b/tests/script/general/table/delete_writing.sim index b7350f26a70bc18023cfff6497ffddc2d5b8ced8..5351d13d80ad732b2a58953f72b3c62086b1090b 100644 --- a/tests/script/general/table/delete_writing.sim +++ b/tests/script/general/table/delete_writing.sim @@ -41,7 +41,7 @@ while $x < 15 sql create table db.tb (ts timestamp, i int) - sleep 2000 + sleep 1000 $x = $x + 1 endw diff --git a/tests/script/jenkins/basic_1.txt b/tests/script/jenkins/basic_1.txt index 17245767344e50389d10bc997be33abb78c289ef..ae72e76b2f6c444e84fdaee3818200983796ace3 100644 --- a/tests/script/jenkins/basic_1.txt +++ b/tests/script/jenkins/basic_1.txt @@ -22,8 +22,6 @@ ./test.sh -f general/http/grafana_bug.sim ./test.sh -f general/http/grafana.sim - - ./test.sh -f general/insert/basic.sim ./test.sh -f general/insert/insert_drop.sim ./test.sh -f general/insert/query_block1_memory.sim diff --git a/tests/test-all.sh b/tests/test-all.sh index 4f7afe7d17bc57d6f62a5d68cac3277914dedb50..db9d6523a0eced09407f958e0adee9a4a8087bf1 100755 --- a/tests/test-all.sh +++ b/tests/test-all.sh @@ -316,6 +316,7 @@ if [ "$2" != "sim" ] && [ "$2" != "python" ] && [ "$2" != "unit" ] && [ "$1" == cd debug/ stopTaosd + rm -rf /var/lib/taos/* nohup build/bin/taosd -c /etc/taos/ > /dev/null 2>&1 & sleep 30 @@ -358,6 +359,7 @@ if [ "$2" != "sim" ] && [ "$2" != "python" ] && [ "$2" != "jdbc" ] && [ "$1" == pwd cd debug/build/bin + rm -rf /var/lib/taos/* nohup ./taosd -c /etc/taos/ > /dev/null 2>&1 & sleep 30