-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Conversation
/run-all-tests |
err: nil, | ||
}, | ||
{ | ||
sql: "select concat(c_str, d_str) from t group by `concat(c_str,d_str)`", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😂 Got it
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
plan/logical_plan_builder.go
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo ther
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
plan/logical_plan_builder.go
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
call -> calls?
plan/logical_plan_builder.go
Outdated
} 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
match -> matches
these -> the?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it.
PTAL @zz-jason |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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 asColumnName
.For example, in this sql:
concat(Class,petallength)
inSelectField
is aFunctionCallExpr
, but it is also a column name of result table.Therefore this SQL is right.
But in TiDB,
FunctionCallExpr
inSelectField
will be ignore whenPlanBuilder
try to looking forColumnName
inGroupByClause
.Check List
Tests
Code changes
Side effects