diff --git a/sqlglot/dialects/trino.py b/sqlglot/dialects/trino.py index e1e1c36905..ea3984e11d 100644 --- a/sqlglot/dialects/trino.py +++ b/sqlglot/dialects/trino.py @@ -16,6 +16,12 @@ class Trino(Presto): SUPPORTS_USER_DEFINED_TYPES = False LOG_BASE_FIRST = True + class Tokenizer(Presto.Tokenizer): + KEYWORDS = { + **Presto.Tokenizer.KEYWORDS, + "REFRESH": TokenType.REFRESH, + } + class Parser(Presto.Parser): FUNCTION_PARSERS = { **Presto.Parser.FUNCTION_PARSERS, diff --git a/sqlglot/expressions.py b/sqlglot/expressions.py index e53a9a0cf1..ce5ec316b5 100644 --- a/sqlglot/expressions.py +++ b/sqlglot/expressions.py @@ -1484,7 +1484,7 @@ class Uncache(Expression): class Refresh(Expression): - pass + arg_types = {"this": True, "kind": True} class DDL(Expression): diff --git a/sqlglot/generator.py b/sqlglot/generator.py index eaad84fc94..5b967ad476 100644 --- a/sqlglot/generator.py +++ b/sqlglot/generator.py @@ -4375,8 +4375,8 @@ def forin_sql(self, expression: exp.ForIn) -> str: def refresh_sql(self, expression: exp.Refresh) -> str: this = self.sql(expression, "this") - table = "" if isinstance(expression.this, exp.Literal) else "TABLE " - return f"REFRESH {table}{this}" + kind = "" if isinstance(expression.this, exp.Literal) else f"{expression.text('kind')} " + return f"REFRESH {kind}{this}" def toarray_sql(self, expression: exp.ToArray) -> str: arg = expression.this diff --git a/sqlglot/parser.py b/sqlglot/parser.py index acd6c428a9..18d2dcb427 100644 --- a/sqlglot/parser.py +++ b/sqlglot/parser.py @@ -7554,9 +7554,19 @@ def _parse_commit_or_rollback(self) -> exp.Commit | exp.Rollback: return self.expression(exp.Commit, chain=chain) - def _parse_refresh(self) -> exp.Refresh: - self._match(TokenType.TABLE) - return self.expression(exp.Refresh, this=self._parse_string() or self._parse_table()) + def _parse_refresh(self) -> exp.Refresh | exp.Command: + if self._match(TokenType.TABLE): + kind = "TABLE" + elif self._match_text_seq("MATERIALIZED", "VIEW"): + kind = "MATERIALIZED VIEW" + else: + kind = "" + + this = self._parse_string() or self._parse_table() + if not kind and not isinstance(this, exp.Literal): + return self._parse_as_command(self._prev) + + return self.expression(exp.Refresh, this=this, kind=kind) def _parse_column_def_with_exists(self): start = self._index diff --git a/tests/dialects/test_trino.py b/tests/dialects/test_trino.py index b73632c893..acb812eb1d 100644 --- a/tests/dialects/test_trino.py +++ b/tests/dialects/test_trino.py @@ -5,6 +5,7 @@ class TestTrino(Validator): dialect = "trino" def test_trino(self): + self.validate_identity("REFRESH MATERIALIZED VIEW mynamespace.test_view") self.validate_identity("JSON_QUERY(m.properties, 'lax $.area' OMIT QUOTES NULL ON ERROR)") self.validate_identity("JSON_EXTRACT(content, json_path)") self.validate_identity("JSON_QUERY(content, 'lax $.HY.*')")