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

Teradata SQL Grammar #4330

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<module>postgresql</module>
<module>snowflake</module>
<module>sqlite</module>
<module>teradata</module>
<module>trino</module>
<module>tsql</module>
<module>informix-sql</module>
Expand Down
31 changes: 31 additions & 0 deletions sql/teradata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Teradata SQL Grammar

An [ANTLR4](https://www.antlr.org/) grammar for Teradata SQL. Based on a grammar of Teradata Database version 17.10.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include a permalink to the actual grammar for version 17.10. Is it a bison grammar? Is there a lex file?


Work in progress!

A few things to consider if you are going to use this parser grammar to check semantics of SQL statements:
- In `CREATE/REPLACE MACRO`, according to documentation,
> SQL DCL and DDL statements for administration of row level security are not allowed.
- There are no distinct parser rules for `SELECT/SELECT AND CONSUME` and `SELECT...INTO/SELECT AND CONSUME...INTO`,
so you should check for mandatory `into_clause` when standalone `select_stat` is used inside procedure definition.
- You should check value format in parsed `interval_literal`.
- And many more.

## Possible performance issues
- `group_by_clause` rule could cause performance penalty due to inherent ambiguity
in `group_by_spec` and `ordinary_grouping_set` rules.

## Roadmap
- 2.0.0 - rules for all SQL statements should be implemented.
- 3.0.0 - all sub-rules should have labels.
- 4.0.0 - all rules should consist of smaller rules appropriate for easy analysis and interpretation
of interesting parts of the parsed SQL.

## Naming
- Rules for SQL statements should have suffix `_stat`.
- Please, do not use obscure abbreviations, ambiguous names and names similar to existing ones.

## Examples
Most of the examples were taken from the official documentation.
Each script could contain multiple statements, so you may wish to use the top-level `sql_script` rule to parse them.
279 changes: 279 additions & 0 deletions sql/teradata/TeradataSQLDCLParser.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
parser grammar TeradataSQLDCLParser;

import TeradataSQLDataTypesParser
, TeradataSQLIdentifiersParser
;

options {
tokenVocab=TeradataSQLLexer;
}

dcl_stat
: give_stat
| grant_stat
| revoke_stat
;

/*****************
GIVE statement
*/
give_stat : GIVE database_name TO recipient_name=database_name ;

/******************
GRANT statement
*/
grant_stat
: grant_role_stat
| grant_monitor_stat
| grant_sql_form_stat
| grant_connect_through_stat
| grant_logon_stat
| grant_map_stat
| grant_zone_stat
| grant_zone_override_stat
;

grant_monitor_stat
: GRANT ( MONITOR (PRIVILEGES|BUT NOT monitor_privilege (',' monitor_privilege)* )?
| monitor_privilege (',' monitor_privilege)*
) TO (( grantee (',' grantee)*|PUBLIC) with_grant_option?
| role_name (',' role_name)*
)
;

grant_role_stat
: GRANT role_name (',' role_name)
TO (user_or_role=role_name) (',' (user_or_role=role_name) )*
with_admin_option?
;

grant_sql_form_stat
: GRANT ( ( ALL PRIVILEGES?
| (ALL BUT)? privilege (',' privilege)*
| CTCONTROL
) ON privilege_object
| map_privilege (',' map_privilege)*
| role_privilege (',' role_privilege)*
| profile_privilege (',' role_privilege)*
| zone_privilege (',' zone_privilege)*
| CONSTRAINT ASSIGNMENT
| CONSTRAINT DEFINITION
)
TO ( grantee (',' grantee)* with_grant_option?
| PUBLIC with_grant_option?
| role_name (',' role_name)*
)
;

grant_connect_through_stat
: GRANT CONNECT THROUGH trusted_user_name=user_name (WITH TRUST_ONLY)?
TO ( application_user_name=user_name (',' application_user_name=user_name)*
( WITH ROLE role_name (',' role_name)* (WITH PROFILE profile_name)?
| WITH PROFILE profile_name
)
| PERMANENT permanent_user_name=user_name (',' permanent_user_name=user_name)*
(WITH ROLE role_name (',' role_name)*|WITHOUT ROLE)
)
;

grant_logon_stat
: GRANT LOGON ON (host_id+=integer_literal (',' host_id+=integer_literal)* | ALL)
(AS DEFAULT|(TO|FROM) user_name (',' user_name)* )
(WITH NULL PASSWORD)?
;

grant_map_stat
: GRANT MAP map_name=unqualified_name TO (user_or_role=role_name (',' user_or_role=role_name)* with_grant_option?|PUBLIC)
;

grant_zone_stat
: GRANT ZONE zone_name=unqualified_name TO user_or_role=role_name (',' user_or_role=role_name)*
;

grant_zone_override_stat
: GRANT ZONE OVERRIDE TO user_name (',' user_name)*
;

/*******************
REVOKE statement
*/
revoke_stat
: revoke_monitor_stat
| revoke_role_stat
| revoke_sql_form_stat
| revoke_connect_through_stat
| revoke_logon_stat
| revoke_map_stat
| revoke_zone_stat
| revoke_zone_override_stat
;

revoke_monitor_stat
: REVOKE grant_option_for?
( MONITOR (PRIVILEGES|BUT NOT monitor_privilege (',' monitor_privilege)* )?
| monitor_privilege (',' monitor_privilege)*
) (TO|FROM) ( revokee (',' revokee)*|PUBLIC)
;

revoke_role_stat
: REVOKE (ADMIN OPTION FOR)? role_name (',' role_name)*
(TO|FROM) (user_or_role=role_name) (',' (user_or_role=role_name) )*
;

revoke_sql_form_stat
: REVOKE grant_option_for?
( ( ALL PRIVILEGES?
| (ALL BUT)? privilege (',' privilege)*
) ON privilege_object
| map_privilege (',' map_privilege)*
| role_privilege (',' role_privilege)*
| profile_privilege (',' role_privilege)*
| zone_privilege (',' zone_privilege)*
)
(TO|FROM) ( revokee (',' revokee)*
| PUBLIC
| role_name (',' role_name)*
)
;

revoke_connect_through_stat
: REVOKE CONNECT THROUGH trusted_user_name=user_name
( (TO|FROM) ( application_user_name=user_name (',' application_user_name=user_name)*
( WITH ROLE role_name (',' role_name)* (WITH PROFILE profile_name)?
| WITH PROFILE profile_name
)
| PERMANENT permanent_user_name=user_name (',' permanent_user_name=user_name)*
(WITH ROLE role_name (',' role_name)* )?
)
| WITH TRUST ONLY
)
;

revoke_logon_stat
: REVOKE LOGON ON (host_id+=integer_literal (',' host_id+=integer_literal)* | ALL)
(AS DEFAULT|(TO|FROM) user_name (',' user_name)* )
;

revoke_map_stat
: REVOKE grant_option_for? MAP map_name=unqualified_name
(TO|FROM) (user_or_role=role_name (',' user_or_role=role_name)*|PUBLIC)
;

revoke_zone_stat
: REVOKE ZONE zone_name=unqualified_name (TO|FROM) user_or_role=role_name (',' user_or_role=role_name)*
;

revoke_zone_override_stat
: REVOKE ZONE OVERRIDE (TO|FROM) user_name (',' user_name)*
;

/*
Shared rules
*/

privilege
: ALTER EXTERNAL PROCEDURE
| ALTER FUNCTION
| ALTER PROCEDURE
| ANY
| CHECKPOINT
| CREATE AUTHORIZATION
| CREATE DATABASE
| CREATE DATASET SCHEMA
| CREATE EXTERNAL PROCEDURE
| CREATE FUNCTION
| CREATE GLOP
| CREATE MACRO
| CREATE OWNER PROCEDURE
| CREATE PROCEDURE
| CREATE SERVER
| CREATE TABLE
| CREATE TRIGGER
| CREATE USER
| CREATE VIEW
| DATABASE
| DELETE
| DROP AUTHORIZATION
| DROP DATABASE
| DROP DATASET SCHEMA
| DROP FUNCTION
| DROP GLOP
| DROP MACRO
| DROP PROCEDURE
| DROP SERVER
| DROP TABLE
| DROP TRIGGER
| DROP USER
| DROP VIEW
| DUMP
| EXECUTE
| EXECUTE FUNCTION
| EXECUTE PROCEDURE
| FUNCTION
| GLOP
| GLOP MEMBER
| INDEX
| INSERT
| INSERT column_list
| MACRO
| NONTEMPORAL
| OVERRIDE
| OVERRIDE DELETE
| OVERRIDE DUMP
| OVERRIDE INSERT
| OVERRIDE RESTORE
| OVERRIDE SELECT
| OVERRIDE UPDATE
| PROCEDURE
| REFERENCES
| REFERENCES column_list
| RESTORE
| RETRIEVE
| SELECT
| SELECT column_list
| SHOW
| STATISTICS
| TABLE
| TRIGGER
| UDT METHOD
| UDTMETHOD
| UDT TYPE
| UDTTYPE
| UDT USAGE
| UDTUSAGE
| UPDATE
| UPDATE column_list
| USER
| VIEW
| WITH DATASET SCHEMA
;

privilege_object
: object_name
| PROCEDURE procedure_name
| SPECIFIC FUNCTION function_name
| FUNCTION? function_name '(' (function_parameter (',' function_parameter)* )? ')'
| TYPE sysudtlib? udt_name
;

map_privilege : CREATE MAP | DROP MAP | MAP ;

role_privilege : CREATE ROLE | DROP ROLE | ROLE ;

profile_privilege : CREATE PROFILE | DROP PROFILE | PROFILE ;

zone_privilege : CREATE ZONE | DROP ZONE | ZONE ;

monitor_privilege : ABORTSESSION | MONRESOURCE | MONSESSION | SETRESRATE | SETSESSRATE ;

grantee : ALL? database_name;

revokee : ALL? database_name;

function_parameter : parameter_name? data_type ;

with_admin_option : WITH ADMIN OPTION ;

with_grant_option : WITH GRANT OPTION ;

grant_option_for : GRANT OPTION FOR ;
Loading
Loading