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

ast, plan: return error when the arg of VALUES() function is not a column #7817

Merged
merged 5 commits into from
Sep 30, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,9 @@ func (n *ValuesExpr) Accept(v Visitor) (Node, bool) {
if !ok {
return n, false
}
n.Column = node.(*ColumnNameExpr)
// `node` may be *ast.ValueExpr, to avoid panic, we write `ok` but do not use
// it.
n.Column, ok = node.(*ColumnNameExpr)
return v.Leave(n)
}

Expand Down
4 changes: 4 additions & 0 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,10 @@ func (g *gbyResolver) Leave(inNode ast.Node) (ast.Node, bool) {
return inNode, false
}
return ret, true
case *ast.ValuesExpr:
if v.Column == nil {
g.err = ErrUnknownColumn.GenWithStackByArgs("", "VALUES() function")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

For ValuesExpr, if v.Column is valid, shall we extract it and return this valid ColumnNameExpr as what we do in PositionExpr branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems not.
i.e.
MySQL:

set @@sql_mode='only_full_group_by';
mysql> SELECT a AS b FROM t GROUP BY VALUES(b);
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

If we return ColumnNameExpr here, the check for only_full_group_by will be passed, this is not compatible with MySQL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

related issue:
#7822

}
return inNode, true
}
Expand Down
1 change: 1 addition & 0 deletions planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,7 @@ func (s *testPlanSuite) TestNameResolver(c *C) {
{"select a from t group by t11.c1", "[planner:1054]Unknown column 't11.c1' in 'group statement'"},
{"delete a from (select * from t ) as a, t", "[planner:1288]The target table a of the DELETE is not updatable"},
{"delete b from (select * from t ) as a, t", "[planner:1109]Unknown table 'b' in MULTI DELETE"},
{"select '' as fakeCol from t group by values(fakeCol)", "[planner:1054]Unknown column '' in 'VALUES() function'"},
{"update t, (select * from t) as b set b.a = t.a", "[planner:1288]The target table b of the UPDATE is not updatable"},
}

Expand Down