Skip to content

Commit

Permalink
Merge branch 'main' into tempfs
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Nov 29, 2024
2 parents dd71dd7 + 6386d14 commit c45c36a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
17 changes: 7 additions & 10 deletions pkg/sql/plan/order_binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package plan
import (
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/pb/plan"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
)

Expand Down Expand Up @@ -53,22 +54,18 @@ func (b *OrderBinder) BindExpr(astExpr tree.Expr) (*plan.Expr, error) {
},
}, nil
} else {
var matchedFields []int32 // SelectField index used to record matches
// SelectField index used to record matches
matchedFields := make(map[string]int)
var matchedExpr *plan.Expr // Used to save matched expr

for _, selectField := range b.ctx.projectByAst {
// alias has already been matched earlier, no further processing is needed
if selectField.aliasName != "" {
if selectField.aliasName == colRef.ColName() {
// Record the selectField index that matches
matchedFields = append(matchedFields, selectField.pos)
// Save matching expr
matchedExpr = b.ctx.projects[selectField.pos]
} else {
continue
}
continue
} else if projectField, ok1 := selectField.ast.(*tree.UnresolvedName); ok1 && projectField.ColName() == colRef.ColName() {
// Record the selectField index that matches
matchedFields = append(matchedFields, selectField.pos)
field := tree.String(selectField.ast, dialect.MYSQL)
matchedFields[field] += 1
// Save matching expr
matchedExpr = &plan.Expr{
Typ: b.ctx.projects[selectField.pos].Typ,
Expand Down
42 changes: 42 additions & 0 deletions test/distributed/cases/dml/select/order_by_clause.result
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,47 @@ select empno, 20 as empno from emp order by empno;
invalid input: Column 'empno' in order clause is ambiguous
select empno, space(50) as empno from emp order by empno;
invalid input: Column 'empno' in order clause is ambiguous
select empno, ename, job, mgr, hiredate, sal, empno from emp where deptno != 20 order by empno;
empno ename job mgr hiredate sal empno
7499 ALLEN SALESMAN 7698 1981-02-20 1600.00 7499
7521 WARD SALESMAN 7698 1981-02-22 1250.00 7521
7654 MARTIN SALESMAN 7698 1981-09-28 1250.00 7654
7698 BLAKE MANAGER 7839 1981-05-01 2850.00 7698
7782 CLARK MANAGER 7839 1981-06-09 2450.00 7782
7839 KING PRESIDENT null 1981-11-17 5000.00 7839
7844 TURNER SALESMAN 7698 1981-09-08 1500.00 7844
7900 JAMES CLERK 7698 1981-12-03 950.00 7900
7934 MILLER CLERK 7782 1982-01-23 1300.00 7934
select empno, ename, job, mgr, hiredate, sal, emp.empno from emp where deptno != 20 order by empno;
empno ename job mgr hiredate sal empno
7499 ALLEN SALESMAN 7698 1981-02-20 1600.00 7499
7521 WARD SALESMAN 7698 1981-02-22 1250.00 7521
7654 MARTIN SALESMAN 7698 1981-09-28 1250.00 7654
7698 BLAKE MANAGER 7839 1981-05-01 2850.00 7698
7782 CLARK MANAGER 7839 1981-06-09 2450.00 7782
7839 KING PRESIDENT null 1981-11-17 5000.00 7839
7844 TURNER SALESMAN 7698 1981-09-08 1500.00 7844
7900 JAMES CLERK 7698 1981-12-03 950.00 7900
7934 MILLER CLERK 7782 1982-01-23 1300.00 7934
select t1.ename, t2.loc, t1.deptno, t2.deptno as deptno from emp t1 left join dept t2 on t1.deptno = t2.deptno order by t1.deptno;
ename loc deptno deptno
CLARK NEW YORK 10 10
MILLER NEW YORK 10 10
KING NEW YORK 10 10
JONES DALLAS 20 20
SMITH DALLAS 20 20
SCOTT DALLAS 20 20
ADAMS DALLAS 20 20
FORD DALLAS 20 20
MARTIN CHICAGO 30 30
BLAKE CHICAGO 30 30
WARD CHICAGO 30 30
TURNER CHICAGO 30 30
JAMES CHICAGO 30 30
ALLEN CHICAGO 30 30
select t1.ename, t2.loc, deptno, t2.deptno as deptno from emp t1 left join dept t2 on t1.deptno = t2.deptno order by deptno;
invalid input: ambiguouse column reference to 'deptno'
select t1.ename, t2.loc, t1.deptno, t2.deptno as deptno from emp t1 left join dept t2 on t1.deptno = t2.deptno order by deptno;
invalid input: Column 'deptno' in order clause is ambiguous
drop table if exists dept;
drop table if exists emp;
16 changes: 16 additions & 0 deletions test/distributed/cases/dml/select/order_by_clause.sql
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,21 @@ select empno, 20 as empno from emp order by empno;
--17..mysql: ok, mo: error(bug,暂时选择报错) (同上)
select empno, space(50) as empno from emp order by empno;

--18.mysql:ok, mo: ok
select empno, ename, job, mgr, hiredate, sal, empno from emp where deptno != 20 order by empno;

--19.mysql:ok, mo: ok
select empno, ename, job, mgr, hiredate, sal, emp.empno from emp where deptno != 20 order by empno;

--20.mysql:ok, mo: ok
select t1.ename, t2.loc, t1.deptno, t2.deptno as deptno from emp t1 left join dept t2 on t1.deptno = t2.deptno order by t1.deptno;

--21.mysql:error, mo: error
select t1.ename, t2.loc, deptno, t2.deptno as deptno from emp t1 left join dept t2 on t1.deptno = t2.deptno order by deptno;

--22.mysql:error, mo: error
select t1.ename, t2.loc, t1.deptno, t2.deptno as deptno from emp t1 left join dept t2 on t1.deptno = t2.deptno order by deptno;


drop table if exists dept;
drop table if exists emp;

0 comments on commit c45c36a

Please sign in to comment.