Skip to content

Commit

Permalink
plan: process wildstar correctly. (pingcap#2038)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored and ngaut committed Nov 20, 2016
1 parent 6dc49a7 commit 8fa03ee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
8 changes: 3 additions & 5 deletions plan/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,17 +738,15 @@ func (b *planBuilder) resolveGbyExprs(p LogicalPlan, gby *ast.GroupByClause, fie
}

func (b *planBuilder) unfoldWildStar(p LogicalPlan, selectFields []*ast.SelectField) (resultList []*ast.SelectField) {
hasWildCard := false
for _, field := range selectFields {
for i, field := range selectFields {
if field.WildCard == nil {
resultList = append(resultList, field)
continue
}
if hasWildCard && field.WildCard.Table.L == "" {
b.err = ErrMultiWildCard
if field.WildCard.Table.L == "" && i > 0 {
b.err = ErrInvalidWildCard
return
}
hasWildCard = true
dbName := field.WildCard.Schema
tblName := field.WildCard.Table
for _, col := range p.GetSchema() {
Expand Down
6 changes: 3 additions & 3 deletions plan/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func PrepareStmt(is infoschema.InfoSchema, ctx context.Context, node ast.Node) e
const (
CodeOneColumn terror.ErrCode = 1
CodeSameColumns terror.ErrCode = 2
CodeMultiWildCard terror.ErrCode = 3
CodeInvalidWildCard terror.ErrCode = 3
CodeUnsupported terror.ErrCode = 4
CodeInvalidGroupFuncUse terror.ErrCode = 5
CodeIllegalReference terror.ErrCode = 6
Expand All @@ -111,7 +111,7 @@ const (
var (
ErrOneColumn = terror.ClassOptimizer.New(CodeOneColumn, "Operand should contain 1 column(s)")
ErrSameColumns = terror.ClassOptimizer.New(CodeSameColumns, "Operands should contain same columns")
ErrMultiWildCard = terror.ClassOptimizer.New(CodeMultiWildCard, "wildcard fields exist more than once without any table name")
ErrInvalidWildCard = terror.ClassOptimizer.New(CodeInvalidWildCard, "Wildcard fields without any table name appears in wrong place")
ErrCartesianProductUnsupported = terror.ClassOptimizer.New(CodeUnsupported, "Cartesian product is unsupported")
ErrInvalidGroupFuncUse = terror.ClassOptimizer.New(CodeInvalidGroupFuncUse, "Invalid use of group function")
ErrIllegalReference = terror.ClassOptimizer.New(CodeIllegalReference, "Illegal reference")
Expand All @@ -121,7 +121,7 @@ func init() {
mySQLErrCodes := map[terror.ErrCode]uint16{
CodeOneColumn: mysql.ErrOperandColumns,
CodeSameColumns: mysql.ErrOperandColumns,
CodeMultiWildCard: mysql.ErrParse,
CodeInvalidWildCard: mysql.ErrParse,
CodeInvalidGroupFuncUse: mysql.ErrInvalidGroupFuncUse,
CodeIllegalReference: mysql.ErrIllegalReference,
}
Expand Down
12 changes: 12 additions & 0 deletions plan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2038,6 +2038,18 @@ func (s *testPlanSuite) TestValidate(c *C) {
sql: "select (1,2) < 3",
err: ErrSameColumns,
},
{
sql: "select 1, * from t",
err: ErrInvalidWildCard,
},
{
sql: "select *, 1 from t",
err: nil,
},
{
sql: "select 1, t.* from t",
err: nil,
},
}
for _, ca := range cases {
sql := ca.sql
Expand Down

0 comments on commit 8fa03ee

Please sign in to comment.