Skip to content

Commit

Permalink
support all ALTER DATABASE cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifersp committed Jan 5, 2024
1 parent b798e6e commit 5b0f471
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 69 deletions.
63 changes: 48 additions & 15 deletions postgres/parser/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,9 @@ func (u *sqlSymUnion) databaseOption() tree.DatabaseOption {
func (u *sqlSymUnion) databaseOptionList() []tree.DatabaseOption {
return u.val.([]tree.DatabaseOption)
}
func (u *sqlSymUnion) setVar() *tree.SetVar {
return u.val.(*tree.SetVar)
}
%}

// NB: the %token definitions must come before the %type definitions in this
Expand Down Expand Up @@ -667,7 +670,7 @@ func (u *sqlSymUnion) databaseOptionList() []tree.DatabaseOption {
%token <str> START STATISTICS STATUS STDIN STRICT STRING STORAGE STORE STORED STORING SUBSTRING
%token <str> SYMMETRIC SYNTAX SYSTEM SQRT SUBSCRIPTION

%token <str> TABLE TABLES TEMP TEMPLATE TEMPORARY TENANT TESTING_RELOCATE EXPERIMENTAL_RELOCATE TEXT THEN
%token <str> TABLE TABLES TABLESPACE TEMP TEMPLATE TEMPORARY TENANT TESTING_RELOCATE EXPERIMENTAL_RELOCATE TEXT THEN
%token <str> TIES TIME TIMETZ TIMESTAMP TIMESTAMPTZ TO THROTTLING TRAILING TRACE
%token <str> TRANSACTION TRANSACTIONS TREAT TRIGGER TRIM TRUE
%token <str> TRUNCATE TRUSTED TYPE TYPES
Expand Down Expand Up @@ -740,8 +743,7 @@ func (u *sqlSymUnion) databaseOptionList() []tree.DatabaseOption {
%type <tree.Statement> alter_rename_database_stmt
%type <tree.Statement> alter_database_to_schema_stmt
%type <tree.Statement> alter_zone_database_stmt
%type <tree.Statement> alter_database_owner
%type <tree.Statement> alter_database_with_options
%type <tree.Statement> opt_alter_database

// ALTER INDEX
%type <tree.Statement> alter_oneindex_stmt
Expand Down Expand Up @@ -853,7 +855,7 @@ func (u *sqlSymUnion) databaseOptionList() []tree.DatabaseOption {
%type <tree.Statement> set_csetting_stmt
%type <tree.Statement> set_transaction_stmt
%type <tree.Statement> set_exprs_internal
%type <tree.Statement> generic_set
%type <tree.Statement> generic_set_config generic_set_single_config
%type <tree.Statement> set_rest_more
%type <tree.Statement> set_names

Expand Down Expand Up @@ -1432,23 +1434,40 @@ alter_sequence_options_stmt:
alter_database_stmt:
alter_rename_database_stmt
| alter_zone_database_stmt
| alter_database_owner
| alter_database_with_options
| opt_alter_database
| alter_database_to_schema_stmt
// ALTER DATABASE has its error help token here because the ALTER DATABASE
// prefix is spread over multiple non-terminals.
| ALTER DATABASE error // SHOW HELP: ALTER DATABASE

alter_database_owner:
opt_alter_database:
ALTER DATABASE database_name opt_owner_to
{
$$.val = &tree.AlterDatabaseOwner{Name: tree.Name($3), Owner: $4}
$$.val = &tree.AlterDatabase{Name: tree.Name($3), Owner: $4}
}

alter_database_with_options:
ALTER DATABASE database_name opt_database_with_options
| ALTER DATABASE database_name opt_database_with_options
{
$$.val = &tree.AlterDatabase{Name: tree.Name($3), Options: $4.databaseOptionList()}
}
| ALTER DATABASE database_name SET TABLESPACE non_reserved_word
{
$$.val = &tree.AlterDatabase{Name: tree.Name($3), Tablespace: $5}
}
| ALTER DATABASE database_name REFRESH COLLATION VERSION
{
$$.val = &tree.AlterDatabase{Name: tree.Name($3), RefreshCollationVersion: true}
}
| ALTER DATABASE database_name SET generic_set_single_config
{
$$.val = &tree.AlterDatabase{Name: tree.Name($3), SetVar: $5.setVar()}
}
| ALTER DATABASE database_name RESET name
{
$$.val = &tree.AlterDatabaseOptions{Name: tree.Name($3), Options: $4.databaseOptionList()}
$$.val = &tree.AlterDatabase{Name: tree.Name($3), Tablespace: $5}
}
| ALTER DATABASE database_name RESET ALL
{
$$.val = &tree.AlterDatabase{Name: tree.Name($3), ResetAll: true}
}

opt_database_with_options:
Expand Down Expand Up @@ -4180,7 +4199,17 @@ set_transaction_stmt:
}
| SET SESSION TRANSACTION error // SHOW HELP: SET TRANSACTION

generic_set:
generic_set_single_config:
name to_or_eq var_value
{
$$.val = &tree.SetVar{Name: $1, Values: tree.Exprs{$3.expr()}}
}
| name FROM CURRENT
{
$$.val = &tree.SetVar{Name: $1, FromCurrent: true}
}

generic_set_config:
var_name to_or_eq var_list
{
// We need to recognize the "set tracing" specially here; couldn't make "set
Expand All @@ -4192,10 +4221,14 @@ generic_set:
$$.val = &tree.SetVar{Name: strings.Join($1.strs(), "."), Values: $3.exprs()}
}
}
| var_name FROM CURRENT
{
$$.val = &tree.SetVar{Name: strings.Join($1.strs(), "."), FromCurrent: true}
}

set_rest_more:
// Generic SET syntaxes:
generic_set
generic_set_config
// Special SET syntax forms in addition to the generic form.
// See: https://www.postgresql.org/docs/10/static/sql-set.html
//
Expand Down Expand Up @@ -4223,7 +4256,6 @@ set_rest_more:
}
// See comment for the non-terminal for SET NAMES below.
| set_names
| var_name FROM CURRENT { return unimplemented(sqllex, "set from current") }
| error // SHOW HELP: SET SESSION

