From 0170c3bb7770bc128c1e3552708add2ec462812f Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Thu, 4 Nov 2021 18:07:45 +0800 Subject: [PATCH] add json tag support for group by --- src/client/src/tscSQLParser.c | 46 ++++++++++++++++++++++++----------- src/query/src/qExtbuffer.c | 3 ++- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 6e8efbba62..d78b4b8093 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -5922,15 +5922,25 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq return invalidOperationMsg(pMsgBuf, msg10); } - // handle the first part of order by - tVariant* pVar = &((CommonItem*)taosArrayGet(pSortOrder, 0))->pVar; + SStrToken columnName = {0}; + CommonItem* pItem = taosArrayGet(pSortOrder, 0); + if (pItem->isJsonExp){ + assert(pItem->jsonExp->tokenId == TK_ARROW); + columnName = pItem->jsonExp->pLeft->columnName; + }else{ + // handle the first part of order by + tVariant* pVar = &pItem->pVar; - // e.g., order by 1 asc, return directly with out further check. - if (pVar->nType >= TSDB_DATA_TYPE_TINYINT && pVar->nType <= TSDB_DATA_TYPE_BIGINT) { - return TSDB_CODE_SUCCESS; + // e.g., order by 1 asc, return directly with out further check. + if (pVar->nType >= TSDB_DATA_TYPE_TINYINT && pVar->nType <= TSDB_DATA_TYPE_BIGINT) { + return TSDB_CODE_SUCCESS; + } + + columnName.n = pVar->nLen; + columnName.type = pVar->nType; + columnName.z = pVar->pz; } - SStrToken columnName = {pVar->nLen, pVar->nType, pVar->pz}; SColumnIndex index = COLUMN_INDEX_INITIALIZER; bool udf = false; @@ -6048,7 +6058,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq } } } else { - CommonItem *pItem = taosArrayGet(pSqlNode->pSortOrder, 0); + pItem = taosArrayGet(pSqlNode->pSortOrder, 0); if (orderByTags) { pQueryInfo->groupbyExpr.orderIndex = index.columnIndex - tscGetNumOfColumns(pTableMetaInfo->pTableMeta); pQueryInfo->groupbyExpr.orderType = pItem->sortOrder; @@ -6066,9 +6076,18 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq } } + SStrToken cname = {0}; pItem = taosArrayGet(pSqlNode->pSortOrder, 1); - tVariant* pVar2 = &pItem->pVar; - SStrToken cname = {pVar2->nLen, pVar2->nType, pVar2->pz}; + if (pItem->isJsonExp){ + assert(pItem->jsonExp->tokenId == TK_ARROW); + cname = pItem->jsonExp->pLeft->columnName; + }else{ + tVariant* pVar = &pItem->pVar; + + cname.n = pVar->nLen; + cname.type = pVar->nType; + cname.z = pVar->pz; + } if (getColumnIndexByName(&cname, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd)) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(pMsgBuf, msg1); } @@ -6076,8 +6095,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq if (index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX) { return invalidOperationMsg(pMsgBuf, msg6); } else { - CommonItem* p1 = taosArrayGet(pSortOrder, 1); - pQueryInfo->order.order = p1->sortOrder; + pQueryInfo->order.order = pItem->sortOrder; pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; } } @@ -6133,7 +6151,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq } } - CommonItem* pItem = taosArrayGet(pSqlNode->pSortOrder, 0); + pItem = taosArrayGet(pSqlNode->pSortOrder, 0); pQueryInfo->order.order = pItem->sortOrder; pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; @@ -6144,7 +6162,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq return invalidOperationMsg(pMsgBuf, msg11); } - CommonItem* pItem = taosArrayGet(pSqlNode->pSortOrder, 0); + pItem = taosArrayGet(pSqlNode->pSortOrder, 0); pQueryInfo->order.order = pItem->sortOrder; pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; } else { @@ -6160,7 +6178,7 @@ int32_t validateOrderbyNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode* pSq return invalidOperationMsg(pMsgBuf, msg11); } - CommonItem* pItem = taosArrayGet(pSqlNode->pSortOrder, 0); + pItem = taosArrayGet(pSqlNode->pSortOrder, 0); pQueryInfo->order.order = pItem->sortOrder; pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; } diff --git a/src/query/src/qExtbuffer.c b/src/query/src/qExtbuffer.c index 5994099a0d..85ac9b72aa 100644 --- a/src/query/src/qExtbuffer.c +++ b/src/query/src/qExtbuffer.c @@ -391,7 +391,8 @@ int32_t columnValueAscendingComparator(char *f1, char *f2, int32_t type, int32_t } }; - case TSDB_DATA_TYPE_NCHAR: { // todo handle the var string compare + case TSDB_DATA_TYPE_NCHAR: + case TSDB_DATA_TYPE_JSON: { // todo handle the var string compare int32_t len1 = varDataLen(f1); int32_t len2 = varDataLen(f2); -- GitLab