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

server, mysql: return correct column name and column label name #7600

Merged
merged 4 commits into from
Sep 8, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,18 @@ func schema2ResultFields(schema *expression.Schema, defaultDB string) (rfs []*as
if dbName == "" && col.TblName.L != "" {
dbName = defaultDB
}
origColName := col.OrigColName
if origColName.L == "" {
origColName = col.ColName
}
rf := &ast.ResultField{
ColumnAsName: col.ColName,
TableAsName: col.TblName,
DBName: model.NewCIStr(dbName),
Table: &model.TableInfo{Name: col.OrigTblName},
Column: &model.ColumnInfo{
FieldType: *col.RetType,
Name: col.ColName,
Name: origColName,
},
}
rfs = append(rfs, rf)
Expand Down
11 changes: 8 additions & 3 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1768,26 +1768,31 @@ func (s *testSuite) TestColumnName(c *C) {
fields := rs.Fields()
c.Check(len(fields), Equals, 2)
c.Check(fields[0].Column.Name.L, Equals, "1 + c")
c.Check(fields[0].ColumnAsName.L, Equals, "1 + c")
c.Check(fields[1].Column.Name.L, Equals, "count(*)")
c.Check(fields[1].ColumnAsName.L, Equals, "count(*)")
rs, err = tk.Exec("select (c) > all (select c from t) from t")
c.Check(err, IsNil)
fields = rs.Fields()
c.Check(len(fields), Equals, 1)
c.Check(fields[0].Column.Name.L, Equals, "(c) > all (select c from t)")
c.Check(fields[0].ColumnAsName.L, Equals, "(c) > all (select c from t)")
tk.MustExec("begin")
tk.MustExec("insert t values(1,1)")
rs, err = tk.Exec("select c d, d c from t")
c.Check(err, IsNil)
fields = rs.Fields()
c.Check(len(fields), Equals, 2)
c.Check(fields[0].Column.Name.L, Equals, "d")
c.Check(fields[1].Column.Name.L, Equals, "c")
c.Check(fields[0].Column.Name.L, Equals, "c")
c.Check(fields[0].ColumnAsName.L, Equals, "d")
c.Check(fields[1].Column.Name.L, Equals, "d")
c.Check(fields[1].ColumnAsName.L, Equals, "c")
// Test case for query a column of a table.
// In this case, all attributes have values.
rs, err = tk.Exec("select c as a from t as t2")
c.Check(err, IsNil)
fields = rs.Fields()
c.Check(fields[0].Column.Name.L, Equals, "a")
c.Check(fields[0].Column.Name.L, Equals, "c")
c.Check(fields[0].ColumnAsName.L, Equals, "a")
c.Check(fields[0].Table.Name.L, Equals, "t")
c.Check(fields[0].TableAsName.L, Equals, "t2")
Expand Down
1 change: 1 addition & 0 deletions expression/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (col *CorrelatedColumn) resolveIndices(_ *Schema) {

// Column represents a column.
type Column struct {
OrigColName model.CIStr
ColName model.CIStr
DBName model.CIStr
OrigTblName model.CIStr
Expand Down
13 changes: 8 additions & 5 deletions plan/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,20 +472,22 @@ func (b *planBuilder) buildSelection(p LogicalPlan, where ast.ExprNode, AggMappe
}

// buildProjectionFieldNameFromColumns builds the field name, table name and database name when field expression is a column reference.
func (b *planBuilder) buildProjectionFieldNameFromColumns(field *ast.SelectField, c *expression.Column) (colName, tblName, origTblName, dbName model.CIStr) {
func (b *planBuilder) buildProjectionFieldNameFromColumns(field *ast.SelectField, c *expression.Column) (colName, origColName, tblName, origTblName, dbName model.CIStr) {
if astCol, ok := getInnerFromParentheses(field.Expr).(*ast.ColumnNameExpr); ok {
colName, tblName, dbName = astCol.Name.Name, astCol.Name.Table, astCol.Name.Schema
origColName, tblName, dbName = astCol.Name.Name, astCol.Name.Table, astCol.Name.Schema
}
if field.AsName.L != "" {
colName = field.AsName
} else {
colName = origColName
}
if tblName.L == "" {
tblName = c.TblName
}
if dbName.L == "" {
dbName = c.DBName
}
return colName, tblName, c.OrigTblName, c.DBName
return colName, origColName, tblName, c.OrigTblName, c.DBName
}

// buildProjectionFieldNameFromExpressions builds the field name when field expression is a normal expression.
Expand Down Expand Up @@ -531,10 +533,10 @@ func (b *planBuilder) buildProjectionFieldNameFromExpressions(field *ast.SelectF

// buildProjectionField builds the field object according to SelectField in projection.
func (b *planBuilder) buildProjectionField(id, position int, field *ast.SelectField, expr expression.Expression) *expression.Column {
var origTblName, tblName, colName, dbName model.CIStr
var origTblName, tblName, origColName, colName, dbName model.CIStr
if c, ok := expr.(*expression.Column); ok && !c.IsAggOrSubq {
// Field is a column reference.
colName, tblName, origTblName, dbName = b.buildProjectionFieldNameFromColumns(field, c)
colName, origColName, tblName, origTblName, dbName = b.buildProjectionFieldNameFromColumns(field, c)
} else if field.AsName.L != "" {
// Field has alias.
colName = field.AsName
Expand All @@ -547,6 +549,7 @@ func (b *planBuilder) buildProjectionField(id, position int, field *ast.SelectFi
TblName: tblName,
OrigTblName: origTblName,
ColName: colName,
OrigColName: origColName,
DBName: dbName,
RetType: expr.GetType(),
}
Expand Down