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

Conversation

XuHuaiyu
Copy link
Contributor

PTAL @eurekaka @winoros

What problem does this PR solve?

create table t(a int);

Before this PR:

select '' as b from t group by values(b);

-- tidb-server will panic

After this PR:

tidb> SELECT '' AS b FROM t GROUP BY VALUES(b);
ERROR 1054 (42S22): Unknown column '' in 'VALUES() function'

What is changed and how it works?

Before this PR, we assume that the argument for VALUES must be a ColumnNameExpr,
which is promised as the example above shows.

This PR checks the type of argument for VALUES, if it's not a ColumnNameExpr, we will raise an error.

Check List

Tests

  • Integration test

Code changes

  • Has exported function/method change

Side effects

none

Related changes

none

@XuHuaiyu XuHuaiyu added type/bugfix This PR fixes a bug. sig/planner SIG: Planner labels Sep 29, 2018
Copy link
Contributor

@crazycs520 crazycs520 left a comment

Choose a reason for hiding this comment

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

LGTM

@crazycs520
Copy link
Contributor

/run-all-tests

@crazycs520 crazycs520 added the status/LGT1 Indicates that a PR has LGTM 1. label Sep 29, 2018
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

@@ -927,7 +927,10 @@ func (n *ValuesExpr) Accept(v Visitor) (Node, bool) {
if !ok {
return n, false
}
n.Column = node.(*ColumnNameExpr)
n.Column, ok = node.(*ColumnNameExpr)
if !ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer to put this check in ColumnNameExpr branch of gbyResolver::Leave, i.e, for a ColumnNameExpr, if the extracted result is not ColumnNameExpr, we should report error.

Accept acts as a general walker, it should not involve detailed visitor logic IMHO.

@XuHuaiyu
Copy link
Contributor Author

PTAL @eurekaka

Copy link
Contributor

@zimulala zimulala left a comment

Choose a reason for hiding this comment

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

LGTM

@zimulala zimulala added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Sep 30, 2018
@zimulala
Copy link
Contributor

/run-all-tests

Copy link
Contributor

@eurekaka eurekaka left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/planner SIG: Planner status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants