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
…#18349)

* test

* planner: resolveFromPlan when tblName.colName exists in having clause

Co-authored-by: Kenan Yao <[email protected]>
Co-authored-by: ti-srebot <[email protected]>
  • Loading branch information
3 people authored Jul 8, 2020
1 parent 2dfae98 commit 62979f6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
22 changes: 17 additions & 5 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,6 @@ type havingWindowAndOrderbyExprResolver struct {
inWindowFunc bool
inWindowSpec bool
inExpr bool
orderBy bool
err error
p LogicalPlan
selectFields []*ast.SelectField
Expand Down Expand Up @@ -1528,10 +1527,10 @@ func (a *havingWindowAndOrderbyExprResolver) Leave(n ast.Node) (node ast.Node, o
a.inWindowSpec = false
case *ast.ColumnNameExpr:
resolveFieldsFirst := true
if a.inAggFunc || a.inWindowFunc || a.inWindowSpec || (a.orderBy && a.inExpr) || a.curClause == fieldList {
if a.inAggFunc || a.inWindowFunc || a.inWindowSpec || (a.curClause == orderByClause && a.inExpr) || a.curClause == fieldList {
resolveFieldsFirst = false
}
if !a.inAggFunc && !a.orderBy {
if !a.inAggFunc && a.curClause != orderByClause {
for _, item := range a.gbyItems {
if col, ok := item.Expr.(*ast.ColumnNameExpr); ok &&
(colMatch(v.Name, col.Name) || colMatch(col.Name, v.Name)) {
Expand All @@ -1551,8 +1550,22 @@ func (a *havingWindowAndOrderbyExprResolver) Leave(n ast.Node) (node ast.Node, o
return node, false
}
if index == -1 {
if a.orderBy {
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 Expand Up @@ -1624,7 +1637,6 @@ func (b *PlanBuilder) resolveHavingAndOrderBy(sel *ast.SelectStmt, p LogicalPlan
}
havingAggMapper := extractor.aggMapper
extractor.aggMapper = make(map[*ast.AggregateFuncExpr]int)
extractor.orderBy = true
extractor.inExpr = false
// Extract agg funcs from order by clause.
if sel.OrderBy != nil {
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 62979f6

Please sign in to comment.