Skip to content

Commit 29c17a1

Browse files
zyguanwinoros
authored andcommitted
planner/core: correct column name with unary plus sign (#8702)
1 parent 1c4d2d9 commit 29c17a1

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

cmd/explaintest/r/select.result

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ select !(1 + 2);
137137
!(1 + 2)
138138
0
139139
select + - 1, --1, +-+-+1, + "123";
140-
+ - 1 --1 +-+-+1 + "123"
140+
+ - 1 --1 +-+-+1 123
141141
-1 1 1 123
142142
select --------------------1, ++++++++++++++++++++1;
143143
--------------------1 ++++++++++++++++++++1

executor/executor_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,15 @@ func (s *testSuite) TestColumnName(c *C) {
19541954
c.Check(fields[0].Table.Name.L, Equals, "")
19551955
c.Check(fields[0].TableAsName.L, Equals, "")
19561956
c.Check(fields[0].DBName.L, Equals, "")
1957+
// Test case for query a column wrapped with parentheses and unary plus.
1958+
// In this case, the column name should be its original name.
1959+
rs, err = tk.Exec("select (c), (+c), +(c), +(+(c)), ++c from t")
1960+
c.Check(err, IsNil)
1961+
fields = rs.Fields()
1962+
for i := 0; i < 5; i++ {
1963+
c.Check(fields[0].Column.Name.L, Equals, "c")
1964+
c.Check(fields[0].ColumnAsName.L, Equals, "c")
1965+
}
19571966
}
19581967

19591968
func (s *testSuite) TestSelectVar(c *C) {

planner/core/logical_plan_builder.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ func (b *PlanBuilder) buildSelection(p LogicalPlan, where ast.ExprNode, AggMappe
513513

514514
// buildProjectionFieldNameFromColumns builds the field name, table name and database name when field expression is a column reference.
515515
func (b *PlanBuilder) buildProjectionFieldNameFromColumns(field *ast.SelectField, c *expression.Column) (colName, origColName, tblName, origTblName, dbName model.CIStr) {
516-
if astCol, ok := getInnerFromParentheses(field.Expr).(*ast.ColumnNameExpr); ok {
516+
if astCol, ok := getInnerFromParenthesesAndUnaryPlus(field.Expr).(*ast.ColumnNameExpr); ok {
517517
origColName, tblName, dbName = astCol.Name.Name, astCol.Name.Table, astCol.Name.Schema
518518
}
519519
if field.AsName.L != "" {
@@ -537,7 +537,7 @@ func (b *PlanBuilder) buildProjectionFieldNameFromExpressions(field *ast.SelectF
537537
return agg.Args[0].(*ast.ColumnNameExpr).Name.Name
538538
}
539539

540-
innerExpr := getInnerFromParentheses(field.Expr)
540+
innerExpr := getInnerFromParenthesesAndUnaryPlus(field.Expr)
541541
valueExpr, isValueExpr := innerExpr.(*driver.ValueExpr)
542542

543543
// Non-literal: Output as inputed, except that comments need to be removed.
@@ -2468,9 +2468,12 @@ func appendVisitInfo(vi []visitInfo, priv mysql.PrivilegeType, db, tbl, col stri
24682468
})
24692469
}
24702470

2471-
func getInnerFromParentheses(expr ast.ExprNode) ast.ExprNode {
2471+
func getInnerFromParenthesesAndUnaryPlus(expr ast.ExprNode) ast.ExprNode {
24722472
if pexpr, ok := expr.(*ast.ParenthesesExpr); ok {
2473-
return getInnerFromParentheses(pexpr.Expr)
2473+
return getInnerFromParenthesesAndUnaryPlus(pexpr.Expr)
2474+
}
2475+
if uexpr, ok := expr.(*ast.UnaryOperationExpr); ok && uexpr.Op == opcode.Plus {
2476+
return getInnerFromParenthesesAndUnaryPlus(uexpr.V)
24742477
}
24752478
return expr
24762479
}

0 commit comments

Comments
 (0)