fix(query)[TS-5443]. Fix invalid key in ORDER BY within subqueries

Resolved an issue where an ORDER BY alias in subqueries failed to
convert to the expected ColumnNode when the corresponding column
could be omitted. Updated the conversion logic to handle this case
correctly and ensure proper query execution.
This commit is contained in:
Jinqing Kuang 2024-09-20 11:52:43 +08:00
parent a49c7a84eb
commit 1ccaaf75bd
5 changed files with 69 additions and 7 deletions

View File

@ -57,7 +57,6 @@ typedef struct SExprNode {
char aliasName[TSDB_COL_NAME_LEN]; char aliasName[TSDB_COL_NAME_LEN];
char userAlias[TSDB_COL_NAME_LEN]; char userAlias[TSDB_COL_NAME_LEN];
SArray* pAssociation; SArray* pAssociation;
bool orderAlias;
bool asAlias; bool asAlias;
bool asParam; bool asParam;
bool asPosition; bool asPosition;

View File

@ -102,7 +102,6 @@ static int32_t exprNodeCopy(const SExprNode* pSrc, SExprNode* pDst) {
COPY_OBJECT_FIELD(resType, sizeof(SDataType)); COPY_OBJECT_FIELD(resType, sizeof(SDataType));
COPY_CHAR_ARRAY_FIELD(aliasName); COPY_CHAR_ARRAY_FIELD(aliasName);
COPY_CHAR_ARRAY_FIELD(userAlias); COPY_CHAR_ARRAY_FIELD(userAlias);
COPY_SCALAR_FIELD(orderAlias);
COPY_SCALAR_FIELD(projIdx); COPY_SCALAR_FIELD(projIdx);
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }

View File

@ -2443,8 +2443,7 @@ typedef struct SCollectFuncsCxt {
static EDealRes collectFuncs(SNode* pNode, void* pContext) { static EDealRes collectFuncs(SNode* pNode, void* pContext) {
SCollectFuncsCxt* pCxt = (SCollectFuncsCxt*)pContext; SCollectFuncsCxt* pCxt = (SCollectFuncsCxt*)pContext;
if (QUERY_NODE_FUNCTION == nodeType(pNode) && pCxt->classifier(((SFunctionNode*)pNode)->funcId) && if (QUERY_NODE_FUNCTION == nodeType(pNode) && pCxt->classifier(((SFunctionNode*)pNode)->funcId)) {
!(((SExprNode*)pNode)->orderAlias)) {
SFunctionNode* pFunc = (SFunctionNode*)pNode; SFunctionNode* pFunc = (SFunctionNode*)pNode;
if (FUNCTION_TYPE_TBNAME == pFunc->funcType && pCxt->tableAlias) { if (FUNCTION_TYPE_TBNAME == pFunc->funcType && pCxt->tableAlias) {
SValueNode* pVal = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 0); SValueNode* pVal = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 0);

View File

@ -6644,7 +6644,6 @@ static EDealRes replaceOrderByAliasImpl(SNode** pNode, void* pContext) {
pCxt->pTranslateCxt->errCode = code; pCxt->pTranslateCxt->errCode = code;
return DEAL_RES_ERROR; return DEAL_RES_ERROR;
} }
((SExprNode*)pNew)->orderAlias = true;
nodesDestroyNode(*pNode); nodesDestroyNode(*pNode);
*pNode = pNew; *pNode = pNew;
return DEAL_RES_CONTINUE; return DEAL_RES_CONTINUE;
@ -6668,7 +6667,6 @@ static EDealRes replaceOrderByAliasImpl(SNode** pNode, void* pContext) {
pCxt->pTranslateCxt->errCode = code; pCxt->pTranslateCxt->errCode = code;
return DEAL_RES_ERROR; return DEAL_RES_ERROR;
} }
((SExprNode*)pNew)->orderAlias = true;
((SOrderByExprNode*)*pNode)->pExpr = pNew; ((SOrderByExprNode*)*pNode)->pExpr = pNew;
nodesDestroyNode(pExpr); nodesDestroyNode(pExpr);
return DEAL_RES_CONTINUE; return DEAL_RES_CONTINUE;
@ -7055,7 +7053,6 @@ static int32_t addOrderByPrimaryKeyToQueryImpl(STranslateContext* pCxt, SNode* p
nodesDestroyNode((SNode*)pOrderByExpr); nodesDestroyNode((SNode*)pOrderByExpr);
return code; return code;
} }
((SExprNode*)pOrderByExpr->pExpr)->orderAlias = true;
// NODES_DESTORY_LIST(*pOrderByList); // NODES_DESTORY_LIST(*pOrderByList);
return nodesListMakeStrictAppend(pOrderByList, (SNode*)pOrderByExpr); return nodesListMakeStrictAppend(pOrderByList, (SNode*)pOrderByExpr);
} }

View File

@ -78,6 +78,72 @@ class TDTestCase(TBase):
rows = [row1, row2, row3, row4] rows = [row1, row2, row3, row4]
tdSql.checkDataMem(sql1, rows) tdSql.checkDataMem(sql1, rows)
def ts_5443(self):
tdLog.info("create database ts_5443")
tdSql.execute("create database ts_5443")
tdSql.execute("use ts_5443")
sqls = [
"CREATE STABLE demo (ts TIMESTAMP, site NCHAR(8), expected BIGINT) TAGS (group_id BIGINT UNSIGNED)",
"CREATE TABLE demo_1 USING demo (group_id) TAGS (1)",
"INSERT INTO demo_1 VALUES ('2022-10-25 16:05:00.000', 'MN-01', 1)",
"CREATE TABLE demo_2 USING demo (group_id) TAGS (2)",
"INSERT INTO demo_2 VALUES ('2022-10-25 16:10:00.000', 'MN-02', 2)",
"CREATE TABLE demo_3 USING demo (group_id) TAGS (3)",
"INSERT INTO demo_3 VALUES ('2022-10-25 16:15:00.000', 'MN-03', 3)",
]
tdSql.executes(sqls)
# test result of order by in plain query
query = '''
SELECT _wend, site, SUM(expected) AS check
FROM ts_5443.demo
PARTITION BY site INTERVAL(5m) SLIDING (5m)
ORDER BY 1 DESC, 2, 3
'''
tdSql.query(query)
tdSql.checkRows(3)
rows = [
['2022-10-25 16:20:00.000', 'MN-03', 3],
['2022-10-25 16:15:00.000', 'MN-02', 2],
['2022-10-25 16:10:00.000', 'MN-01', 1],
]
tdSql.checkDataMem(query, rows)
# test order by position alias within subquery
query = '''
SELECT COUNT(*) FROM (
SELECT _wend, site, SUM(expected) AS check
FROM ts_5443.demo
PARTITION BY site INTERVAL(5m) SLIDING (5m)
ORDER BY 1 DESC, 2, 3
) WHERE check <> 0
'''
tdSql.query(query)
tdSql.checkRows(1)
tdSql.checkData(0, 0, 3)
# test order by target name within subquery
query = '''
SELECT COUNT(*) FROM (
SELECT _wend, site, SUM(expected) AS check
FROM ts_5443.demo
PARTITION BY site INTERVAL(5m) SLIDING (5m)
ORDER BY _wend DESC, site, check
) WHERE check <> 0
'''
tdSql.query(query)
tdSql.checkRows(1)
tdSql.checkData(0, 0, 3)
# test having clause within subquery
query = '''
SELECT COUNT(*) FROM (
SELECT _wend, site, SUM(expected) AS check
FROM ts_5443.demo
PARTITION BY site INTERVAL(5m) SLIDING (5m)
HAVING _wend > '2022-10-25 16:13:00.000'
) WHERE check <> 0
'''
tdSql.query(query)
tdSql.checkRows(1)
tdSql.checkData(0, 0, 2)
# run # run
def run(self): def run(self):
tdLog.debug(f"start to excute {__file__}") tdLog.debug(f"start to excute {__file__}")
@ -85,6 +151,8 @@ class TDTestCase(TBase):
# TS-30189 # TS-30189
self.ts_30189() self.ts_30189()
# TS-5443
self.ts_5443()
tdLog.success(f"{__file__} successfully executed") tdLog.success(f"{__file__} successfully executed")