diff --git a/planner/core/issuetest/BUILD.bazel b/planner/core/issuetest/BUILD.bazel index c12ed06ebee23..a54a1b7361675 100644 --- a/planner/core/issuetest/BUILD.bazel +++ b/planner/core/issuetest/BUILD.bazel @@ -2,6 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test") go_test( name = "issuetest_test", + timeout = "short", srcs = ["planner_issue_test.go"], + flaky = True, + race = "on", + shard_count = 5, deps = ["//testkit"], ) diff --git a/planner/core/issuetest/planner_issue_test.go b/planner/core/issuetest/planner_issue_test.go index 347de9b703ea7..a342c3452634d 100644 --- a/planner/core/issuetest/planner_issue_test.go +++ b/planner/core/issuetest/planner_issue_test.go @@ -33,3 +33,15 @@ func TestIssue44051(t *testing.T) { rs := tk.MustQuery("WITH tmp AS (SELECT t2.* FROM t2) SELECT * FROM t1 WHERE t1.id = (select id from tmp where id = 1) or t1.id = (select id from tmp where id = 2) or t1.id = (select id from tmp where id = 3)") rs.Sort().Check(testkit.Rows("1 ", "2 ", "3 ")) } + +func TestIssue42732(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + + tk.MustExec("use test") + tk.MustExec("CREATE TABLE t1 (a INT, b INT)") + tk.MustExec("CREATE TABLE t2 (a INT, b INT)") + tk.MustExec("INSERT INTO t1 VALUES (1, 1)") + tk.MustExec("INSERT INTO t2 VALUES (1, 1)") + tk.MustQuery("SELECT one.a, one.b as b2 FROM t1 one ORDER BY (SELECT two.b FROM t2 two WHERE two.a = one.b)").Check(testkit.Rows("1 1")) +} diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index b7491c94c53dc..5e5e2edc01b0e 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -2432,8 +2432,9 @@ func (b *PlanBuilder) resolveHavingAndOrderBy(ctx context.Context, sel *ast.Sele if colName != nil { columnNameExpr := &ast.ColumnNameExpr{Name: colName} for _, field := range sel.Fields.Fields { - if c, ok := field.Expr.(*ast.ColumnNameExpr); ok && colMatch(c.Name, columnNameExpr.Name) { + if c, ok := field.Expr.(*ast.ColumnNameExpr); ok && colMatch(c.Name, columnNameExpr.Name) && field.AsName.L == "" { // deduplicate select fields: don't append it once it already has one. + // TODO: we add the field if it has alias, but actually they are the same column. We should not have two duplicate one. columnNameExpr = nil break }