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) #18433

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
25 changes: 21 additions & 4 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,6 @@ type havingWindowAndOrderbyExprResolver struct {
inWindowFunc bool
inWindowSpec bool
inExpr bool
orderBy bool
err error
p LogicalPlan
selectFields []*ast.SelectField
Expand Down Expand Up @@ -1387,10 +1386,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 @@ -1410,8 +1409,27 @@ func (a *havingWindowAndOrderbyExprResolver) Leave(n ast.Node) (node ast.Node, o
return node, false
}
if index == -1 {
<<<<<<< HEAD
if a.orderBy {
index, a.err = a.resolveFromSchema(v, a.p.Schema())
=======
Comment on lines +1412 to +1415
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please resolve conflicts.

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)
}
>>>>>>> 62979f6... planner: resolveFromPlan when tblName.colName exists in having clause (#18349)
} else {
index, a.err = resolveFromSelectFields(v, a.selectFields, true)
}
Expand Down Expand Up @@ -1482,7 +1500,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