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

plan: fix concat in group statement #7448

Merged
merged 6 commits into from
Aug 21, 2018
Merged

plan: fix concat in group statement #7448

merged 6 commits into from
Aug 21, 2018

Conversation

imtbkcat
Copy link

What problem does this PR solve?

Fix #7331 to make JDBC test happy.

What is changed and how it works?

In MySQL, function name in SelectFieldList can be regard as ColumnName.
For example, in this sql:

mysql> SELECT concat(Class,petallength), COUNT(*) FROM `testBug24886` GROUP BY 
`concat(Class,petallength)`;
+---------------------------+----------+
| concat(Class,petallength) | COUNT(*) |
+---------------------------+----------+
| 12343                     |        1 |
| 123456787                 |        1 |
+---------------------------+----------+
2 rows in set (0.01 sec)

concat(Class,petallength) in SelectField is a FunctionCallExpr, but it is also a column name of result table.
Therefore this SQL is right.

But in TiDB, FunctionCallExpr in SelectField will be ignore when PlanBuilder try to looking for ColumnName in GroupByClause.

Check List

Tests

  • Unit test

Code changes

  • Has exported function/method change

Side effects

  • Increased code complexity

@jackysp
Copy link
Member

jackysp commented Aug 21, 2018

/run-all-tests

err: nil,
},
{
sql: "select concat(c_str, d_str) from t group by `concat(c_str,d_str)`",
Copy link
Member

Choose a reason for hiding this comment

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

This should not result in an error.

Copy link
Member

Choose a reason for hiding this comment

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

this is the result of mysql:

MySQL(localhost:3306) > select concat(k1, k2) from t group by concat(k1,k2);
+----------------+
| concat(k1, k2) |
+----------------+
| 12             |
+----------------+
1 row in set (0.01 sec)

Copy link
Author

Choose a reason for hiding this comment

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

mysql> select concat(k1, k2) from t group by `concat(k1,k2)`;
ERROR 1054 (42S22): Unknown column 'concat(k1,k2)' in 'group statement'

`concat(k1,k2)` is different from concat(k1,k2).

Copy link
Member

Choose a reason for hiding this comment

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

😂 Got it

Copy link
Contributor

Choose a reason for hiding this comment

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

It'll be better to add the test case select concat(k1, k2) from t group by concat(k1,k2); here.

Copy link
Author

Choose a reason for hiding this comment

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

@XuHuaiyu TiDB always support this case.😂

@@ -849,6 +849,8 @@ func matchField(f *ast.SelectField, col *ast.ColumnNameExpr, ignoreAsName bool)
if f.AsName.L == "" || ignoreAsName {
if curCol, isCol := f.Expr.(*ast.ColumnNameExpr); isCol {
return curCol.Name.Name.L == col.Name.Name.L
} else if _, isFunc := f.Expr.(*ast.FuncCallExpr); isFunc {
Copy link
Member

Choose a reason for hiding this comment

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

it's better to add a comment here to explain why we add this check and what issue it fixes.

Copy link
Author

Choose a reason for hiding this comment

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

ok

@@ -849,6 +849,13 @@ func matchField(f *ast.SelectField, col *ast.ColumnNameExpr, ignoreAsName bool)
if f.AsName.L == "" || ignoreAsName {
if curCol, isCol := f.Expr.(*ast.ColumnNameExpr); isCol {
return curCol.Name.Name.L == col.Name.Name.L
} else if _, isFunc := f.Expr.(*ast.FuncCallExpr); isFunc {
// Fix issue 7331
// If ther are some function call in SelectField, we
Copy link
Member

Choose a reason for hiding this comment

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

typo ther

Copy link
Member

@jackysp jackysp left a comment

Choose a reason for hiding this comment

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

LGTM

@imtbkcat imtbkcat added the status/LGT1 Indicates that a PR has LGTM 1. label Aug 21, 2018
@@ -849,6 +849,13 @@ func matchField(f *ast.SelectField, col *ast.ColumnNameExpr, ignoreAsName bool)
if f.AsName.L == "" || ignoreAsName {
if curCol, isCol := f.Expr.(*ast.ColumnNameExpr); isCol {
return curCol.Name.Name.L == col.Name.Name.L
} else if _, isFunc := f.Expr.(*ast.FuncCallExpr); isFunc {
// Fix issue 7331
// If there are some function call in SelectField, we
Copy link
Contributor

Choose a reason for hiding this comment

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

call -> calls?

} else if _, isFunc := f.Expr.(*ast.FuncCallExpr); isFunc {
// Fix issue 7331
// If there are some function call in SelectField, we
// check if ColumnNameExpr in GroupByClause match these FunctionCallExpr
Copy link
Contributor

Choose a reason for hiding this comment

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

match -> matches
these -> the?

Copy link
Author

Choose a reason for hiding this comment

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

Got it.

@imtbkcat
Copy link
Author

PTAL @zz-jason

Copy link
Member

@winoros winoros left a comment

Choose a reason for hiding this comment

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

lgtm

@imtbkcat imtbkcat added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Aug 21, 2018
Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

LGTM

@imtbkcat imtbkcat merged commit d455e6a into pingcap:master Aug 21, 2018
@imtbkcat imtbkcat deleted the fix7331 branch September 25, 2018 05:34
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/compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants