Skip to content

Commit

Permalink
parser: polish the error message for generated column with default va…
Browse files Browse the repository at this point in the history
…lue (#43669)

close #22535
  • Loading branch information
tiancaiamao authored May 11, 2023
1 parent 2e8bc40 commit bc98c8d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
24 changes: 15 additions & 9 deletions parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,10 @@ const (
)

var (
invalidOptionForGeneratedColumn = map[ColumnOptionType]struct{}{
ColumnOptionAutoIncrement: {},
ColumnOptionOnUpdate: {},
ColumnOptionDefaultValue: {},
invalidOptionForGeneratedColumn = map[ColumnOptionType]string{
ColumnOptionAutoIncrement: "AUTO_INCREMENT",
ColumnOptionOnUpdate: "ON UPDATE",
ColumnOptionDefaultValue: "DEFAULT",
}
)

Expand Down Expand Up @@ -1007,17 +1007,22 @@ func (n *ColumnDef) Accept(v Visitor) (Node, bool) {
// For example, generated column definitions that contain such
// column options as `ON UPDATE`, `AUTO_INCREMENT`, `DEFAULT`
// are illegal.
func (n *ColumnDef) Validate() bool {
func (n *ColumnDef) Validate() error {
generatedCol := false
illegalOpt4gc := false
var illegalOpt4gc string
for _, opt := range n.Options {
if opt.Tp == ColumnOptionGenerated {
generatedCol = true
}
_, found := invalidOptionForGeneratedColumn[opt.Tp]
illegalOpt4gc = illegalOpt4gc || found
msg, found := invalidOptionForGeneratedColumn[opt.Tp]
if found {
illegalOpt4gc = msg
}
}
if generatedCol && illegalOpt4gc != "" {
return ErrWrongUsage.GenWithStackByArgs(illegalOpt4gc, "generated column")
}
return !(generatedCol && illegalOpt4gc)
return nil
}

type TemporaryKeyword int
Expand Down Expand Up @@ -3689,6 +3694,7 @@ var (
ErrWrongPartitionTypeExpectedSystemTime = terror.ClassDDL.NewStd(mysql.ErrWrongPartitionTypeExpectedSystemTime)
ErrUnknownCharacterSet = terror.ClassDDL.NewStd(mysql.ErrUnknownCharacterSet)
ErrCoalescePartitionNoPartition = terror.ClassDDL.NewStd(mysql.ErrCoalescePartitionNoPartition)
ErrWrongUsage = terror.ClassDDL.NewStd(mysql.ErrWrongUsage)
)

type SubPartitionDefinition struct {
Expand Down
8 changes: 4 additions & 4 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14012,8 +14012,8 @@ yynewstate:
case 212:
{
colDef := &ast.ColumnDef{Name: yyS[yypt-2].item.(*ast.ColumnName), Tp: yyS[yypt-1].item.(*types.FieldType), Options: yyS[yypt-0].item.([]*ast.ColumnOption)}
if !colDef.Validate() {
yylex.AppendError(yylex.Errorf("Invalid column definition"))
if err := colDef.Validate(); err != nil {
yylex.AppendError(err)
return 1
}
parser.yyVAL.item = colDef
Expand All @@ -14026,8 +14026,8 @@ yynewstate:
options = append(options, yyS[yypt-0].item.([]*ast.ColumnOption)...)
tp.AddFlag(mysql.UnsignedFlag)
colDef := &ast.ColumnDef{Name: yyS[yypt-2].item.(*ast.ColumnName), Tp: tp, Options: options}
if !colDef.Validate() {
yylex.AppendError(yylex.Errorf("Invalid column definition"))
if err := colDef.Validate(); err != nil {
yylex.AppendError(err)
return 1
}
parser.yyVAL.item = colDef
Expand Down
8 changes: 4 additions & 4 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -3140,8 +3140,8 @@ ColumnDef:
ColumnName Type ColumnOptionListOpt
{
colDef := &ast.ColumnDef{Name: $1.(*ast.ColumnName), Tp: $2.(*types.FieldType), Options: $3.([]*ast.ColumnOption)}
if !colDef.Validate() {
yylex.AppendError(yylex.Errorf("Invalid column definition"))
if err := colDef.Validate(); err != nil {
yylex.AppendError(err)
return 1
}
$$ = colDef
Expand All @@ -3154,8 +3154,8 @@ ColumnDef:
options = append(options, $3.([]*ast.ColumnOption)...)
tp.AddFlag(mysql.UnsignedFlag)
colDef := &ast.ColumnDef{Name: $1.(*ast.ColumnName), Tp: tp, Options: options}
if !colDef.Validate() {
yylex.AppendError(yylex.Errorf("Invalid column definition"))
if err := colDef.Validate(); err != nil {
yylex.AppendError(err)
return 1
}
$$ = colDef
Expand Down
7 changes: 7 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5716,6 +5716,13 @@ func TestGeneratedColumn(t *testing.T) {
require.Error(t, err)
}
}

_, _, err := p.Parse("create table t1 (a int, b int as (a + 1) default 10);", "", "")
require.Equal(t, err.Error(), "[ddl:1221]Incorrect usage of DEFAULT and generated column")
_, _, err = p.Parse("create table t1 (a int, b int as (a + 1) on update now());", "", "")
require.Equal(t, err.Error(), "[ddl:1221]Incorrect usage of ON UPDATE and generated column")
_, _, err = p.Parse("create table t1 (a int, b int as (a + 1) auto_increment);", "", "")
require.Equal(t, err.Error(), "[ddl:1221]Incorrect usage of AUTO_INCREMENT and generated column")
}

func TestSetTransaction(t *testing.T) {
Expand Down

0 comments on commit bc98c8d

Please sign in to comment.