Skip to content

Commit 83cff79

Browse files
authored
feat!: add comprehensive reserved keywords for presto and redshift (#3322)
1 parent 6f6f768 commit 83cff79

File tree

8 files changed

+241
-8
lines changed

8 files changed

+241
-8
lines changed

sqlglot/dialects/presto.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,67 @@ class Generator(generator.Generator):
443443
exp.Xor: bool_xor_sql,
444444
}
445445

446+
RESERVED_KEYWORDS = {
447+
"alter",
448+
"and",
449+
"as",
450+
"between",
451+
"by",
452+
"case",
453+
"cast",
454+
"constraint",
455+
"create",
456+
"cross",
457+
"current_time",
458+
"current_timestamp",
459+
"deallocate",
460+
"delete",
461+
"describe",
462+
"distinct",
463+
"drop",
464+
"else",
465+
"end",
466+
"escape",
467+
"except",
468+
"execute",
469+
"exists",
470+
"extract",
471+
"false",
472+
"for",
473+
"from",
474+
"full",
475+
"group",
476+
"having",
477+
"in",
478+
"inner",
479+
"insert",
480+
"intersect",
481+
"into",
482+
"is",
483+
"join",
484+
"left",
485+
"like",
486+
"natural",
487+
"not",
488+
"null",
489+
"on",
490+
"or",
491+
"order",
492+
"outer",
493+
"prepare",
494+
"right",
495+
"select",
496+
"table",
497+
"then",
498+
"true",
499+
"union",
500+
"using",
501+
"values",
502+
"when",
503+
"where",
504+
"with",
505+
}
506+
446507
def strtounix_sql(self, expression: exp.StrToUnix) -> str:
447508
# Since `TO_UNIXTIME` requires a `TIMESTAMP`, we need to parse the argument into one.
448509
# To do this, we first try to `DATE_PARSE` it, but since this can fail when there's a

sqlglot/dialects/redshift.py

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,165 @@ class Generator(Postgres.Generator):
196196
# Redshift supports LAST_DAY(..)
197197
TRANSFORMS.pop(exp.LastDay)
198198

199-
RESERVED_KEYWORDS = {*Postgres.Generator.RESERVED_KEYWORDS, "snapshot", "type"}
199+
RESERVED_KEYWORDS = {
200+
"aes128",
201+
"aes256",
202+
"all",
203+
"allowoverwrite",
204+
"analyse",
205+
"analyze",
206+
"and",
207+
"any",
208+
"array",
209+
"as",
210+
"asc",
211+
"authorization",
212+
"az64",
213+
"backup",
214+
"between",
215+
"binary",
216+
"blanksasnull",
217+
"both",
218+
"bytedict",
219+
"bzip2",
220+
"case",
221+
"cast",
222+
"check",
223+
"collate",
224+
"column",
225+
"constraint",
226+
"create",
227+
"credentials",
228+
"cross",
229+
"current_date",
230+
"current_time",
231+
"current_timestamp",
232+
"current_user",
233+
"current_user_id",
234+
"default",
235+
"deferrable",
236+
"deflate",
237+
"defrag",
238+
"delta",
239+
"delta32k",
240+
"desc",
241+
"disable",
242+
"distinct",
243+
"do",
244+
"else",
245+
"emptyasnull",
246+
"enable",
247+
"encode",
248+
"encrypt ",
249+
"encryption",
250+
"end",
251+
"except",
252+
"explicit",
253+
"false",
254+
"for",
255+
"foreign",
256+
"freeze",
257+
"from",
258+
"full",
259+
"globaldict256",
260+
"globaldict64k",
261+
"grant",
262+
"group",
263+
"gzip",
264+
"having",
265+
"identity",
266+
"ignore",
267+
"ilike",
268+
"in",
269+
"initially",
270+
"inner",
271+
"intersect",
272+
"interval",
273+
"into",
274+
"is",
275+
"isnull",
276+
"join",
277+
"leading",
278+
"left",
279+
"like",
280+
"limit",
281+
"localtime",
282+
"localtimestamp",
283+
"lun",
284+
"luns",
285+
"lzo",
286+
"lzop",
287+
"minus",
288+
"mostly16",
289+
"mostly32",
290+
"mostly8",
291+
"natural",
292+
"new",
293+
"not",
294+
"notnull",
295+
"null",
296+
"nulls",
297+
"off",
298+
"offline",
299+
"offset",
300+
"oid",
301+
"old",
302+
"on",
303+
"only",
304+
"open",
305+
"or",
306+
"order",
307+
"outer",
308+
"overlaps",
309+
"parallel",
310+
"partition",
311+
"percent",
312+
"permissions",
313+
"pivot",
314+
"placing",
315+
"primary",
316+
"raw",
317+
"readratio",
318+
"recover",
319+
"references",
320+
"rejectlog",
321+
"resort",
322+
"respect",
323+
"restore",
324+
"right",
325+
"select",
326+
"session_user",
327+
"similar",
328+
"snapshot",
329+
"some",
330+
"sysdate",
331+
"system",
332+
"table",
333+
"tag",
334+
"tdes",
335+
"text255",
336+
"text32k",
337+
"then",
338+
"timestamp",
339+
"to",
340+
"top",
341+
"trailing",
342+
"true",
343+
"truncatecolumns",
344+
"type",
345+
"union",
346+
"unique",
347+
"unnest",
348+
"unpivot",
349+
"user",
350+
"using",
351+
"verbose",
352+
"wallet",
353+
"when",
354+
"where",
355+
"with",
356+
"without",
357+
}
200358

