Skip to content

Commit

Permalink
sql: parse and recognize ALTER SCHEMA and DROP SCHEMA
Browse files Browse the repository at this point in the history
Touches #50880.
Touches #50884.

This commit adds the boilerplate for adding the `ALTER SCHEMA` and `DROP
SCHEMA` statements.

Release note: None
  • Loading branch information
rohany committed Aug 3, 2020
1 parent 6ff0528 commit 1d34680
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/generated/sql/bnf/drop_stmt.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ drop_stmt ::=
| drop_table_stmt
| drop_view_stmt
| drop_sequence_stmt
| drop_schema_stmt
| drop_type_stmt
| drop_role_stmt
| drop_schedule_stmt
18 changes: 15 additions & 3 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ alter_ddl_stmt ::=
| alter_database_stmt
| alter_range_stmt
| alter_partition_stmt
| alter_schema_stmt
| alter_type_stmt

alter_role_stmt ::=
Expand Down Expand Up @@ -406,6 +407,7 @@ drop_ddl_stmt ::=
| drop_table_stmt
| drop_view_stmt
| drop_sequence_stmt
| drop_schema_stmt
| drop_type_stmt

drop_role_stmt ::=
Expand Down Expand Up @@ -1064,6 +1066,9 @@ alter_range_stmt ::=
alter_partition_stmt ::=
alter_zone_partition_stmt

alter_schema_stmt ::=
'ALTER' 'SCHEMA' schema_name 'RENAME' 'TO' schema_name

alter_type_stmt ::=
'ALTER' 'TYPE' type_name 'ADD' 'VALUE' 'SCONST' opt_add_val_placement
| 'ALTER' 'TYPE' type_name 'ADD' 'VALUE' 'IF' 'NOT' 'EXISTS' 'SCONST' opt_add_val_placement
Expand Down Expand Up @@ -1203,6 +1208,10 @@ drop_sequence_stmt ::=
'DROP' 'SEQUENCE' table_name_list opt_drop_behavior
| 'DROP' 'SEQUENCE' 'IF' 'EXISTS' table_name_list opt_drop_behavior

drop_schema_stmt ::=
'DROP' 'SCHEMA' schema_name_list opt_drop_behavior
| 'DROP' 'SCHEMA' 'IF' 'EXISTS' schema_name_list opt_drop_behavior

drop_type_stmt ::=
'DROP' 'TYPE' type_name_list opt_drop_behavior
| 'DROP' 'TYPE' 'IF' 'EXISTS' type_name_list opt_drop_behavior
Expand Down Expand Up @@ -1455,6 +1464,9 @@ alter_zone_partition_stmt ::=
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_index_name set_zone_config
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' '*' set_zone_config

schema_name ::=
name

type_name ::=
db_object_name

Expand All @@ -1463,9 +1475,6 @@ opt_add_val_placement ::=
| 'AFTER' 'SCONST'
|

schema_name ::=
name

opt_with ::=
'WITH'
|
Expand Down Expand Up @@ -1645,6 +1654,9 @@ table_index_name_list ::=
table_name_list ::=
( table_name ) ( ( ',' table_name ) )*

schema_name_list ::=
( schema_name ) ( ( ',' schema_name ) )*

type_name_list ::=
( type_name ) ( ( ',' type_name ) )*

Expand Down
39 changes: 39 additions & 0 deletions pkg/sql/alter_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 sql

import (
"context"

"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/errors"
)

type alterSchemaNode struct {
n *tree.AlterSchema
}

// Use to satisfy the linter.
var _ planNode = &alterSchemaNode{n: nil}

func (p *planner) AlterSchema(ctx context.Context, n *tree.AlterSchema) (planNode, error) {
return nil, unimplemented.NewWithIssue(50880, "ALTER SCHEMA")
}

func (n *alterSchemaNode) startExec(params runParams) error {
return errors.AssertionFailedf("unimplemented")
}