// SET NAMES is the SQL standard syntax for SET client_encoding.
Expand Down Expand Up @@ -11832,6 +11864,7 @@ unreserved_keyword:
| SYNTAX
| SYSTEM
| TABLES
| TABLESPACE
| TEMP
| TEMPLATE
| TEMPORARY
Expand Down
54 changes: 31 additions & 23 deletions postgres/parser/sem/tree/alter_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ import (
"strings"
)

// AlterDatabaseOwner represents a ALTER DATABASE OWNER TO statement.
type AlterDatabaseOwner struct {
Name Name
Owner string
}

// Format implements the NodeFormatter interface.
func (node *AlterDatabaseOwner) Format(ctx *FmtCtx) {
ctx.WriteString("ALTER DATABASE ")
ctx.FormatNode(&node.Name)
ctx.WriteString(" OWNER TO ")
ctx.FormatNameP(&node.Owner)
}

type AlterDatabaseOption string

// Names of options on ALTER DATABASE.
Expand All @@ -58,22 +44,44 @@ type DatabaseOption struct {
Val Expr
}

// AlterDatabaseOptions represents a ALTER DATABASE OWNER TO statement.
type AlterDatabaseOptions struct {
// AlterDatabase represents a ALTER DATABASE statement.
type AlterDatabase struct {
Name Name
Options []DatabaseOption
// Rename is handled by RenameDatabase
Owner string
Tablespace string
RefreshCollationVersion bool
SetVar *SetVar
ResetVar string
ResetAll bool
}

// Format implements the NodeFormatter interface.
func (node *AlterDatabaseOptions) Format(ctx *FmtCtx) {
func (node *AlterDatabase) Format(ctx *FmtCtx) {
ctx.WriteString("ALTER DATABASE ")
ctx.FormatNode(&node.Name)
opts := make([]string, len(node.Options))
for i, opt := range node.Options {
opts[i] = fmt.Sprintf("%s %s", opt.Opt, AsString(opt.Val))
}

if len(opts) > 0 {
if len(node.Options) > 0 {
opts := make([]string, len(node.Options))
for i, opt := range node.Options {
opts[i] = fmt.Sprintf("%s %s", opt.Opt, AsString(opt.Val))
}
ctx.WriteString(fmt.Sprintf(" WITH %s", strings.Join(opts, " ")))
} else if node.Owner != "" {
ctx.WriteString(" OWNER TO ")
ctx.FormatNameP(&node.Owner)
} else if node.Tablespace != "" {
ctx.WriteString(" SET TABLESPACE ")
ctx.FormatNameP(&node.Tablespace)
} else if node.RefreshCollationVersion {
ctx.WriteString(" REFRESH COLLATION VERSION")
} else if node.SetVar != nil {
ctx.WriteByte(' ')
node.SetVar.Format(ctx)
} else if node.ResetVar != "" {
ctx.WriteString(" RESET ")
ctx.FormatNameP(&node.ResetVar)
} else if node.ResetAll {
ctx.WriteString(" RESET ALL")
}
}
13 changes: 9 additions & 4 deletions postgres/parser/sem/tree/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ package tree

// SetVar represents a SET or RESET statement.
type SetVar struct {
Name string
Values Exprs
Name string
Values Exprs
FromCurrent bool
}

// Format implements the NodeFormatter interface.
Expand All @@ -53,8 +54,12 @@ func (node *SetVar) Format(ctx *FmtCtx) {
ctx.FormatNameP(&node.Name)
})

ctx.WriteString(" = ")
ctx.FormatNode(&node.Values)
if node.FromCurrent {
ctx.WriteString(" FROM CURRENT")
} else {
ctx.WriteString(" = ")
ctx.FormatNode(&node.Values)
}
}
}

