Skip to content

Commit

Permalink
Refactor create schema statement parse logic (#17703)
Browse files Browse the repository at this point in the history
* Fix npe when execute drop schema if exist statement

* Refactor create schema statement parse logic

* update sql parse test case

* update sql parse test case
  • Loading branch information
strongduanmu authored May 16, 2022
1 parent 7d861f9 commit 5f5edc6
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,73 +27,10 @@ revoke
: REVOKE optionForClause? (privilegeClause | roleClause) (CASCADE | RESTRICT)?
;

privilegeClause
: privilegeTypes ON onObjectClause (FROM | TO) granteeList (WITH GRANT OPTION)?
;

roleClause
: privilegeList (FROM | TO) roleList (WITH ADMIN OPTION)? (GRANTED BY roleSpec)?
;

optionForClause
: (GRANT | ADMIN) OPTION FOR
;

privilegeTypes
: privilegeType columnNames? (COMMA_ privilegeType columnNames?)*
;

privilegeType
: SELECT
| INSERT
| UPDATE
| DELETE
| TRUNCATE
| REFERENCES
| TRIGGER
| CREATE
| CONNECT
| TEMPORARY
| TEMP
| EXECUTE
| USAGE
| ALL PRIVILEGES?
;

onObjectClause
: DATABASE nameList
| SCHEMA nameList
| DOMAIN anyNameList
| FUNCTION functionWithArgtypesList
| PROCEDURE functionWithArgtypesList
| ROUTINE functionWithArgtypesList
| LANGUAGE nameList
| LARGE OBJECT numericOnlyList
| TABLESPACE nameList
| TYPE anyNameList
| SEQUENCE qualifiedNameList
| TABLE? privilegeLevel
| FOREIGN DATA WRAPPER nameList
| FOREIGN SERVER nameList
| ALL TABLES IN SCHEMA nameList
| ALL SEQUENCES IN SCHEMA nameList
| ALL FUNCTIONS IN SCHEMA nameList
| ALL PROCEDURES IN SCHEMA nameList
| ALL ROUTINES IN SCHEMA nameList
;

privilegeLevel
: ASTERISK_ | ASTERISK_ DOT_ASTERISK_ | identifier DOT_ASTERISK_ | tableNames | schemaName DOT_ routineName
;

routineName
: identifier
;

numericOnlyList
: numericOnly (COMMA_ numericOnly)*
;

createUser
: CREATE USER roleSpec WITH? createOptRoleElem*
;
Expand Down Expand Up @@ -161,4 +98,3 @@ dropDroup
reassignOwned
: REASSIGN OWNED BY roleList TO roleSpec
;

Original file line number Diff line number Diff line change
Expand Up @@ -1869,7 +1869,74 @@ schemaEltList

schemaStmt
: createTable | createIndex | createSequence | createTrigger | grant | createView
;
;

grant
: GRANT (privilegeClause | roleClause)
;

privilegeClause
: privilegeTypes ON onObjectClause (FROM | TO) granteeList (WITH GRANT OPTION)?
;

roleClause
: privilegeList (FROM | TO) roleList (WITH ADMIN OPTION)? (GRANTED BY roleSpec)?
;

privilegeTypes
: privilegeType columnNames? (COMMA_ privilegeType columnNames?)*
;

onObjectClause
: DATABASE nameList
| SCHEMA nameList
| DOMAIN anyNameList
| FUNCTION functionWithArgtypesList
| PROCEDURE functionWithArgtypesList
| ROUTINE functionWithArgtypesList
| LANGUAGE nameList
| LARGE OBJECT numericOnlyList
| TABLESPACE nameList
| TYPE anyNameList
| SEQUENCE qualifiedNameList
| TABLE? privilegeLevel
| FOREIGN DATA WRAPPER nameList
| FOREIGN SERVER nameList
| ALL TABLES IN SCHEMA nameList
| ALL SEQUENCES IN SCHEMA nameList
| ALL FUNCTIONS IN SCHEMA nameList
| ALL PROCEDURES IN SCHEMA nameList
| ALL ROUTINES IN SCHEMA nameList
;

numericOnlyList
: numericOnly (COMMA_ numericOnly)*
;

privilegeLevel
: ASTERISK_ | ASTERISK_ DOT_ASTERISK_ | identifier DOT_ASTERISK_ | tableNames | schemaName DOT_ routineName
;

routineName
: identifier
;

privilegeType
: SELECT
| INSERT
| UPDATE
| DELETE
| TRUNCATE
| REFERENCES
| TRIGGER
| CREATE
| CONNECT
| TEMPORARY
| TEMP
| EXECUTE
| USAGE
| ALL PRIVILEGES?
;

alterSchema
: ALTER SCHEMA name (RENAME TO name | OWNER TO roleSpec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,73 +29,10 @@ revoke
: REVOKE optionForClause? (privilegeClause | roleClause) (CASCADE | RESTRICT)?
;

privilegeClause
: privilegeTypes ON onObjectClause (FROM | TO) granteeList (WITH GRANT OPTION)?
;

roleClause
: privilegeList (FROM | TO) roleList (WITH ADMIN OPTION)? (GRANTED BY roleSpec)?
;

optionForClause
: (GRANT | ADMIN) OPTION FOR
;

privilegeTypes
: privilegeType columnNames? (COMMA_ privilegeType columnNames?)*
;

privilegeType
: SELECT
| INSERT
| UPDATE
| DELETE
| TRUNCATE
| REFERENCES
| TRIGGER
| CREATE
| CONNECT
| TEMPORARY
| TEMP
| EXECUTE
| USAGE
| ALL PRIVILEGES?
;

onObjectClause
: DATABASE nameList
| SCHEMA nameList
| DOMAIN anyNameList
| FUNCTION functionWithArgtypesList
| PROCEDURE functionWithArgtypesList
| ROUTINE functionWithArgtypesList
| LANGUAGE nameList
| LARGE OBJECT numericOnlyList
| TABLESPACE nameList
| TYPE anyNameList
| SEQUENCE qualifiedNameList
| TABLE? privilegeLevel
| FOREIGN DATA WRAPPER nameList
| FOREIGN SERVER nameList
| ALL TABLES IN SCHEMA nameList
| ALL SEQUENCES IN SCHEMA nameList
| ALL FUNCTIONS IN SCHEMA nameList
| ALL PROCEDURES IN SCHEMA nameList
| ALL ROUTINES IN SCHEMA nameList
;

privilegeLevel
: ASTERISK_ | ASTERISK_ DOT_ASTERISK_ | identifier DOT_ASTERISK_ | tableNames | schemaName DOT_ routineName
;

routineName
: identifier
;

numericOnlyList
: numericOnly (COMMA_ numericOnly)*
;

createUser
: CREATE USER roleSpec WITH? createOptRoleElem*
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,73 @@ schemaStmt
: createTable | createIndex | createSequence | createTrigger | grant | createView
;

grant
: GRANT (privilegeClause | roleClause)
;

privilegeClause
: privilegeTypes ON onObjectClause (FROM | TO) granteeList (WITH GRANT OPTION)?
;

roleClause
: privilegeList (FROM | TO) roleList (WITH ADMIN OPTION)? (GRANTED BY roleSpec)?
;

privilegeTypes
: privilegeType columnNames? (COMMA_ privilegeType columnNames?)*
;

onObjectClause
: DATABASE nameList
| SCHEMA nameList
| DOMAIN anyNameList
| FUNCTION functionWithArgtypesList
| PROCEDURE functionWithArgtypesList
| ROUTINE functionWithArgtypesList
| LANGUAGE nameList
| LARGE OBJECT numericOnlyList
| TABLESPACE nameList
| TYPE anyNameList
| SEQUENCE qualifiedNameList
| TABLE? privilegeLevel
| FOREIGN DATA WRAPPER nameList
| FOREIGN SERVER nameList
| ALL TABLES IN SCHEMA nameList
| ALL SEQUENCES IN SCHEMA nameList
| ALL FUNCTIONS IN SCHEMA nameList
| ALL PROCEDURES IN SCHEMA nameList
| ALL ROUTINES IN SCHEMA nameList
;

numericOnlyList
: numericOnly (COMMA_ numericOnly)*
;

privilegeLevel
: ASTERISK_ | ASTERISK_ DOT_ASTERISK_ | identifier DOT_ASTERISK_ | tableNames | schemaName DOT_ routineName
;

routineName
: identifier
;

privilegeType
: SELECT
| INSERT
| UPDATE
| DELETE
| TRUNCATE
| REFERENCES
| TRIGGER
| CREATE
| CONNECT
| TEMPORARY
| TEMP
| EXECUTE
| USAGE
| ALL PRIVILEGES?
;

alterSchema
: ALTER SCHEMA name (RENAME TO name | OWNER TO roleSpec)
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
-->

<sql-parser-test-cases>
<create-schema sql-case-id="create_schema_grant_permissions" />
<create-schema sql-case-id="create_schema" />
<create-schema sql-case-id="create_schema_with_grant_permissions" />
<create-schema sql-case-id="create_schema_with_if_not_exists" />
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
-->

<sql-cases>
<sql-case id="create_schema_grant_permissions" value="CREATE SCHEMA Sprockets AUTHORIZATION Annik
<sql-case id="create_schema" value="CREATE SCHEMA test_schema" db-types="SQLServer,PostgreSQL,openGauss"/>
<sql-case id="create_schema_with_grant_permissions" value="CREATE SCHEMA test_schema AUTHORIZATION Annik
CREATE TABLE NineProngs (source int, cost int, partnumber int)
GRANT SELECT ON SCHEMA::Sprockets TO Mandar
DENY SELECT ON SCHEMA::Sprockets TO Prasanna" db-types="SQLServer" />
<sql-case id="create_schema" value="CREATE SCHEMA alt_nsp1" db-types="SQLServer,PostgreSQL,openGauss"/>
<sql-case id="create_schema_with_if_not_exists" value="CREATE SCHEMA IF NOT EXISTS test_schema" db-types="PostgreSQL"/>
</sql-cases>

0 comments on commit 5f5edc6

Please sign in to comment.