Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions source/libs/parser/src/parTranslater.c
Original file line number Diff line number Diff line change
Expand Up @@ -2115,20 +2115,21 @@ static EDealRes translateColumn(STranslateContext* pCxt, SColumnNode** pCol) {
if (pCxt->currClause == SQL_CLAUSE_ORDER_BY) {
if ((isSelectStmt(pCxt->pCurrStmt) && !(*pCol)->node.asParam) ||
isSetOperator(pCxt->pCurrStmt)) {
// match column in alias first if column is 'bare' in select stmt
// or it is in set operator
// match column in select list first if column is
// NOT param in select stmt or it is in set operator
res = translateColumnUseAlias(pCxt, pCol, &found);
}
}
if (DEAL_RES_ERROR != res && !found) {
if (isSetOperator(pCxt->pCurrStmt)) {
res = generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_ORDERBY_UNKNOWN_EXPR, (*pCol)->colName);
} else {
// match column in table if not found or column is part of an expression in select stmt
// match column in table if not found or
// column is part of an expression in select stmt
res = translateColumnWithoutPrefix(pCxt, pCol);
}
}
if (clauseSupportAlias(pCxt->currClause) && !(*pCol)->node.asParam && res != DEAL_RES_CONTINUE &&
if (clauseSupportAlias(pCxt->currClause) && res != DEAL_RES_CONTINUE &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里逻辑有点混乱,应该是基于找没找到的结果进行处理,res为错误可能是真的错误,也可能是没找到,要区分处理

res != DEAL_RES_END) {
res = translateColumnUseAlias(pCxt, pCol, &found);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from new_test_framework.utils import tdLog, tdSql

class TestOrderBySelectList:
def setup(cls):
tdLog.debug(f"start to execute {__file__}")

def test_order_by_select_list(self):
"""Order By Select List
Query the table with order by select list
Catalog:
- Query:OrderBy
Since: v3.3.6.34
Labels: common,ci
Jira: TD-38284
History:
- 2025-11-26 Tony Zhang add this test case for TD-38284
"""

tdLog.info(f"=============== Start test Order By Select List")

tdSql.execute("create database if not exists db keep 3650;", show=1)
tdSql.execute("use db;", show=1)
tdSql.execute("create table tt (ts timestamp, v1 int, v2 int);", show=1)
tdSql.execute("""
insert into tt values
('2025-01-01 09:00:00', 1, -3),
('2025-01-01 10:00:00', -2, 2),
('2025-01-01 11:00:00', 3, 1);
""", show=1)

# 'vv' only exists in select list
tdSql.query("select ts, v1 as vv from tt order by vv asc", show=1)
tdSql.checkRows(3)
tdSql.checkData(0, 0, '2025-01-01 10:00:00')
tdSql.checkData(0, 1, -2)
tdSql.checkData(1, 0, '2025-01-01 09:00:00')
tdSql.checkData(1, 1, 1)
tdSql.checkData(2, 0, '2025-01-01 11:00:00')
tdSql.checkData(2, 1, 3)

# 'vv' only exists in select list
tdSql.query("select ts, v1 as vv from tt order by abs(vv) asc", show=1)
tdSql.checkRows(3)
tdSql.checkData(0, 0, '2025-01-01 09:00:00')
tdSql.checkData(0, 1, 1)
tdSql.checkData(1, 0, '2025-01-01 10:00:00')
tdSql.checkData(1, 1, -2)
tdSql.checkData(2, 0, '2025-01-01 11:00:00')
tdSql.checkData(2, 1, 3)

# 'v1' exists in both select list and table tt
# prefer to use column from select list
tdSql.query("select ts, v2 as v1 from tt order by v1 asc", show=1)
tdSql.checkRows(3)
tdSql.checkData(0, 0, '2025-01-01 09:00:00')
tdSql.checkData(0, 1, -3)
tdSql.checkData(1, 0, '2025-01-01 11:00:00')
tdSql.checkData(1, 1, 1)
tdSql.checkData(2, 0, '2025-01-01 10:00:00')
tdSql.checkData(2, 1, 2)

# 'v1' exists in both select list and table tt
# prefer to use column from table tt
tdSql.query("select ts, v2 as v1 from tt order by abs(v1) asc", show=1)
tdSql.checkRows(3)
tdSql.checkData(0, 0, '2025-01-01 09:00:00')
tdSql.checkData(0, 1, -3)
tdSql.checkData(1, 0, '2025-01-01 10:00:00')
tdSql.checkData(1, 1, 2)
tdSql.checkData(2, 0, '2025-01-01 11:00:00')
tdSql.checkData(2, 1, 1)

tdLog.info(f"=============== End test Order By Select List")

This comment was marked as off-topic.

1 change: 0 additions & 1 deletion test/ci/cases.task
Original file line number Diff line number Diff line change
Expand Up @@ -624,4 +624,3 @@
,,y,.,./ci/pytest.sh pytest cases/50-Others/01-Valgrind/test_valgrind_checkerror7.py
,,y,.,./ci/pytest.sh pytest cases/50-Others/01-Valgrind/test_valgrind_checkerror8.py
#,,y,.,./ci/pytest.sh pytest cases/50-Others/01-Valgrind/test_valgrind_udf.py

Loading