Skip to content

Commit

Permalink
Merge pull request #5957 from bdarnell/cascade
Browse files Browse the repository at this point in the history
sql: Parse (and ignore) "drop behavior" options
  • Loading branch information
bdarnell committed Apr 13, 2016
2 parents a5c5665 + e17134f commit f53025d
Show file tree
Hide file tree
Showing 7 changed files with 792 additions and 680 deletions.
11 changes: 5 additions & 6 deletions sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,24 @@ func (p *planner) AlterTable(n *parser.AlterTable) (planNode, *roachpb.Error) {
}
}

case *parser.AlterTableSetDefault, *parser.AlterTableDropNotNull:
colMut := t.(parser.ColumnMutationCmd)
case parser.ColumnMutationCmd:
// Column mutations
status, i, err := tableDesc.FindColumnByName(colMut.GetColumn())
status, i, err := tableDesc.FindColumnByName(t.GetColumn())
if err != nil {
return nil, roachpb.NewError(err)
}

switch status {
case DescriptorActive:
applyColumnMutation(&tableDesc.Columns[i], colMut)
applyColumnMutation(&tableDesc.Columns[i], t)
descriptorChanged = true

case DescriptorIncomplete:
switch tableDesc.Mutations[i].Direction {
case DescriptorMutation_ADD:
return nil, roachpb.NewUErrorf("column %q in the middle of being added, try again later", colMut.GetColumn())
return nil, roachpb.NewUErrorf("column %q in the middle of being added, try again later", t.GetColumn())
case DescriptorMutation_DROP:
return nil, roachpb.NewUErrorf("column %q in the middle of being dropped", colMut.GetColumn())
return nil, roachpb.NewUErrorf("column %q in the middle of being dropped", t.GetColumn())
}
}

Expand Down
20 changes: 17 additions & 3 deletions sql/parser/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type AlterTableDropColumn struct {
columnKeyword bool
IfExists bool
Column string
DropBehavior DropBehavior
}

func (node *AlterTableDropColumn) String() string {
Expand All @@ -118,17 +119,30 @@ func (node *AlterTableDropColumn) String() string {
_, _ = buf.WriteString(" IF EXISTS")
}
fmt.Fprintf(&buf, " %s", node.Column)
if node.DropBehavior != DropDefault {
fmt.Fprintf(&buf, " %s", node.DropBehavior)
}
return buf.String()
}

// AlterTableDropConstraint represents a DROP CONSTRAINT command.
type AlterTableDropConstraint struct {
IfExists bool
Constraint string
IfExists bool
Constraint string
DropBehavior DropBehavior
}

func (node *AlterTableDropConstraint) String() string {
return fmt.Sprintf("DROP CONSTRAINT %s", node.Constraint)
var buf bytes.Buffer
_, _ = buf.WriteString("DROP CONSTRAINT ")
if node.IfExists {
_, _ = buf.WriteString("IF EXISTS ")
}
_, _ = buf.WriteString(node.Constraint)
if node.DropBehavior != DropDefault {
fmt.Fprintf(&buf, " %s", node.DropBehavior)
}
return buf.String()
}

// AlterTableSetDefault represents an ALTER COLUMN SET DEFAULT
Expand Down
33 changes: 30 additions & 3 deletions sql/parser/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,30 @@

package parser

import "bytes"
import (
"bytes"
"fmt"
)

// DropBehavior represents options for dropping schema elements.
type DropBehavior int

// DropBehavior values.
const (
DropDefault DropBehavior = iota
DropRestrict
DropCascade
)

var dropBehaviorName = [...]string{
DropDefault: "",
DropRestrict: "RESTRICT",
DropCascade: "CASCADE",
}

func (d DropBehavior) String() string {
return dropBehaviorName[d]
}

// DropDatabase represents a DROP DATABASE statement.
type DropDatabase struct {
Expand All @@ -42,8 +65,9 @@ func (node *DropDatabase) String() string {

// DropIndex represents a DROP INDEX statement.
type DropIndex struct {
IndexList TableNameWithIndexList
IfExists bool
IndexList TableNameWithIndexList
IfExists bool
DropBehavior DropBehavior
}

func (node *DropIndex) String() string {
Expand All @@ -53,6 +77,9 @@ func (node *DropIndex) String() string {
buf.WriteString("IF EXISTS ")
}
buf.WriteString(node.IndexList.String())
if node.DropBehavior != DropDefault {
fmt.Fprintf(&buf, " %s", node.DropBehavior)
}
return buf.String()
}

Expand Down
7 changes: 7 additions & 0 deletions sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func TestParse(t *testing.T) {
{`DROP INDEX IF EXISTS a.b@c`},
{`DROP INDEX a.b@c, d@f`},
{`DROP INDEX IF EXISTS a.b@c, d@f`},
{`DROP INDEX a.b@c CASCADE`},
{`DROP INDEX IF EXISTS a.b@c RESTRICT`},

{`EXPLAIN SELECT 1`},
{`EXPLAIN (DEBUG) SELECT 1`},
Expand Down Expand Up @@ -370,6 +372,7 @@ func TestParse(t *testing.T) {

{`TRUNCATE TABLE a`},
{`TRUNCATE TABLE a, b.c`},
{`TRUNCATE TABLE a CASCADE`},

{`UPDATE a SET b = 3`},
{`UPDATE a.b SET b = 3`},
Expand Down Expand Up @@ -411,6 +414,10 @@ func TestParse(t *testing.T) {
{`ALTER TABLE a DROP COLUMN IF EXISTS b, DROP CONSTRAINT a_idx`},
{`ALTER TABLE IF EXISTS a DROP COLUMN b, DROP CONSTRAINT a_idx`},
{`ALTER TABLE IF EXISTS a DROP COLUMN IF EXISTS b, DROP CONSTRAINT a_idx`},
{`ALTER TABLE a DROP COLUMN b CASCADE`},
{`ALTER TABLE a DROP COLUMN b RESTRICT`},
{`ALTER TABLE a DROP CONSTRAINT b CASCADE`},
{`ALTER TABLE a DROP CONSTRAINT IF EXISTS b RESTRICT`},

{`ALTER TABLE a ALTER COLUMN b SET DEFAULT 42`},
{`ALTER TABLE a ALTER COLUMN b SET DEFAULT NULL`},
Expand Down
Loading

0 comments on commit f53025d

Please sign in to comment.