Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support parsing ALTER INDEX stmt #135

Merged
merged 16 commits into from
Feb 23, 2024
69 changes: 47 additions & 22 deletions postgres/parser/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,6 @@ func (u *sqlSymUnion) alterTableCmds() tree.AlterTableCmds {
func (u *sqlSymUnion) alterIndexCmd() tree.AlterIndexCmd {
return u.val.(tree.AlterIndexCmd)
}
func (u *sqlSymUnion) alterIndexCmds() tree.AlterIndexCmds {
return u.val.(tree.AlterIndexCmds)
}
func (u *sqlSymUnion) isoLevel() tree.IsolationLevel {
return u.val.(tree.IsolationLevel)
}
Expand Down Expand Up @@ -644,7 +641,7 @@ func (u *sqlSymUnion) storageType() tree.StorageType {
%token <str> CURRENT_USER CYCLE

%token <str> DATA DATABASE DATABASES DATE DAY DEALLOCATE DEC DECIMAL DECLARE
%token <str> DEFAULT DEFAULTS DEFERRABLE DEFERRED DEFINER DELETE DESC DESTINATION DETACH DETACHED
%token <str> DEFAULT DEFAULTS DEFERRABLE DEFERRED DEFINER DELETE DEPENDS DESC DESTINATION DETACH DETACHED
%token <str> DISABLE DISCARD DISTINCT DO DOMAIN DOUBLE DROP

%token <str> ELSE ENABLE ENCODING ENCRYPTION_PASSPHRASE END ENUM ENUMS ESCAPE EXCEPT EXCLUDE EXCLUDING
Expand Down Expand Up @@ -780,6 +777,7 @@ func (u *sqlSymUnion) storageType() tree.StorageType {
// ALTER INDEX
%type <tree.Statement> alter_oneindex_stmt
%type <tree.Statement> alter_rename_index_stmt
%type <tree.Statement> alter_index_all_in_tablespace_stmt

// ALTER VIEW
%type <tree.Statement> alter_rename_view_stmt
Expand Down Expand Up @@ -983,9 +981,8 @@ func (u *sqlSymUnion) storageType() tree.StorageType {
%type <tree.AlterColComputed> alter_column_set_seq_elem
%type <[]tree.AlterColComputed> alter_column_set_seq_elem_list
%type <tree.AlterIndexCmd> alter_index_cmd
%type <tree.AlterIndexCmds> alter_index_cmds
%type <tree.StorageType> col_storage_option
%type <bool> unique_or_primary logged_or_unlogged opt_nowait
%type <bool> unique_or_primary logged_or_unlogged opt_nowait opt_no
%type <str> trigger_name trigger_option

%type <tree.DropBehavior> opt_drop_behavior
Expand Down Expand Up @@ -1621,6 +1618,7 @@ opt_role:
alter_index_stmt:
alter_oneindex_stmt
| alter_rename_index_stmt
| alter_index_all_in_tablespace_stmt
// ALTER INDEX has its error help token here because the ALTER INDEX
// prefix is spread over multiple non-terminals.
| ALTER INDEX error // SHOW HELP: ALTER INDEX
Expand All @@ -1636,13 +1634,39 @@ alter_onetable_stmt:
}

alter_oneindex_stmt:
ALTER INDEX table_index_name alter_index_cmds
ALTER INDEX table_index_name alter_index_cmd
{
$$.val = &tree.AlterIndex{Index: $3.tableIndexName(), IfExists: false, Cmd: $4.alterIndexCmd()}
}
| ALTER INDEX IF EXISTS table_index_name alter_index_cmd
{
$$.val = &tree.AlterIndex{Index: $5.tableIndexName(), IfExists: true, Cmd: $6.alterIndexCmd()}
}
| ALTER INDEX table_index_name ATTACH PARTITION index_name
{
$$.val = &tree.AlterIndex{Index: $3.tableIndexName(), Cmd: &tree.AlterIndexAttachPartition{Index: tree.UnrestrictedName($6)}}
}
| ALTER INDEX table_index_name opt_no DEPENDS ON EXTENSION name
{
$$.val = &tree.AlterIndex{Index: $3.tableIndexName(), Cmd: &tree.AlterIndexExtension{No: $4.bool(), Extension: $8}}
}

opt_no:
/* EMPTY */
{
$$.val = false
}
| NO
{
$$.val = &tree.AlterIndex{Index: $3.tableIndexName(), IfExists: false, Cmds: $4.alterIndexCmds()}
$$.val = true
}
| ALTER INDEX IF EXISTS table_index_name alter_index_cmds

alter_index_all_in_tablespace_stmt:
ALTER INDEX ALL IN TABLESPACE tablespace_name opt_owned_by_list SET TABLESPACE tablespace_name opt_nowait
{
$$.val = &tree.AlterIndex{Index: $5.tableIndexName(), IfExists: true, Cmds: $6.alterIndexCmds()}
$$.val = &tree.AlterIndexAllInTablespace{
Name: tree.Name($6), OwnedBy: $7.strs(), Tablespace: tree.Name($10),
}
}

alter_table_cmd:
Expand Down Expand Up @@ -2045,22 +2069,22 @@ opt_if_exists:
$$.val = true
}

alter_index_cmds:
alter_index_cmd
alter_index_cmd:
SET TABLESPACE tablespace_name
{
$$.val = tree.AlterIndexCmds{$1.alterIndexCmd()}
$$.val = &tree.AlterIndexSetTablespace{Tablespace: tree.Name($3)}
}
| alter_index_cmds ',' alter_index_cmd
| SET '(' storage_parameter_list ')'
{
$$.val = append($1.alterIndexCmds(), $3.alterIndexCmd())
$$.val = &tree.AlterIndexSetStorage{Params: $3.storageParams()}
}

alter_index_cmd:
partition_by
| RESET '(' storage_parameter_list ')'
{
$$.val = &tree.AlterIndexPartitionBy{
PartitionBy: $1.partitionBy(),
}
$$.val = &tree.AlterIndexSetStorage{IsReset: true, Params: $3.storageParams()}
}
| ALTER opt_column iconst32 SET STATISTICS iconst32
{
$$.val = &tree.AlterIndexSetStatistics{ColumnIdx: $3.expr(), Stats: $6.expr()}
}

alter_column_default:
Expand Down Expand Up @@ -12372,10 +12396,11 @@ unreserved_keyword:
| DAY
| DEALLOCATE
| DECLARE
| DELETE
| DEFAULTS
| DEFERRED
| DEFINER
| DELETE
| DEPENDS
| DESTINATION
| DETACH
| DETACHED
Expand Down
116 changes: 99 additions & 17 deletions postgres/parser/sem/tree/alter_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@

package tree

import "strings"

// AlterIndex represents an ALTER INDEX statement.
type AlterIndex struct {
IfExists bool
Index TableIndexName
Cmds AlterIndexCmds
Cmd AlterIndexCmd
}

var _ Statement = &AlterIndex{}
Expand All @@ -40,19 +42,31 @@ func (node *AlterIndex) Format(ctx *FmtCtx) {
ctx.WriteString("IF EXISTS ")
}
ctx.FormatNode(&node.Index)
ctx.FormatNode(&node.Cmds)
ctx.FormatNode(node.Cmd)
}

// AlterIndexCmds represents a list of index alterations.
type AlterIndexCmds []AlterIndexCmd
var _ Statement = &AlterIndexAllInTablespace{}

// AlterIndexAllInTablespace represents an ALTER INDEX ALL IN TABLESPACE statement.
type AlterIndexAllInTablespace struct {
Name Name
OwnedBy []string
Tablespace Name
NoWait bool
}

// Format implements the NodeFormatter interface.
func (node *AlterIndexCmds) Format(ctx *FmtCtx) {
for i, n := range *node {
if i > 0 {
ctx.WriteString(",")
}
ctx.FormatNode(n)
func (node *AlterIndexAllInTablespace) Format(ctx *FmtCtx) {
ctx.WriteString("ALTER INDEX ALL IN TABLESPACE ")
ctx.FormatNode(&node.Name)
if node.OwnedBy != nil {
ctx.WriteString(" OWNED BY ")
ctx.WriteString(strings.Join(node.OwnedBy, ", "))
}
ctx.WriteString(" SET TABLESPACE ")
ctx.FormatNode(&node.Tablespace)
if node.NoWait {
ctx.WriteString(" NOWAIT")
}
}

Expand All @@ -64,17 +78,85 @@ type AlterIndexCmd interface {
alterIndexCmd()
}

func (*AlterIndexPartitionBy) alterIndexCmd() {}
func (*AlterIndexAttachPartition) alterIndexCmd() {}
func (*AlterIndexExtension) alterIndexCmd() {}
func (*AlterIndexSetStatistics) alterIndexCmd() {}
func (*AlterIndexSetStorage) alterIndexCmd() {}
func (*AlterIndexSetTablespace) alterIndexCmd() {}

var _ AlterIndexCmd = &AlterIndexPartitionBy{}
var _ AlterIndexCmd = &AlterIndexAttachPartition{}
var _ AlterIndexCmd = &AlterIndexExtension{}
var _ AlterIndexCmd = &AlterIndexSetStatistics{}
var _ AlterIndexCmd = &AlterIndexSetStorage{}
var _ AlterIndexCmd = &AlterIndexSetTablespace{}

// AlterIndexAttachPartition represents an ALTER INDEX ... ATTACH PARTITION statement.
type AlterIndexAttachPartition struct {
Index UnrestrictedName
}

// Format implements the NodeFormatter interface.
func (node *AlterIndexAttachPartition) Format(ctx *FmtCtx) {
ctx.WriteString(" ATTACH PARTITION ")
ctx.FormatNode(&node.Index)
}

// AlterIndexExtension represents an ALTER INDEX ... [NO] DEPENDS ON EXTENSION statement.
type AlterIndexExtension struct {
No bool
Extension string
}

// Format implements the NodeFormatter interface.
func (node *AlterIndexExtension) Format(ctx *FmtCtx) {
if node.No {
ctx.WriteString(" NO")
}
ctx.WriteString(" DEPENDS ON EXTENSION ")
ctx.WriteString(node.Extension)
}

// AlterIndexSetStatistics represents an ALTER INDEX ... SET TABLESPACE
// command.
type AlterIndexSetStatistics struct {
ColumnIdx Expr
Stats Expr
}

// Format implements the NodeFormatter interface.
func (node *AlterIndexSetStatistics) Format(ctx *FmtCtx) {
ctx.WriteString(" ALTER COLUMN ")
ctx.FormatNode(node.ColumnIdx)
ctx.WriteString(" SET STATISTICS ")
ctx.FormatNode(node.Stats)
}

// AlterIndexSetStorage represents an ALTER INDEX ... SET TABLESPACE
// command.
type AlterIndexSetStorage struct {
Params StorageParams
IsReset bool
}

// Format implements the NodeFormatter interface.
func (node *AlterIndexSetStorage) Format(ctx *FmtCtx) {
if node.IsReset {
ctx.WriteString(" RESET ( ")
} else {
ctx.WriteString(" SET ( ")
}
ctx.FormatNode(&node.Params)
ctx.WriteString(" )")
}

// AlterIndexPartitionBy represents an ALTER INDEX PARTITION BY
// AlterIndexSetTablespace represents an ALTER INDEX ... SET TABLESPACE
// command.
type AlterIndexPartitionBy struct {
*PartitionBy
type AlterIndexSetTablespace struct {
Tablespace Name
}

// Format implements the NodeFormatter interface.
func (node *AlterIndexPartitionBy) Format(ctx *FmtCtx) {
ctx.FormatNode(node.PartitionBy)
func (node *AlterIndexSetTablespace) Format(ctx *FmtCtx) {
ctx.WriteString(" SET TABLESPACE ")
ctx.FormatNode(&node.Tablespace)
}
9 changes: 9 additions & 0 deletions postgres/parser/sem/tree/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ func (*AlterIndex) StatementTag() string { return "ALTER INDEX" }

func (*AlterIndex) hiddenFromShowQueries() {}

// StatementType implements the Statement interface.
func (*AlterIndexAllInTablespace) StatementType() StatementType { return DDL }

// StatementTag returns a short string identifying the type of statement.
func (*AlterIndexAllInTablespace) StatementTag() string { return "ALTER INDEX ALL IN TABLESPACE" }

func (*AlterIndexAllInTablespace) hiddenFromShowQueries() {}

// StatementType implements the Statement interface.
func (*AlterTable) StatementType() StatementType { return DDL }

Expand Down Expand Up @@ -1086,6 +1094,7 @@ func (n *AlterAggregate) String() string { return AsString(n) }
func (n *AlterCollation) String() string { return AsString(n) }
func (n *AlterConversion) String() string { return AsString(n) }
func (n *AlterIndex) String() string { return AsString(n) }
func (n *AlterIndexAllInTablespace) String() string { return AsString(n) }
func (n *AlterDatabase) String() string { return AsString(n) }
func (n *AlterDefaultPrivileges) String() string { return AsString(n) }
func (n *AlterSchema) String() string { return AsString(n) }
Expand Down
1 change: 1 addition & 0 deletions testing/generation/command_docs/custom_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var GlobalCustomVariables = map[string]StatementGenerator{
"code": customDefinition(`'code'`),
"collation": customDefinition(`en_US`),
"column_definition": customDefinition(`v1 INTEGER`),
"column_number": customDefinition(`1`),
"connlimit": customDefinition(`-1`),
"cycle_mark_default": customDefinition(`'cycle_mark_default'`),
"cycle_mark_value": customDefinition(`'cycle_mark_value'`),
Expand Down
Loading
Loading