Skip to content

Commit

Permalink
planner: resolveFromPlan when tblName.colName exists in having clause
Browse files Browse the repository at this point in the history
  • Loading branch information
XuHuaiyu committed Jul 6, 2020
1 parent 5545dd6 commit 9decf2e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
16 changes: 15 additions & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,8 +1550,22 @@ func (a *havingWindowAndOrderbyExprResolver) Leave(n ast.Node) (node ast.Node, o
return node, false
}
if index == -1 {
if a.curClause == orderByClause || a.curClause == havingClause {
if a.curClause == orderByClause {
index, a.err = a.resolveFromPlan(v, a.p)
} else if a.curClause == havingClause && v.Name.Table.L != "" {
// For SQLs like:
// select a from t b having b.a;
index, a.err = a.resolveFromPlan(v, a.p)
if a.err != nil {
return node, false
}
if index != -1 {
// For SQLs like:
// select a+1 from t having t.a;
newV := v
newV.Name = &ast.ColumnName{Name: v.Name.Name}
index, a.err = resolveFromSelectFields(newV, a.selectFields, true)
}
} else {
index, a.err = resolveFromSelectFields(v, a.selectFields, true)
}
Expand Down
16 changes: 16 additions & 0 deletions planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,22 @@ func (s *testPlanSuite) TestValidate(c *C) {
sql: "select concat(c_str, d_str) from t group by `concat(c_str,d_str)`",
err: ErrUnknownColumn,
},
{
sql: "select a from t b having b.a",
err: nil,
},
{
sql: "select b.a from t b having b.a",
err: nil,
},
{
sql: "select b.a from t b having a",
err: nil,
},
{
sql: "select a+1 from t having t.a",
err: ErrUnknownColumn,
},
}

ctx := context.Background()
Expand Down

0 comments on commit 9decf2e

Please sign in to comment.