201359
def unnest_sql(self, expression: exp.Unnest) -> str:
202360
args = expression.expressions

sqlglot/parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,11 @@ def _parse_stored(self) -> exp.FileFormatProperty:
17641764
def _parse_property_assignment(self, exp_class: t.Type[E], **kwargs: t.Any) -> E:
17651765
self._match(TokenType.EQ)
17661766
self._match(TokenType.ALIAS)
1767-
return self.expression(exp_class, this=self._parse_field(), **kwargs)
1767+
field = self._parse_field()
1768+
if isinstance(field, exp.Identifier) and not field.quoted:
1769+
field = exp.var(field)
1770+
1771+
return self.expression(exp_class, this=field, **kwargs)
17681772

17691773
def _parse_properties(self, before: t.Optional[bool] = None) -> t.Optional[exp.Properties]:
17701774
properties = []

tests/dialects/test_dialect.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,3 +2426,13 @@ def test_create_sequence(self):
24262426
"""CREATE TEMPORARY SEQUENCE seq START WITH = 1 INCREMENT BY = 2""",
24272427
"""CREATE TEMPORARY SEQUENCE seq START WITH 1 INCREMENT BY 2""",
24282428
)
2429+
2430+
def test_reserved_keywords(self):
2431+
order = exp.select("*").from_("order")
2432+
2433+
for dialect in ("presto", "redshift"):
2434+
dialect = Dialect.get_or_raise(dialect)
2435+
self.assertEqual(
2436+
order.sql(dialect=dialect),
2437+
f"SELECT * FROM {dialect.IDENTIFIER_START}order{dialect.IDENTIFIER_END}",
2438+
)

tests/dialects/test_presto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ def test_presto(self):
871871
"SELECT ARRAY_SORT(x, (left, right) -> -1)",
872872
write={
873873
"duckdb": "SELECT ARRAY_SORT(x)",
874-
"presto": "SELECT ARRAY_SORT(x, (left, right) -> -1)",
874+
"presto": 'SELECT ARRAY_SORT(x, ("left", "right") -> -1)',
875875
"hive": "SELECT SORT_ARRAY(x)",
876876
"spark": "SELECT ARRAY_SORT(x, (left, right) -> -1)",
877877
},

tests/dialects/test_snowflake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ def test_regexp_substr(self, logger):
14901490
write={
14911491
"bigquery": "REGEXP_EXTRACT(subject, pattern, pos, occ)",
14921492
"hive": "REGEXP_EXTRACT(subject, pattern, group)",
1493-
"presto": "REGEXP_EXTRACT(subject, pattern, group)",
1493+
"presto": 'REGEXP_EXTRACT(subject, pattern, "group")',
14941494
"snowflake": "REGEXP_SUBSTR(subject, pattern, pos, occ, params, group)",
14951495
"spark": "REGEXP_EXTRACT(subject, pattern, group)",
14961496
},

tests/dialects/test_spark.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def test_spark(self):
234234
self.validate_identity("first_value(col, true)", "FIRST_VALUE(col) IGNORE NULLS")
235235
self.validate_identity("last(col, true)", "LAST(col) IGNORE NULLS")
236236
self.validate_identity("last_value(col, true)", "LAST_VALUE(col) IGNORE NULLS")
237-
self.validate_identity("DESCRIBE EXTENDED db.table")
237+
self.validate_identity("DESCRIBE EXTENDED db.tbl")
238238
self.validate_identity("SELECT * FROM test TABLESAMPLE (50 PERCENT)")
239239
self.validate_identity("SELECT * FROM test TABLESAMPLE (5 ROWS)")
240240
self.validate_identity("SELECT * FROM test TABLESAMPLE (BUCKET 4 OUT OF 10)")
@@ -566,7 +566,7 @@ def test_spark(self):
566566
"ARRAY_SORT(x, (left, right) -> -1)",
567567
write={
568568
"duckdb": "ARRAY_SORT(x)",
569-
"presto": "ARRAY_SORT(x, (left, right) -> -1)",
569+
"presto": 'ARRAY_SORT(x, ("left", "right") -> -1)',
570570
"hive": "SORT_ARRAY(x)",
571571
"spark": "ARRAY_SORT(x, (left, right) -> -1)",
572572
},

tests/fixtures/optimizer/normalize_identifiers.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ SELECT a /* sqlglot.meta case_sensitive */, b FROM table /* sqlglot.meta case_se
6868
SELECT a /* sqlglot.meta case_sensitive */, B FROM table /* sqlglot.meta case_sensitive */;
6969

7070
# dialect: redshift
71-
SELECT COALESCE(json_val.a /* sqlglot.meta case_sensitive */, json_val.A /* sqlglot.meta case_sensitive */) FROM table;
72-
SELECT COALESCE(json_val.a /* sqlglot.meta case_sensitive */, json_val.A /* sqlglot.meta case_sensitive */) FROM table;
71+
SELECT COALESCE(json_val.a /* sqlglot.meta case_sensitive */, json_val.A /* sqlglot.meta case_sensitive */) FROM tbl;
72+
SELECT COALESCE(json_val.a /* sqlglot.meta case_sensitive */, json_val.A /* sqlglot.meta case_sensitive */) FROM tbl;
7373

7474
SELECT @X;
7575
SELECT @X;

0 commit comments

Comments
 (0)