提交 2720f943 编写于 作者: wmmhello's avatar wmmhello

optimize order by logic

...@@ -5937,10 +5937,17 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq ...@@ -5937,10 +5937,17 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq
* for table query, there is only one or none order option is allowed, which is the * for table query, there is only one or none order option is allowed, which is the
* ts or values(top/bottom) order is supported. * ts or values(top/bottom) order is supported.
* *
* for super table query, the order option must be less than 3. * for super table query, the order option must be less than 3 and the second must be ts.
*
* order by has 5 situations
* 1. from stable group by tag1 order by tag1 [ts]
* 2. from stable group by tbname order by tbname [ts]
* 3. from stable/table group by column1 order by column1
* 4. from stable/table order by ts
* 5. select stable/table top(column2,1) ... order by column2
*/ */
size_t size = taosArrayGetSize(pSortOrder); size_t size = taosArrayGetSize(pSortOrder);
if (UTIL_TABLE_IS_NORMAL_TABLE(pTableMetaInfo) || UTIL_TABLE_IS_TMP_TABLE(pTableMetaInfo)) { if (!UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) {
if (size > 1) { if (size > 1) {
return invalidOperationMsg(pMsgBuf, msg0); return invalidOperationMsg(pMsgBuf, msg0);
} }
...@@ -5950,7 +5957,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq ...@@ -5950,7 +5957,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq
} }
} }
if (size > 0 && pQueryInfo->distinct) { if (size > 0 && pQueryInfo->distinct) {
return invalidOperationMsg(pMsgBuf, msg10); return invalidOperationMsg(pMsgBuf, msg10);
} }
SStrToken columnName = {0}; SStrToken columnName = {0};
...@@ -5962,9 +5969,8 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq ...@@ -5962,9 +5969,8 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq
// handle the first part of order by // handle the first part of order by
tVariant* pVar = &pItem->pVar; tVariant* pVar = &pItem->pVar;
// e.g., order by 1 asc, return directly with out further check. if (pVar->nType != TSDB_DATA_TYPE_BINARY){
if (pVar->nType >= TSDB_DATA_TYPE_TINYINT && pVar->nType <= TSDB_DATA_TYPE_BIGINT) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1);
return TSDB_CODE_SUCCESS;
} }
columnName.n = pVar->nLen; columnName.n = pVar->nLen;
...@@ -5977,7 +5983,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq ...@@ -5977,7 +5983,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq
if (pQueryInfo->pUdfInfo && taosArrayGetSize(pQueryInfo->pUdfInfo) > 0) { if (pQueryInfo->pUdfInfo && taosArrayGetSize(pQueryInfo->pUdfInfo) > 0) {
int32_t usize = (int32_t)taosArrayGetSize(pQueryInfo->pUdfInfo); int32_t usize = (int32_t)taosArrayGetSize(pQueryInfo->pUdfInfo);
for (int32_t i = 0; i < usize; ++i) { for (int32_t i = 0; i < usize; ++i) {
SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, i); SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, i);
if (pUdfInfo->funcType == TSDB_UDF_TYPE_SCALAR) { if (pUdfInfo->funcType == TSDB_UDF_TYPE_SCALAR) {
...@@ -5996,9 +6002,9 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq ...@@ -5996,9 +6002,9 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq
bool orderByTS = false; bool orderByTS = false;
bool orderByGroupbyCol = false; bool orderByGroupbyCol = false;
if (index.columnIndex >= tscGetNumOfColumns(pTableMetaInfo->pTableMeta)) { if (index.columnIndex >= tscGetNumOfColumns(pTableMetaInfo->pTableMeta)) { // order by tag1
int32_t relTagIndex = index.columnIndex - tscGetNumOfColumns(pTableMetaInfo->pTableMeta); int32_t relTagIndex = index.columnIndex - tscGetNumOfColumns(pTableMetaInfo->pTableMeta);
// it is a tag column // it is a tag column
if (pQueryInfo->groupbyExpr.columnInfo == NULL) { if (pQueryInfo->groupbyExpr.columnInfo == NULL) {
return invalidOperationMsg(pMsgBuf, msg4); return invalidOperationMsg(pMsgBuf, msg4);
...@@ -6007,26 +6013,29 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq ...@@ -6007,26 +6013,29 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq
if (relTagIndex == pColIndex->colIndex) { if (relTagIndex == pColIndex->colIndex) {
orderByTags = true; orderByTags = true;
} }
} else if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { } else if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { // order by tbname
orderByTags = true; // it is a tag column
} if (pQueryInfo->groupbyExpr.columnInfo == NULL) {
return invalidOperationMsg(pMsgBuf, msg4);
if (PRIMARYKEY_TIMESTAMP_COL_INDEX == index.columnIndex) { }
SColIndex* pColIndex = taosArrayGet(pQueryInfo->groupbyExpr.columnInfo, 0);
if (TSDB_TBNAME_COLUMN_INDEX == pColIndex->colIndex) {
orderByTags = true;
}
}else if (index.columnIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX) { // order by ts
orderByTS = true; orderByTS = true;
} }else{ // order by normal column
SArray *columnInfo = pQueryInfo->groupbyExpr.columnInfo;
SArray *columnInfo = pQueryInfo->groupbyExpr.columnInfo; if (columnInfo != NULL && taosArrayGetSize(columnInfo) > 0) {
if (columnInfo != NULL && taosArrayGetSize(columnInfo) > 0) { SColIndex* pColIndex = taosArrayGet(columnInfo, 0);
SColIndex* pColIndex = taosArrayGet(columnInfo, 0); if (pColIndex->colIndex == index.columnIndex) {
if (PRIMARYKEY_TIMESTAMP_COL_INDEX != index.columnIndex && pColIndex->colIndex == index.columnIndex) { orderByGroupbyCol = true;
orderByGroupbyCol = true; }
} }
} }
if (!(orderByTags || orderByTS || orderByGroupbyCol) && !isTopBottomQuery(pQueryInfo)) { if (!(orderByTags || orderByTS || orderByGroupbyCol) && !isTopBottomQuery(pQueryInfo)) {
return invalidOperationMsg(pMsgBuf, msg3); return invalidOperationMsg(pMsgBuf, msg3);
} else { // order by top/bottom result value column is not supported in case of interval query.
assert(!(orderByTags && orderByTS && orderByGroupbyCol));
} }
size_t s = taosArrayGetSize(pSortOrder); size_t s = taosArrayGetSize(pSortOrder);
......
...@@ -97,7 +97,7 @@ class TDTestCase: ...@@ -97,7 +97,7 @@ class TDTestCase:
self.checkColumnSorted(0, "desc") self.checkColumnSorted(0, "desc")
print("======= step 2: verify order for special column =========") print("======= step 2: verify order for special column =========")
tdSql.query("select tbcol1 from st order by ts desc") tdSql.query("select tbcol1 from st order by ts desc")
tdSql.query("select tbcol6 from st order by ts desc") tdSql.query("select tbcol6 from st order by ts desc")
...@@ -122,6 +122,26 @@ class TDTestCase: ...@@ -122,6 +122,26 @@ class TDTestCase:
(i, i)) (i, i))
self.checkColumnSorted(1, "desc") self.checkColumnSorted(1, "desc")
tdSql.error("select tbcol1 from st order by 123")
tdSql.error("select tbcol1 from st order by tbname")
tdSql.query("select avg(tbcol1) from st group by tbname order by tbname")
tdSql.checkData(1, 0, 5.5)
tdSql.checkData(5, 1, "st6")
tdSql.query("select top(tbcol1, 2) from st group by tbname order by tbname")
tdSql.checkData(1, 1, 10)
tdSql.checkData(2, 2, "st2")
tdSql.query("select top(tbcol1, 12) from st order by tbcol1")
tdSql.checkData(1, 1, 9)
tdSql.error("select top(tbcol1, 12) from st1 order by tbcol1,ts")
tdSql.error("select top(tbcol1, 12),tbname from st order by tbcol1,tbname")
tdSql.query("select top(tbcol1, 12) from st group by tbname order by tbname desc")
tdSql.checkData(1, 2, "st10")
tdSql.checkData(10, 2, "st9")
def stop(self): def stop(self):
tdSql.close() tdSql.close()
tdLog.success("%s successfully executed" % __file__) tdLog.success("%s successfully executed" % __file__)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册