Skip to content

Commit

Permalink
server: return empty org_name for resultField (#45146)
Browse files Browse the repository at this point in the history
close #45145
  • Loading branch information
YangKeao committed Jul 5, 2023
1 parent d677a0c commit d639cb8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
3 changes: 3 additions & 0 deletions executor/adapter.go
Expand Up @@ -109,12 +109,15 @@ func colNames2ResultFields(schema *expression.Schema, names []*types.FieldName,
dbName = defaultDBCIStr
}
origColName := names[i].OrigColName
emptyOrgName := false
if origColName.L == "" {
origColName = names[i].ColName
emptyOrgName = true
}
rf := &ast.ResultField{
Column: &model.ColumnInfo{Name: origColName, FieldType: *schema.Columns[i].RetType},
ColumnAsName: names[i].ColName,
EmptyOrgName: emptyOrgName,
Table: &model.TableInfo{Name: names[i].OrigTblName},
TableAsName: names[i].TblName,
DBName: dbName,
Expand Down
18 changes: 7 additions & 11 deletions parser/ast/ast.go
Expand Up @@ -125,18 +125,14 @@ type DMLNode interface {
type ResultField struct {
Column *model.ColumnInfo
ColumnAsName model.CIStr
Table *model.TableInfo
TableAsName model.CIStr
DBName model.CIStr
// EmptyOrgName indicates whether this field has an empty org_name. A field has an empty org name, if it's an
// expression. It's not sure whether it's safe to use empty string in `.Column.Name`, so a new field is added to
// indicate whether it's empty.
EmptyOrgName bool

// Expr represents the expression for the result field. If it is generated from a select field, it would
// be the expression of that select field, otherwise the type would be ValueExpr and value
// will be set for every retrieved row.
Expr ExprNode
TableName *TableName
// Referenced indicates the result field has been referenced or not.
// If not, we don't need to get the values.
Referenced bool
Table *model.TableInfo
TableAsName model.CIStr
DBName model.CIStr
}

// ResultSetNode interface has a ResultFields property, represents a Node that returns result set.
Expand Down
46 changes: 34 additions & 12 deletions server/conn_test.go
Expand Up @@ -386,8 +386,8 @@ func TestDispatch(t *testing.T) {
in: []byte("select 1"),
err: nil,
out: []byte{
0xc, 0x0, 0x0, 0x3, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x18,
0x0, 0x0, 0x4, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0, 0x1, 0x31, 0x1, 0x31, 0xc, 0x3f,
0xc, 0x0, 0x0, 0x3, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x17,
0x0, 0x0, 0x4, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0, 0x1, 0x31, 0x0, 0xc, 0x3f,
0x0, 0x1, 0x0, 0x0, 0x0, 0x8, 0x81, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x5, 0xfe,
},
},
Expand All @@ -396,8 +396,8 @@ func TestDispatch(t *testing.T) {
in: []byte{0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x1, 0x0},
err: nil,
out: []byte{
0x1, 0x0, 0x0, 0x6, 0x1, 0x18, 0x0, 0x0, 0x7, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0,
0x1, 0x31, 0x1, 0x31, 0xc, 0x3f, 0x0, 0x1, 0x0, 0x0, 0x0, 0x8, 0x81, 0x0, 0x0, 0x0,
0x1, 0x0, 0x0, 0x6, 0x1, 0x17, 0x0, 0x0, 0x7, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0,
0x1, 0x31, 0x0, 0xc, 0x3f, 0x0, 0x1, 0x0, 0x0, 0x0, 0x8, 0x81, 0x0, 0x0, 0x0,
0x0, 0x1, 0x0, 0x0, 0x8, 0xfe,
},
},
Expand Down Expand Up @@ -505,8 +505,8 @@ func TestDispatchClientProtocol41(t *testing.T) {
in: []byte("select 1"),
err: nil,
out: []byte{
0xc, 0x0, 0x0, 0x3, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x18,
0x0, 0x0, 0x4, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0, 0x1, 0x31, 0x1, 0x31, 0xc, 0x3f,
0xc, 0x0, 0x0, 0x3, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x17,
0x0, 0x0, 0x4, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0, 0x1, 0x31, 0x0, 0xc, 0x3f,
0x0, 0x1, 0x0, 0x0, 0x0, 0x8, 0x81, 0x0, 0x0, 0x0, 0x0, 0x5, 0x0, 0x0, 0x5, 0xfe,
0x0, 0x0, 0x2, 0x0,
},
Expand All @@ -516,8 +516,8 @@ func TestDispatchClientProtocol41(t *testing.T) {
in: []byte{0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x1, 0x0},
err: nil,
out: []byte{
0x1, 0x0, 0x0, 0x6, 0x1, 0x18, 0x0, 0x0, 0x7, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0,
0x1, 0x31, 0x1, 0x31, 0xc, 0x3f, 0x0, 0x1, 0x0, 0x0, 0x0, 0x8, 0x81, 0x0, 0x0, 0x0,
0x1, 0x0, 0x0, 0x6, 0x1, 0x17, 0x0, 0x0, 0x7, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0,
0x1, 0x31, 0x0, 0xc, 0x3f, 0x0, 0x1, 0x0, 0x0, 0x0, 0x8, 0x81, 0x0, 0x0, 0x0,
0x0, 0x5, 0x0, 0x0, 0x8, 0xfe, 0x0, 0x0, 0x42, 0x0,
},
},
Expand Down Expand Up @@ -591,8 +591,8 @@ func TestQueryEndWithZero(t *testing.T) {
in: append([]byte("select 1"), 0x0),
err: nil,
out: []byte{
0xc, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x18,
0x0, 0x0, 0x1, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0, 0x1, 0x31, 0x1, 0x31, 0xc, 0x3f,
0xc, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x17,
0x0, 0x0, 0x1, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0, 0x1, 0x31, 0x0, 0xc, 0x3f,
0x0, 0x1, 0x0, 0x0, 0x0, 0x8, 0x81, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x2, 0xfe,
},
},
Expand All @@ -601,8 +601,8 @@ func TestQueryEndWithZero(t *testing.T) {
in: append([]byte("select 1"), 0x0),
err: nil,
out: []byte{
0x1, 0x0, 0x0, 0x3, 0x1, 0x18, 0x0, 0x0, 0x4, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0,
0x1, 0x31, 0x1, 0x31, 0xc, 0x3f, 0x0, 0x1, 0x0, 0x0, 0x0, 0x8, 0x81, 0x0, 0x0, 0x0,
0x1, 0x0, 0x0, 0x3, 0x1, 0x17, 0x0, 0x0, 0x4, 0x3, 0x64, 0x65, 0x66, 0x0, 0x0, 0x0,
0x1, 0x31, 0x0, 0xc, 0x3f, 0x0, 0x1, 0x0, 0x0, 0x0, 0x8, 0x81, 0x0, 0x0, 0x0,
0x0, 0x1, 0x0, 0x0, 0x5, 0xfe, 0x2, 0x0, 0x0, 0x6, 0x1, 0x31, 0x1, 0x0, 0x0, 0x7, 0xfe,
},
},
Expand Down Expand Up @@ -2055,3 +2055,25 @@ func TestLDAPAuthSwitch(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []byte(mysql.AuthMySQLClearPassword), respAuthSwitch)
}

func TestEmptyOrgName(t *testing.T) {
inputs := []dispatchInput{
{
com: mysql.ComQuery,
in: append([]byte("SELECT DATE_FORMAT(CONCAT('2023-07-0', a), '%Y') AS 'YEAR' FROM test.t"), 0x0),
err: nil,
out: []byte{0x1, 0x0, 0x0, 0x0, 0x1, // 1 column
0x1a, 0x0, 0x0,
0x1, 0x3, 0x64, 0x65, 0x66, // catalog
0x0, // schema
0x0, // table name
0x0, // org table
0x4, 0x59, 0x45, 0x41, 0x52, // name 'YEAR'
0x0, // org name
0xc, 0x2e, 0x0, 0x2c, 0x0, 0x0, 0x0, 0xfd, 0x0, 0x0, 0x1f, 0x0, 0x0, 0x1, 0x0, 0x0, 0x2, 0xfe, 0x5, 0x0,
0x0, 0x3, 0x4, 0x32, 0x30, 0x32, 0x33, 0x1, 0x0, 0x0, 0x4, 0xfe},
},
}

testDispatch(t, inputs, 0)
}
3 changes: 3 additions & 0 deletions server/driver_tidb.go
Expand Up @@ -498,6 +498,9 @@ func convertColumnInfo(fld *ast.ResultField) (ci *column.Info) {
DefaultValue: fld.Column.GetDefaultValue(),
}

if fld.EmptyOrgName {
ci.OrgName = ""
}
if fld.Table != nil {
ci.OrgTable = fld.Table.Name.O
}
Expand Down
2 changes: 1 addition & 1 deletion server/tidb_test.go
Expand Up @@ -1100,7 +1100,7 @@ func TestFieldList(t *testing.T) {
rs, err := Execute(ctx, qctx, "select "+tooLongColumnAsName)
require.NoError(t, err)
cols := rs.Columns()
require.Equal(t, tooLongColumnAsName, cols[0].OrgName)
require.Equal(t, "", cols[0].OrgName)
require.Equal(t, columnAsName, cols[0].Name)
rs.Close()

Expand Down
14 changes: 6 additions & 8 deletions session/nontransactional.go
Expand Up @@ -264,10 +264,9 @@ func runJobs(ctx context.Context, jobs []job, stmt *ast.NonTransactionalDMLStmt,
for _, col := range tableName.TableInfo.Columns {
if col.Name.L == stmt.ShardColumn.Name.L {
shardColumnRefer = &ast.ResultField{
Column: col,
Table: tableName.TableInfo,
DBName: tableName.Schema,
TableName: tableName,
Column: col,
Table: tableName.TableInfo,
DBName: tableName.Schema,
}
shardColumnType = col.FieldType
}
Expand Down Expand Up @@ -301,10 +300,9 @@ func runJobs(ctx context.Context, jobs []job, stmt *ast.NonTransactionalDMLStmt,
if shardColumnRefer == nil {
shardColumnType = *types.NewFieldType(mysql.TypeLonglong)
shardColumnRefer = &ast.ResultField{
Column: model.NewExtraHandleColInfo(),
Table: tableName.TableInfo,
DBName: tableName.Schema,
TableName: tableName,
Column: model.NewExtraHandleColInfo(),
Table: tableName.TableInfo,
DBName: tableName.Schema,
}
}
stmtBuildInfo := statementBuildInfo{
Expand Down

0 comments on commit d639cb8

Please sign in to comment.