Skip to content

Commit

Permalink
planner: a quick fix to solve the column alias in ORDER BY's subquery (
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Jun 30, 2023
1 parent 09ecce1 commit c699537
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions planner/core/issuetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
12 changes: 12 additions & 0 deletions planner/core/issuetest/planner_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <nil> <nil> <nil>", "2 <nil> <nil> <nil>", "3 <nil> <nil> <nil>"))
}

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"))
}
3 changes: 2 additions & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit c699537

Please sign in to comment.