diff --git a/source/libs/nodes/src/nodesTraverseFuncs.c b/source/libs/nodes/src/nodesTraverseFuncs.c index b3623a4b0a..8b44e478c0 100644 --- a/source/libs/nodes/src/nodesTraverseFuncs.c +++ b/source/libs/nodes/src/nodesTraverseFuncs.c @@ -214,14 +214,15 @@ void nodesWalkExprsPostOrder(SNodeList* pList, FNodeWalker walker, void* pContex (void)walkExprs(pList, TRAVERSAL_POSTORDER, walker, pContext); } -static void checkParamIsFunc(SFunctionNode *pFunc) { +static void checkParamIsFunc(SFunctionNode* pFunc) { int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList); - if (numOfParams > 1) { - for (int32_t i = 0; i < numOfParams; ++i) { - SNode* pPara = nodesListGetNode(pFunc->pParameterList, i); - if (nodeType(pPara) == QUERY_NODE_FUNCTION) { - ((SFunctionNode *)pPara)->node.asParam = true; - } + for (int32_t i = 0; i < numOfParams; ++i) { + SNode* pPara = nodesListGetNode(pFunc->pParameterList, i); + if (numOfParams > 1 && nodeType(pPara) == QUERY_NODE_FUNCTION) { + ((SFunctionNode*)pPara)->node.asParam = true; + } + if (nodeType(pPara) == QUERY_NODE_COLUMN) { + ((SColumnNode*)pPara)->node.asParam = true; } } } diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 69e464b3c3..d246641576 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -1323,7 +1323,7 @@ static EDealRes translateColumn(STranslateContext* pCxt, SColumnNode** pCol) { res = translateColumnWithPrefix(pCxt, pCol); } else { bool found = false; - if (SQL_CLAUSE_ORDER_BY == pCxt->currClause) { + if (SQL_CLAUSE_ORDER_BY == pCxt->currClause && !(*pCol)->node.asParam) { res = translateColumnUseAlias(pCxt, pCol, &found); } if (DEAL_RES_ERROR != res && !found) { @@ -1333,6 +1333,10 @@ static EDealRes translateColumn(STranslateContext* pCxt, SColumnNode** pCol) { res = translateColumnWithoutPrefix(pCxt, pCol); } } + if(SQL_CLAUSE_ORDER_BY == pCxt->currClause && !(*pCol)->node.asParam + && res != DEAL_RES_CONTINUE && res != DEAL_RES_END) { + res = translateColumnUseAlias(pCxt, pCol, &found); + } } return res; } diff --git a/tests/system-test/2-query/orderBy.py b/tests/system-test/2-query/orderBy.py index 2d7b7d9a1f..af1ddadc39 100644 --- a/tests/system-test/2-query/orderBy.py +++ b/tests/system-test/2-query/orderBy.py @@ -302,6 +302,11 @@ class TDTestCase: tdSql.error(f"SELECT last(ts) as t2, ts FROM t1 order by last(t2)") + tdSql.execute(f"alter local 'keepColumnName' '1'") + tdSql.no_error(f"SELECT last(ts), first(ts) FROM t1 order by last(ts)") + tdSql.no_error(f"SELECT last(c1), first(c1) FROM t1 order by last(c1)") + tdSql.error(f"SELECT last(ts) as t, first(ts) as t FROM t1 order by last(t)") + def queryOrderByAmbiguousName(self): tdSql.error(sql="select c1 as name, c2 as name, c3 from t1 order by name", expectErrInfo='ambiguous', fullMatched=False) diff --git a/tests/system-test/2-query/td-28068.py b/tests/system-test/2-query/td-28068.py index d77e012d9a..0dfaf8e126 100644 --- a/tests/system-test/2-query/td-28068.py +++ b/tests/system-test/2-query/td-28068.py @@ -20,9 +20,10 @@ class TDTestCase: tdSql.execute("insert into td_28068.ct4 using td_28068.st (branch, scenario) tags ('3.1', 'scenario2') values (now(), 'query1', 9,10);") def run(self): - tdSql.error('select last(ts) as ts, last(branch) as branch, last(scenario) as scenario, last(test_case) as test_case from td_28068.st group by branch, scenario order by last(branch);') - tdSql.error('select last(ts) as ts, last(branch) as branch1, last(scenario) as scenario, last(test_case) as test_case from td_28068.st group by branch, scenario order by last(branch), last(scenario); ') - + tdSql.query('select last(ts) as ts, last(branch) as branch, last(scenario) as scenario, last(test_case) as test_case from td_28068.st group by branch, scenario order by last(branch);') + tdSql.checkRows(4) + tdSql.query('select last(ts) as ts, last(branch) as branch1, last(scenario) as scenario, last(test_case) as test_case from td_28068.st group by branch, scenario order by last(branch), last(scenario); ') + tdSql.checkRows(4) tdSql.query('select last(ts) as ts, last(branch) as branch1, last(scenario) as scenario, last(test_case) as test_case from td_28068.st group by branch, scenario order by last(branch); ') tdSql.checkRows(4)