Skip to content

Commit

Permalink
sql/parser: parsing for interleaved tables/indexes
Browse files Browse the repository at this point in the history
I ran doc generation locally and the SKIP DOC seems to work and keep this off
the diagrams.

For cockroachdb#2972.
  • Loading branch information
danhhz committed Jul 8, 2016
1 parent ffcdab5 commit 5bfef97
Show file tree
Hide file tree
Showing 8 changed files with 4,704 additions and 4,512 deletions.
9 changes: 9 additions & 0 deletions sql/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/sql/privilege"
"github.com/cockroachdb/cockroach/sql/sqlbase"
"github.com/cockroachdb/cockroach/util"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -151,6 +152,10 @@ func (n *createIndexNode) Start() error {
}
}

if n.n.Interleave != nil {
return util.UnimplementedWithIssueErrorf(2972, "interleaving is not yet supported")
}

indexDesc := sqlbase.IndexDescriptor{
Name: string(n.n.Name),
Unique: n.n.Unique,
Expand Down Expand Up @@ -302,6 +307,10 @@ func (n *createTableNode) Start() error {
return err
}

if n.n.Interleave != nil {
return util.UnimplementedWithIssueErrorf(2972, "interleaving is not yet supported")
}

created, err := n.p.createDescriptor(tableKey{n.dbDesc.ID, n.n.Table.Table()}, &desc, n.n.IfNotExists)
if err != nil {
return err
Expand Down
49 changes: 45 additions & 4 deletions sql/parser/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ type CreateIndex struct {
Columns IndexElemList
// Extra columns to be stored together with the indexed ones as an optimization
// for improved reading performance.
Storing NameList
Storing NameList
Interleave *InterleaveDef
}

// Format implements the NodeFormatter interface.
Expand All @@ -107,6 +108,9 @@ func (node *CreateIndex) Format(buf *bytes.Buffer, f FmtFlags) {
if node.Storing != nil {
fmt.Fprintf(buf, " STORING (%s)", node.Storing)
}
if node.Interleave != nil {
FormatNode(buf, f, node.Interleave)
}
}

// TableDef represents a column, index or constraint definition within a CREATE
Expand Down Expand Up @@ -351,9 +355,10 @@ func NameListToIndexElems(lst NameList) IndexElemList {
// IndexTableDef represents an index definition within a CREATE TABLE
// statement.
type IndexTableDef struct {
Name Name
Columns IndexElemList
Storing NameList
Name Name
Columns IndexElemList
Storing NameList
Interleave *InterleaveDef
}

func (node *IndexTableDef) setName(name Name) {
Expand All @@ -375,6 +380,9 @@ func (node *IndexTableDef) Format(buf *bytes.Buffer, f FmtFlags) {
FormatNode(buf, f, node.Storing)
buf.WriteByte(')')
}
if node.Interleave != nil {
FormatNode(buf, f, node.Interleave)
}
}

// ConstraintTableDef represents a constraint definition within a CREATE TABLE
Expand Down Expand Up @@ -413,6 +421,9 @@ func (node *UniqueConstraintTableDef) Format(buf *bytes.Buffer, f FmtFlags) {
FormatNode(buf, f, node.Storing)
buf.WriteByte(')')
}
if node.Interleave != nil {
FormatNode(buf, f, node.Interleave)
}
}

func (*CheckConstraintTableDef) tableDef() {}
Expand Down Expand Up @@ -486,10 +497,37 @@ func (node *FamilyTableDef) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteByte(')')
}

// InterleaveDef represents an interleave definition within a CREATE TABLE
// or CREATE INDEX statement.
type InterleaveDef struct {
Parent *QualifiedName
Fields []string
DropBehavior DropBehavior
}

// Format implements the NodeFormatter interface.
func (node *InterleaveDef) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString(" INTERLEAVE IN PARENT ")
FormatNode(buf, f, node.Parent)
buf.WriteString(" (")
for i, field := range node.Fields {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(field)
}
buf.WriteString(")")
if node.DropBehavior != DropDefault {
buf.WriteString(" ")
buf.WriteString(node.DropBehavior.String())
}
}

