Skip to content
Open
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
4 changes: 4 additions & 0 deletions sqlglot/dialects/trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class Tokenizer(Presto.Tokenizer):
KEYWORDS = {
**Presto.Tokenizer.KEYWORDS,
"REFRESH": TokenType.REFRESH,
"NOT DETERMINISTIC": TokenType.VOLATILE,
}
# Trino has no `SQL SECURITY` clause, only bare `SECURITY DEFINER`/`INVOKER`;
# the merged base keyword otherwise eats the `SQL` in `LANGUAGE SQL SECURITY DEFINER`.
KEYWORDS.pop("SQL SECURITY")

Parser = TrinoParser

Expand Down
3 changes: 3 additions & 0 deletions sqlglot/generators/trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class TrinoGenerator(PrestoGenerator):
amend_exploded_column_table,
]
),
exp.StabilityProperty: lambda self, e: (
"DETERMINISTIC" if e.name == "IMMUTABLE" else "NOT DETERMINISTIC"
),
exp.TimeStrToTime: lambda self, e: timestrtotime_sql(self, e, include_precision=True),
exp.Trim: trim_sql,
}
Expand Down
7 changes: 7 additions & 0 deletions sqlglot/parsers/trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class TrinoParser(PrestoParser):
TokenType.CURRENT_CATALOG: exp.CurrentCatalog,
}

PROPERTY_PARSERS = {
**PrestoParser.PROPERTY_PARSERS,
"NOT DETERMINISTIC": lambda self: self.expression(
exp.StabilityProperty(this=exp.Literal.string("VOLATILE"))
),
}

FUNCTIONS = {
**PrestoParser.FUNCTIONS,
"VERSION": exp.CurrentVersion.from_arg_list,
Expand Down
26 changes: 26 additions & 0 deletions tests/dialects/test_trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,29 @@ def test_inline_udf(self):
)
self.validate_identity("WITH function AS (SELECT 1 AS x) SELECT x FROM function")
self.validate_identity("WITH function(x) AS (SELECT 1) SELECT x FROM function")

self.validate_identity("WITH FUNCTION f() RETURNS INTEGER LANGUAGE SQL RETURN 1 SELECT F()")
self.validate_identity(
"WITH FUNCTION f() RETURNS INTEGER DETERMINISTIC RETURN 1 SELECT F()"
)
self.validate_identity(
"WITH FUNCTION f() RETURNS INTEGER NOT DETERMINISTIC RETURN 1 SELECT F()"
)
self.validate_identity(
"WITH FUNCTION f() RETURNS INTEGER CALLED ON NULL INPUT RETURN 1 SELECT F()"
)
self.validate_identity(
"WITH FUNCTION f() RETURNS INTEGER RETURNS NULL ON NULL INPUT RETURN 1 SELECT F()"
)
self.validate_identity(
"WITH FUNCTION f() RETURNS INTEGER SECURITY DEFINER RETURN 1 SELECT F()"
)
self.validate_identity(
"WITH FUNCTION f() RETURNS INTEGER SECURITY INVOKER RETURN 1 SELECT F()"
)
self.validate_identity("WITH FUNCTION f() RETURNS INTEGER COMMENT 'hi' RETURN 1 SELECT F()")
self.validate_identity(
"WITH FUNCTION custom_sqrt(a INTEGER) RETURNS DOUBLE COMMENT 'Custom sqrt function' "
"RETURNS NULL ON NULL INPUT NOT DETERMINISTIC LANGUAGE SQL SECURITY DEFINER "
"RETURN a SELECT CUSTOM_SQRT(4)"
)
Loading