Skip to content

Commit 2336436

Browse files
Teradata SQL Grammar (#4330)
* [TeradataSQL] initial commit * update readme * move readme from examples dir to satisfy automated checks * remove some target languages * fixes after review * add links to documentation
1 parent 349d8d1 commit 2336436

File tree

100 files changed

+8324
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+8324
-0
lines changed

sql/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<module>postgresql</module>
2525
<module>snowflake</module>
2626
<module>sqlite</module>
27+
<module>teradata</module>
2728
<module>trino</module>
2829
<module>tsql</module>
2930
<module>informix-sql</module>

sql/teradata/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Teradata SQL Grammar
2+
3+
An [ANTLR4](https://www.antlr.org/) grammar for Teradata SQL. Based on a grammar of Teradata Database version 17.10.
4+
This grammar was written using vendor's official documentation:
5+
- [SQL Fundamentals](https://docs.teradata.com/r/SQL-Fundamentals/July-2021)
6+
- [SQL Data Types and Literals](https://docs.teradata.com/r/SQL-Data-Types-and-Literals/July-2021)
7+
- [SQL Functions, Expressions, and Predicates](https://docs.teradata.com/r/SQL-Functions-Expressions-and-Predicates/July-2021)
8+
- [SQL Operators and User-Defined Functions](https://docs.teradata.com/r/SQL-Operators-and-User-Defined-Functions/July-2021)
9+
- [SQL Date and Time Functions and Expressions](https://docs.teradata.com/r/SQL-Date-and-Time-Functions-and-Expressions/July-2021)
10+
- [SQL Data Manipulation Language](https://docs.teradata.com/r/SQL-Data-Manipulation-Language/July-2021)
11+
- [SQL Data Definition Language Syntax and Examples](https://docs.teradata.com/r/SQL-Data-Definition-Language-Syntax-and-Examples/July-2021)
12+
- [SQL Data Definition Language Detailed Topics](https://docs.teradata.com/r/SQL-Data-Definition-Language-Detailed-Topics/July-2021)
13+
- [SQL Stored Procedures and Embedded SQL](https://docs.teradata.com/r/SQL-Stored-Procedures-and-Embedded-SQL/July-2021)
14+
- [SQL Data Control Language](https://docs.teradata.com/r/SQL-Data-Control-Language/July-2021)
15+
16+
Work in progress!
17+
18+
A few things to consider if you are going to use this parser grammar to check semantics of SQL statements:
19+
- In `CREATE/REPLACE MACRO`, according to documentation,
20+
> SQL DCL and DDL statements for administration of row level security are not allowed.
21+
- There are no distinct parser rules for `SELECT/SELECT AND CONSUME` and `SELECT...INTO/SELECT AND CONSUME...INTO`,
22+
so you should check for mandatory `into_clause` when standalone `select_stat` is used inside procedure definition.
23+
- You should check value format in parsed `interval_literal`.
24+
- And many more.
25+
26+
## Possible performance issues
27+
- `group_by_clause` rule could cause performance penalty due to inherent ambiguity
28+
in `group_by_spec` and `ordinary_grouping_set` rules.
29+
30+
## Roadmap
31+
- 2.0.0 - rules for all SQL statements should be implemented.
32+
- 3.0.0 - all sub-rules should have labels.
33+
- 4.0.0 - all rules should consist of smaller rules appropriate for easy analysis and interpretation
34+
of interesting parts of the parsed SQL.
35+
36+
## Naming
37+
- Rules for SQL statements should have suffix `_stat`.
38+
- Please, do not use obscure abbreviations, ambiguous names and names similar to existing ones.
39+
40+
## Examples
41+
Most of the examples were taken from the official documentation.
42+
Each script could contain multiple statements, so you may wish to use the top-level `sql_script` rule to parse them.
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
parser grammar TeradataSQLDCLParser;
2+
3+
import TeradataSQLDataTypesParser
4+
, TeradataSQLIdentifiersParser
5+
;
6+
7+
options {
8+
tokenVocab=TeradataSQLLexer;
9+
}
10+
11+
dcl_stat
12+
: give_stat
13+
| grant_stat
14+
| revoke_stat
15+
;
16+
17+
/*****************
18+
GIVE statement
19+
*/
20+
give_stat : GIVE database_name TO recipient_name=database_name ;
21+
22+
/******************
23+
GRANT statement
24+
*/
25+
grant_stat
26+
: grant_role_stat
27+
| grant_monitor_stat
28+
| grant_sql_form_stat
29+
| grant_connect_through_stat
30+
| grant_logon_stat
31+
| grant_map_stat
32+
| grant_zone_stat
33+
| grant_zone_override_stat
34+
;
35+
36+
grant_monitor_stat
37+
: GRANT ( MONITOR (PRIVILEGES|BUT NOT monitor_privilege (',' monitor_privilege)* )?
38+
| monitor_privilege (',' monitor_privilege)*
39+
) TO (( grantee (',' grantee)*|PUBLIC) with_grant_option?
40+
| role_name (',' role_name)*
41+
)
42+
;
43+
44+
grant_role_stat
45+
: GRANT role_name (',' role_name)
46+
TO (user_or_role=role_name) (',' (user_or_role=role_name) )*
47+
with_admin_option?
48+
;
49+
50+
grant_sql_form_stat
51+
: GRANT ( ( ALL PRIVILEGES?
52+
| (ALL BUT)? privilege (',' privilege)*
53+
| CTCONTROL
54+
) ON privilege_object
55+
| map_privilege (',' map_privilege)*
56+
| role_privilege (',' role_privilege)*
57+
| profile_privilege (',' role_privilege)*
58+
| zone_privilege (',' zone_privilege)*
59+
| CONSTRAINT ASSIGNMENT
60+
| CONSTRAINT DEFINITION
61+
)
62+
TO ( grantee (',' grantee)* with_grant_option?
63+
| PUBLIC with_grant_option?
64+
| role_name (',' role_name)*
65+
)
66+
;
67+
68+
grant_connect_through_stat
69+
: GRANT CONNECT THROUGH trusted_user_name=user_name (WITH TRUST_ONLY)?
70+
TO ( application_user_name=user_name (',' application_user_name=user_name)*
71+
( WITH ROLE role_name (',' role_name)* (WITH PROFILE profile_name)?
72+
| WITH PROFILE profile_name
73+
)
74+
| PERMANENT permanent_user_name=user_name (',' permanent_user_name=user_name)*
75+
(WITH ROLE role_name (',' role_name)*|WITHOUT ROLE)
76+
)
77+
;
78+
79+
grant_logon_stat
80+
: GRANT LOGON ON (host_id+=integer_literal (',' host_id+=integer_literal)* | ALL)
81+
(AS DEFAULT|(TO|FROM) user_name (',' user_name)* )
82+
(WITH NULL PASSWORD)?
83+
;
84+
85+
grant_map_stat
86+
: GRANT MAP map_name=unqualified_name TO (user_or_role=role_name (',' user_or_role=role_name)* with_grant_option?|PUBLIC)
87+
;
88+
89+
grant_zone_stat
90+
: GRANT ZONE zone_name=unqualified_name TO user_or_role=role_name (',' user_or_role=role_name)*
91+
;
92+
93+
grant_zone_override_stat
94+
: GRANT ZONE OVERRIDE TO user_name (',' user_name)*
95+
;
96+
97+
/*******************
98+
REVOKE statement
99+
*/
100+
revoke_stat
101+
: revoke_monitor_stat
102+
| revoke_role_stat
103+
| revoke_sql_form_stat
104+
| revoke_connect_through_stat
105+
| revoke_logon_stat
106+
| revoke_map_stat
107+
| revoke_zone_stat
108+
| revoke_zone_override_stat
109+
;
110+
111+
revoke_monitor_stat
112+
: REVOKE grant_option_for?
113+
( MONITOR (PRIVILEGES|BUT NOT monitor_privilege (',' monitor_privilege)* )?
114+
| monitor_privilege (',' monitor_privilege)*
115+
) (TO|FROM) ( revokee (',' revokee)*|PUBLIC)
116+
;
117+
118+
revoke_role_stat
119+
: REVOKE (ADMIN OPTION FOR)? role_name (',' role_name)*
120+
(TO|FROM) (user_or_role=role_name) (',' (user_or_role=role_name) )*
121+
;
122+
123+
revoke_sql_form_stat
124+
: REVOKE grant_option_for?
125+
( ( ALL PRIVILEGES?
126+
| (ALL BUT)? privilege (',' privilege)*
127+
) ON privilege_object
128+
| map_privilege (',' map_privilege)*
129+
| role_privilege (',' role_privilege)*
130+
| profile_privilege (',' role_privilege)*
131+
| zone_privilege (',' zone_privilege)*
132+
)
133+
(TO|FROM) ( revokee (',' revokee)*
134+
| PUBLIC
135+
| role_name (',' role_name)*
136+
)
137+
;
138+
139+
revoke_connect_through_stat
140+
: REVOKE CONNECT THROUGH trusted_user_name=user_name
141+
( (TO|FROM) ( application_user_name=user_name (',' application_user_name=user_name)*
142+
( WITH ROLE role_name (',' role_name)* (WITH PROFILE profile_name)?
143+
| WITH PROFILE profile_name
144+
)
145+
| PERMANENT permanent_user_name=user_name (',' permanent_user_name=user_name)*
146+
(WITH ROLE role_name (',' role_name)* )?
147+
)
148+
| WITH TRUST ONLY
149+
)
150+
;
151+
152+
revoke_logon_stat
153+
: REVOKE LOGON ON (host_id+=integer_literal (',' host_id+=integer_literal)* | ALL)
154+
(AS DEFAULT|(TO|FROM) user_name (',' user_name)* )
155+
;
156+
157+
revoke_map_stat
158+
: REVOKE grant_option_for? MAP map_name=unqualified_name
159+
(TO|FROM) (user_or_role=role_name (',' user_or_role=role_name)*|PUBLIC)
160+
;
161+
162+
revoke_zone_stat
163+
: REVOKE ZONE zone_name=unqualified_name (TO|FROM) user_or_role=role_name (',' user_or_role=role_name)*
164+
;
165+
166+
revoke_zone_override_stat
167+
: REVOKE ZONE OVERRIDE (TO|FROM) user_name (',' user_name)*
168+
;
169+
170+
/*
171+
Shared rules
172+
*/
173+
174+
privilege
175+
: ALTER EXTERNAL PROCEDURE
176+
| ALTER FUNCTION
177+
| ALTER PROCEDURE
178+
| ANY
179+
| CHECKPOINT
180+
| CREATE AUTHORIZATION
181+
| CREATE DATABASE
182+
| CREATE DATASET SCHEMA
183+
| CREATE EXTERNAL PROCEDURE
184+
| CREATE FUNCTION
185+
| CREATE GLOP
186+
| CREATE MACRO
187+
| CREATE OWNER PROCEDURE
188+
| CREATE PROCEDURE
189+
| CREATE SERVER
190+
| CREATE TABLE
191+
| CREATE TRIGGER
192+
| CREATE USER
193+
| CREATE VIEW
194+
| DATABASE
195+
| DELETE
196+
| DROP AUTHORIZATION
197+
| DROP DATABASE
198+
| DROP DATASET SCHEMA
199+
| DROP FUNCTION
200+
| DROP GLOP
201+
| DROP MACRO
202+
| DROP PROCEDURE
203+
| DROP SERVER
204+
| DROP TABLE
205+
| DROP TRIGGER
206+
| DROP USER
207+
| DROP VIEW
208+
| DUMP
209+
| EXECUTE
210+
| EXECUTE FUNCTION
211+
| EXECUTE PROCEDURE
212+
| FUNCTION
213+
| GLOP
214+
| GLOP MEMBER
215+
| INDEX
216+
| INSERT
217+
| INSERT column_list
218+
| MACRO
219+
| NONTEMPORAL
220+
| OVERRIDE
221+
| OVERRIDE DELETE
222+
| OVERRIDE DUMP
223+
| OVERRIDE INSERT
224+
| OVERRIDE RESTORE
225+
| OVERRIDE SELECT
226+
| OVERRIDE UPDATE
227+
| PROCEDURE
228+
| REFERENCES
229+
| REFERENCES column_list
230+
| RESTORE
231+
| RETRIEVE
232+
| SELECT
233+
| SELECT column_list
234+
| SHOW
235+
| STATISTICS
236+
| TABLE
237+
| TRIGGER
238+
| UDT METHOD
239+
| UDTMETHOD
240+
| UDT TYPE
241+
| UDTTYPE
242+
| UDT USAGE
243+
| UDTUSAGE
244+
| UPDATE
245+
| UPDATE column_list
246+
| USER
247+
| VIEW
248+
| WITH DATASET SCHEMA
249+
;
250+
251+
privilege_object
252+
: object_name
253+
| PROCEDURE procedure_name
254+
| SPECIFIC FUNCTION function_name
255+
| FUNCTION? function_name '(' (function_parameter (',' function_parameter)* )? ')'
256+
| TYPE sysudtlib? udt_name
257+
;
258+
259+
map_privilege : CREATE MAP | DROP MAP | MAP ;
260+
261+
role_privilege : CREATE ROLE | DROP ROLE | ROLE ;
262+
263+
profile_privilege : CREATE PROFILE | DROP PROFILE | PROFILE ;
264+
265+
zone_privilege : CREATE ZONE | DROP ZONE | ZONE ;
266+
267+
monitor_privilege : ABORTSESSION | MONRESOURCE | MONSESSION | SETRESRATE | SETSESSRATE ;
268+
269+
grantee : ALL? database_name;
270+
271+
revokee : ALL? database_name;
272+
273+
function_parameter : parameter_name? data_type ;
274+
275+
with_admin_option : WITH ADMIN OPTION ;
276+
277+
with_grant_option : WITH GRANT OPTION ;
278+
279+
grant_option_for : GRANT OPTION FOR ;

0 commit comments

Comments
 (0)