Expand Down
17 changes: 4 additions & 13 deletions postgres/parser/sem/tree/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,12 @@ func (*AlterConversion) StatementTag() string { return "ALTER CONVERSION" }
func (*AlterConversion) hiddenFromShowQueries() {}

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

// StatementTag returns a short string identifying the type of statement.
func (*AlterDatabaseOwner) StatementTag() string { return "ALTER DATABASE OWNER" }
func (*AlterDatabase) StatementTag() string { return "ALTER DATABASE" }

func (*AlterDatabaseOwner) hiddenFromShowQueries() {}

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

// StatementTag returns a short string identifying the type of statement.
func (*AlterDatabaseOptions) StatementTag() string { return "ALTER DATABASE <options>" }

func (*AlterDatabaseOptions) hiddenFromShowQueries() {}
func (*AlterDatabase) hiddenFromShowQueries() {}

// StatementType implements the Statement interface.
func (*AlterIndex) StatementType() StatementType { return DDL }
Expand Down Expand Up @@ -1052,8 +1044,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 *AlterDatabaseOwner) String() string { return AsString(n) }
func (n *AlterDatabaseOptions) String() string { return AsString(n) }
func (n *AlterDatabase) String() string { return AsString(n) }
func (n *AlterSchema) String() string { return AsString(n) }
func (n *AlterTable) String() string { return AsString(n) }
func (n *AlterTableCmds) String() string { return AsString(n) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"github.com/dolthub/doltgresql/postgres/parser/sem/tree"
)

// nodeAlterDatabaseOwner handles *tree.AlterDatabaseOwner nodes.
func nodeAlterDatabaseOwner(node *tree.AlterDatabaseOwner) (vitess.Statement, error) {
// nodeAlterDatabase handles *tree.AlterDatabase nodes.
func nodeAlterDatabase(node *tree.AlterDatabase) (vitess.Statement, error) {
if node == nil {
return nil, nil
}
return nil, fmt.Errorf("ALTER DATABASE OWNER is not yet supported")
return nil, fmt.Errorf("ALTER DATABASE is not yet supported")
}
4 changes: 2 additions & 2 deletions server/ast/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
// Convert converts a Postgres AST into a Vitess AST.
func Convert(postgresStmt parser.Statement) (vitess.Statement, error) {
switch stmt := postgresStmt.AST.(type) {
case *tree.AlterDatabaseOwner:
return nodeAlterDatabaseOwner(stmt)
case *tree.AlterDatabase:
return nodeAlterDatabase(stmt)
case *tree.AlterIndex:
return nodeAlterIndex(stmt)
case *tree.AlterRole:
Expand Down
18 changes: 9 additions & 9 deletions testing/generation/command_docs/output/alter_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ func TestAlterDatabase(t *testing.T) {
Parses("ALTER DATABASE name OWNER TO CURRENT_ROLE"),
Parses("ALTER DATABASE name OWNER TO CURRENT_USER"),
Parses("ALTER DATABASE name OWNER TO SESSION_USER"),
Unimplemented("ALTER DATABASE name SET TABLESPACE new_tablespace"),
Unimplemented("ALTER DATABASE name REFRESH COLLATION VERSION"),
Unimplemented("ALTER DATABASE name SET configuration_parameter TO value"),
Unimplemented("ALTER DATABASE name SET configuration_parameter = value"),
Unimplemented("ALTER DATABASE name SET configuration_parameter TO DEFAULT"),
Unimplemented("ALTER DATABASE name SET configuration_parameter = DEFAULT"),
Unimplemented("ALTER DATABASE name SET configuration_parameter FROM CURRENT"),
Unimplemented("ALTER DATABASE name RESET configuration_parameter"),
Unimplemented("ALTER DATABASE name RESET ALL"),
Parses("ALTER DATABASE name SET TABLESPACE new_tablespace"),
Parses("ALTER DATABASE name REFRESH COLLATION VERSION"),
Parses("ALTER DATABASE name SET configuration_parameter TO value"),
Parses("ALTER DATABASE name SET configuration_parameter = value"),
Parses("ALTER DATABASE name SET configuration_parameter TO DEFAULT"),
Parses("ALTER DATABASE name SET configuration_parameter = DEFAULT"),
Parses("ALTER DATABASE name SET configuration_parameter FROM CURRENT"),
Parses("ALTER DATABASE name RESET configuration_parameter"),
Parses("ALTER DATABASE name RESET ALL"),
}
RunTests(t, tests)
}

0 comments on commit 5b0f471

Please sign in to comment.