Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: resolveFromPlan when tblName.colName exists in having clause (#18349) #18434

Merged
merged 4 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,6 @@ type havingWindowAndOrderbyExprResolver struct {
inWindowFunc bool
inWindowSpec bool
inExpr bool
orderBy bool
err error
p LogicalPlan
selectFields []*ast.SelectField
Expand Down Expand Up @@ -1523,10 +1522,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 @@ -1546,8 +1545,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 @@ -1619,7 +1632,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