Repository: databendlabs/databend (language: rust)

Issue:
bug: order by literal with limit return error
### Search before asking

- [X] I had searched in the [issues](https://github.com/databendlabs/databend/issues) and found no similar issues.


### Version

v1.2.662



### What's Wrong?

order by literal with limit return error

we can ignore order by item if it is a scalar value

### How to Reproduce?

```sql
MySQL [(none)]> create table tt(a int, b int);
Query OK, 0 rows affected (0.187 sec)

MySQL [(none)]> insert into tt values(1, 2),(3, 4);
Query OK, 2 rows affected (0.209 sec)

MySQL [(none)]> select eq(tt.a, b) FROM tt ORDER BY 'a' LIMIT 89, 7;
ERROR 1105 (HY000): BadArguments. Code: 1006, Text = Unable to get field named "0". Valid fields: ["4"].
```

### Are you willing to submit PR?

- [ ] Yes I am willing to submit a PR!


Discussion / hints:

Fix patch:
diff --git a/src/query/sql/src/planner/binder/sort.rs b/src/query/sql/src/planner/binder/sort.rs
index 8af8d17a89cf8..a036e42939b07 100644
--- a/src/query/sql/src/planner/binder/sort.rs
+++ b/src/query/sql/src/planner/binder/sort.rs
@@ -148,6 +148,10 @@ impl Binder {
                             )
                             .map_err(|e| ErrorCode::SemanticError(e.message()))?;
 
+                        if let ScalarExpr::ConstantExpr(..) = rewrite_scalar {
+                            continue;
+                        }
+
                         let column_binding =
                             if let ScalarExpr::BoundColumnRef(col) = &rewrite_scalar {
                                 col.column.clone()


Test patch:
diff --git a/tests/sqllogictests/suites/base/03_common/03_0004_select_order_by.test b/tests/sqllogictests/suites/base/03_common/03_0004_select_order_by.test
index f25779b7bee29..56bfef5b164ff 100644
--- a/tests/sqllogictests/suites/base/03_common/03_0004_select_order_by.test
+++ b/tests/sqllogictests/suites/base/03_common/03_0004_select_order_by.test
@@ -132,3 +132,60 @@ SELECT number FROM numbers_mt(10) ORDER BY sum(number)
 
 statement error 1065
 SELECT number FROM numbers_mt(10) ORDER BY count(*) + 1
+
+statement ok
+CREATE TABLE t1(a int, b int);
+
+statement ok
+INSERT INTO t1 VALUES(1, 3),(2, 1), (3, 2)
+
+query I
+SELECT * from t1 order by 1;
+----
+1 3
+2 1
+3 2
+
+query I
+SELECT * from t1 order by 2;
+----
+2 1
+3 2
+1 3
+
+statement error 1065
+SELECT * from t1 order by 3;
+
+query I
+SELECT * from t1 order by t1.a;
+----
+1 3
+2 1
+3 2
+
+query I
+SELECT * from t1 order by t1.b;
+----
+2 1
+3 2
+1 3
+
+statement error 1065
+SELECT * from t1 order by t1.1;
+
+query I 
+SELECT * from t1 order by 'a';
+----
+1 3
+2 1
+3 2
+
+query I 
+SELECT * from t1 order by '1', 1;
+----
+1 3
+2 1
+3 2
+
+statement ok
+DROP TABLE if EXISTS t1
\ No newline at end of file