Skip to content

Commit

Permalink
planner: resolveFromPlan when tbl.col in havingClause (#18349) (#18432)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot committed Jul 24, 2020
1 parent 28000d3 commit f048fc7
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 @@ -1225,7 +1225,6 @@ type havingWindowAndOrderbyExprResolver struct {
inWindowFunc bool
inWindowSpec bool
inExpr bool
orderBy bool
err error
p LogicalPlan
selectFields []*ast.SelectField
Expand Down Expand Up @@ -1311,10 +1310,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 @@ -1334,8 +1333,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.resolveFromSchema(v, a.p.Schema())
} else if a.curClause == havingClause && v.Name.Table.L != "" {
// For SQLs like:
// select a from t b having b.a;
index, a.err = a.resolveFromSchema(v, a.p.Schema())
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 @@ -1406,7 +1419,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 @@ -1466,6 +1466,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 f048fc7

Please sign in to comment.