Skip to content

Commit

Permalink
support for ALTER CONVERSION
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifersp committed Jan 4, 2024
1 parent 3057518 commit f5fbca1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 11 deletions.
26 changes: 21 additions & 5 deletions postgres/parser/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ func (u *sqlSymUnion) aggregateArg() *tree.AggregateArg {

%type <tree.Statement> alter_aggregate_stmt
%type <tree.Statement> alter_collation_stmt
%type <tree.Statement> alter_conversion_stmt

%type <tree.Statement> backup_stmt
%type <tree.Statement> begin_stmt
Expand Down Expand Up @@ -1272,11 +1273,12 @@ stmt:
// %Category: Group
// %Text: ALTER TABLE, ALTER INDEX, ALTER VIEW, ALTER SEQUENCE, ALTER DATABASE, ALTER USER, ALTER ROLE
alter_stmt:
alter_ddl_stmt // help texts in sub-rule
| alter_role_stmt // EXTEND WITH HELP: ALTER ROLE
| alter_collation_stmt // EXTEND WITH HELP: ALTER COLLATION
| alter_aggregate_stmt // EXTEND WITH HELP: ALTER AGGREGATE
| ALTER error // SHOW HELP: ALTER
alter_ddl_stmt // help texts in sub-rule
| alter_role_stmt // EXTEND WITH HELP: ALTER ROLE
| alter_aggregate_stmt // EXTEND WITH HELP: ALTER AGGREGATE
| alter_collation_stmt // EXTEND WITH HELP: ALTER COLLATION
| alter_conversion_stmt // EXTEND WITH HELP: ALTER CONVERSION
| ALTER error // SHOW HELP: ALTER

alter_ddl_stmt:
alter_table_stmt // EXTEND WITH HELP: ALTER TABLE
Expand Down Expand Up @@ -2181,6 +2183,20 @@ alter_collation_stmt:
$$.val = &tree.AlterCollation{Name: tree.Name($3), Schema: $4}
}

alter_conversion_stmt:
ALTER CONVERSION unrestricted_name RENAME TO unrestricted_name
{
$$.val = &tree.AlterConversion{Name: tree.Name($3), Rename: tree.Name($6)}
}
| ALTER CONVERSION unrestricted_name opt_owner_to
{
$$.val = &tree.AlterConversion{Name: tree.Name($3), Owner: $4}
}
| ALTER CONVERSION unrestricted_name opt_set_schema
{
$$.val = &tree.AlterConversion{Name: tree.Name($3), Schema: $4}
}

alter_attribute_action_list:
alter_attribute_action
| alter_attribute_action_list ',' alter_attribute_action
Expand Down
60 changes: 60 additions & 0 deletions postgres/parser/sem/tree/alter_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package tree

//ALTER COLLATION name REFRESH VERSION

//ALTER COLLATION name RENAME TO new_name
//ALTER COLLATION name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
//ALTER COLLATION name SET SCHEMA new_schema

var _ Statement = &AlterConversion{}

// AlterConversion represents a ALTER COLLATION statement.
type AlterConversion struct {
Name Name
RefreshVersion bool
Rename Name
Owner string
Schema string
}

// Format implements the NodeFormatter interface.
func (node *AlterConversion) Format(ctx *FmtCtx) {
ctx.WriteString("ALTER COLLATION ")
ctx.FormatNode(&node.Name)
if node.RefreshVersion {
ctx.WriteString(" REFRESH VERSION")
} else if node.Rename != "" {
ctx.WriteString(" RENAME ")
ctx.FormatNode(&node.Rename)
} else if node.Owner != "" {
ctx.WriteString(" OWNER TO ")
ctx.FormatNameP(&node.Owner)
} else if node.Schema != "" {
ctx.WriteString(" SET SCHEMA ")
ctx.FormatNameP(&node.Schema)
}
}
9 changes: 9 additions & 0 deletions postgres/parser/sem/tree/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ func (*AlterCollation) StatementTag() string { return "ALTER COLLATION" }

func (*AlterCollation) hiddenFromShowQueries() {}

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

// StatementTag returns a short string identifying the type of statement.
func (*AlterConversion) StatementTag() string { return "ALTER CONVERSION" }

func (*AlterConversion) hiddenFromShowQueries() {}

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

Expand Down Expand Up @@ -1034,6 +1042,7 @@ func (*ValuesClause) StatementTag() string { return "VALUES" }

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 *AlterSchema) String() string { return AsString(n) }
Expand Down
12 changes: 6 additions & 6 deletions testing/generation/command_docs/output/alter_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import "testing"

func TestAlterConversion(t *testing.T) {
tests := []QueryParses{
Unimplemented("ALTER CONVERSION name RENAME TO new_name"),
Unimplemented("ALTER CONVERSION name OWNER TO new_owner"),
Unimplemented("ALTER CONVERSION name OWNER TO CURRENT_ROLE"),
Unimplemented("ALTER CONVERSION name OWNER TO CURRENT_USER"),
Unimplemented("ALTER CONVERSION name OWNER TO SESSION_USER"),
Unimplemented("ALTER CONVERSION name SET SCHEMA new_schema"),
Parses("ALTER CONVERSION name RENAME TO new_name"),
Parses("ALTER CONVERSION name OWNER TO new_owner"),
Parses("ALTER CONVERSION name OWNER TO CURRENT_ROLE"),
Parses("ALTER CONVERSION name OWNER TO CURRENT_USER"),
Parses("ALTER CONVERSION name OWNER TO SESSION_USER"),
Parses("ALTER CONVERSION name SET SCHEMA new_schema"),
}
RunTests(t, tests)
}

0 comments on commit f5fbca1

Please sign in to comment.