func (n *alterSchemaNode) Next(params runParams) (bool, error) { return false, nil }
func (n *alterSchemaNode) Values() tree.Datums { return tree.Datums{} }
func (n *alterSchemaNode) Close(ctx context.Context) {}
func (n *alterSchemaNode) ReadingOwnWrites() {}
39 changes: 39 additions & 0 deletions pkg/sql/drop_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 sql

import (
"context"

"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/errors"
)

type dropSchemaNode struct {
n *tree.DropSchema
}

// Use to satisfy the linter.
var _ planNode = &dropSchemaNode{n: nil}

func (p *planner) DropSchema(ctx context.Context, n *tree.DropSchema) (planNode, error) {
return nil, unimplemented.NewWithIssue(50884, "DROP SCHEMA")
}

func (n *dropSchemaNode) startExec(params runParams) error {
return errors.AssertionFailedf("unimplemented")
}

func (n *dropSchemaNode) Next(params runParams) (bool, error) { return false, nil }
func (n *dropSchemaNode) Values() tree.Datums { return tree.Datums{} }
func (n *dropSchemaNode) Close(ctx context.Context) {}
func (n *dropSchemaNode) ReadingOwnWrites() {}
6 changes: 6 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/schema
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ CREATE TEMP TABLE myschema.tmp (x int)
# We should error out trying to modify any virtual schemas.
statement error pq: schema cannot be modified: "pg_catalog"
CREATE TABLE pg_catalog.bad (x int)

statement error pq: unimplemented: DROP SCHEMA
DROP SCHEMA myschema

