diff --git a/source/libs/nodes/src/nodesCodeFuncs.c b/source/libs/nodes/src/nodesCodeFuncs.c index 5d5e95cd2b3dda9f99df1ded071cfd1f8ca6bf4d..294488a38d01506939af876f3ca6e9f86bde1f65 100644 --- a/source/libs/nodes/src/nodesCodeFuncs.c +++ b/source/libs/nodes/src/nodesCodeFuncs.c @@ -543,11 +543,40 @@ static int32_t jsonToLogicPlanNode(const SJson* pJson, void* pObj) { static const char* jkScanLogicPlanScanCols = "ScanCols"; static const char* jkScanLogicPlanScanPseudoCols = "ScanPseudoCols"; -static const char* jkScanLogicPlanTableId = "TableId"; static const char* jkScanLogicPlanTableType = "TableType"; +static const char* jkScanLogicPlanTableId = "TableId"; +static const char* jkScanLogicPlanStableId = "StableId"; +static const char* jkScanLogicPlanScanCount = "ScanCount"; +static const char* jkScanLogicPlanReverseScanCount = "ReverseScanCount"; static const char* jkScanLogicPlanTagCond = "TagCond"; static const char* jkScanLogicPlanGroupTags = "GroupTags"; +// typedef struct SScanLogicNode { +// uint64_t stableId; +// SVgroupsInfo* pVgroupList; +// EScanType scanType; +// uint8_t scanSeq[2]; // first is scan count, and second is reverse scan count +// STimeWindow scanRange; +// SName tableName; +// bool showRewrite; +// double ratio; +// SNodeList* pDynamicScanFuncs; +// int32_t dataRequired; +// int64_t interval; +// int64_t offset; +// int64_t sliding; +// int8_t intervalUnit; +// int8_t slidingUnit; +// SNode* pTagCond; +// SNode* pTagIndexCond; +// int8_t triggerType; +// int64_t watermark; +// int8_t igExpired; +// SArray* pSmaIndexes; +// SNodeList* pGroupTags; +// bool groupSort; +// } SScanLogicNode; + static int32_t logicScanNodeToJson(const void* pObj, SJson* pJson) { const SScanLogicNode* pNode = (const SScanLogicNode*)pObj; @@ -558,11 +587,20 @@ static int32_t logicScanNodeToJson(const void* pObj, SJson* pJson) { if (TSDB_CODE_SUCCESS == code) { code = nodeListToJson(pJson, jkScanLogicPlanScanPseudoCols, pNode->pScanPseudoCols); } + if (TSDB_CODE_SUCCESS == code) { + code = tjsonAddIntegerToObject(pJson, jkScanLogicPlanTableType, pNode->tableType); + } if (TSDB_CODE_SUCCESS == code) { code = tjsonAddIntegerToObject(pJson, jkScanLogicPlanTableId, pNode->tableId); } if (TSDB_CODE_SUCCESS == code) { - code = tjsonAddIntegerToObject(pJson, jkScanLogicPlanTableType, pNode->tableType); + code = tjsonAddIntegerToObject(pJson, jkScanLogicPlanStableId, pNode->stableId); + } + if (TSDB_CODE_SUCCESS == code) { + code = tjsonAddIntegerToObject(pJson, jkScanLogicPlanScanCount, pNode->scanSeq[0]); + } + if (TSDB_CODE_SUCCESS == code) { + code = tjsonAddIntegerToObject(pJson, jkScanLogicPlanReverseScanCount, pNode->scanSeq[1]); } if (TSDB_CODE_SUCCESS == code) { code = tjsonAddObject(pJson, jkScanLogicPlanTagCond, nodeToJson, pNode->pTagCond); @@ -585,11 +623,20 @@ static int32_t jsonToLogicScanNode(const SJson* pJson, void* pObj) { if (TSDB_CODE_SUCCESS == code) { code = jsonToNodeList(pJson, jkScanLogicPlanScanPseudoCols, &pNode->pScanPseudoCols); } + if (TSDB_CODE_SUCCESS == code) { + code = tjsonGetTinyIntValue(pJson, jkScanLogicPlanTableType, &pNode->tableType); + } if (TSDB_CODE_SUCCESS == code) { code = tjsonGetUBigIntValue(pJson, jkScanLogicPlanTableId, &pNode->tableId); } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetTinyIntValue(pJson, jkScanLogicPlanTableType, &pNode->tableType); + code = tjsonGetUBigIntValue(pJson, jkScanLogicPlanStableId, &pNode->stableId); + } + if (TSDB_CODE_SUCCESS == code) { + code = tjsonGetUTinyIntValue(pJson, jkScanLogicPlanScanCount, &pNode->scanSeq[0]); + } + if (TSDB_CODE_SUCCESS == code) { + code = tjsonGetUTinyIntValue(pJson, jkScanLogicPlanReverseScanCount, &pNode->scanSeq[1]); } if (TSDB_CODE_SUCCESS == code) { code = jsonToNodeObject(pJson, jkScanLogicPlanTagCond, &pNode->pTagCond); diff --git a/source/libs/parser/src/parInsert.c b/source/libs/parser/src/parInsert.c index a5cf755a74107f586f4d34c9b586de2208c6fb94..2494c5a8a7405875a15e09698924a2234b6f142c 100644 --- a/source/libs/parser/src/parInsert.c +++ b/source/libs/parser/src/parInsert.c @@ -120,86 +120,6 @@ static int32_t skipInsertInto(char** pSql, SMsgBuf* pMsg) { return TSDB_CODE_SUCCESS; } -static int32_t parserValidateIdToken(SToken* pToken) { - if (pToken == NULL || pToken->z == NULL || pToken->type != TK_NK_ID) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // it is a token quoted with escape char '`' - if (pToken->z[0] == TS_ESCAPE_CHAR && pToken->z[pToken->n - 1] == TS_ESCAPE_CHAR) { - return TSDB_CODE_SUCCESS; - } - - char* sep = strnchr(pToken->z, TS_PATH_DELIMITER[0], pToken->n, true); - if (sep == NULL) { // It is a single part token, not a complex type - if (isNumber(pToken)) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - strntolower(pToken->z, pToken->z, pToken->n); - } else { // two part - int32_t oldLen = pToken->n; - char* pStr = pToken->z; - - if (pToken->type == TK_NK_SPACE) { - pToken->n = (uint32_t)strtrim(pToken->z); - } - - pToken->n = tGetToken(pToken->z, &pToken->type); - if (pToken->z[pToken->n] != TS_PATH_DELIMITER[0]) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - if (pToken->type != TK_NK_ID) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - int32_t firstPartLen = pToken->n; - - pToken->z = sep + 1; - pToken->n = (uint32_t)(oldLen - (sep - pStr) - 1); - int32_t len = tGetToken(pToken->z, &pToken->type); - if (len != pToken->n || pToken->type != TK_NK_ID) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } - - // re-build the whole name string - if (pStr[firstPartLen] == TS_PATH_DELIMITER[0]) { - // first part do not have quote do nothing - } else { - pStr[firstPartLen] = TS_PATH_DELIMITER[0]; - memmove(&pStr[firstPartLen + 1], pToken->z, pToken->n); - uint32_t offset = (uint32_t)(pToken->z - (pStr + firstPartLen + 1)); - memset(pToken->z + pToken->n - offset, ' ', offset); - } - - pToken->n += (firstPartLen + sizeof(TS_PATH_DELIMITER[0])); - pToken->z = pStr; - - strntolower(pToken->z, pToken->z, pToken->n); - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t buildName(SInsertParseContext* pCxt, SToken* pStname, char* fullDbName, char* tableName) { - if (parserValidateIdToken(pStname) != TSDB_CODE_SUCCESS) { - return buildSyntaxErrMsg(&pCxt->msg, "invalid table name", pStname->z); - } - - char* p = strnchr(pStname->z, TS_PATH_DELIMITER[0], pStname->n, false); - if (NULL != p) { // db.table - int32_t n = sprintf(fullDbName, "%d.", pCxt->pComCxt->acctId); - strncpy(fullDbName + n, pStname->z, p - pStname->z); - strncpy(tableName, p + 1, pStname->n - (p - pStname->z) - 1); - } else { - snprintf(fullDbName, TSDB_DB_FNAME_LEN, "%d.%s", pCxt->pComCxt->acctId, pCxt->pComCxt->db); - strncpy(tableName, pStname->z, pStname->n); - } - - return TSDB_CODE_SUCCESS; -} - static int32_t createSName(SName* pName, SToken* pTableName, int32_t acctId, const char* dbName, SMsgBuf* pMsgBuf) { const char* msg1 = "name too long"; const char* msg2 = "invalid database name"; @@ -978,11 +898,11 @@ static int32_t parseTagToken(char** end, SToken* pToken, SSchema* pSchema, int16 case TSDB_DATA_TYPE_NCHAR: { int32_t output = 0; - void* p = taosMemoryCalloc(1, pToken->n * TSDB_NCHAR_SIZE); + void* p = taosMemoryCalloc(1, pSchema->bytes - VARSTR_HEADER_SIZE); if (p == NULL) { return TSDB_CODE_OUT_OF_MEMORY; } - if (!taosMbsToUcs4(pToken->z, pToken->n, (TdUcs4*)(p), pToken->n * TSDB_NCHAR_SIZE, &output)) { + if (!taosMbsToUcs4(pToken->z, pToken->n, (TdUcs4*)(p), pSchema->bytes - VARSTR_HEADER_SIZE, &output)) { if (errno == E2BIG) { taosMemoryFree(p); return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name); diff --git a/source/libs/parser/src/parser.c b/source/libs/parser/src/parser.c index b36adbe5d4b44c70bab137be83b84c63945ce0e9..218b5d9f4bedabe015804915e37defb5a8fed5ff 100644 --- a/source/libs/parser/src/parser.c +++ b/source/libs/parser/src/parser.c @@ -38,6 +38,8 @@ bool qIsInsertValuesSql(const char* pStr, size_t length) { t = tStrGetToken((char*)pStr, &index, false); if (TK_USING == t.type || TK_VALUES == t.type) { return true; + } else if (TK_SELECT == t.type) { + return false; } if (0 == t.type) { break; diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c index fb3c420a19f9621eba2ada9b2ca051de856b079b..d4c470be3c5b88a30b3805ded02e991d6c48bb7e 100644 --- a/source/libs/planner/src/planOptimizer.c +++ b/source/libs/planner/src/planOptimizer.c @@ -1010,8 +1010,13 @@ static int32_t sortPriKeyOptApply(SOptimizeContext* pCxt, SLogicSubplan* pLogicS SNodeList* pScanNodes) { EOrder order = sortPriKeyOptGetPriKeyOrder(pSort); if (ORDER_DESC == order) { - SNode* pScan = NULL; - FOREACH(pScan, pScanNodes) { TSWAP(((SScanLogicNode*)pScan)->scanSeq[0], ((SScanLogicNode*)pScan)->scanSeq[1]); } + SNode* pScanNode = NULL; + FOREACH(pScanNode, pScanNodes) { + SScanLogicNode* pScan = (SScanLogicNode*)pScanNode; + if (pScan->scanSeq[0] > 0) { + TSWAP(pScan->scanSeq[0], pScan->scanSeq[1]); + } + } } int32_t code = @@ -1020,6 +1025,7 @@ static int32_t sortPriKeyOptApply(SOptimizeContext* pCxt, SLogicSubplan* pLogicS NODES_CLEAR_LIST(pSort->node.pChildren); nodesDestroyNode((SNode*)pSort); } + pCxt->optimized = true; return code; } diff --git a/source/libs/planner/src/planSpliter.c b/source/libs/planner/src/planSpliter.c index 2137108386f0c55fc37293ae4a97e5c5de51d865..d7eccf4b8ef252e3ff0c92a378bdc61f23449afe 100644 --- a/source/libs/planner/src/planSpliter.c +++ b/source/libs/planner/src/planSpliter.c @@ -945,6 +945,7 @@ static int32_t stbSplSplitPartitionNode(SSplitContext* pCxt, SStableSplitInfo* p code = nodesListMakeStrictAppend(&pInfo->pSubplan->pChildren, (SNode*)splCreateScanSubplan(pCxt, pInfo->pSplitNode, SPLIT_FLAG_STABLE_SPLIT)); } + pInfo->pSubplan->subplanType = SUBPLAN_TYPE_MERGE; ++(pCxt->groupId); return code; } diff --git a/source/libs/planner/test/planOrderByTest.cpp b/source/libs/planner/test/planOrderByTest.cpp index e542f4772f9f209a31207f1cf948409a28df7b0f..13dfbad78cfc1f2957bd48ef1e163a9c887881e0 100644 --- a/source/libs/planner/test/planOrderByTest.cpp +++ b/source/libs/planner/test/planOrderByTest.cpp @@ -53,6 +53,12 @@ TEST_F(PlanOrderByTest, withGroupBy) { run("SELECT SUM(c1) AS a FROM t1 GROUP BY c2 ORDER BY a"); } +TEST_F(PlanOrderByTest, withSubquery) { + useDb("root", "test"); + + run("SELECT ts FROM (SELECT * FROM t1 ORDER BY ts DESC) ORDER BY ts DESC"); +} + TEST_F(PlanOrderByTest, stable) { useDb("root", "test"); diff --git a/source/libs/planner/test/planPartByTest.cpp b/source/libs/planner/test/planPartByTest.cpp index 3b71ae3b8880b1f4c3d06f8474d133729f205b6a..48a4c12577a97d76c63e3892f55d0b1f435a317f 100644 --- a/source/libs/planner/test/planPartByTest.cpp +++ b/source/libs/planner/test/planPartByTest.cpp @@ -37,6 +37,8 @@ TEST_F(PlanPartitionByTest, withAggFunc) { run("select count(*) from st1 partition by c1"); + run("select sample(c1, 2) from st1 partition by c1"); + run("select count(*), c1 from t1 partition by c1"); }