diff --git a/query/src/sql/executor/physical_plan_builder.rs b/query/src/sql/executor/physical_plan_builder.rs index c93deea1bea4..a9c295554499 100644 --- a/query/src/sql/executor/physical_plan_builder.rs +++ b/query/src/sql/executor/physical_plan_builder.rs @@ -239,8 +239,8 @@ impl PhysicalPlanBuilder { .items .iter() .map(|v| SortDesc { - asc: v.asc.unwrap_or(true), - nulls_first: v.nulls_first.unwrap_or_default(), + asc: v.asc, + nulls_first: v.nulls_first, order_by: v.index.to_string(), }) .collect(), diff --git a/query/src/sql/planner/binder/sort.rs b/query/src/sql/planner/binder/sort.rs index b0d03d48272a..1a890d87a1d3 100644 --- a/query/src/sql/planner/binder/sort.rs +++ b/query/src/sql/planner/binder/sort.rs @@ -78,7 +78,28 @@ impl<'a> Binder { // We first search the identifier in select list let mut found = false; for item in projections.iter() { - if item.column_name == ident.name { + let matched = match ( + (&database_name, &table_name), + (&item.database_name, &item.table_name), + ) { + ( + (Some(ident_database), Some(ident_table)), + (Some(database), Some(table)), + ) if &ident_database.name == database + && &ident_table.name == table + && ident.name == item.column_name => + { + true + } + ((None, Some(ident_table)), (_, Some(table))) + if &ident_table.name == table && ident.name == item.column_name => + { + true + } + ((None, None), (_, _)) if ident.name == item.column_name => true, + _ => false, + }; + if matched { order_items.push(OrderItem { expr: order.clone(), index: item.index, @@ -229,8 +250,8 @@ impl<'a> Binder { } let order_by_item = SortItem { index: order.index, - asc: order.expr.asc, - nulls_first: order.expr.nulls_first, + asc: order.expr.asc.unwrap_or(true), + nulls_first: order.expr.nulls_first.unwrap_or(false), }; order_by_items.push(order_by_item); } @@ -266,8 +287,8 @@ impl<'a> Binder { Scalar::BoundColumnRef(BoundColumnRef { column }) => { let order_by_item = SortItem { index: column.index, - asc: order.asc, - nulls_first: order.nulls_first, + asc: order.asc.unwrap_or(true), + nulls_first: order.nulls_first.unwrap_or(false), }; order_by_items.push(order_by_item); } diff --git a/query/src/sql/planner/format/display_rel_operator.rs b/query/src/sql/planner/format/display_rel_operator.rs index 2c28d2a95195..dd6cc3f2fd4d 100644 --- a/query/src/sql/planner/format/display_rel_operator.rs +++ b/query/src/sql/planner/format/display_rel_operator.rs @@ -330,11 +330,7 @@ pub fn format_sort( "{} (#{}) {}", name, item.index, - if item.asc.unwrap_or(false) { - "ASC" - } else { - "DESC" - } + if item.asc { "ASC" } else { "DESC" } ) }) .collect::>() diff --git a/query/src/sql/planner/plans/sort.rs b/query/src/sql/planner/plans/sort.rs index 478e8a574157..6269af3b3db8 100644 --- a/query/src/sql/planner/plans/sort.rs +++ b/query/src/sql/planner/plans/sort.rs @@ -33,8 +33,8 @@ pub struct Sort { #[derive(Clone, Debug)] pub struct SortItem { pub index: IndexType, - pub asc: Option, - pub nulls_first: Option, + pub asc: bool, + pub nulls_first: bool, } impl Operator for Sort { diff --git a/tests/logictest/suites/gen/03_dml/03_0004_select_order_by_db_table_col_v2 b/tests/logictest/suites/gen/03_dml/03_0004_select_order_by_db_table_col_v2 index 692b49a29ba4..7416a95805da 100644 --- a/tests/logictest/suites/gen/03_dml/03_0004_select_order_by_db_table_col_v2 +++ b/tests/logictest/suites/gen/03_dml/03_0004_select_order_by_db_table_col_v2 @@ -47,12 +47,9 @@ SELECT DISTINCT(id) FROM a.t ORDER BY a.t.id; statement error 1065 SELECT DISTINCT(id) FROM a.t ORDER BY a.t.id2; -statement query I +statement error SELECT SUM(id) as id2 FROM a.t ORDER BY a.t.id2; ----- -3 - statement query I SELECT DISTINCT(id) as id2 FROM a.t ORDER BY a.t.id2; @@ -67,7 +64,7 @@ SELECT * FROM a.t ORDER BY a.t.id ASC; 1 1 2 2 -statement error 1003 +statement error 1065 SELECT * FROM a.t ORDER BY B.T.id ASC; statement query II @@ -77,7 +74,7 @@ SELECT * FROM a.t ORDER BY a.t.id DESC; 2 2 1 1 -statement error 1003 +statement error 1065 SELECT * FROM a.t ORDER BY B.T.id DESC; statement ok diff --git a/tests/logictest/suites/query/order.test b/tests/logictest/suites/query/order.test new file mode 100644 index 000000000000..e7f6a71c450b --- /dev/null +++ b/tests/logictest/suites/query/order.test @@ -0,0 +1,41 @@ +statement ok +set enable_planner_v2 = 1; + +statement ok +create table order_test(a int null); + +statement ok +insert into order_test values(1),(2),(null); + +statement query I +select * from order_test order by a; + +---- +1 +2 +NULL + +statement query I +select * from order_test order by a desc; + +---- +2 +1 +NULL + +statement query I +select * from order_test order by a nulls first; + +---- +NULL +1 +2 + +statement error +select number from numbers(10) as a order by b.number; + +statement error +select number from (select * from numbers(10) as b) as a order by b.number; + +statement ok +drop table order_test;