statement error pq: unimplemented: ALTER SCHEMA
ALTER SCHEMA myschema RENAME TO yourschema
6 changes: 6 additions & 0 deletions pkg/sql/opaque.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func buildOpaque(
switch n := stmt.(type) {
case *tree.AlterIndex:
plan, err = p.AlterIndex(ctx, n)
case *tree.AlterSchema:
plan, err = p.AlterSchema(ctx, n)
case *tree.AlterTable:
plan, err = p.AlterTable(ctx, n)
case *tree.AlterTableSetSchema:
Expand Down Expand Up @@ -93,6 +95,8 @@ func buildOpaque(
plan, err = p.DropIndex(ctx, n)
case *tree.DropRole:
plan, err = p.DropRole(ctx, n)
case *tree.DropSchema:
plan, err = p.DropSchema(ctx, n)
case *tree.DropTable:
plan, err = p.DropTable(ctx, n)
case *tree.DropType:
Expand Down Expand Up @@ -169,6 +173,7 @@ func buildOpaque(
func init() {
for _, stmt := range []tree.Statement{
&tree.AlterIndex{},
&tree.AlterSchema{},
&tree.AlterTable{},
&tree.AlterTableSetSchema{},
&tree.AlterType{},
Expand All @@ -190,6 +195,7 @@ func init() {
&tree.Discard{},
&tree.DropDatabase{},
&tree.DropIndex{},
&tree.DropSchema{},
&tree.DropTable{},
&tree.DropType{},
&tree.DropView{},
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/parser/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func TestContextualHelp(t *testing.T) {
{`ALTER SEQUENCE blah RENAME ??`, `ALTER SEQUENCE`},
{`ALTER SEQUENCE blah RENAME TO blih ??`, `ALTER SEQUENCE`},

{`ALTER SCHEMA ??`, `ALTER SCHEMA`},
{`ALTER SCHEMA x RENAME ??`, `ALTER SCHEMA`},
{`ALTER SCHEMA x OWNER ??`, `ALTER SCHEMA`},

{`ALTER USER IF ??`, `ALTER ROLE`},
{`ALTER USER foo WITH PASSWORD ??`, `ALTER ROLE`},

Expand Down Expand Up @@ -182,6 +186,8 @@ func TestContextualHelp(t *testing.T) {
{`DROP SCHEDULE ???`, `DROP SCHEDULES`},
{`DROP SCHEDULES ???`, `DROP SCHEDULES`},

{`DROP SCHEMA ??`, `DROP SCHEMA`},

{`EXPLAIN (??`, `EXPLAIN`},
{`EXPLAIN SELECT 1 ??`, `SELECT`},
{`EXPLAIN INSERT INTO xx (SELECT 1) ??`, `INSERT`},
Expand Down
10 changes: 9 additions & 1 deletion pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@ func TestParse(t *testing.T) {
{`CREATE TYPE a.b AS ENUM ('a', 'b', 'c')`},
{`CREATE TYPE a.b.c AS ENUM ('a', 'b', 'c')`},

{`DROP SCHEMA a`},
{`DROP SCHEMA a, b`},
{`DROP SCHEMA IF EXISTS a, b, c`},
{`DROP SCHEMA IF EXISTS a, b CASCADE`},
{`DROP SCHEMA IF EXISTS a, b RESTRICT`},
{`DROP SCHEMA a RESTRICT`},

{`DROP TYPE a`},
{`DROP TYPE a, b, c`},
{`DROP TYPE db.sc.a, sc.a`},
Expand Down Expand Up @@ -1244,6 +1251,8 @@ func TestParse(t *testing.T) {
{`ALTER INDEX IF EXISTS a@b RENAME TO b`},
{`ALTER INDEX IF EXISTS a@primary RENAME TO like`},

{`ALTER SCHEMA s RENAME TO s2`},

{`ALTER TABLE a RENAME TO b`},
{`EXPLAIN ALTER TABLE a RENAME TO b`},
{`ALTER TABLE IF EXISTS a RENAME TO b`},
Expand Down Expand Up @@ -2814,7 +2823,6 @@ func TestUnimplementedSyntax(t *testing.T) {
{`DROP OPERATOR a`, 0, `drop operator`, ``},
{`DROP PUBLICATION a`, 0, `drop publication`, ``},
{`DROP RULE a`, 0, `drop rule`, ``},
{`DROP SCHEMA a`, 26443, `drop`, ``},
{`DROP SERVER a`, 0, `drop server`, ``},
{`DROP SUBSCRIPTION a`, 0, `drop subscription`, ``},
{`DROP TEXT SEARCH a`, 7821, `drop text`, ``},
Expand Down
61 changes: 59 additions & 2 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ func (u *sqlSymUnion) executorType() tree.ScheduledJobExecutorType {
%type <tree.Statement> alter_partition_stmt
%type <tree.Statement> alter_role_stmt
%type <tree.Statement> alter_type_stmt
%type <tree.Statement> alter_schema_stmt

// ALTER RANGE
%type <tree.Statement> alter_zone_range_stmt
Expand Down Expand Up @@ -773,6 +774,7 @@ func (u *sqlSymUnion) executorType() tree.ScheduledJobExecutorType {
%type <tree.Statement> drop_database_stmt
%type <tree.Statement> drop_index_stmt
%type <tree.Statement> drop_role_stmt
%type <tree.Statement> drop_schema_stmt
%type <tree.Statement> drop_table_stmt
%type <tree.Statement> drop_type_stmt
%type <tree.Statement> drop_view_stmt
Expand Down Expand Up @@ -907,6 +909,7 @@ func (u *sqlSymUnion) executorType() tree.ScheduledJobExecutorType {
%type <*tree.UnresolvedObjectName> table_name standalone_index_name sequence_name type_name view_name db_object_name simple_db_object_name complex_db_object_name
%type <[]*tree.UnresolvedObjectName> type_name_list
%type <str> schema_name
%type <[]string> schema_name_list
%type <*tree.UnresolvedName> table_pattern complex_table_pattern
%type <*tree.UnresolvedName> column_path prefixed_column_path column_path_with_star
%type <tree.TableExpr> insert_target create_stats_target analyze_target
Expand Down Expand Up @@ -1234,6 +1237,7 @@ alter_ddl_stmt:
| alter_database_stmt // EXTEND WITH HELP: ALTER DATABASE
| alter_range_stmt // EXTEND WITH HELP: ALTER RANGE
| alter_partition_stmt // EXTEND WITH HELP: ALTER PARTITION
| alter_schema_stmt // EXTEND WITH HELP: ALTER SCHEMA
| alter_type_stmt // EXTEND WITH HELP: ALTER TYPE

// %Help: ALTER TABLE - change the definition of a table
Expand Down Expand Up @@ -2727,7 +2731,6 @@ drop_unsupported:
| DROP OPERATOR error { return unimplemented(sqllex, "drop operator") }
| DROP PUBLICATION error { return unimplemented(sqllex, "drop publication") }
| DROP RULE error { return unimplemented(sqllex, "drop rule") }
| DROP SCHEMA error { return unimplementedWithIssueDetail(sqllex, 26443, "drop") }
| DROP SERVER error { return unimplemented(sqllex, "drop server") }
| DROP SUBSCRIPTION error { return unimplemented(sqllex, "drop subscription") }
| DROP TEXT error { return unimplementedWithIssueDetail(sqllex, 7821, "drop text") }
Expand Down Expand Up @@ -2949,6 +2952,7 @@ drop_ddl_stmt:
| drop_table_stmt // EXTEND WITH HELP: DROP TABLE
| drop_view_stmt // EXTEND WITH HELP: DROP VIEW
| drop_sequence_stmt // EXTEND WITH HELP: DROP SEQUENCE
| drop_schema_stmt // EXTEND WITH HELP: DROP SCHEMA
| drop_type_stmt // EXTEND WITH HELP: DROP TYPE

// %Help: DROP VIEW - remove a view
Expand Down Expand Up @@ -3066,7 +3070,6 @@ drop_type_stmt:
}
| DROP TYPE error // SHOW HELP: DROP TYPE


type_name_list:
type_name
{
Expand All @@ -3077,6 +3080,37 @@ type_name_list:
$$.val = append($1.unresolvedObjectNames(), $3.unresolvedObjectName())
}

// %Help: DROP SCHEMA - remove a schema
// %Category: DDL
// %Text: DROP SCHEMA [IF EXISTS] <schema_name> [, ...] [CASCADE | RESTRICT]
drop_schema_stmt:
DROP SCHEMA schema_name_list opt_drop_behavior
{
$$.val = &tree.DropSchema{
Names: $3.strs(),
IfExists: false,
DropBehavior: $4.dropBehavior(),
}
}
| DROP SCHEMA IF EXISTS schema_name_list opt_drop_behavior
{
$$.val = &tree.DropSchema{
Names: $5.strs(),
IfExists: true,
DropBehavior: $6.dropBehavior(),
}
}
| DROP SCHEMA error // SHOW HELP: DROP SCHEMA

schema_name_list:
schema_name
{
$$.val = []string{$1}
}
| schema_name_list ',' schema_name
{
$$.val = append($1.strs(), $3)
}

// %Help: DROP ROLE - remove a user
// %Category: Priv
Expand Down Expand Up @@ -4929,6 +4963,29 @@ create_schema_stmt:
}
| CREATE SCHEMA error // SHOW HELP: CREATE SCHEMA

// %Help: ALTER SCHEMA - alter an existing schema
// %Category: DDL
// %Text:
//
// Commands:
// ALTER SCHEMA ... RENAME TO <newschemaname>
// ALTER SCHEMA ... OWNER TO {<newowner> | CURRENT_USER | SESSION_USER }
alter_schema_stmt:
ALTER SCHEMA schema_name RENAME TO schema_name
{
$$.val = &tree.AlterSchema{
Schema: $3,
Cmd: &tree.AlterSchemaRename{
NewName: $6,
},
}
}
| ALTER SCHEMA schema_name OWNER TO role_spec
{
return unimplementedWithIssueDetail(sqllex, 50882, "ALTER SCHEMA OWNER TO")
}
| ALTER SCHEMA error // SHOW HELP: ALTER SCHEMA

// %Help: CREATE TABLE - create a new table
// %Category: DDL
// %Text:
Expand Down
Loading

0 comments on commit 1d34680

Please sign in to comment.