// CreateTable represents a CREATE TABLE statement.
type CreateTable struct {
IfNotExists bool
Table *QualifiedName
Interleave *InterleaveDef
Defs TableDefs
}

Expand All @@ -503,4 +541,7 @@ func (node *CreateTable) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString(" (")
FormatNode(buf, f, node.Defs)
buf.WriteByte(')')
if node.Interleave != nil {
FormatNode(buf, f, node.Interleave)
}
}
2 changes: 2 additions & 0 deletions sql/parser/keywords.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ func TestParse(t *testing.T) {
{`CREATE INDEX a ON b.c (d)`},
{`CREATE INDEX ON a (b)`},
{`CREATE INDEX ON a (b) STORING (c)`},
{`CREATE INDEX ON a (b) INTERLEAVE IN PARENT c (d)`},
{`CREATE INDEX ON a (b ASC, c DESC)`},
{`CREATE UNIQUE INDEX a ON b (c)`},
{`CREATE UNIQUE INDEX a ON b (c) STORING (d)`},
{`CREATE UNIQUE INDEX a ON b (c) INTERLEAVE IN PARENT d (e, f)`},
{`CREATE UNIQUE INDEX a ON b.c (d)`},

{`CREATE TABLE a ()`},
Expand Down Expand Up @@ -96,6 +98,7 @@ func TestParse(t *testing.T) {
{`CREATE TABLE a (b INT, c TEXT, INDEX (b, c))`},
{`CREATE TABLE a (b INT, c TEXT, INDEX d (b, c))`},
{`CREATE TABLE a (b INT, c TEXT, CONSTRAINT d UNIQUE (b, c))`},
{`CREATE TABLE a (b INT, c TEXT, CONSTRAINT d UNIQUE (b, c) INTERLEAVE IN PARENT d (e, f))`},
{`CREATE TABLE a (b INT, UNIQUE (b))`},
{`CREATE TABLE a (b INT, UNIQUE (b) STORING (c))`},
{`CREATE TABLE a (b INT, INDEX (b))`},
Expand All @@ -104,8 +107,11 @@ func TestParse(t *testing.T) {
{`CREATE TABLE a (b INT, c INT REFERENCES foo (bar))`},
{`CREATE TABLE a (b INT, INDEX (b) STORING (c))`},
{`CREATE TABLE a (b INT, c TEXT, INDEX (b ASC, c DESC) STORING (c))`},
{`CREATE TABLE a (b INT, INDEX (b) INTERLEAVE IN PARENT c (d, e))`},
{`CREATE TABLE a (b INT, FAMILY (b))`},
{`CREATE TABLE a (b INT, c STRING, FAMILY foo (b), FAMILY (c))`},
{`CREATE TABLE a (b INT) INTERLEAVE IN PARENT foo (c, d)`},
{`CREATE TABLE a (b INT) INTERLEAVE IN PARENT foo (c) CASCADE`},
{`CREATE TABLE a.b (b INT)`},
{`CREATE TABLE IF NOT EXISTS a (b INT)`},

Expand Down Expand Up @@ -505,6 +511,8 @@ func TestParse2(t *testing.T) {
}{
{`CREATE TABLE a (b INT, UNIQUE INDEX foo (b))`,
`CREATE TABLE a (b INT, CONSTRAINT foo UNIQUE (b))`},
{`CREATE TABLE a (b INT, UNIQUE INDEX foo (b) INTERLEAVE IN PARENT c (d))`,
`CREATE TABLE a (b INT, CONSTRAINT foo UNIQUE (b) INTERLEAVE IN PARENT c (d))`},
{`CREATE INDEX ON a (b) COVERING (c)`, `CREATE INDEX ON a (b) STORING (c)`},

{`SELECT BOOL 'foo'`, `SELECT CAST('foo' AS BOOL)`},
Expand Down
Loading

0 comments on commit 5bfef97

Please sign in to comment.